STM32的LED闪烁程序编译

    科技2024-11-23  24

    一、安装mdk5软件和stm32包 二、STM32的LED闪烁程序编译

    1、 在keil5下建立一个工程,选择芯片 2、将startup_stm32f10x_md.c文件添加到source group 1中 3、编写main.c文件,程序如下

    #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; //配置PA8为推挽输出,50MHZ } //粗略延时 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); } } void SystemInit(void) { }//在启动文件中有调用SystemInit函数,所以定义一个空函数

    4、保存main.c文件并加入工程中 5、点击编译 6.生成hex文件 三、Proteus下的51程序设计和仿真 利用Proteus软件设计简易交通灯,东西反向为红灯时南北方向为绿灯,南北方向通车时间为20s,东西方向通车时间为30s,两方向交替之际有黄灯闪烁提醒5s 1、在keil4下编写源文件,并生成hex文件

    #include<reg51.h> #define uint unsigned int #define uchar unsigned char uchar temp,aa,nn; sbit rn=P2^0; sbit yn=P2^1; sbit gn=P2^2; sbit rw=P2^3; sbit yw=P2^4; sbit gw=P2^5; sbit shi=P3^6; sbit ge=P3^7; bit flag=0; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; void delay(uint z); void disp(uint n); void yn_flash(void); void yw_flash(void); void xint0(); void timer0(); void LED_ON(); void xint1(); void LED_OFF(); void rnzhu(); void rwzhi(); void main() { uint num; num=0;aa=0; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1;ET0=1;TR0=1;EX0=1; IT0=0;EX1=1;IT1=1; while(1) { rnzhu(); yn_flash(); rwzhi(); yw_flash(); } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; aa++; if(aa==18) { aa=0; flag=1; } } void disp(uint n) { while (!flag) { ge=0; P0=table[n%10]; delay(50); shi=1;ge=1; shi=0; P0=table[n/10]; delay(50); shi=1;ge=1; } if(flag==1) flag=0; } void rnzhu() { uint num; num=30; disp(num); while(num<=30&&num>0) { num--; P2=0xde; disp(num); } } void rwzhi() { uint num; num=20; disp(num); while(num<=20&&num>0) { P2=0xf3; disp(num); num--; } } void yn_flash(void) { uint a; for(a=5;a>0;a--) { P2=0xdd; yn=1; delay(100); yn=0; disp(a); } } void yw_flash(void) { uint a; for(a=5;a>0;a--) { P2=0xeb; yw=1; delay(100); yw=0; disp(a); } } void xint0() interrupt 2 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; LED_ON(); } void LED_ON() { uint b; rn=0;rw=0;gn=1; gw=1; yn=1; yw=1; for(b=5;b>0;b--) { ge=0;shi=1; P0=table[b]; delay(1000); ge=1;shi=1; } return; }

    2、proteus仿真图如下: 3、将hex文件添加至单片机中,进行仿真,仿真视频: 链接:https://pan.baidu.com/s/1JbAsbIZawYimq6gm0_sBwQ 提取码:123y 仿真截图如下: 图一 东西方向通行 图二 黄灯闪烁 图三 南北方向通行

    代码参考: https://blog.csdn.net/aifuxun2845/article/details/102026416?utm_medium=distribute.pc_relevant_download.none-task-blog-blogcommendfrombaidu-1.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-blogcommendfrombaidu-1.nonecas

    Processed: 0.016, SQL: 8