本帖最后由 weshiluwei6 于 2011-4-15 20:09 编辑
看到Swallow_0322大哥 一直用串口编程序 很是心动啊,便仔细阅读了手册和Swallow_0322大哥的程序,按自己的编程的习惯修改了下,有些语句做了点注释,比大哥的差很多,不过自己能看懂。向菜农大叔致敬,感谢大叔和Swallow_0322大哥。-
- /***************************************************************************:**************
- ** 文件名称:NUC120_HOT_UART.c
- ** 文件说明:NUC120助学板串口0练习程序
- ** 创建日期:2011-04-014
- ** 修改日期:
- ** 现 象:打开串口调试工具,会出现以下的几行字,
- 输入数据之后,会显示你输入的数据
- ** 备 注:Uart0中断接收查询发送练习
- ***********************************************************:****::************************/
- #include <stdio.h>
- #include "NUC1xx.h"
- #include "Driver\DrvGPIO.h"
- #include "Driver\DrvSYS.h"
- #include "Driver\DrvUART.h"
- volatile uint16_t comRnum = 0;
- volatile uint16_t comTnum = 0;
- uint8_t comRbuf[100];
- uint32_t comRbytes = 0;
- /*****************************************************************************************
- 函数声明
- *****************************************************************************************/
- void Init_System (void);
- void Init_Uart (void);
- void UART_INT_HANDLE(uint32_t u32IntStatus);
- /*****************************************************************************************
- ** Function Name : UART_INT_HANDLE
- ** Descriptions : UART Callback function
- ** Input parameters: u32IntStatus
- ** OutPut parameters: None
- ** Returned value :
- *****************************************************************************************/
- void UART_INT_HANDLE(uint32_t u32IntStatus)
- {
- uint8_t InChar[1]={0xFF};
- if(u32IntStatus & DRVUART_RDAINT)
- {
- /* Get all the input characters */
- while(UART0->ISR.RDA_IF==1) //字节数等于RFITL,RDA_if 1
- {
- /* Get the character from UART Buffer */
- DrvUART_Read(UART_PORT0,InChar,1);
- /* 2-存放接受到的数据缓冲指针,3-要接收的字节数 */
- /* Check if buffer full */
- /* Enqueue the character */
- comRbuf[comRnum] = InChar[0];
- comRnum++;
- comRbytes++;
-
- }
- }
- }
- /****************************************************************************************
- ** Function Name : Init_System
- ** Descriptions : 系统初始化函数
- ** Input parameters: None
- ** OutPut parameters: None
- ** Returned value :
- ****************************************************************************************/
- void Init_System(void)
- {
- UNLOCKREG();
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
- DrvSYS_Delay(5000);
- LOCKREG();
- }
- /***************************************************************************************
- ** Function Name : Init_Uart
- ** Descriptions : 串口初始化函数
- ** Input parameters: None
- ** OutPut parameters: None
- ** Returned value :
- ***************************************************************************************/
- void Init_Uart(void)
- {
- STR_UART_T param;
- /* 声明 UART设置的结构体 位于DRVUART.H
- 结构体如下
- typedef struct DRVUART_STRUCT
- {
- uint32_t u32BaudRate;
- E_DATABITS_SETTINGS u8cDataBits;
- E_STOPBITS_SETTINGS u8cStopBits;
- E_PARITY_SETTINGS u8cParity;
- E_FIFO_SETTINGS u8cRxTriggerLevel;
- uint8_t u8TimeOut;
- }STR_UAR_T;
- */
- DrvSYS_SelectIPClockSource( E_SYS_UART_CLKSRC,0 ); //设定IP时钟源,看门狗,模数,定时器,UART,CAN,PWM
- /* UART时钟源选择. 00 =外部12MHz 晶振 01 = PLL 1x =内部 22MHz 振荡器 */
-
- DrvGPIO_InitFunction(E_FUNC_UART0); //GPB_MFP0-1-2-3置位 GPIO使能UART功能
- param.u32BaudRate = 115200; //波特率
- param.u8cDataBits = DRVUART_DATABITS_8; //数据位
- param.u8cStopBits = DRVUART_STOPBITS_1; //停止位
- param.u8cParity = DRVUART_PARITY_NONE; //校验位
- param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; //FIFO存储深度 1 字节
- param.u8TimeOut = 0; //FIFO超时设定
- if( DrvUART_Open(UART_PORT0,¶m) != E_SUCCESS) //初始化串口错误
- printf("UART0 open failed\n");
- /* Enable Interrupt and install the call back function */
- DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT,UART_INT_HANDLE);//回调函数指针
- /* DRVUART_MOSINT/DRVUART_THREINT/DRVUART_RDAINT/DRVUART_TOUTINT */
- }
- /***************************************************************************************
- ** Function Name : main
- ** Descriptions : 主函数
- ** Input parameters: None
- ** OutPut parameters: None
- ** Returned value :
- ***************************************************************************************/
- int main (void)
- {
- uint8_t test = 250;
- uint8_t OutChar[1] = {0xff};
- uint16_t temp;
-
- Init_System();
- Init_Uart();
- DrvGPIO_Open (E_GPA,2,E_IO_OUTPUT);
- DrvGPIO_ClrBit(E_GPA,2);
-
- printf("\n");
- printf("/*==========================\n");
- printf("======菜农 %d 助学计划======\n",test);
- printf("========NUC120助学板========\n");
- printf("======菜鸟傻瓜250水王======\n");
- printf("=======2011年04月14日=======\n");
- printf("==========Uart0实验=========\n");
- printf("===请输入任意字符开始测试!==\n");
- printf("==========================*/\n");
- printf("您输入的内容为:");
- while(1)
- {
- temp = comRnum;
- if(comTnum != temp)
- {
- OutChar[0] = comRbuf[comTnum];
- DrvUART_Write(UART_PORT0,OutChar,1);
- /* u32Port -[in] UART Channel: UART_PORT0 / UART_PORT1 /UART_PORT2 */
- /* pu8RxBuf -[in] Specify the buffer to send the data to transmission FIFO. */
- /* u32ReadBytes -[in] Specify the byte number of data. */
- comTnum++;
- comRbytes--;
- }
- }
- }
|