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

📄 node_b_main.c

📁 针对日本瑞莎单片机r8c/23系列开发的LIN网络开发编程
💻 C
字号:
/******************************************************************************
* DISCLAIMER:                                                        
* The software supplied by Renesas Technology America Inc. is        
* intended and supplied for use on Renesas Technology products.      
* This software is owned by Renesas Technology America, Inc. or      
* Renesas Technology Corporation and is protected under applicable   
* copyright laws. All rights are reserved.                           
*                                                                    
* THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, 
* IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO IMPLIED         
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
* APPLY TO THIS SOFTWARE. RENESAS TECHNOLOGY AMERICA, INC. AND       
* AND RENESAS TECHNOLOGY CORPORATION RESERVE THE RIGHT, WITHOUT      
* NOTICE, TO MAKE CHANGES TO THIS SOFTWARE. NEITHER RENESAS          
* TECHNOLOGY AMERICA, INC. NOR RENESAS TECHNOLOGY CORPORATION SHALL, 
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR        
* CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER ARISING OUT OF THE 
* USE OR APPLICATION OF THIS SOFTWARE.                               
******************************************************************************

******************************************************************************
File: node_b_main.c

This file contains the application routines needed to implement a LIN slave 
using the R8C/23 RSK board.

tab space = 4 

Revision History  
21.05.2007 AE version 1.16r. Released to RTA CAN download page.
01.08.2007 AE version 1.17. id0x8 fix done in lin_low_level.c by Bob C.
15.08.2007 AE version 1.18. Linted.
******************************************************************************/

/******************************************************************************
System includes (Compiler Level Includes)
******************************************************************************/
//#include <xxxx.h>

/******************************************************************************
User includes (Project level includes)
******************************************************************************/
#include "lin_api.h"
#include "lcd.h"
#include "rskR8C23def.h"

/******************************************************************************
Macro definitions
******************************************************************************/
#define PAUSEFACTOR 100
#define INTROPAUSE	20

#pragma INTERRUPT Timer_ISR

/******************************************************************************
Global variables and functions imported
******************************************************************************/
/* Functions */
extern void HardwareSetup(void);

/******************************************************************************
Global variables and functions private to the file
******************************************************************************/
/* Data */
static const char* renstring = "RENESAS\0";
static const char* techstring = "Technlgy\0";
/* Node B data */
static unsigned char app_run = 0;		/* Application start flag */
static unsigned int ad_val;			/* Application AD transducer data */
static unsigned int prev_ad_val;		/* "- */
static unsigned char counter;      	/* Application demo counter */
static unsigned char prev_counter;		/* "- */
static unsigned char master_switches;	/* Application Master switch data */

/* Functions */
static void InitialJobs(void);
static void NormalJobs(void);
static void counter_init(void);
static void read_ad(void);
static void display_intro_0(void);
static void display_intro_1(void);
static void display_intro_2(void);
static void display_intro_3(void);
static void pause(int i);

/******************************************************************************
Function Name:       main
Parameters:          none
Return value:        none
Description:         Main function
******************************************************************************/
void main(void)
{
   InitialJobs();
   NormalJobs();
   return;  /* Should never execute.  */
}

/******************************************************************************
Function Name: 	    InitialJobs
Parameters:         none
Return value:       none
Description:	    init all
******************************************************************************/
static void InitialJobs(void)
{
   _asm("FCLR I");         /* Interrupts disabled */

	HardwareSetup();
	ENABLE_LEDS
	ENABLE_SWITCHES

	InitDisplay();
	/* Node display intro sequence */
	//display_intro_0(); Too slow if running on internal xtal without changing lcd DELAY_TIMING
	display_intro_1();
	display_intro_2();
	
	/* LIN init */
	l_sys_init();
	l_ifc_init(LIN);
	l_ifc_connect(LIN);

	/* 20ms application timer for node counter */
	counter_init();

	_asm("FSET I");         /* Interrupts enabled */
	read_ad();
	l_u16_wr((unsigned char)LIN_SIG_NODE_B_AD, ad_val);
	prev_ad_val = ad_val;
	return;
}

/******************************************************************************
Function Name: 	    NormalJobs
Parameters:         none
Return value:       none
Description:	    Loop to receive, transmit and display. Starts when app 
                    is ready
******************************************************************************/
static void NormalJobs(void)
{
	/* Remove this line to get LCD feedback on bus fault! */
	display_intro_3();

	while (app_run)
	{
		/*** READ MASTER ***/
		/* Master pushed switch 1? */
        if (l_flg_tst((unsigned char)LIN_SIG_CMD_SWITCH) == 1)
        {
			/* Show this node received command switch data */
			GRN_LED = ACTIVE;
			
            master_switches = l_u8_rd((unsigned char)LIN_SIG_CMD_SWITCH);
			if (master_switches == 1)
			{
				DisplayString((unsigned char)LINE2_1STCH , "MasterS1");
			}
			else if (master_switches == 2)
			{
				DisplayString((unsigned char)LINE2_1STCH , "MasterS2");
			}
			else if (master_switches == 3)
			{
				DisplayString((unsigned char)LINE2_1STCH , "MasterS3");
			}
			else
			{
				DisplayString((unsigned char)(LINE2_1STCH), "--------");
			}
        }

		/*** SLAVE B PUBLISH ***/
		/* Publish Switch Node B */
		/* Switch 1-3 pressed? */ 
        if (SW1 == ACTIVE)
		{
			DisplayString((unsigned char)(LINE1_1STCH + 2), "S1");
			l_u8_wr((unsigned char)LIN_SIG_NODE_B_SWITCH, 0x01);
		}
        else if	(SW2 == ACTIVE)
		{
			DisplayString((unsigned char)(LINE1_1STCH + 2), "S2");
			l_u8_wr((unsigned char)LIN_SIG_NODE_B_SWITCH, 0x02);
		}
		else if	(SW3 == ACTIVE)
		{
			DisplayString((unsigned char)(LINE1_1STCH + 2), "S3");
			l_u8_wr((unsigned char)LIN_SIG_NODE_B_SWITCH, 0x03);
		}
		else /* Switches off */
		{
			DisplayString((unsigned char)(LINE1_1STCH + 2), "--");
			l_u8_wr((unsigned char)LIN_SIG_NODE_B_SWITCH, 0x00);
		}
			
		/* Publish Node B AD */
		read_ad();
     	if (ad_val != prev_ad_val)
     	{
			l_u16_wr((unsigned char)LIN_SIG_NODE_B_AD, ad_val);
			prev_ad_val = ad_val;
			lcd_show_2dig_hex((unsigned char)ad_val, (unsigned char)(LINE1_1STCH + 6));
     	}

		/* Publish Node B Counter */
		if (counter != prev_counter)
		{
			prev_counter = counter;
			l_u8_wr((unsigned char)LIN_SIG_NODE_B_COUNTER, counter);
			lcd_show_2dig_hex(counter, (unsigned char)(LINE1_1STCH + 4));
		}
   	}
}

/******************************************************************************
Function Name: 	    read_ad
Parameters:         none
Return value:       none
Description:	    Read the potentiometer A/D input (AN11)
******************************************************************************/
static void read_ad(void)
{
   /* Read the potentiometer A/D input (AN11).  */
   adgsel0 = 1;
   adcon2 = 0x01;
   adcon0 = 0x94;
   adcon1 = 0x20;
   adst = 1;
   while (adst == 1)
   		;
   ad_val = (unsigned char) adl;
}

/******************************************************************************
Function Name: 	    counter_init
Parameters:         none
Return value:       none
Description:	    Application timer init for the Node B counter
******************************************************************************/
/* 1 sec. application timer for node counter */
static void counter_init(void)
{
   trbic = 0x01;     /* Enable Timer RB interrupt at priority 1.  */
   trbioc = 0x00;    /* Set Timer RB I/O control register to no output.  */
   trbmr = 0x10;     /* Set Timer RB mode register to timer mode, count from f8.  */
   trbpre = 0xA0;    /* Prescaler = 1/250 (100us): 0xf9.  */
   trbpr = 0xA0;     /* TimerRB = 1/200 (20ms): 0xc7  */
   tstart_trbcr = 1; /* TimerRB start.  */
   pd3_1 = 1;
   return;
}

/******************************************************************************
Function Name: 	    AppTimer_ISR
Parameters:         none (this is an ISR)
Return value:       none (this is an ISR)
Description:	    1 sec. application timer for the Node B counter
******************************************************************************/
/* 1 sec. application timer for node counter interrupt */
#pragma INTERRUPT AppTimer_ISR
void AppTimer_ISR(void)
{
   static unsigned int i = 0;

   _asm("FSET I");      /* Re-enable interrupts.  */
   i++;
   /* Increment every x times; Formula: 10MHz/8*(0xA0+1)(*0xA0+1)) = 0x30. See counter_init. */
   if (i == 0x30)
   {
      	i = 0;
		
		/* The counter increments ~every second and is useful to check 
		that LIN is running without human interaction */
		counter++;
   }
   app_run = 1;    /* Set the application start flag.  */
   return;
}

/******************************************************************************
Function Name: 	    display_intro_0
Parameters:         none
Return value:       none
Description:	    Startup LCD message
******************************************************************************/
static void display_intro_0(void)
{
   	/* Slave display intro 0 */

	const char* rp = renstring;
	const char* tp = techstring;
	unsigned char i = 0;

	/* Leave for test */
	//lcd_show_2dig_hex(0x12, 0); lcd_show_2dig_hex(0x34, 4); lcd_show_4dig_hex(0x12, 0x34, 16); lcd_show_4dig_hex(0x56, 0x78, 20);
	DisplayString((unsigned char)(LINE1_1STCH + 0), rp);
	DisplayString((unsigned char)(LINE2_1STCH + 0), tp);
	pause(INTROPAUSE);
	DisplayString((unsigned char)(LINE1_1STCH + 0), "        ");
	DisplayString((unsigned char)(LINE2_1STCH + 0), "        ");
	while (i++ < 8)
	{
		DisplayString((unsigned char)(LINE1_1STCH + i), rp);
		DisplayString((unsigned char)(LINE2_1STCH + i), tp);
		if (i < 8)
		{
			DisplayString((unsigned char)(LINE1_1STCH + 0), "        ");
			DisplayString((unsigned char)(LINE2_1STCH + 0), "        ");
		}
	}
}

/******************************************************************************
Function Name: 	    display_intro_1
Parameters:         none
Return value:       none
Description:	    Startup LCD message
******************************************************************************/
void display_intro_1(void)
{
   	/* Slave display intro 1 */

	DisplayString((unsigned char)(LINE1_1STCH + 0), "  LIN   ");
	DisplayString((unsigned char)(LINE2_1STCH + 0), " Node B ");
	pause(INTROPAUSE);
}

/******************************************************************************
Function Name: 	    display_intro_2
Parameters:         none
Return value:       none
Description:	    Startup LCD message
******************************************************************************/
void display_intro_2(void)
{
   	/* Slave display intro 2 */
	/* This should be shown if the code gets stuck in using the uart. */
	DisplayString((unsigned char)(LINE1_1STCH + 0), "power up");
	DisplayString((unsigned char)(LINE2_1STCH + 0), "trnscvr");
}

/******************************************************************************
Function Name: 	    display_intro_3
Parameters:         none
Return value:       none
Description:	    Startup LCD message
******************************************************************************/
void display_intro_3(void)
{
	/* Slave display intro 3 */
	DisplayString((unsigned char)(LINE1_1STCH), 	"B:------");
	DisplayString((unsigned char)(LINE2_1STCH ), 	"M:------");
	pause(INTROPAUSE);
}

/******************************************************************************
Function Name: 	    pause
Parameters:         i; nr of loops to do x n
Return value:       none
Description:        Simple pause for switch debounce and displaying text
******************************************************************************/
void pause(int i)
{
	/* Pausing with i = 1 will cause delay looping of n times */
	unsigned int n = PAUSEFACTOR;
	while (i--)
	{	
		while (n--)
		{	
			/*asm("nop")*/;
		}
	}
	n = PAUSEFACTOR;
}
/* eof */

⌨️ 快捷键说明

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