本帖最后由 tao560532 于 2011-12-3 19:20 编辑
以前玩的最熟的就是串口了,在51里只要设置好波特率,直接往SBUF赋值就可以发送了,
然后接收数据就是查询RI位,当至高时就进入串口服务函数读取SBUF中的数据,
现在看看M0中的UART是怎么回事,首先看看芯片手册:
图中说120提供3个通道,现在对产品规格还不是很清楚。
在规格说明书里就清楚明白的显示了这款芯片所有的资源。
有关于串口的一些配置就看手册,
串口的参数主要有:波特率,数据位,停止位,校验位,当然在M0还有FIFO存储深度,还有超时设定。
- /***************************************************************************************
- * File Name : UART_KEY_TIME.c
- * Copyright : 2011-2011 ecjtu Corporation,All Rights Reserved
- * Module Name : UART_KEY_TIME
- *
- * CPU : Cortex-M0
- * RTOS : NO
- * Create Date : 2011/12/02
- * Author/Corporation : 涛行天下/华东交通大学
- *
- * Abstract Description : 串口通信,按键
- *---------------------------Revision History--------------------------------
- * Revision History
- * No. Version Date Revised by Item Description
- * 1 V0.0 11.12.02 涛行天下 ... ....
- ***************************************************************************************/
- /*
- 利用定时器对1秒时间内KEY2键的操作情况进行检测,
- 并通过串口将测试结果传给PC
- */
- #include <stdio.h>
- #include "NUC1xx.h"
- #include "DrvGPIO.h"
- #include "DrvSYS.h"
- #include "DrvUART.h"
- ///////////////////////////////////////////////////////
- uint8_t test ;
- uint8_t inchar[1];
- uint32_t TimerCOUN=0;
- /***************************************************************************************
- * Function Name : TMR0_IRQHandler
- * Create Date : 2011/12/02
- * Author/Corporation : your name/your company name
- *
- * Description : Find a proper thread in thread array
- *
- * Param : ThreadNo : someParam description
- ThreadStaus : someParam description
- *
- *
- * Return Code : Return Code description
- eg:
- ERROR_Fail : not find a thread
- ERROR_SUCCEED : found
- * Global Variable : DISP_wuiSegmentAppID
- * File Static Variable : naucThreadNo
- * Function Static Variable : None
- *
- *----------------------------------------------------
- * Revision History
- * No. Date Revised by Item Description
- * V0.5 2011/12/02 涛行天下 ... ....
- ***************************************************************************************/
- void TMR0_IRQHandler(void) // Timer0 interrupt subroutine
- {
- TIMER0->TISR.TIF =1;
- TimerCOUN++;
- if(test)
- {
- if( TimerCOUN&0x0020 )
- DrvGPIO_SetBit(E_GPA,5);
- else
- DrvGPIO_ClrBit(E_GPA,5);
- }
- }
- void UART_INT_HANDLE(uint32_t u32IntStatus)
- {
- if(u32IntStatus & DRVUART_RDAINT)
- {
- /* Get all the input characters */
- while(UART0->ISR.RDA_IF==1)
- {
- DrvUART_Read(UART_PORT0,inchar,1); /* 保存输入按键 */
- }
- }
- }
- void delay()
- {
- uint32_t i;
- for(i=80000;i;i--);
- }
- void delay_1s()
- {
- uint32_t i;
- for(i = 0; i < 200; i++)
- {
- DrvSYS_Delay(1000);
- }
- }
- int main (void)
- {
- uint8_t count;
- uint32_t counter;//计数输出
- uint32_t loop=1;
- STR_UART_T param; //串口参数
- UNLOCKREG(); //解除锁定寄存器
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //SYSCLK->WRCON.XTL12M_EN = 1;
- DrvSYS_Delay(5000);
- LOCKREG(); //重新锁定寄存器
- DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); //使能UART时钟 UART时钟源选择. 00 =外部12MHz 晶振
- DrvGPIO_InitFunction(E_FUNC_UART0); //GPB_MFP0-1-2-3置位 GPIO使能UART功能
- param.u32BaudRate = 115200; //波特率为115200
- param.u8cDataBits = DRVUART_DATABITS_8; //数据位8
- param.u8cStopBits = DRVUART_STOPBITS_1; //停止位 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("串口0打开失败!\n");
- DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT,UART_INT_HANDLE); //使能接收数据中断,回调函数 UART_INT_HANDLE
- /*---------------------------------------------------------------------------------------------------------------------*/
- /*设置IO口都是输出状态*/
- DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT ); //led端口设置为输出
- DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPB, 14, E_IO_INPUT ); //按键端口设置为输入
- DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );
- DrvGPIO_ClrBit( E_GPA, 2 ); //程序运行指示
- DrvGPIO_ClrBit( E_GPA, 3 );
- DrvGPIO_ClrBit( E_GPA, 4 );
- DrvGPIO_ClrBit( E_GPA, 5 );
- while(1)
- {
- printf("Counter:\n %d",counter);
- counter += 1;
- delay_1s();
- }
- }
|