1.按键中断检测
打开AT32 Work Bench,将PA0功能选择为EXINT
查看原理图,按键是下拉的
配置成上升沿中断
开启中断
生成代码,在中断函数中加入处理代码
void EXINT0_IRQHandler(void)
{
/* add user code begin EXINT0_IRQ 0 */
if(exint_flag_get(EXINT_LINE_0) != RESET)
{
userkey_int();
exint_flag_clear(EXINT_LINE_0);
}
/* add user code end EXINT0_IRQ 0 */
/* add user code begin EXINT0_IRQ 1 */
/* add user code end EXINT0_IRQ 1 */
}
按键需要进行去抖处理
uint8_t key_down_flag = 0;
void userkey_int(void)
{
key_down_flag = 20;
}
void AppMsTick(void)
{
if(key_down_flag > 0)
{
key_down_flag -= 1;
if(key_down_flag == 0 && gpio_input_data_bit_read(GPIOA,GPIO_PINS_0))
{
gpio_bits_toggle(LED2_GPIO_PORT,LED2_PIN);
gpio_bits_toggle(LED3_GPIO_PORT,LED3_PIN);
gpio_bits_toggle(LED4_GPIO_PORT,LED4_PIN);
}
}
}
运行效果
2.PWM输出
开启定时器2的通道1、2、3,配置定时器频率为1000Hz
自动分配的IO
要外接的是这个LED模块
模块是共阳极的,这里将PWM极性设置为低
实现颜色渐变效果代码
uint8_t ledstep = 0;
uint16_t pwm = 0;
void LedChangeColor(void)
{
uint8_t ch1 = TMR_SELECT_CHANNEL_1;
uint8_t ch2 = TMR_SELECT_CHANNEL_2;
if(ledstep >1)
{
ch1 = TMR_SELECT_CHANNEL_3;
ch2 = TMR_SELECT_CHANNEL_1;
}
else if(ledstep >0)
{
ch1 = TMR_SELECT_CHANNEL_2;
ch2 = TMR_SELECT_CHANNEL_3;
}
tmr_channel_value_set(TMR2,ch1,1000 - pwm);
tmr_channel_value_set(TMR2,ch2,pwm);
pwm += 1;
if(pwm > 1000)
{
ledstep += 1;
pwm = 0;
}
if(ledstep == 3)
ledstep = 0;
}
void AppMsTick(void)
{
LedChangeColor();
}
运行效果
|