GPIO口模拟PWM输出

#define MY_PWM_NUM 2 typedef struct { unsigned int myPwmPeriod; unsigned int myPwmArr; unsigned int myPwmTime; unsigned int myPwmStart; unsigned int myPwmCount; void (* function)(int value); }my_pwm_t; extern my_pwm_t my_pwm[MY_PWM_NUM]; void myGpioPwmInit(int timeX, int period, int arr, int start,void (* function)(int value)) { if(timeX > MY_PWM_NUM) return; if(arr > period) return; my_pwm[timeX].myPwmStart = start; my_pwm[timeX].myPwmPeriod = period; my_pwm[timeX].myPwmArr = arr; my_pwm[timeX].function = function; } void myGpioPwmRun(void) { for(int i = 0; i < MY_PWM_NUM; i++) { if(my_pwm[i].myPwmStart == 1) { my_pwm[i].myPwmCount++; } else { my_pwm[i].myPwmCount = 0; } if(my_pwm[i].myPwmCount <= my_pwm[i].myPwmArr) { //高电平 my_pwm[i].function(1); } else if(my_pwm[i].myPwmCount <= my_pwm[i].myPwmPeriod) { //低电平 my_pwm[i].function(0); } else { my_pwm[i].myPwmCount = 0; } } } void keyUpDownBacklight(int value) { if(value == 1) { W_GPOI1; } else if(value == 0) { W_GPOI0; } } void keyStartBacklight(int value) { if(value == 1) { Y_GPOI1; } else if(value == 0) { Y_GPOI0; } } #define KEYBUTTON GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8) int main(void) { Board_RCC_Config(); //时钟初始化 myGpioPwmInit(0, 10, 5, 1,keyUpDownBacklight);//0号引脚,周期10ms,占空比50%,默认开启,回调函数keyUpDownBacklight myGpioPwmInit(1, 10, 5, 1,keyStartBacklight); while(1) { myGpioPwmRun(); delay_ms(1); if(KEYBUTTON == 0)//检测按键按下 { key_count++; } else if(key_count > 50)//消抖时间50ms { flag = !flag; if(flag== 1) { //执行按键开的逻辑 myGpioPwmInit(0, 10, 8, 1,keyUpDownBacklight);//占空比改为80% } else { //执行按键关的逻辑 myGpioPwmInit(0, 10, 2, 1,keyUpDownBacklight);//占空比改为20% } } else { key_count = 0; } } }
上一篇:MongoDB 如何做mapreduce


下一篇:SPI的学习