www.pudn.com > RL78_G14-Demo.rar > main.c, change:2013-04-25,size:5897b


/************************************************************* 
  Copyright (C),NEC Electronics (China) 
  File name:	main.c 
  Author:    	Version:1.0        Date:2009.5.15 
  Description: USE uart & timer for infrared transmit & receive. 
  Others: 
  Function List: 
  History: 
    1. Date: 
       Author: 
       Modification: 
    2. ... 
 
**************************************************************/ 
 
 
#pragma sfr 
#pragma di 
#pragma ei 
#pragma nop 
#pragma interrupt INTST0 MD_INTST0 
#pragma interrupt INTSR0 MD_INTSR0 
#pragma interrupt INTSRE0 MD_INTSRE0 
 
#define TXCOUNT		10 
 
void CG_init(); 
void Macro_init(); 
void Macro_start(); 
void TAU_Init(); 
void TAU_Channel1_Start(); 
void TAU_Channel1_Stop(); 
void SAU0_Init(); 
void UART0_Start(); 
void UART0_Stop(); 
void main(); 
 
unsigned char send_cnt; 
unsigned char rec_cnt; 
unsigned char rec_buf[TXCOUNT]; 
 
/*main routine */ 
void main() 
{ 
	DI(); 
	CG_init();			//Clock init:  CPU 4M(default)	 
	Macro_init(); 
	EI(); 
	Macro_start(); 
	 
	send_cnt = TXCOUNT; 
	TXD0 = 0xAA;		//send first data 
	 
	while(1) 
		{ 
			/* main loop, enquiry one by one */ 
		}; 
} 
 
/* This function is used to initial the CG macro */ 
void CG_init() 
{ 
	CMC = 0x00; 
	CSC = 0xc0; 
	CKC = 0x09;			/*select MCU frequency:       MDIV2    MDIV1    MDIV0            */ 
						/*                              0        0        0      fIH     */ 
						/*                              0        0        1      fIH/2   */ 
						/*                              0        0        0      fIH/2^2 */ 
						/*                              0        0        0      fIH/2^3 */ 
						/*                              0        0        0      fIH/2^4 */ 
} 
 
/* This function is used to initial all the macro used */ 
void Macro_init() 
{ 
	SAU0_Init(); 
	TAU_Init(); 
} 
 
/* This function is used to start the macro you need*/ 
void Macro_start() 
{ 
	TAU_Channel1_Start();		//start 38K square wave output 
	UART0_Start(); 
 
} 
/**-----------------------------------------------------------------------------*/ 
/*38K Square wave output*/ 
void TAU_Init() 
{ 
	TAU0EN = 1;			/* supplies input clock */ 
	TPS0 = 0x00F0;		/* ck01-fclk/2^15 */ 
	/* Stop all channels */ 
	TT0 = 0; 
 
	/* Mask channel 1 interrupt */ 
	TMMK01 = 1;		/* INTTM01 disabled */ 
	TMIF01 = 0;		/* INTTM01 interrupt flag clear */ 
 
 
	/* Channel 1 used as square output function */ 
	TMR01 = 0;	 
	TDR01 = 0x33;	 
	TOM0 &= ~0x0002;	//set toggle mode 
	TOL0 &= ~0x0002;	//clear to 0 when TOM0n = 0 (toggle mode) 
	TO0 &= ~0x0002;	    //set Timer output level register 
	TOE0 |= 0x0002;	    //Enable TO01 output operation by counting operation 
 
	/* TO01(P16) output */ 
	PM1 &= ~0x40; 
	P1 &= ~0x40; 
} 
 
void TAU_Channel1_Start() 
{ 
	TOE0 |= 0x0002; 
	TS0 |= 0x0002;		//count start 
} 
/* 
void TAU_Channel1_Stop() 
{ 
	TT0 |= TAU_CH1_STOP_TRG_ON; 
	TOE0 &= ~TAU_CH1_OUTPUT_ENABLE; 
	// Mask channel 1 interrupt  
	TMMK01 = 1;		//INTTM01 disabled  
	TMIF01 = 0;		// INTTM01 interrupt flag clear  
v} 
*/ 
 
/* This function is used to initial SAU0,receive & transmit,1200bps,8bit data */ 
void SAU0_Init() 
{ 
	SAU0EN = 1;	/* SAU0 clock supply */ 
	NOP(); 
	NOP(); 
	NOP(); 
	NOP(); 
	SPS0 = 0xB7; /* SAU_CK00_FCLK_7 | SAU_CK01_FCLK_11*/ 
 
		/* UART0 initial setting */ 
	ST0 |= 0x0003;	/* UART0 receive & transmit disable */ 
	SRMK0 = 1;		/* INTSR0 disable */ 
	SREMK0 = 1;		/* INTSRE0 disable */ 
	STMK0 = 1;		/* INTST0 disable */ 
	SRIF0 = 0;		/* INTSR0 IF clear */ 
	SREIF0 = 0;		/* INTSRE0 IF clear */ 
	STIF0 = 0;		/* INTST0 IF clear */ 
	 
	SIR01 = 0x0007;  //clear PEFmn,PEFmn,OVFmn flag of SSRmn 
	NFEN0 |= 0x01; 	//RXD0 noise filter on 
	SMR01 = 0x0122;  //set operation mode 
	//use CKm0 clcok,valid edge of RXD,falling edge as start bit,uart mode 
	SCR01 = 0x4417; 
	//Reception only,INTSRE_ENABLE,No PARITY bit,MSB first,1 STOP bit,8 data bit 
	SDR01 = 0x1800;//set a receive band rate DIVISOR;   
	 
	SMR00 = 0x0023; 
	//use CKm0 clcok,only software trigger is valid,uart mode,buffer empty interrupt 
	SCR00 = 0x8017; 
	//TRANSMISSION,INTSRE_MASK,No PARITY bit,MSB first,1 STOP bit,8 data bit 
	SDR00 = 0x1800;//UART0_TRANSMIT_DIVISOR; 
	SOL0 |= 0;	//Communication data is output as is 
	SO0 |= 0x0001;	/* Serial data output value is "1" */ 
	SOE0 |= 0x0001;	/* UART0 output enable */ 
	 
	/* INTSR0 priority high */ 
	SRPR10 = 0; 
	SRPR00 = 0; 
	/* INTSRE0 priority low */ 
	SREPR10 = 1; 
	SREPR00 = 1; 
	/* INTST0 priority low */ 
	STPR10 = 1; 
	STPR00 = 1; 
	/* RXD0 pin setting */ 
	PM3 |= 0x01; 
	/* TxD0 pin setting */ 
	P5 |= 0x02; 
	PM5 &= ~0x02; 
 
} 
/* This function is used to start UART0 operation */ 
void UART0_Start() 
{ 
	SRMK0 = 0;			// INTSR0 enable  
	SREMK0 = 0;			// INTSRE0 enable 
	STMK0 = 0;			// INTST0 enable  
	SS0 |= 0x0002;		// UART0 receive enable  
	SS0 |= 0x0001;		// UART0 transmit enable  
} 
 
/* This function is used to stop UART0 operation */ 
/* 
void UART0_Stop() 
{ 
	ST0 |= 0x0002;	// UART0 receive disable  
	ST0 |= 0x0001;	// UART0 transmit disable  
	SRMK0 = 1;		// INTSR0 disable  
	SREMK0 = 1;		// INTSRE0 disable  
	STMK0 = 1;		// INTST0 disable  
	SRIF0 = 0;		// INTSR0 IF clear  
	SREIF0 = 0;		// INTSRE0 IF clear  
	STIF0 = 0;		// INTST0 IF clear  
}*/ 
 
/*UART0 receive interrupt routine*/ 
__interrupt void MD_INTSR0( void ) 
{ 
	if(rec_cnt < TXCOUNT) 
	{ 
	rec_buf[rec_cnt++] = RXD0;			//read the receive data 
	}	 
} 
 
/*UART0 receive error interrupt routine*/ 
__interrupt void MD_INTSRE0( void ) 
{ 
	unsigned char err_type; 
	if(rec_cnt < TXCOUNT) 
	{ 
	rec_buf[rec_cnt++] = RXD0;				//read the receive data 
	}	 
	err_type = (unsigned char)(SSR01 & 0x0007);	 
	SIR01 = (unsigned short)err_type;		//clear error flag 
} 
 
/*UART0 transmit interrupt routine*/ 
__interrupt void MD_INTST0( void ) 
{ 
	send_cnt--; 
	if(send_cnt> 0) 
	{ 
		TXD0 = 0xAA + send_cnt;			//send data 
	} 
}