[菜农助学交流] UART--printf打印counter计数

[复制链接]
4235|2
 楼主| tao560532 发表于 2011-12-3 18:58 | 显示全部楼层 |阅读模式
本帖最后由 tao560532 于 2011-12-3 19:20 编辑

串口通信UART

以前玩的最熟的就是串口了,在51里只要设置好波特率,直接往SBUF赋值就可以发送了,

然后接收数据就是查询RI位,当至高时就进入串口服务函数读取SBUF中的数据,
现在看看M0中的UART是怎么回事,首先看看芯片手册:


图中说120提供3个通道,现在对产品规格还不是很清楚。

在规格说明书里就清楚明白的显示了这款芯片所有的资源。


有关于串口的一些配置就看手册,
串口的参数主要有:波特率,数据位,停止位,校验位,当然在M0还有FIFO存储深度,还有超时设定。

  1. /***************************************************************************************
  2. * File Name : UART_KEY_TIME.c
  3. * Copyright : 2011-2011 ecjtu Corporation,All Rights Reserved
  4. * Module Name : UART_KEY_TIME
  5. *
  6. * CPU : Cortex-M0
  7. * RTOS : NO
  8. * Create Date : 2011/12/02
  9. * Author/Corporation : 涛行天下/华东交通大学
  10. *
  11. * Abstract Description : 串口通信,按键
  12. *---------------------------Revision History--------------------------------
  13. * Revision History
  14. * No. Version Date Revised by Item Description
  15. * 1 V0.0 11.12.02 涛行天下 ... ....
  16. ***************************************************************************************/
  17. /*
  18. 利用定时器对1秒时间内KEY2键的操作情况进行检测,
  19. 并通过串口将测试结果传给PC
  20. */

  21. #include <stdio.h>
  22. #include "NUC1xx.h"
  23. #include "DrvGPIO.h"
  24. #include "DrvSYS.h"
  25. #include "DrvUART.h"
  26. ///////////////////////////////////////////////////////
  27. uint8_t test ;
  28. uint8_t inchar[1];
  29. uint32_t TimerCOUN=0;


  30. /***************************************************************************************
  31. * Function Name : TMR0_IRQHandler
  32. * Create Date : 2011/12/02
  33. * Author/Corporation : your name/your company name
  34. *
  35. * Description : Find a proper thread in thread array
  36. *
  37. * Param : ThreadNo : someParam description
  38. ThreadStaus : someParam description
  39. *
  40. *
  41. * Return Code : Return Code description
  42. eg:
  43. ERROR_Fail : not find a thread
  44. ERROR_SUCCEED : found
  45. * Global Variable : DISP_wuiSegmentAppID
  46. * File Static Variable : naucThreadNo
  47. * Function Static Variable : None
  48. *
  49. *----------------------------------------------------
  50. * Revision History
  51. * No. Date Revised by Item Description
  52. * V0.5 2011/12/02 涛行天下 ... ....
  53. ***************************************************************************************/
  54. void TMR0_IRQHandler(void) // Timer0 interrupt subroutine
  55. {
  56. TIMER0->TISR.TIF =1;
  57. TimerCOUN++;
  58. if(test)
  59. {
  60. if( TimerCOUN&0x0020 )
  61. DrvGPIO_SetBit(E_GPA,5);
  62. else
  63. DrvGPIO_ClrBit(E_GPA,5);
  64. }
  65. }


  66. void UART_INT_HANDLE(uint32_t u32IntStatus)
  67. {
  68. if(u32IntStatus & DRVUART_RDAINT)
  69. {
  70. /* Get all the input characters */
  71. while(UART0->ISR.RDA_IF==1)
  72. {

  73. DrvUART_Read(UART_PORT0,inchar,1); /* 保存输入按键 */

  74. }
  75. }
  76. }

  77. void delay()
  78. {
  79. uint32_t i;
  80. for(i=80000;i;i--);
  81. }

  82. void delay_1s()
  83. {
  84. uint32_t i;
  85. for(i = 0; i < 200; i++)
  86. {
  87. DrvSYS_Delay(1000);
  88. }
  89. }

  90. int main (void)
  91. {
  92. uint8_t count;
  93. uint32_t counter;//计数输出
  94. uint32_t loop=1;
  95. STR_UART_T param; //串口参数

  96. UNLOCKREG(); //解除锁定寄存器
  97. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //SYSCLK->WRCON.XTL12M_EN = 1;
  98. DrvSYS_Delay(5000);
  99. LOCKREG(); //重新锁定寄存器

  100. DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); //使能UART时钟 UART时钟源选择. 00 =外部12MHz 晶振
  101. DrvGPIO_InitFunction(E_FUNC_UART0); //GPB_MFP0-1-2-3置位 GPIO使能UART功能

  102. param.u32BaudRate = 115200; //波特率为115200
  103. param.u8cDataBits = DRVUART_DATABITS_8; //数据位8
  104. param.u8cStopBits = DRVUART_STOPBITS_1; //停止位 1
  105. param.u8cParity = DRVUART_PARITY_NONE; //校验位 无
  106. param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; //FIFO存储深度 1 字节
  107. param.u8TimeOut = 0; //FIFO超时设定

  108. if(DrvUART_Open(UART_PORT0,¶m) != E_SUCCESS) // 串口开启、结构体整体赋值
  109. printf("串口0打开失败!\n");

  110. DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT,UART_INT_HANDLE); //使能接收数据中断,回调函数 UART_INT_HANDLE

  111. /*---------------------------------------------------------------------------------------------------------------------*/



  112. /*设置IO口都是输出状态*/
  113. DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT ); //led端口设置为输出
  114. DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
  115. DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
  116. DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );

  117. DrvGPIO_Open( E_GPB, 14, E_IO_INPUT ); //按键端口设置为输入
  118. DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );


  119. DrvGPIO_ClrBit( E_GPA, 2 ); //程序运行指示
  120. DrvGPIO_ClrBit( E_GPA, 3 );
  121. DrvGPIO_ClrBit( E_GPA, 4 );
  122. DrvGPIO_ClrBit( E_GPA, 5 );

  123. while(1)
  124. {
  125. printf("Counter:\n %d",counter);
  126. counter += 1;
  127. delay_1s();
  128. }
  129. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
hotpower 发表于 2011-12-3 19:26 | 显示全部楼层
库函数效率确实太低下了,不过学习是很不错的。
 楼主| tao560532 发表于 2011-12-3 21:10 | 显示全部楼层
恩,下次找内部寄存器设置的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:技术源于积累,成功源于执着!

31

主题

366

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部