📄 slave.c
字号:
/******************************************************************************
*
* Copyright (C) 2007 Freescale Semiconductor, Inc.
* All Rights Reserved
*
* Filename: slave.c
*
* Revision:
*
* Functions: LIN slave sample application for LIN S08DZ60 Driver.
*
* Description: Receive message $17 from master and display contents on LEDs.
*
* Notes: Sample code that may be modified by the user.
*
******************************************************************************/
#include <MC9S08DZ60.h>
#include <linapi.h>
#include "slave.h"
#include "main_h.h"
unsigned char MsgRcvd [2]; /* received data */
unsigned int Time; /* time from real time counter */
unsigned char Lin_data [2]; // send data
unsigned char data=1;
unsigned char ErrorCount; // errorcount
/*****************************************************************************
* 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: busfreq indicates the bus operating frequency in KHz
*
*
*****************************************************************************/
void InitDelay(void)
{
/* Initialise RTC */
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++;
/* 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: void PeriphInit(void);
*
* Description:
*
*
* Returns:
*
* Notes:
*
*
*****************************************************************************/
void PeriphInit(void)
{
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 responses periodically
*
* Returns: never return
*
* Notes:
*
*****************************************************************************/
void main( void )
{
int i;
LINStatusType ret;
Time = 0;
/* 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);
//enable LED1
PTCDD_PTCDD5 = 1;
/* Initialize driver */
LIN_Init();
/* Enable LIN transceiver */ //
//PTEDD_PTEDD4 = 1; //
// PTED_PTED4 = 1;
PTADD_PTADD7 = 1; //TJA1020 IRQ PTA7
PTAD_PTAD7 = 1;
/* Enable LED display */ //
// PTDDD = 0xFF; //
// PTDD = 0xFF;
/* Initialise RTC */
InitDelay();
/* Enable interrupt */
asm cli;
PeriphInit(); //initial led1,led2,sw1,sw2
LED2=~LED2; //
Lin_data[0] = data; //initial Lin_data
Lin_data[1] = 0;
/* Main Loop */
while( 1 )
{
/* Wait for a message */
// ret = LIN_MsgStatus( 0x17 );
// if(( ret == LIN_OK ) || ( ret & LIN_MSG_OVERRUN ))
// {
/* Read message */
// ret = LIN_GetMsg( 0x17, MsgRcvd );
// LED1=~LED1;
// Delay(500); //led1 indicates msg17 has been received
// }
//DZ60 send a message LIN_MSG_11 to Lin_data
// if(SW2==0)
// {
for(i=0;i<20;i++)
{
ret = LIN_PutMsg( 0x11,Lin_data) ;
LED1=~LED1;
Delay(500);
}
// do
// {
// ret = LIN_DriverStatus();
// }while( ret & LIN_STATUS_PENDING );
// ret = LIN_MsgStatus( 0x11 );
// }
// }
// }
//}
} /* while (1) */
} /* main */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -