⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slave.c

📁 摩托罗拉公司发布的QL4的Lin总线kit,freescale网站上花钱买的
💻 C
字号:

/***********************************************************
Demo software for QL4 LINKit

Application Code:  Steve McAslan 
SLIC driver Code:  Matt Ruff

 **********************************************************/

#include <hidef.h>
#include <startup.h>
#include "MC68HC908QL4.h"
#include "global.h"

#define debounce 1                                
#define modecount 145                              

// --- Use only one of the two following define statements --
#define QL4LINKit					// Use this define for the QL4 LINkit Board
//#define QL4EVB						// Use this define for the QL4 EVB Board

static unsigned char temp_msg_buffer[8];	// Temporary storage of SLIC buffer contents;
unsigned char LINdata;
unsigned char LINID;
unsigned char BusOff;
extern unsigned char LINSleep;

unsigned char data = 1;
unsigned char ID = 0;
unsigned char key_last;
unsigned char count;
unsigned char mode;

int keycount;

// Function Prototypes
void Read_button (unsigned char);
void LED_display (void);
void init_hardware(void);
void Check_LIN_Sleep(void);
extern void SLIC_Init(void);


void LIN_message (unsigned char data, unsigned char ID)
	{
    LINdata = data;
    switch (ID)
    	{
        case 0 : LINID = 0x11;  break;        
        case 1 : LINID = 0x92;  break;
        case 2 : LINID = 0xD3;  break;
        case 3 : LINID = 0x50;  break;
        default: LINID = 0x11;  break;
    	}
	}//_________end LIN_message________

void main(void)
	{ 
	CONFIG1 = 0x09;             /* disable COP             */
	CONFIG2 = 0x00;             /* default (int. osc.)     */
	TSC = 0x00;                 /* Start timer */
	BusOff = 0;

	init_hardware();
	SLIC_Init();					

	SLCC1_SLCIE = 1;			// Enable SLIC Interrupt Flag
	EnableInterrupts;

	#ifdef QL4LINKit			// Initialize IO pins for LED display and button reads
	PTB_PTB0 = 1;			// D8 -- LED outputs
	PTB_PTB1 = 0;			// D7
	PTB_PTB2 = 0;   		// D6
	PTB_PTB6 = 0;			// D5
	DDRB_DDRB0 = 1;			// D8 -- Set pins as outputs
	DDRB_DDRB1 = 1;			// D7
	DDRB_DDRB2 = 1;			// D6
	DDRB_DDRB6 = 1;			// D5

	PTB_PTB7 = 1;			// Enable 33399 physical layer device
	DDRB_DDRB7 = 1;			// EN pin on MC33399
	#endif

	#ifdef QL4EVB				// Initialize IO pins for LED display and button reads
	PTB_PTB0 = 0;			// D1 - LED Inverse logic -- LED outputs
	PTB_PTB1 = 0;			// NC
	PTB_PTB2 = 0;   		// NC
	PTB_PTB6 = 0;			// NC
	DDRB_DDRB0 = 1;			// D1 -- Set pins as outputs
	DDRB_DDRB1 = 1;			// NC
	DDRB_DDRB2 = 1;			// NC
	DDRB_DDRB6 = 1;			// NC

	PTB_PTB7 = 1;			// Enable MC33661 eLIN physical layer device
	DDRB_DDRB7 = 1;			// EN pin on MC33661
							// Make sure jumper 'LIN_EN' is on pins 1 & 2 for SW control
	#endif
            
	while(1)
   		{
        COPCTL = 0;  					// clear the COP counter
        if (TSC & 0x80)                 // is overflow flag set?
        	{
            TSC &= ~(0x80);             // yes, clear it           
            count++;                    // used for LED flashing      

            if ((PTA & 0x20) == 0x20)	// toggle tick output      
            	{
                PTA &= ~(0x20);
            	}
            else
           		{
                PTA |= 0x20;
            	}
			#ifdef QL4LINKit						// Initialize button for LINkit 
	        Read_button (PTA_PTA1);         		// read button on PTA1
			#endif
			#ifdef QL4EVB							// Initialize button for EVB
	        Read_button (PTA_PTA3);         		// read button on PTA3
			#endif

            LED_display ();  	                      // update LEDs on PTB0-2,6
            LIN_message (data,ID);                    // update LIN response msg.
            Check_LIN_Sleep();						  // Check to see if LINSleep is set
       		}						// end if(TSC &0x08)
    	}						// end while(1)
	}						// end main()


/******************************************************************************
*                                                                             *
*    Function name: LED_display                                               *
*    Originator:    P. Topping                                                *
*    Date:          4th September 2003                                        *
*    Function:      According to mode, the LEDs display the 4-bit data field  *
*                   or the flashing ID (0:19, 1:1A, 2:1B, 3:18).              *
*                   The LEDs are switched separately so that the compiler     *
*                   uses only BSETs and BCLRs thus avoiding an STA to port B  *
*                   which would be incompatible with the LIN drivers.         * 
*                                                                             *
******************************************************************************/
void LED_display (void)
	{
	unsigned char LEDs;
 
    if (mode)
    	{
        if (count & 0x04)
        	{
            LEDs = 1 << (ID+3);                   // ID mode LED display
       		}
        else
        	{
            LEDs = 0;                             // ID mode flash, LEDs off
       		}
    	}
    else                                          // normal mode so
    	{
        LEDs = data << 3;                         // drive LEDs with data
    	}
    
	#ifdef QL4LINKit							   // LED active high 
    if (LEDs & 0x08) PTB_PTB0 = 1; else PTB_PTB0 = 0; 
	#endif
	#ifdef QL4EVB								  // LED inverse logic
    if (LEDs & 0x08) PTB_PTB0 = 0; else PTB_PTB0 = 1; 
	#endif
    if (LEDs & 0x10) PTB_PTB1 = 1; else PTB_PTB1 = 0; 
    if (LEDs & 0x20) PTB_PTB2 = 1; else PTB_PTB2 = 0; 
    if (LEDs & 0x40) PTB_PTB6 = 1; else PTB_PTB6 = 0; 
	}        

/******************************************************************************
*    Function name: init_hardware                                             *
*    Originator:                                                              *
*    Date:                                                                    *
*    Function:                                                                *
******************************************************************************/
void init_hardware(void) 
	{
	OSCTRIM = Optional;		// Pull TRIM from FFC0	
	}

/******************************************************************************
*                                                                             *
*    Function name: Read_button                                               *
*    Originator:    P. Topping                                                *
*    Date:          21st June 2003                                            *
*    Function:      The port line is read and its level compared with what    *
*                   it was the previous time through the loop. If it is the   *
*                   same, the counter "keycount" is used for debounce (80ms)  *
*                   and to decide if the same state has been present long     *
*                   enough (3 seconds) for a mode change. In each mode the    *
*                   appropriate increment (data or ID) takes place.           *
*                   If the switch status changes the counter is reset.        *
*                                                                             *
******************************************************************************/
void Read_button (unsigned char key)
	{
    if (key == key_last)                          // same as last time ?
    	{
        if (keycount == debounce)                 // yes, (debounce + 2)th ?
        	{
            if (key == 0)                         // yes, key pressed ?
            	{
                if (mode)                         // yes, ID mode ?
                	{
                    ID++;                         // yes, increment ID
                    if (ID == 4) ID = 0;          // wrap round from 3 to 0
                	}
                else                              // no, normal mode
                	{
                    data++;                       // increment data
                    if (data == 16) data = 1;     // wrapping from 15 to 1
                	}

				LINSleep=0;						  // Clear LINSleep flag
												  // on button press
            	}
            keycount ++;                          // prevents re-entry
        	}
        else if (keycount < modecount)            // prevents wraparound
        	{
            keycount ++;
        	}
        if (keycount == modecount)                // time for modechange ?
        	{
            if (key == 0)                         // yes, key pressed ?
            	{
                mode = 1;                         // yes, change to ID mode
            	}
            else                                  // no key pressed
            	{
                mode = 0;                         // so back to normal mode
            	}
        	}
    	}
    else
    	{   
        keycount = 0;                             // no, different, so reset 
        key_last = key;                           // count and save status 
    	}
	}

/******************************************************************************
*    Function name: Check_LIN_Sleep                                           *
*    Originator:    Matt Ruff                                                 *
*    Date:          10 Nov 2003                                               *
*    Function:      This function checks the LINSleep flag and turns off      *
* 					the LED display when LINSleep is set. If LINSleep is clear*
*					then it allows LED_Display to turns LEDs back on again    *
******************************************************************************/
void Check_LIN_Sleep(void)
	{
	if (LINSleep==1)
		{
		#ifdef QL4LINKit			// Turn off LEDS to indicate SLEEP to user
			PTB_PTB0 = 0;			// D8 -- LED outputs
			PTB_PTB1 = 0;			// D7
			PTB_PTB2 = 0;   		// D6
			PTB_PTB6 = 0;			// D5
			DDRB_DDRB0 = 0;			// D8 -- Set as inputs so LED display doesn't update
			DDRB_DDRB1 = 0;			// D7
			DDRB_DDRB2 = 0;			// D6
			DDRB_DDRB6 = 0;			// D5
		#endif
		#ifdef QL4EVB				// Turn off LEDS to indicate SLEEP to user
			PTB_PTB0 = 1;			// D1 - LED Inverse logic -- LED outputs
			PTB_PTB1 = 0;			// NC
			PTB_PTB2 = 0;   		// NC
			PTB_PTB6 = 0;			// NC
			DDRB_DDRB0 = 0;			// D1 -- Set as inputs so LED display doesn't update
			DDRB_DDRB1 = 0;			// NC
			DDRB_DDRB2 = 0;			// NC
			DDRB_DDRB6 = 0;			// NC
		#endif
		}
	else							// LINSleep is clear, so enable LED outputs
		{
		#ifdef QL4LINKit			// Initialize IO pins for LED display and button reads
			DDRB_DDRB0 = 1;			// D8 -- Set pins as outputs
			DDRB_DDRB1 = 1;			// D7
			DDRB_DDRB2 = 1;			// D6
			DDRB_DDRB6 = 1;			// D5
		#endif
		#ifdef QL4EVB				// Initialize IO pins for LED display and button reads
			DDRB_DDRB0 = 1;			// D1 -- Set pins as outputs
			DDRB_DDRB1 = 1;			// NC
			DDRB_DDRB2 = 1;			// NC
			DDRB_DDRB6 = 1;			// NC
		#endif
		}

	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -