基于单片机GD32E230F8P6,检测引脚6,引脚7,引脚8,引脚9的高低电平状态,引脚6为低电平时,PB1引脚PWM输出占空比为0.25,引脚6和引脚7均为低电平时,PB1引脚PWM输出占空比为0.5,引脚6和引脚7,引脚8均为低电平时,PB1引脚PWM输出占空比为0.75,引脚6,引脚7,引脚8,引脚9均为低电平时,PB1引脚PWM输出占空比为1
时间: 2023-05-26 17:02:01 浏览: 138
占空比与PWM,以及GPIO背光开关(单双相)电路
首先需要初始化GPIO,将引脚6、7、8、9设置为输入模式:
```c
/* 初始化GPIOB时钟 */
rcu_periph_clock_enable(RCU_GPIOB);
/* 初始化PB6、PB7、PB8、PB9引脚 */
gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9);
```
接着初始化PWM,将PB1设置为PWM输出引脚:
```c
/* 初始化TIMER2时钟 */
rcu_periph_clock_enable(RCU_TIMER2);
/* 设置TIMER2重载值 */
timer_prescaler_config(TIMER2, SystemCoreClock / 1000000 - 1, TIMER_PSC_RELOAD_NOW);
/* PWM模式设置 */
timer_pwm_mode_config(TIMER2, TIMER_CH_1, TIMER_PWM_MODE_1);
timer_channel_output_shadow_config(TIMER2, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_PWM0);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, 0);
/* PWM输出设置 */
timer_auto_reload_shadow_enable(TIMER2);
timer_primary_output_config(TIMER2, ENABLE);
timer_update_config(TIMER2);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_1);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
```
最后,在主函数中循环检测引脚状态并设置PWM占空比:
```c
while (1)
{
/* 检测引脚状态 */
int p6 = gpio_input_bit_get(GPIOB, GPIO_PIN_6);
int p7 = gpio_input_bit_get(GPIOB, GPIO_PIN_7);
int p8 = gpio_input_bit_get(GPIOB, GPIO_PIN_8);
int p9 = gpio_input_bit_get(GPIOB, GPIO_PIN_9);
/* 设置PWM占空比 */
if (!p6 && p7 && p8 && p9) // 引脚6为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD / 4);
else if (!p6 && !p7 && p8 && p9) // 引脚6和引脚7均为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD / 2);
else if (!p6 && !p7 && !p8 && p9) // 引脚6、引脚7、引脚8均为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD * 3 / 4);
else if (!p6 && !p7 && !p8 && !p9) // 引脚6、引脚7、引脚8、引脚9均为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD);
}
```
完整代码:
```c
#include "gd32e230.h"
#define PWM_PERIOD 4000 // PWM周期为4ms
int main(void)
{
/* 初始化GPIOB时钟 */
rcu_periph_clock_enable(RCU_GPIOB);
/* 初始化PB6、PB7、PB8、PB9引脚 */
gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9);
/* 初始化TIMER2时钟 */
rcu_periph_clock_enable(RCU_TIMER2);
/* 设置TIMER2重载值 */
timer_prescaler_config(TIMER2, SystemCoreClock / 1000000 - 1, TIMER_PSC_RELOAD_NOW);
/* PWM模式设置 */
timer_pwm_mode_config(TIMER2, TIMER_CH_1, TIMER_PWM_MODE_1);
timer_channel_output_shadow_config(TIMER2, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_PWM0);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, 0);
/* PWM输出设置 */
timer_auto_reload_shadow_enable(TIMER2);
timer_primary_output_config(TIMER2, ENABLE);
timer_update_config(TIMER2);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_1);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
while (1)
{
/* 检测引脚状态 */
int p6 = gpio_input_bit_get(GPIOB, GPIO_PIN_6);
int p7 = gpio_input_bit_get(GPIOB, GPIO_PIN_7);
int p8 = gpio_input_bit_get(GPIOB, GPIO_PIN_8);
int p9 = gpio_input_bit_get(GPIOB, GPIO_PIN_9);
/* 设置PWM占空比 */
if (!p6 && p7 && p8 && p9) // 引脚6为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD / 4);
else if (!p6 && !p7 && p8 && p9) // 引脚6和引脚7均为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD / 2);
else if (!p6 && !p7 && !p8 && p9) // 引脚6、引脚7、引脚8均为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD * 3 / 4);
else if (!p6 && !p7 && !p8 && !p9) // 引脚6、引脚7、引脚8、引脚9均为低电平
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, PWM_PERIOD);
}
}
```
阅读全文