📄 master.c
字号:
/******************************************************************************
*
* Copyright (C) 2007 Freescale Semiconductor, Inc.
* All Rights Reserved
*
* Filename: master.c
*
* Revision:
*
* Functions: LIN master sample application for LIN S08DZ60 Driver.
*
* Description: Communicates with 16 LIN nodes and displays their data.
*
* Notes: Also serves as an example of use for the LIN driver.
*
******************************************************************************/
#include <MC9S08DZ60.h>
#include <linapi.h>
#include "master.h"
#include "main_h.h"
unsigned char ErrCount; /* errors counter */
unsigned char MsgCount; /* messages counter */
unsigned char MsgSent [2]; /* transmited data */
unsigned char MsgRcvd [4]; /* received deta */
//unsigned char MsgRcvd [2]; // modify MsgRcvd size from 4 to 2 //
unsigned int Time; /* time from real time counter */
/*****************************************************************************
* Function: LIN_Command
*
* Description: User call-back.
* Called by the driver after successful transmission or receiving
* of the Master Request Command Frame (ID Field value '0x3C').
*
* Returns: never return
*
* Notes:
*
*****************************************************************************/
void LIN_Command()
{
/* endless loop */
while(1);
}
/*****************************************************************************
* Function: Delay
*
* Description: Initialise RTC for use in Delay() function
*
*
* Returns: none
*
* Notes:
*
*
*****************************************************************************/
void InitDelay(void)
{
/* Initialise RTC -real time counter */
RTCSC = 0x98; /*?????? internal clock source;1ms RTC tick; enable interrupt */
RTCMOD = 1; /* period of 1 ms */
}
/*****************************************************************************
* Function: RTC_ISR
*
* Description: Handle RTC ISR
* Add one tick to time
*
* Returns: none
*
* Notes:
*
*
*****************************************************************************/
interrupt void RTC_ISR( void )
{
//Time++;
Time++;
/* Clear RTC flag */
RTCSC |= 0x80;
}
/*****************************************************************************
* Function: Delay
*
* Description: Simple delay routine.
* Delay for n ms
*
* Returns: after n ms
*
* Notes: Uses real time interrupt function - must be initialised
* elsewhere to give 1ms timeouts
*
*****************************************************************************/
void Delay(unsigned int n)
{
unsigned int stopTime;
stopTime = Time+n;
while (stopTime != Time); //
}
/*****************************************************************************
* Function: NewNode
*
* Description: Flash LEDs in sequence low to high.
*
* Returns: none
*
* Notes:
*
*****************************************************************************/
void NewNode()
{
char i;
unsigned char strip = 0x01;
for (i = 0; i < 8; i++)
{
PTDD = ~strip;
Delay(300);
strip = strip<<1;
}
}
/*****************************************************************************
* Function: NewNode
*
* Description: Flash LEDs in sequence low to high.
*
* Returns: none
*
* Notes:
*
*****************************************************************************/
void LostNode()
{
char i;
unsigned char strip = 0x80;
for (i = 0; i < 8; i++)
{
PTDD = ~strip;
Delay(300);
strip = strip>>1;
}
}
/*****************************************************************************
* Function: LINActive
*
* Description: Returns only if master switch has enabled LIN
*
* Returns: none
*
* Notes: Flashes LEDs if LIN is disabled (PTB0 = 1 - disabled)
*
*****************************************************************************/
void LINActive()
{
/* Check switch position */
while ((PTBD & 0x01) == 1)
{
NewNode();
LostNode();
}
}
/*****************************************************************************
* Function: PeriphInit
*
* Description:
*
* Returns:
*
* Notes:
*
*****************************************************************************/
void PeriphInit(void)
{
//DDRF = 0xFF; // Port F[0..7] equals output
LED1 = OFF; // Turn Off LEDs
LED2 = OFF;
PTCDD_PTCDD5 = 1; // Port C_5 Port B_3 output
PTBDD_PTBDD3 = 1;
PTBDD_PTBDD6 = 0; // Clears Port B_6 and B_7
PTBDD_PTBDD7 = 0;
}
/*****************************************************************************
* Function: main
*
* Description: Sends and receives 2 bytes messages periodically
*
* Returns: never return
*
* Notes:
*
*****************************************************************************/
void main( void )
{
int i,nodeId;
unsigned char statusDisplay;
LINStatusType ret;
unsigned char idList[16] = {0x10, 0x11, 0x12, 0x13, 0x18, 0x19, 0x1A, 0x1B, 0x20, 0x21, 0x22, 0x23, 0x28, 0x29, 0x2A, 0x2B};
char activeList[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char messageList = 16;
//Time = 0;
Time = 0; // Time to Time1
/* disable COP */
SOPT1 = 0x20; /* disable COP, enable stop mode */
/* Configure MCG to produce a 16MHz bus clock from a 8MHz external crystal via PLL */
/* Select High Range, High Gain, Bus divided by 1, Oscillator, ERCLK enabled */
MCGC2 = 0x36;
while (!MCGSC_OSCINIT);
/* Select External Clock as bus clock source, Reference divided by 128, Reference = external */
MCGC1 = 0xB8;
/* wait for Reference Status bit to update */
while (!MCGSC_IREFST);
/* Wait for clock status bits to update */
while (MCGSC_CLKST != 0b10);
/* NOW IN FBE MODE */
MCGC2 = 0x3E;
/* NOW IN BLPE MODE */
/* Change RDIV for PLL reference */
MCGC1 = 0x98;
/* Select the VCO divider and PLL */
MCGC3 = 0x48;
/* Wait for PLL status to update */
while (!MCGSC_PLLST);
/* Clear LP bit */
MCGC2 = 0x36;
/* NOW IN PBE MODE */
/* Wait for LOCK bit to set */
while (!MCGSC_LOCK);
/* Enter PEE mode */
MCGC1 = 0x18;
/* Wait for Clock status to indicate PLL output */
while (MCGSC_CLKST != 0b11);
/* Initialize driver */
LIN_Init();
/* Enable LIN transceiver */ //TJA1020
//PTEDD_PTEDD4 = 1;
//PTED_PTED4 = 1;
PTADD_PTADD7 = 1; //enable LIN transceiver TJA1020
PTAD_PTAD7 = 1;
/* Initialise RTC */
InitDelay();
/* Enable interrupt */
asm cli;
/* Check/Enable LIN interface */
//LINActive(); // disable LINActive()
PeriphInit();
LED1=~LED1; //initial led1,led2,sw1,sw2
/* Test message for comms debug */
MsgSent[0] = 0x55;
MsgSent[1] = 0xAA;
while(1)
{
/* Update message */
ret = LIN_PutMsg( 0x17, MsgSent );
/* Send a request for message */
ret = LIN_RequestMsg( 0x17 );
LED2=~LED2;
ret = LIN_MsgStatus(0x11);
if(ret == LIN_OK)
{
ret = LIN_GetMsg(0x11,MsgRcvd);
LED2=~LED2;
/* Wait for message processing */
// do
// {
// ret = LIN_DriverStatus();
// }
// while( ret & LIN_STATUS_PENDING );
/* Check sent message status */
// ret = LIN_MsgStatus( 0x17 );
// if ( ret != LIN_OK )
// {
// ErrCount = 1;
// while(1)
// {
// }
// }
}
}
/* Schedule Loop */
// while( 1 )
// {
/* Check/Enable LIN interface */
// LINActive(); // disable LINActive()
// ret = LIN_MsgStatus(0x11);
// if(ret == LIN_OK)
// {
// ret = LIN_GetMsg(0x11,MsgRcvd);
// LED2=~LED2;
// }
// if(SW2==0) // SW2 trigger message sent
// {
//
/* Update message */ //
// ret = LIN_PutMsg( 0x17, MsgSent ); //
//
/* Send a request for message */ //
// ret = LIN_RequestMsg( 0x17 );
//
/* Wait for message processing */ ///
// do //
// { //
// ret = LIN_DriverStatus(); //
// } //
// while( ret & LIN_STATUS_PENDING );
/* Check sent message status */
// ret = LIN_MsgStatus( 0x17 );
// if ( ret != LIN_OK )
// {
// ErrCount = 1;
// while(1)
// {
// }
// }
//Led2 indicates msg17 has been sent
// LED2=~LED2;
// Delay(500);
// }
/* Slave received messages - cycle here */
// for (i = 0; i < messageList; i++)
// {
/* Send a request for message */
// ret = LIN_RequestMsg( idList[i] );
/* Wait for message processing */
// do
// {
// ret = LIN_DriverStatus();
// }
// while( ret & LIN_STATUS_PENDING );
//
/* Check received message status */
// ret = LIN_MsgStatus( idList[i] );
// if ( ret == LIN_OK )
// {
/* Read message */
// ret = LIN_GetMsg( idList[i], MsgRcvd );
// LED1=~LED1; //led1 indicates msg11 has been received
// Delay(500);
/* Was message found last time? */
// if (activeList[i] == 0)
// {
/* No so new Id was added */
// activeList[i] = 1;
/*Flash LEDs to indicate new node */
// NewNode();
// }
// MsgCount++;
// statusDisplay = MsgRcvd[0] & 0x0F;
// nodeId = i*16;
// statusDisplay = statusDisplay + nodeId;
// PTDD = ~statusDisplay;
// Delay(300);
// }
// else
// {
/* Id not found */
/* Was message found last time? */
// if (activeList[i] == 1)
// {
/* Yes so Id was lost */
/*Flash LEDs to indicate lost node */
// LostNode();
// }
// activeList[i] = 0;
// }
// }
// } /* while (1) */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -