?? main.c
字號:
/////////////////////////////////////////////////////////////////////////////////////////
//
// Sample for Freescale EVB9S08DZ60
//
// LIN communication data from master to slave
//
// This example must be programmed in two EVB9S08DZ60 Demo Boards connected
// through the LIN bus.
//
// This program sends the potentiometer value of the Master board through
// the LIN bus and displays the value received on the PTD[7..0] LEDs of the Slave board.
//
// A LIN cable must connect the two boards.
//
// Before to run this example, verify that all jumpers are in their default
// position. The "SERIAL SETTINGS" jumper (J301) must be in LIN position.
// Moreover, the example on the Master board must be compiled with
//
// #define _MASTER 1
//
// while the Slave board must be compiled with
//
// #define _MASTER 0
//
// See the "Summary of Jumper and Connector Settings" chapter in
// the user's manual.
//
// NOTE: This example serves the only purpose of showing how to manage the SCI
// peripheral to reconstruct a LIN bus, and is not intended to be a starting
// point for a real-world LIN application.
//
// --------------------------------------------------------------------------------------
//
// This project has been written for CodeWarrior 5.0 for HC(S)08
// and uses Registers Files MC9S08DZ60.H and MC9S08DZ60.C, version 2.87.006
// --------------------------------------------------------------------------------------
// Copyright (c) 2006 SofTec Microsystems
// http://www.softecmicro.com/
//
/////////////////////////////////////////////////////////////////////////////////////////
#include <hidef.h>
#include "derivative.h"
#include "lin.h"
#define _ID 0x0001
#define _MASTER 1 // 0 = SLAVE TASK
// 1 = MASTER TASK
Bool lin_send_enable = FALSE;
unsigned char potentiometer_value;
struct message msg_send, msg_get;
/////////////////////////////////////////////////////////////////////////////////////////
// PeriphInit
// --------------------------------------------------------------------------------------
// Initializes various registers and peripherals
/////////////////////////////////////////////////////////////////////////////////////////
void PeriphInit(void)
{
// Clears COP Watchdog timeout
SOPT1 = 0x00;
// Selects FBE MCG mode (IREFS=0, CLKS=10)
MCGC1 = 0xB8;
// LP=0 and selects external high frequency crystal clock
MCGC2 = 0x36;
// PLLS=0
MCGC3 = 0x00;
// Waits until the initialization cycle of the external crystal clock is completed
while(!(MCGSC&0x02))
;
// Sets TJA1020 LIN transceiver in normal mode operation (NSLP HIGH)
PTED = 0x10;
PTEDD = 0x10;
//Selects fBUS/2 as ADC clock source, clock divide 2 and 8 bit conversion mode
ADCFG = 0x21;
// Configures PTD port as output and power off LEDs
PTDD = 0xFF;
PTDDD = 0xFF;
// AD2 pin I/O control disabled
APCTL1 = 0x04;
// Timer1 Overflow every 5 ms
TPM1MODH = 0x27;
TPM1MODL = 0x10;
// Enables Interrupt Overflow Timer1 and
// selects bus clock as clock source. Prescaler Divisor = 1
TPM1SC = 0x48;
LINInit();
EnableInterrupts;
}
/////////////////////////////////////////////////////////////////////////////////////////
// MAIN
/////////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
PeriphInit();
for(;;)
{
#if(_MASTER) // Master
if (lin_send_enable)
{
// Selects ADC2(PTA2) channel and continuous conversion
ADSC1 = 0x22;
while (!(ADSC1 & 0x80))
;
// Sends the potentiometer value to the LIN bus
potentiometer_value = ADRL;
msg_send.identifier = _ID;
msg_send.data_field[0] = potentiometer_value;
(void)LINSendMsg(TRUE, TRUE, msg_send);
PTDD = ~potentiometer_value;
lin_send_enable = FALSE;
}
#else // Slave
if (LINCheckState() == CHECKSUM)
{
// Reads the potentiometer value from the LIN bus
if (LINGetMsg(TRUE, &msg_get))
{
if (msg_get.identifier == _ID)
// Shows the potentiometer value on the LEDs
PTDD = ~msg_get.data_field[0];
}
}
#endif
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// Timer1_overflow
// --------------------------------------------------------------------------------------
// TIMER1 Overflow Interrupt
/////////////////////////////////////////////////////////////////////////////////////////
interrupt void Timer1_overflow (void)
{
lin_send_enable = TRUE;
TPM1SC &=0x7F;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -