📄 uart_main.c
字号:
/*""FILE COMMENT""*****************************************************
* M32R C Programming Rev. 1.00
* < Sample Program for Serial I/O (UART) (main routine) >
*
* Copyright (c) 2003 Renesas Technology Corporation
* And Renesas Solutions Corporation
* All Rights Reserved
*********************************************************************/
/************************************************************************/
/* Include file */
/************************************************************************/
#include "..\inc\sfr32170_pragma.h"
/************************************************************************/
/* Function prototype declaration */
/************************************************************************/
void main(void); /* Main function */
void init_func(void); /* Initial setup function */
void port_init(void); /* Initialize port */
/************************************************************************/
/* Definition of external reference */
/************************************************************************/
extern void DisInt( void ); /* Interrupt disable function */
extern void EnInt( void ); /* Interrupt enable function */
extern void SIO0_init( void); /* Initialize SIO0 */
extern void SIO1_init( void); /* Initialize SIO1 */
extern void SIO0_Tr( unsigned char *, unsigned int); /* Transmit from SIO0 */
extern void SIO1_Tr( unsigned char *, unsigned int); /* Transmit from SIO1 */
/************************************************************************/
/* Global variable */
/************************************************************************/
unsigned char RcvBuf0[10]; /* Receive buffer */
unsigned char RcvBuf1[10]; /* Receive buffer */
unsigned char RcvBuf2[10]; /* Receive buffer */
unsigned char RcvBuf3[10]; /* Receive buffer */
unsigned char RcvBuf4[10]; /* Receive buffer */
unsigned char RcvBuf5[10]; /* Receive buffer */
unsigned char rx_cnt0; /* Pointer to receive buffer */
unsigned char rx_cnt1; /* Pointer to receive buffer */
unsigned char rx_cnt2; /* Pointer to receive buffer */
unsigned char rx_cnt3; /* Pointer to receive buffer */
unsigned char rx_cnt4; /* Pointer to receive buffer */
unsigned char rx_cnt5; /* Pointer to receive buffer */
/*""FUNC COMMENT""*******************************************************
* Function name: init_func()
*-----------------------------------------------------------------------
* Description : Call various initialization functions
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : -
*""FUNC COMMENT END""***************************************************/
void init_func(void)
{
port_init(); /* Initialize port */
}
/*""FUNC COMMENT""*******************************************************
* Function name: port_init()
*-----------------------------------------------------------------------
* Description : Port initialization
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : -
*""FUNC COMMENT END""***************************************************/
void port_init(void)
{
PIEN = PIEN0; /* Enable port input */
/*** LED output port ***/
P11DATA = 0x00; /* Output data (must be set prior to mode) */
P11DIR = 0xff; /* P110-P117 : Output mode */
P11MOD = 0x00; /* P110-P117 : Input/output port */
}
/*""FUNC COMMENT""*******************************************************
* Function name: main()
*-----------------------------------------------------------------------
* Description : Receives data (increment pattern) from SIO1 after being transmitted in 500 Kbps clock-synchronized
* : serial mode (when the source clock frequency = 10 MHz) from SIO0 and
* : if the received data is the same as transmitted, displays it on LED(port 11).
* : If unable to receive the same data, it displays 0x55 in blinking inverse video.
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : -
*""FUNC COMMENT END""***************************************************/
void main(void)
{
unsigned char i;
unsigned long j;
unsigned char data;
unsigned char rx_cnt1_bak;
rx_cnt1 = rx_cnt1_bak = 0;
i = 0;
DisInt(); /* Disable interrupt */
init_func(); /* Initialize microcomputer */
SIO0_init(); /* Initialize SIO0 */
SIO1_init(); /* Initialize SIO1 */
EnInt(); /* Enable interrupt */
while(1) {
SIO0_Tr( &i, 1); /* Send data from SIO0 */
while( rx_cnt1 == rx_cnt1_bak); /* Wait for data to receive */
data = RcvBuf1[ rx_cnt1 - 1]; /* Read out received data */
if( rx_cnt1 == 10) { /* Check for receive counter overflow */
rx_cnt1 = 0;
}
rx_cnt1_bak = rx_cnt1;
if( data == i) {
P11DATA = data; /* Display received (transmitted) data */
i++; /* Increment transmit data */
} else { /* Received data in error */
P11DATA = 0x55;
while(1) {
for( j = 0; j < 1000000; j++); /* Wait */
P11DATA ^= 0xff; /* Display 0x55 in blinking inverse video */
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -