1、使用寄存器映射把GPIO口映射出来。
2、在主函数里使用清除位操作进行循环清除位为1和清除位为0,使其led灯亮和灭。
3、本次的LED灯集成了红灯、绿灯、蓝灯。
4、本次用到的delay延迟函数随意定的,能达到亮灭即可。
//外设perirhral
#define PERIPH_BASE 0x40000000//外设总基地址
#define APB1_PERIPH_BASE PERIPH_BASE//外设低速APB1基地址
#define APB2_PERIPH_BASE (PERIPH_BASE+0x10000)//外设高速APB2基地址
#define AHB_PERIPH_BASE (PERIPH_BASE+0x20000)//外设超高度AHB基地址
#define RCC_BASE (AHB_PERIPH_BASE+0x1000)//外设时钟控制寄存器
#define GPIO_A_BASE (APB2_PERIPH_BASE+0x0800)//外设GPIO_X的基地址
#define GPIO_B_BASE (APB2_PERIPH_BASE+0x0c00)
#define GPIO_C_BASE (APB2_PERIPH_BASE+0x1000)
#define GPIO_D_BASE (APB2_PERIPH_BASE+0x1400)
#define GPIO_E_BASE (APB2_PERIPH_BASE+0x1800)
#define GPIO_F_BASE (APB2_PERIPH_BASE+0x1c00)
#define GPIO_G_BASE (APB2_PERIPH_BASE+0x2000)
#define RCC_APB1ENR *(unsigned int *)(RCC_BASE+0x1c)//APB1外设时钟使能寄存器
#define RCC_APB2ENR *(unsigned int *)(RCC_BASE+0x18)//APB2外设时钟使能寄存器
#define GPIO_A_CRL *(unsigned int *)(GPIO_A_BASE+0x00)//端口低8口输出配置寄存器
#define GPIO_B_CRL *(unsigned int *)(GPIO_B_BASE+0x00)
#define GPIO_C_CRL *(unsigned int *)(GPIO_C_BASE+0x00)
#define GPIO_D_CRL *(unsigned int *)(GPIO_D_BASE+0x00)
#define GPIO_E_CRL *(unsigned int *)(GPIO_E_BASE+0x00)
#define GPIO_F_CRL *(unsigned int *)(GPIO_F_BASE+0x00)
#define GPIO_G_CRL *(unsigned int *)(GPIO_G_BASE+0x00)
#define GPIO_A_CRH *(unsigned int *)(GPIO_A_BASE+0x04)//端口高8口输出配置寄存器
#define GPIO_B_CRH *(unsigned int *)(GPIO_B_BASE+0x04)
#define GPIO_C_CRH *(unsigned int *)(GPIO_C_BASE+0x04)
#define GPIO_D_CRH *(unsigned int *)(GPIO_D_BASE+0x04)
#define GPIO_E_CRH *(unsigned int *)(GPIO_E_BASE+0x04)
#define GPIO_F_CRH *(unsigned int *)(GPIO_F_BASE+0x04)
#define GPIO_G_CRH *(unsigned int *)(GPIO_G_BASE+0x04)
#define GPIO_A_ODR *(unsigned int *)(GPIO_A_BASE+0x0c)//IO端口输出数据寄存器
#define GPIO_B_ODR *(unsigned int *)(GPIO_B_BASE+0x0c)
#define GPIO_C_ODR *(unsigned int *)(GPIO_C_BASE+0x0c)
#define GPIO_D_ODR *(unsigned int *)(GPIO_D_BASE+0x0c)
#define GPIO_E_ODR *(unsigned int *)(GPIO_E_BASE+0x0c)
#define GPIO_F_ODR *(unsigned int *)(GPIO_F_BASE+0x0c)
#define GPIO_G_ODR *(unsigned int *)(GPIO_G_BASE+0x0c)
#define GPIO_A_BSRR *(unsigned int *)(GPIO_A_BASE+0x10)//IO端口输出位清除数据寄存器,只能更改对应gpio端口为1不能更改为0
#define GPIO_B_BSRR *(unsigned int *)(GPIO_B_BASE+0x10)
#define GPIO_C_BSRR *(unsigned int *)(GPIO_C_BASE+0x10)
#define GPIO_D_BSRR *(unsigned int *)(GPIO_D_BASE+0x10)
#define GPIO_E_BSRR *(unsigned int *)(GPIO_E_BASE+0x10)
#define GPIO_F_BSRR *(unsigned int *)(GPIO_F_BASE+0x10)
#define GPIO_G_BSRR *(unsigned int *)(GPIO_G_BASE+0x10)
#define GPIO_A_BRR *(unsigned int *)(GPIO_A_BASE+0x14)//IO端口输出位清除数据寄存器,只能更改对应gpio端口为0不能更改为1
#define GPIO_B_BRR *(unsigned int *)(GPIO_B_BASE+0x14)
#define GPIO_C_BRR *(unsigned int *)(GPIO_C_BASE+0x14)
#define GPIO_D_BRR *(unsigned int *)(GPIO_D_BASE+0x14)
#define GPIO_E_BRR *(unsigned int *)(GPIO_E_BASE+0x14)
#define GPIO_F_BRR *(unsigned int *)(GPIO_F_BASE+0x14)
#define GPIO_G_BRR *(unsigned int *)(GPIO_G_BASE+0x14)
#include "stm32f10x.h"
void delay(unsigned int z)
{
unsigned int x;
unsigned int y;
for(x=z;x>0;x--)
for(y=0;y<114;y++);
}
int main(void)
{
/*
//方案1
*(unsigned int*)0x40021018 |=(1<<3);//配置RCC 打开GPIOB的时钟
*(unsigned int*)0x40010c00 |=1<<(4*0);//配置CRL IO口为输出
*(unsigned int*)0x40010c0c &=~(1<<0);//控制ODR寄存器的B0口
while(1)
{
delay(2111);//延迟一段时间
*(unsigned int*)0x40010c0c |=(1<<0);//控制ODR寄存器,灯灭
delay(2111);//延迟一段时间
*(unsigned int*)0x40010c0c &=~(1<<0);//控制ODR寄存器,灯亮
}
*/
//方案2
RCC_APB2ENR |=1<<3;//配置gpiob的rcc时钟
GPIO_B_CRL |=1<<(4*0);//配置CRL IO0口为输出
GPIO_B_CRL |=1<<(4*1);//配置CRL IO1口为输出
GPIO_B_CRL |=1<<(4*5);//配置CRL IO5口为输出
GPIO_B_ODR &=~(1<<0);//控制ODR寄存器,灯亮
/* while(1)
{
delay(2111);//延迟一段时间
GPIO_B_ODR |=1<<0;//控制ODR寄存器,灯灭
delay(2111);//延迟一段时间
GPIO_B_ODR &=~(1<<0);//控制ODR寄存器,灯亮
}
*/
//方案3
//使用BSRR寄存器置位1,使用BRR寄存器置0
while(1)
{//操作3个灯使其这个led灯发白光
delay(5000);//延迟一段时间
GPIO_B_BSRR |=1<<0;//操作BSRR使其灯灭,使其对应的GPIO b0位为1
GPIO_B_BSRR |=1<<1;
GPIO_B_BSRR |=1<<5;
delay(5000);//延迟一段时间
GPIO_B_BRR |=1<<0;//操作BRR使其灯亮,使其对应的GPIO b0位为0
GPIO_B_BRR |=1<<1;
GPIO_B_BRR |=1<<5;
}
}
void SystemInit(void)//配置系统时钟函数,芯片内部时钟SHI=8M时钟
{
}