下面是部分源码!可以根据代码进行proteus仿真,亲测可以运行1
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "key.h"
- #include "exti.h"
- #include "iwdg.h"
- #include "pwm.h"
- int main(void)
- {
- u8 key=0;
- static u16 led0pwmval=0;
- //u8 dir=1;
- HAL_Init(); //初始化HAL库
- Stm32_Clock_Init(RCC_PLL_MUL9); //设置时钟,72M
- delay_init(8); //初始化延时函数
- LED_Init(); //初始化LED
- KEY_Init(); //初始化按键
- delay_ms(10);
- LED0=0;
- LED1=1;
- LED2=0;
- LED3=1;
- LED4=0;
- LED5=1;
- TIM1_PWM_Init(500-1,72-1); //72M/72=1M的计数频率,自动重装载为500,那么PWM频率为1M/500=2kHZ
- EXTI_Init();
- // while(1)
- // {
- // delay_ms(10);
- // if(dir)led0pwmval++; //dir==1 led0pwmval递增
- // else led0pwmval--; //dir==0 led0pwmval递减
- // if(led0pwmval>300)dir=0; //led0pwmval到达300后,方向为递减
- // if(led0pwmval==0)dir=1; //led0pwmval递减到0后,方向改为递增
- // TIM_SetTIM1Compare2(led0pwmval); //修改比较值,修改占空比
- // }
- //HAL_GPIO_WritePin(GPIOC,GPIO_PIN_2,GPIO_PIN_RESET); //点亮LED
- //EXTI_Init();
-
- while(1)
- {
- // if(KEY_Scan(0)==WKUP_PRES) //如果WK_UP按下,喂狗
- // {
- // IWDG_Feed(); //喂狗
- // }
- // delay_ms(10);
-
-
- key=KEY_Scan(0); //得到键值
-
-
- switch(key)
- {
- case KEY0_PRES:
- LED0=!LED0;
- break;
- case KEY1_PRES:
- LED2=!LED2;
- break;
- case WKUP_PRES:
- LED0=!LED0;
- LED1=!LED1;
- LED2=!LED2;
- break;
- default:
- delay_ms(10);
- }
- // switch(key)
- // {
- // case KEY0_PRES:
- // led0pwmval+=30;
- // TIM_SetTIM1Compare2(led0pwmval);
- // break;
- // case KEY1_PRES:
- // led0pwmval-=30;
- // TIM_SetTIM1Compare2(led0pwmval);
- // break;
- // case WKUP_PRES:
- // LED0=!LED0;
- // LED1=!LED1;
- // LED2=!LED2;
- // break;
- // default:
- // delay_ms(10);
- // }
- }
- }
复制代码
程序:
STM32程序2.0.7z
(416.45 KB, 下载次数: 16)
|