(新手)关于串口的编程的问题

2019-07-16 09:27发布

在s5pv210中,不是应该先把GPA0寄存器调成控制串口的模式吗?
怎么我一调,编译没有问题,进入板子执行就没有任何显现
当我把GPIO寄存器的那一段代码删除后,稍微改了下代码,就可以了
这是怎么回事呢?
GPIO不是没有调成控制串口的模式吗?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
z00
1楼-- · 2019-07-16 09:52
GPIO具有复用的功能 需要配置GPIO为GPIO模式还是串口模式
z00
2楼-- · 2019-07-16 15:35
在使用S5PV210的串口发送和接收的时候,首先要对S5PV210的串口进行配置

1、配置GPIO,使对应管脚作为串口的发送和接收管脚

  GPA0 0 1 管脚 2 3 可以配置
  GPA0CON寄存器[7:4][3:0] 0x22
  GPA0PUD寄存器[3:0] 0 禁止上下拉电阻
2、配置串口单元本身寄存器
  ULCON0 0xE2900000
  数据位:8位
  停止位:1位
  校验位:无
  使用的正模式,非红外。
3、UCON0 0xE2900004

  串口的收发模式:轮询

  串口的时钟使用的PCLK  UFCON0 0xE2900008

  禁止FIFO
  UMCON0 0xE290000C
  禁止Modem
  UBRDIV0 0xE2900028
  UDIVSLOT0 0xE290002C
  UBRDIV0 = PCLK或者SCLK_UART/波特率/16 - 1 的整数部分
  UDIVSLOT0 查表,怎么查?
  PCLK或者SCLK_UART/波特率/16 - 1 的小数部分 * 16 取整
  查表
  PCLK=66500000
  波特率是115200
  UBRDIV0 = 35
  UDIVSLOT0 = 0x0080
  发送数据流程(轮询方式)
  uart0_putc()
  判断UTRSTAT0的BIT1,如果BIT1是0等待如果BIT1是1,就把要发送的一个字节数据写到发送寄存器(UTXH0,0xE2900020)
接收数据流程(轮询方式)
  uart0_getc
  判断UTRSTAT0的BIT0,如果BIT0是0等待如果BIT0是1,从URXH0 0xE2900024读取一个字节的数据。

编程时:
0xE2900000地址单元写3
0xE2900004地址单元写5
0xE2900008地址单元写0
0xE290000C地址单元写0
0xE2900028地址单元写35
0xE290002C地址单元写0x80

uart.h
  1. #ifndef _UART_H_
  2. #define _UART_H_

  3. #define GPA0CON (*(volatile unsigned int *)0xE0200000)
  4. #define GPA0PUD (*(volatile unsigned int *)0xE0200008)

  5. #define ULCON0 (*(volatile unsigned int *)0xE2900000)
  6. #define UCON0 (*(volatile unsigned int *)0xE2900004)
  7. #define UFCON0 (*(volatile unsigned int *)0xE2900008)
  8. #define UMCON0 (*(volatile unsigned int *)0xE290000C)
  9. #define UTRSTAT0 (*(volatile unsigned int *)0xE2900010)
  10. #define UTXH0 (*(volatile unsigned int *)0xE2900020)
  11. #define URXH0 (*(volatile unsigned int *)0xE2900024)
  12. #define UBRDIV0 (*(volatile unsigned int *)0xE2900028)
  13. #define UDIVSLOT0 (*(volatile unsigned int *)0xE290002C)

  14. #define PCLK (66500000)

  15. //函数原型声明
  16. extern void uart0_init(void);
  17. extern void uart0_puts(const char *);
  18. extern void uart0_putc(char);
  19. extern char uart0_getc(void);
  20. extern void uart0_gets(char *,int);

  21. #endif // _UART_H_
复制代码
uart.c
  1. #include "uart.h"

  2. //初始化串口寄存器
  3. void uart0_init(void){
  4.     //配置GPIO口 根据CPU 手册中设置下面的寄存器
  5.     //GPA0CON GPA0PUD
  6.     ULCON0 = 3;
  7.     UCON0 = 5;
  8.     UFCON0 = 0;
  9.     UMCON0 = 0;
  10.     UBRDIV0 = 35;
  11.     UDIVSLOT0 = 0x0080;
  12.     GPA0CON = 34;
  13.     GPA0PUD = ~0xF;
  14. }
  15. //发送一个字符
  16. void uart0_putc(char c){
  17.     //判断状态位
  18.     while(!(UTRSTAT0 & (1<<1)));
  19.     //发送字符
  20.     UTXH0 = c;
  21. }
  22. //接收一个字符
  23. char uart0_getc(void){
  24.     while(!(UTRSTAT0 & 1));

  25.     return URXH0;
  26. }

  27. //接收一串字符
  28. void uart0_gets(char *str,int len){
  29.     char* tmp = str;
  30.     int in = len;
  31.     //int i;
  32.     while(--len){
  33.         *tmp = uart0_getc();
  34.         if(*tmp == ' '){
  35.             uart0_putc(' '); //若此处为 则不会输出,若为 则在下一行跳跃输出字符的长度,然后输出字符串
  36.             uart0_putc(' ');
  37.             break;
  38.         }
  39.         if(*tmp == 127){  //127 是 ubuntu下 kermit软件中的 BACKSPACE按键 需要实现的效果就是当按下回车键的时候终端的上一个数据会被删掉,
  40.             len++;  //由于此分支的 127 输入到了 *tmp 中,此时的127是无用的,所以要进行 len++ ,但是有一个问题,我们的退格的目的是删除上一个字母,所以127的上一个字符也没用了,需要对len做两次自加进行还原  但是又出现一个问题,如果已经删到第0个元素就不能再自加两次了,这样会造成 len 越来越大。因此要在下面做一个判断
  41.             if(len < in){
  42.                 len++;
  43.             }
  44.             if(tmp == str){
  45.                 continue;
  46.             }

  47.             uart0_putc('');
  48.             uart0_putc(' ');
  49.             uart0_putc('');
  50.             --tmp;
  51.             continue;
  52.         }
  53.         uart0_putc(*tmp);
  54.         tmp++;
  55.     }
  56.     *tmp = 0;
  57. }

  58. //发送一串字符
  59. void uart0_puts(const char *str){
  60.     if(str == 0){
  61.         return;
  62.     }
  63.     while(*str){
  64.         uart0_putc(*str);
  65.         if(*str == ' '){
  66.             uart0_putc(' ');
  67.         }
  68.         str++;
  69.     }
  70. }
复制代码


sswenxh2006
3楼-- · 2019-07-16 21:02
 精彩回答 2  元偷偷看……

一周热门 更多>