自己写的一个LED闪烁程序,调了好久,大家不要笑 啊!!
加了按键选择,目前只学到这里了,到现在什么中断啊寄存器啊时钟啊还是有点蒙的,正在看视频学习,估计数码管的镜子里的屏幕我是做不了了,学起来还是有点困难1!见谅!!!
main.C里的
#include "stm32f0xx.h" // Device header #include "led.h" #include "key.h" #include "delay.h" #include <stdio.h> #include <string.h> #include "system_stm32f0xx.h" #include "stm32f0xx_conf.h" #include "stm32f0xx_it.h " int main() { SystemInit(); uint8_t key; LED_Init(); KEY_Init(); while(1) { key=KEY_Scan(1); if(key) { GPIO_SetBits(LED_PORT,LED_PIN); Delay_Ms(10); GPIO_ResetBits(LED_PORT,LED_PIN ); Delay_Ms(10); } else if(!key) { GPIO_SetBits(LED_PORT,LED_PIN); Delay_Ms(500); GPIO_ResetBits(LED_PORT,LED_PIN ); Delay_Ms(500); } } }LED.C里的
#include "led.h" #include "stm32f0xx.h" void LED_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);//使能GPIOA的时钟 GPIO_InitStructure.GPIO_Pin= LED_PIN; GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_OType= GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP; GPIO_ResetBits(LED_PORT,LED_PIN ); GPIO_Init(GPIOA,&GPIO_InitStructure); }按键 的扫描
#include "key.h" #include "delay.h" void KEY_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(KEY0_PORT_CLK,ENABLE);//使能GPIOA的时钟 GPIO_InitStructure.GPIO_Pin= GPIO_Pin_13 ; GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN; GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; //GPIO_InitStructure.GPIO_OType= GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP; //GPIO_ResetBits(LED_PORT,LED_PIN ); GPIO_Init(KEY0_PORT,&GPIO_InitStructure); } //按键处理函数 //返回按键值 //mode:0,不支持连续按;1,支持连续按; uint8_t KEY_Scan(uint8_t mode) { static uint8_t key_up=1;//按键按松开标志 if(mode)key_up=1; //支持连按 if(key_up&&(KEY0==0)) { Delay_Ms(10);//去抖动 key_up=0; if(KEY0==0)return 1 ; //else if(KEY1==0)return 2; //else if(KEY2==0)return 3; //else if(WK_UP==1)return 4; } else if(KEY0==1)key_up=1; return 0;// 无按键按下 }
延时的
#include "delay.h" void Delay(__IO uint32_t nCount) { while (nCount != 0 ) { nCount--; } } void Delay_Us(__IO uint32_t nCount) { while (nCount != 0 ) { nCount--; Delay(150); } } void Delay_Ms(__IO uint32_t nCount) { while (nCount != 0 ) { nCount--; Delay(15000); } }