CH32F103C8T6滴答定时器应用
//中断方式
SysTick_Config(72000000/5);
void SysTick_Handler(void)
{
GPIO_WriteBit( GPIOA, GPIO_Pin_0,!GPIO_ReadOutputDataBit( GPIOA, GPIO_Pin_0));
}
非中断方式
//1s 72000000 1us 72
void delay_us(uint32_t us)
{
SysTick->CTRL = 0; // Disable SysTick
SysTick->LOAD = 72*us-1; // Count from 255 to 0 (256 cycles)
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set
SysTick->CTRL = 0; // Disable SysTick
}
//1s 72000000 1ms 72000
void delay_ms(uint32_t ms)
{
while(ms--)
{
SysTick->CTRL = 0; // Disable SysTick
SysTick->LOAD = 72000-1; // 计数值
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set
SysTick->CTRL = 0; // Disable SysTick
}
}
void delay_s(uint32_t s)
{
while(s--)
{
delay_ms(1000);
}
}