//PD4引脚控制LED灯的亮灭
void LED_CSH(void) //PD3,PD4端口初始化
{
RCC->APB2PCENR|=0x20; //开启GPIO_D的时钟
GPIOD->CFGLR&=~0xff000;
GPIOD->CFGLR|=0x77000;
GPIOD->OUTDR&=~0x18;
}
void TIM2_CSH(unsigned short shu) //TIM2中断初始化函数
{
RCC->APB1PCENR|=0x1; //使能TIM2的时钟
TIM2->CTLR1|=0x1<<7; //使能自动重装载
TIM2->CTLR1&=~(0x3<<5); //选择边沿对齐
TIM2->CTLR1|=0x1<<4; //向下计数
TIM2->DMAINTENR|=0x1; //允许更新中断
TIM2->PSC=7199; //预分频器
TIM2->ATRLR=shu; //自动重装载
TIM2->CTLR1|=0x1; //使能计数器
*(volatile unsigned int *)0xE000E104=0x1<<12; //使能中断
}
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_IRQHandler(void) //中断函数
{
if(TIM2_UIF)
{
TIM2_UIF0;
if(zj)
{
GPIOD->OUTDR|=0x10;
}
else
{
GPIOD->OUTDR&=~0x10;
}
zj=~zj;
}
}
int main(void)
{
LED_CSH();
TIM2_CSH(9999);
js=0;
while(1)
{
GPIOD->OUTDR|=0x8;
GPIOD->OUTDR&=~0x8;
}
}
如果while里面泵放 GPIOD->OUTDR&=~0x8,中断按照设定的时间执行亮灭。如果加了GPIOD->OUTDR&=~0x8中断就不按照设定的时间中断去控制PD4引脚了。(使用的是ch32V307-VCT6评估板)