×
单片机 > 单片机程序设计 > 详情

S3C2440之UART驱动代码模板(RealView MDK)

发布时间:2020-08-21 发布时间:
|

好记心不如烂笔头,为方便以后查看代码及代码重复利用,这里贴出S3C2440 UART驱动代码。使用友善MINI2440开发板,开发环境为RealView MDK 4.22。需要注意的是,本代码中,对GPIO的初始化放在了s3c2440.s中完成,采用keil自带的html方式进行配置。

该源码结构简单明了,原始工程下载地址:点击打开链接


UART控制器初始化:


  1. void Uart_Init(void)  

  2. {  

  3. #define rULCON0         (*(volatile unsigned int*)0x50000000)  

  4. #define rUCON0          (*(volatile unsigned int*)0x50000004)  

  5. #define rUBRDIV0        (*(volatile unsigned int*)0x50000028)  

  6.   

  7. #define PCLK            50000000  

  8. #define BUADRATE        115200  

  9.   

  10.     rULCON0 = 0x03;     //No parity, One stop bit, 8-bits data  

  11.     rUCON0  = 0x05;     //Tx Enable, Rx Enable, PCLK as source clock  

  12.     rUBRDIV0 = (int)(PCLK / (BUADRATE * 16)) - 1;   //115200bps  

  13. }  



字符发送函数:


  1. void Uart_Putc(unsigned char c)  

  2. {  

  3. #define rUTRSTAT0       (*(volatile unsigned int*)0x50000010)  

  4. #define rUTXH0          (*(volatile unsigned int*)0x50000020)  

  5.   

  6. #define BUFFER_EMPTY    (1 <

  7.   

  8.     while(!(rUTRSTAT0 & BUFFER_EMPTY));  

  9.     rUTXH0 = c;  

  10. }  


字符接收函数:


  1. unsigned char Uart_Getc(void)  

  2. {  

  3. #define rUTRSTAT0       (*(volatile unsigned int*)0x50000010)  

  4. #define rURXH0          (*(volatile unsigned int*)0x50000024)  

  5.   

  6. #define BUFFER_READY    (1 <

  7.   

  8.     while(!(rUTRSTAT0 & BUFFER_READY));  

  9.     return rURXH0;  

  10. }  


为了使用printf库函数,需要进行如下重映射:


  1. struct __FILE    

  2. {    

  3. int handle;    

  4. /* Whatever you require here. If the only file you are using is */    

  5. /* standard output using printf() for debugging, no file handling */    

  6. /* is required. */    

  7. };    

  8.   

  9. /* FILE is typedef'd in stdio.h. */    

  10. FILE __stdout;    

  11.   

  12. int fputc(int ch, FILE *f)   

  13. {   

  14.     Uart_Putc(ch);   

  15.        

  16.     return ch;   

  17. }   

  18.   

  19. int ferror(FILE *f) {  

  20.   /* Your implementation of ferror */  

  21.   return EOF;  

  22. }  


测试代码:


  1. int main(void)  

  2. {  

  3.     unsigned char ch;  

  4.   

  5.     //clock_init();  

  6.     Uart_Init();  

  7.     printf("%s, %d", __FILE__, __LINE__);  

  8.     while(1)  

  9.     {  

  10.         ch = Uart_Getc();  

  11.         Uart_Putc(ch);  

  12.     }  


关键字:S3C2440  UART  驱动代码模板

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机CO2检测显示程序解析