c语言自定义寄存器操作的一些方法

 1 寄存器地址的定义:
    #define UART_BASE_ADRS (0x10000000)     /* 串口的基地址 */
    #define UART_RHR *(volatile unsigned char *)(UART_BASE_ADRS + 0)  /* 数据接受寄存器 */
    #define UART_THR *(volatile unsigned char *)(UART_BASE_ADRS + 0)  /* 数据发送寄存器 */
2 寄存器读写操作:
    UART_THR = ch; /* 发送数据 */
    ch = UART_RHR; /* 接收数据 */
    也可采用定义带参数宏实现
    #define WRITE_REG(addr, ch) *(volatile unsigned char *)(addr) = ch
    #define READ_REG(addr, ch) ch = *(volatile unsigned char *)(addr)
   
   
3 对寄存器相应位的操作方法:
    定义寄存器
    #define UART_LCR *(volatile unsigned char *)(UART_BASE_ADRS + 3)  /* 线控制寄存器 */
   
    定义寄存器相应位的值
    #define CHAR_LEN_5 0x00
    #define CHAR_LEN_6 0x01
    #define CHAR_LEN_7 0x02
    #define CHAR_LEN_8 0x03    /* 8 data bit */
    #define LCR_STB  0x04 /* Stop bit control */
    #define ONE_STOP 0x00 /* One stop bit! */
    #define LCR_PEN  0x08 /* Parity Enable */
    #define PARITY_NONE 0x00
    #define LCR_EPS  0x10 /* Even Parity Select */
    #define LCR_SP  0x20 /* Force Parity */
    #define LCR_SBRK 0x40 /* Start Break */
    #define LCR_DLAB 0x80 /* Divisor Latch Access Bit */
   
    定义寄存器相应位的值另一种方法
    #define CHAR_LEN_5 0<<0
    #define CHAR_LEN_6 1<<0
    #define CHAR_LEN_7 1<<1
    #define CHAR_LEN_8 (1<<0)|(1<<1)    /* 8 data bit */
    #define LCR_STB  1<<2 /* Stop bit control */
    #define ONE_STOP 0<<2 /* One stop bit! */
    #define LCR_PEN  1<<3 /* Parity Enable */
    #define PARITY_NONE 0<<3
    #define LCR_EPS  1<<4 /* Even Parity Select */
    #define LCR_SP  1<<5 /* Force Parity */
    #define LCR_SBRK 1<<6 /* Start Break */
    #define LCR_DLAB 1<<7 /* Divisor Latch Access Bit */
   
    对寄存器操作只需对相应位或赋值
    UART_LCR = CHAR_LEN_8 | ONE_STOP | PARITY_NONE;    /* 设置 8位数据位,1位停止位,无校验位 */
   
4 对寄存器某一位置位与清零
    对某一寄存器第7位置位
    XX_CRTL |= 1<<7;
    XX_CRTL &= ~(1<<7);
      
    UART_LCR |= LCR_DLAB;           /* 时钟分频器锁存使能 */
    UART_LCR &= ~(LCR_DLAB);        /* 禁止时钟分频器锁存 */
   
5 判断寄存器某一位是否置位或为0的方法
    #define UART_LSR *(volatile unsigned char *)(UART_BASE_ADRS + 5)  /* 线状态寄存器 */
    #define LSR_DR  1<<0 /* Data Ready */
   
    当UART_LSR的第0位为1时结束循环
    while (!(UART_LSR & LSR_DR)) /* 等待数据接收完 */
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值