Chinaunix首页 | 论坛 | 博客
  • 博客访问: 361104
  • 博文数量: 53
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 1143
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 11:53
文章分类

全部博文(53)

文章存档

2011年(1)

2008年(52)

我的朋友

分类: C/C++

2008-08-26 12:41:09

 
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)) /* 等待数据接收完 */
阅读(7227) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~