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

📄 master.c

📁 LIN Driver for S08, LIN 1.3. Source code with codewarrior
💻 C
字号:
/******************************************************************************
*                                                                     
*       Copyright (C) 2005 Freescale Semiconductor, Inc.              
*       All Rights Reserved								              
*														              
* Filename:     master.c                
*														              
* Revision:      										              
*														              
* Functions:    Sample application for S08AW60 LINKits Master Driver.
*         
* Description:	Communicates with 16 LIN nodes and displays their data.
*
* Notes:        Also serves as an example of use for the LIN driver.
*
******************************************************************************/

#include <linapi.h>
#include "master.h"

unsigned char ErrCount;         /* errors counter   */
unsigned char MsgCount;         /* messages counter */
unsigned char MsgSent [2];      /* transmited data  */
unsigned char MsgRcvd [4];      /* received deta    */
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 RTI for use in Delay() function
 *
 *
 * Returns:         none
 *
 * Notes:			      RTI is using a 4MHz external oscillator divided by 1024 
 *                  give a RTI period of 256us. 
 *
 *****************************************************************************/
void InitDelay(void)
{
	/* Initialise RTI */
	SRTISC = 0x32;	  /* external clock source/1024;enable interrupt*/
}

/*****************************************************************************
 * Function:        RTI_ISR
 *
 * Description:     Handle RTI ISR
 *				          Add one tick to time
 *
 * Returns:         none
 *
 * Notes:			
 *
 *
 *****************************************************************************/
interrupt void RTI_ISR( void )
{
	Time++;

	/* Clear RTI flag */
	SRTISC |= 0x40;
}


/*****************************************************************************
 * Function:        Delay
 *
 * Description:     Simple delay routine.
 *                  Delay for n * 256us 
 *
 * Returns:         after n n * 256us
 *
 * Notes:			      Uses real time interrupt function - must be initialised
 *					        elsewhere to give 256us 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++)
	{
		PORTF = 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++)
	{
		PORTF = 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
 *
 *****************************************************************************/
void LINActive()
{
	/* Check switch position */
    PORTG = 0x80;
    while ((PORTG & 0x01) == 0)
    {
    	NewNode();
    	LostNode();
    }
	/* Enable LIN interface */
  /* Add code for enabling LIN interface when required. Check schematics of used EVB */
}

/*****************************************************************************
 * 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;

    /* disable COP */
    SOPT &= ~COPE;  
    
    /* init ICG for 8MHz bus clock*/
    ICGC1 = 0xF8;
    ICGC2 = 0x88;		
    
    for (;ICGS1 & LOCK;);

    
    /* Initialize driver */
    LIN_Init();
    
    /* Enable LED display */
    DDRF  = 0xFF;
	  PORTF = 0x00;

    /* Enable switch */
    DDRG  = 0x00;
	  PTGPE = 0x07;

	  /* Initialise RTI */
	  InitDelay();

    /* Enable interrupt */
    asm cli;        


	/* Check/Enable LIN interface */
	LINActive();
	    
    /* Test message for comms debug */
    MsgSent[0] = 0x55;
    MsgSent[1] = 0xAA;

    /* 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)
            {
            }
    }

    /* Schedule Loop */
    while( 1 )
    {
		/* Check/Enable LIN interface */
		LINActive();
	    
		/* 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 );
		        
				    /* 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;
    				PORTF = 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) */

}       /* main */

⌨️ 快捷键说明

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