这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 活动中心» 板卡试用» 学了好几天,历经千辛万苦调试好的一个程序,请大神指导下

共5条 1/1 1 跳转至

学了好几天,历经千辛万苦调试好的一个程序,请大神指导下

菜鸟
2019-01-09 21:31:18 打赏

cx1.JPG自己写的一个LED闪烁程序,调了好久,大家不要笑 啊!!

cx2.JPG

加了按键选择,目前只学到这里了,到现在什么中断啊寄存器啊时钟啊还是有点蒙的,正在看视频学习,估计数码管的镜子里的屏幕我是做不了了,学起来还是有点困难1!见谅!!!

main.C里的

#include "stm32f0xx.h" // Device header #include "led.h" #include "key.h" #include "delay.h" #include  #include  #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); } }





管理员
2019-01-10 09:37:17 打赏
2楼

谢谢楼主分享


管理员
2019-01-10 09:38:00 打赏
3楼

论坛发帖支持代码插入的奥,欢迎尝试 让代码更好看


菜鸟
2019-01-10 10:09:23 打赏
4楼

谢谢楼主分享


院士
2019-01-10 11:10:22 打赏
5楼

继续努力,就朝着这个方向即可。


共5条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]