当前位置:   article > 正文

嵌入式软件开发学习(一)_(*((volatile unsigned long *)(uart_base + 0x00)))

(*((volatile unsigned long *)(uart_base + 0x00)))

最近一直在学习ARM Cortex-M0 Cortex-M0+权威指南,在这做简单总结:
以前玩STM32开发板,一直不明白例程中对外设为什么要定义结构体,但是看了这个书本后才明白,设置结构体在大型程序编程时能降低维护难度,并简化代码。
(1)传统的应用指针定义和访问UART外设寄存器

#define UART_BASE 0x40003000    //基地址
#define UART_DATA (*((volatile unsigned long*)(UART_BASE+0x00)))
#define UART_RSR (*((volatile unsigned long*)(UART_BASE+0x04)))
#define UART_FLAG (*((volatile unsigned long*)(UART_BASE+0x18)))
...
#define UART_DMACR(*((volatile unsigned long*)(UART_BASE+0x48)))
//----------UART初始化--------------
void uartinit(void)
{
UART_IBRD=40;
UART_FBRD=11;
UART_LCR_H=0X60;
UART_CR=0X301;
}
//---------------发送一个字符------------------
int sendchar(int ch)
{
while(UART_FLAG &20)
UART_DATA=ch;
return ch;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

这种方式虽然简单,初学者学习可用,但是项目编程时是不适用的
(2)利用结构体指针访问数据结构定义的UART寄存器实例

typedef struct{
volatile unsigned long DATA;
volatile unsigned long RSR;
unsigned long RESERVED0[4];
volatile unsigned long FLAG;
....
}UART_TypeDef;
#define Uart0((UART_TypeDef * ) 0x40003000)
#define Uart1((UART_TypeDef * ) 0x40004000)
#define Uart2((UART_TypeDef * ) 0x40005000)
//-----------UART初始化------------
void uartinit(UART_Typedef * uartptr)  //参数为指针
{
uartptr->IBRD=40;
uartptr->FBRD=11;
uartptr->LCR_H=0x60;
uartptr->CR=0x301;
......
}
//--------------发送一个字符---------------
int sendchar(UART_Typedef *uartptr,int ch) //参数为指针
{
while(uartptr->FLAG & 0x20)
   uartptr->DATA=ch;
return ch;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/150536
推荐阅读
相关标签
  

闽ICP备14008679号