初识
STM32F103C8
点灯
/* === main.h ===*/
#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
/* === main.c ===*/
/* === version 1 === */
#include "main.h"
#define RCCAPB2ENR (*((volatile unsigned long *)0x40021018))
#define GPIOACRL (*((volatile unsigned long *)0x40010800))
#define GPIOAODR (*((volatile unsigned long *)0x4001080C))
void Delay(unsigned long count);
int main()
{
// int count=10;
// RCC_A 外设时钟寄存器
// GPIOA外设时钟寄存器
*((unsigned long *)0x40021018) |= (1<<2); //0x40021000+0x18 bit[2]
// RCCAPB2ENR |= (1<<2); //0x40021000+0x18 bit[2]
// GPIOA_CRL portA
*((unsigned long *)0x40010800) &= 0xFFF0000F;
// PA1~PA4 设置为通用推挽输出模式
*((unsigned long *)0x40010800) |= 0x00033330; // 4-19λ[19:4] 0011 0011 0011 0011
// GPIOACRL |= 0x00033330; // 4-19λ[19:4] 0011 0011 0011 0011
// GPIOA_ODR 0x40010800+0x0C
*((unsigned long *)0x4001080C) &= ~((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4 输出低电平,LED全灭
// GPIOAODR &= ~((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4 输出低电平
// 点亮LED
while(1)
{
// *((unsigned long *)0x4001080C) |= ((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4输出高电平,LED点亮
GPIOAODR |= ((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4输出高电平,LED点亮
Delay(0xffffff); // 延时
// *((unsigned long *)0x4001080C) &= ~((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4 输出低电平,LED全灭
GPIOAODR &= ~((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4 输出低电平,LED全灭
Delay(0xffffff); // 延时
}
}
void Delay(unsigned long count)
{
while(count--)
{
}
}
/* === version 2 === */
#include "main.h"
void Delay(unsigned long count);
int main()
{
// 使用结构体指针
// version1
// RCC_Typedef *RCC;
// RCC = (RCC_Typedef *)0x40021018; // 结构体指针 指向 RCC的地址基地址
// 结构体中的每个成员都从基地址偏移四个字节,注意内存对齐
// version2 在头文件中定义结构体
// #define RCC (RCC_Typedef *)0x40021018
// RCC_A 外设时钟寄存器
// GPIOA外设时钟寄存器
// *((unsigned long *)0x40021018) |= (1<<2); //0x40021000+0x18 bit[2]
// RCCAPB2ENR |= (1<<2); //0x40021000+0x18 bit[2]
RCC->APB2ENR |= (1 << 2); // 打开某一位,其他位保持不变
// GPIOA_CRL portA
// *((unsigned long *)0x40010800) &= 0xFFF0000F;
// GPIOACRL &= 0xFFF0000F;
GPIOA->CRL &= 0xFFF0000F;
// PA1~PA4 设置为通用推挽输出模式
// *((unsigned long *)0x40010800) |= 0x00033330; // 4-19 [19:4] 0011 0011 0011 0011
// GPIOACRL |= 0x00033330; // 4-19 [19:4] 0011 0011 0011 0011
GPIOA->CRL |= 0x00033330;
// GPIOA_ODR 0x40010800+0x0C
// *((unsigned long *)0x4001080C) &= ~((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4 输出低电平,LED全灭
// GPIOAODR &= ~((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); // PA1~PA4 输出低电平
GPIOA->ODR &= ~((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); // PA1~PA4 输出低电平
// 点亮LED
while (1)
{
// *((unsigned long *)0x4001080C) |= ((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4输出高电平,LED点亮
// GPIOAODR |= ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); // PA1~PA4输出高电平,LED点亮
GPIOA->ODR |= ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); // PA1~PA4输出高电平,LED点亮
Delay(0xffffff); // 延时
// *((unsigned long *)0x4001080C) &= ~((1<<1) | (1<<2) | (1<<3) | (1<<4)); // PA1~PA4 输出低电平,LED全灭
// GPIOAODR &= ~((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); // PA1~PA4 输出低电平,LED全灭
GPIOA->ODR &= ~((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); // PA1~PA4 输出低电平,LED全灭
Delay(0xffffff); // 延时
}
}
void Delay(unsigned long count)
{
while (count--)
{
}
}