赞
踩
最近一直在学习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; }
这种方式虽然简单,初学者学习可用,但是项目编程时是不适用的
(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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。