stm32通过地址操作寄存器
- 0x01 stm32数据类型所占字节数
- 0x02 如何查看寄存器地址(基地址+偏移地址)
- 0x03 操作寄存器地址控制LED闪烁(代码)
- 0x04 通过定义结构体指针操作寄存器(代码)
0x01 stm32数据类型所占字节数
char = 1
short = 2
int = 4
long = 4
long long = 8
float = 4
double = 8
long double = 8
注意:1字节表示为16进制:0xFF
0x02 如何查看寄存器地址(基地址+偏移地址)
通过STM32中文参考手册查看相应寄存器,查看寄存器地址(基地址+偏移地址),然后分析寄存器每一位作用。
0x03 操作寄存器地址控制LED闪烁(代码)
#define RCC_APB2ENR (*(volatile unsigned int *)0x40021018)
#define GPIOA_CRL (*(volatile unsigned int *)0x40010800)
#define GPIOA_ODR (*(volatile unsigned int *)0x4001080C)
void Delay_ms(unsigned int t);
int main(void)
{
RCC_APB2ENR |= (1<<2);
GPIOA_CRL &= 0xFFF0000F;
GPIOA_CRL |= 0x00033330;
while(1)
{
GPIOA_ODR |= (1<<1 | 1<<2 | 1<<3 | 1<<4);
Delay_ms(1000);
GPIOA_ODR &= ~(1<<1 | 1<<2 | 1<<3 | 1<<4);
Delay_ms(1000);
}
}
void Delay_ms(unsigned int t)
{
unsigned int i;
while(t--)
{
i=12000;
while(i--);
}
}
0x04 通过定义结构体指针操作寄存器(代码)
#include "main.h"
void Delay_ms(unsigned int t);
int main(void)
{
RCC->APB2ENR |= 1<<2;
GPIOA->CRL &= 0xFFF0000F;
GPIOA->CRL |= 0x00033330;
while(1)
{
GPIOA->ODR |= (1<<1 | 1<<2 | 1<<3 | 1<<4);
Delay_ms(1000);
GPIOA->ODR &= ~(1<<1 | 1<<2 | 1<<3 | 1<<4);
Delay_ms(1000);
}
}
void Delay_ms(unsigned int t)
{
unsigned int i;
while(t--)
{
i=12000;
while(i--);
}
}
#ifndef __MAIN_H__
#define __MAIN_H__
typedef struct{
unsigned int CR;
unsigned int CFGR;
unsigned int CIR;
unsigned int APB2RSTR;
unsigned int APB1RSTR;
unsigned int AHBENR;
unsigned int APB2ENR;
unsigned int APB1ENR;
unsigned int BDCR;
unsigned int CSR;
unsigned int AHBRSTR;
unsigned int CFGR2;
}RCC_Typedef;
typedef struct{
unsigned int CRL;
unsigned int CRH;
unsigned int IDR;
unsigned int ODR;
unsigned int BSRR;
unsigned int BRR;
unsigned int LCKR;
}GPIOA_Typedef;
#define RCC ((RCC_Typedef *)0x40021000)
#define GPIOA ((GPIOA_Typedef *)0x40010800)
#endif
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)