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

📄 main_async.c

📁 Data logger Serial Communication function based on new Renesus microcontroller. This module has been
💻 C
字号:


/***********************************************************************************
System Includes
***********************************************************************************/
/*	Following header file provides access to intrinsic functions to control interrupt
	levels.	*/
#include <machine.h>

/***********************************************************************************
User Includes
***********************************************************************************/
/*	Following header file provides a structure to access on-chip I/O registers. */
#include "iodefine.h"
/*	Following header file provides common defines for widely used items.	*/
#include "rsk38347def.h"
/*  Following header file provides prototype for main function */
#include "main_async.h"

/***********************************************************************************
Global variables
***********************************************************************************/
/*	declare UART3_2 receive variable	*/
unsigned char uart3_in=0;		 		

/*	String constants used for screen output far since they are outside 64k boundary */
const char cmd_clr_scr[] = {27,'[','2','J',0};
const char cmd_cur_home[] = {27,'[','H',0};

/***********************************************************************************
User Program Code
***********************************************************************************/

/***********************************************************************************
Function Name : main    
Description	  : Main function.
		This function calls a fucntion to configure the SCI in asynchronous
		mode. It then sends message strings to the port. User will receive 
		the numbers 0-9 continuously unless key 'z' is pressed to stop the
		reception. Any other key press will restart the reception.
Parameters	  : none                   
Return Value  : none   
***********************************************************************************/
void main(void)
{
	/*	Variables used during transmission process.	*/
	unsigned char ucCount;	
	unsigned char ucConvert;	
	unsigned short usDelay;	

	/* UART initialization */			
	uart_init();		

	/* Text to be displayed at the beginning of the program output
   	to the terminal window (\r\n = carriage return & linefeed) */
	text_write(cmd_clr_scr);	 	
	text_write(cmd_cur_home);	 	
	text_write("Renesas Technology Corporation \r\n");	   
	text_write("RSKH838347 UART demo. \r\n");	   
	text_write("Press z to stop, any key to continue. \r\n");

	while (1)
	{
		/* count as long as "z" wasn't pressed, send carrige return and
		   line feed and turn on green LED */
		while ( 1)//uart3_in != 'z')
		{
			/* Send carriage return and line feed */
			text_write("\r\n");		

			/* Indicate that the transmission is in progress.	*/
	 		LED1 = LED_OFF;			
	 		LED0 = LED_ON;

			/* count 0 to 9 */
			for (ucCount=0;(ucCount<=9)&&(uart3_in!='z');ucCount ++)	
			{
				/* Convert count data to ASCII */	
	 			ucConvert = (unsigned char)((unsigned short)ucCount + 0x30);	  				

				while(SCI32.SSR.BIT.TDRE == 0)
				{
					/* Wait here until the character is transmitted completely */
				}

		 		/* Put ASCII data in transmit buffer */
				SCI32.TDR = ucConvert;
				LED2 = LED_ON;
					
				for (usDelay=0xffff; usDelay>0; usDelay--)
				{
					/* Delay to allow trasmitting last character */
				}	 	
			}
		}

	/*	Do nothing while stopped (after "z" is pressed)	*/
	nop();

	}									
}
/***********************************************************************************
End of function main
***********************************************************************************/

/***********************************************************************************
Function Name : uart_init   
Description	  : SCI3_2 initialization.
			  	UART Settings - 19200 baud, 8 data bits, 1 stop bit, no parity.
Parameters	  : none				 
Return Value  : none
***********************************************************************************/
void uart_init(void) 
{
	unsigned char dummy;

	/* Bits CKE1 & CKE0 are set to 00 */
	SCI32.SCR3.BYTE = 0x00;
	
	/* SCI is configured for asynchronous communication, 8 bit data, 1 stop bit,
	   no parity. cpu clock has been selected as a source clock for UART */
	SCI32.SMR.BYTE = 0x00;

	/* Set baud rate to 19.2 K */
	SCI32.BRR = BAUD_19200;

	/* Settling delay */
	nop();
	nop();

	/* Pin P4_2 will be used as TXD32 */
	SPCR.BYTE = 0xE0;
	
	/* Clear status flags */
	SCI32.SSR.BYTE = 0x00;
	
	/* enable transmission & reception, enable receive interrupt */
	SCI32.SCR3.BYTE |= 0x70;

	/* dummy read */
	dummy = SCI32.RDR;

	/*	Reset global Interrupt mask bit to 0 in order to enable interrupts.	*/
	set_imask_ccr((unsigned char)0);
}
/***********************************************************************************
End of function uart_init
***********************************************************************************/

/***********************************************************************************
Function Name : text_write
Description	  : This function sends a text string to the terminal program.
Parameters	  : msg_string -> the text string to output.
Return Value  : none   
***********************************************************************************/
void text_write(const char * msg_string)
{
	char i;
			
	/* This loop reads in the text string and puts it in the UART transmit
	   buffer */
	for (i=0; msg_string[i]; i++)
	{	 
		while(SCI32.SSR.BIT.TDRE == 0)
		{
			/* Wait here until the character is transmitted completely */
		}

		/* Write the character out */
		SCI32.TDR = msg_string[i];
	}
}
/***********************************************************************************
End of function text_write
*******************************************************************************/

/***********************************************************************************
ISR Name 	 : INT_SCI3_2       
Description	 : Interrupt routine for UART receive.
			   Reads character received from keyboard and stores in 'uart3_in'
			   variable.
Parameters	 : none                   
Return Value : none   
***********************************************************************************/
__interrupt(vect=18) void INT_SCI3_2(void)
{
	/*	Make sure receive process is complete	*/
	while(SCI32.SSR.BIT.RDRF == 0)
	{
		/* Wait here until the character is received */
	}

	/*	Read in received data.	*/
	uart3_in = SCI32.RDR;

	/*	Clear the receive buffer full flag.	*/
	SCI32.SSR.BIT.RDRF = 0;

	/* If "z" was entered: wait for previous transmission to complete, echo "z"
	   to screen and  turn LED2 ON */
	if (uart3_in == 'z')
	{
		/* wait for previous transmission to complete */			
		while(SCI32.SSR.BIT.TDRE == 0)
		{
			/* Wait here until the character is transmitted completely */
		}
				
		/* echo "z" to screen */ 	 
		SCI32.TDR = 'z';			  	

		/* Indicate that the transmission is stopped.	*/
		LED1 = LED_ON;			
		LED0 = LED_OFF;
	}	
}
/***********************************************************************************
End of ISR INT_SCI3_2
***********************************************************************************/

⌨️ 快捷键说明

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