简要
STM32底座模块上的四个LED实现一个类似流水灯的效果,该实验的关键在于如何控制 STM32 的 IO 口输出。了解了 STM32 的 IO 口如何输出的,就可以实现流水灯了。通过本节的学习,将初步掌握 STM32 基本IO 口的使用,而这是迈向 STM32 的第一步。
LED闪烁的程序设计
主函数代码
#define PERIPH_BASE ((unsigned int)0x40000000)
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
#define GPIOD_BASE (APB2PERIPH_BASE + 0x1400)
#define GPIOE_BASE (APB2PERIPH_BASE + 0x1800)
#define GPIOF_BASE (APB2PERIPH_BASE + 0x1C00)
#define GPIOG_BASE (APB2PERIPH_BASE + 0x2000)
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define LED0 MEM_ADDR(BITBAND(GPIOA_ODR_Addr,8))
//#define LED0 *((volatile unsigned long *)(0x422101a0)) //PA8
typedef struct
{
volatile unsigned int CR;
volatile unsigned int CFGR;
volatile unsigned int CIR;
volatile unsigned int APB2RSTR;
volatile unsigned int APB1RSTR;
volatile unsigned int AHBENR;
volatile unsigned int APB2ENR;
volatile unsigned int APB1ENR;
volatile unsigned int BDCR;
volatile unsigned int CSR;
} RCC_TypeDef;
#define RCC ((RCC_TypeDef *)0x40021000)
typedef struct
{
volatile unsigned int CRL;
volatile unsigned int CRH;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
volatile unsigned int BRR;
volatile unsigned int LCKR;
} GPIO_TypeDef;
#define GPIOA ((GPIO_TypeDef *)GPIOA_BASE)
void LEDInit( void )//????
{
RCC->APB2ENR|=1<<2; //GPIOA ????
GPIOA->CRH&=0XFFFFFFF0;
GPIOA->CRH|=0X00000003;
}
//????
void Delay_ms( volatile unsigned int t)
{
unsigned int i,n;
for (n=0;n<t;n++)
for (i=0;i<800;i++);
}
int main( void )
{
LEDInit();
while (1)
{
LED0=0;
Delay_ms(500);
LED0=1;
Delay_ms(500);
}
}
MDK5实现
创建project之后 确定stm32的安装项目
添加程序并打开Debug栏目下的Use Simulator
进行编译
51单片机仿真
程序代码
只展示部分
#include <reg52.h>
#include <intrins.h>
#include "ds1302.h"
#include "lcd1602.h"
#include "music.h"
#include "shumaguan.h"
//74HC595-数码管
sbit SDA1=P3^6;//串行数据输入,对应595的14脚SER
sbit SCL1=P3^5;//移位寄存器时钟输入,对应595的11脚SCK
sbit SCL2=P3^4;//存储寄存器时钟输入,对应595的12脚RCK
sbit W1=P3^7;
//发音按键
sbit k1=P1^0; //哆1 啦2 咪3 发4 嗦5 啦6 西7 哆
sbit k2=P1^1; //哆1 啦2 咪3 发4 嗦5 啦6 西7 哆
sbit k3=P1^2; //哆1 啦2 咪3 发4 嗦5 啦6 西7 哆
sbit k4=P1^3; //哆1 啦2 咪3 发4 嗦5 啦6 西7 哆
sbit k5=P1^4; //哆1 啦2 咪3 发4 嗦5 啦6 西7 哆
sbit k6=P1^5; //哆1 啦2 咪3 发4 嗦5 啦6 西7 哆
sbit k7=P1^6; //哆1 啦2 咪3 发4 嗦5 啦6 西7 哆
//LED模块
sbit D1=P2^3; //播放音乐模式
sbit D2=P2^4; //弹奏模式