#include <stdio.h>
#include "at32f4xx.h"
#include "at32_board.h"
void Delay_Timer3_Init(void);
void Delay_TMR3_us(uint32_t us_cnt);
int main(void)
{
AT32_Board_Init();
Delay_Timer3_Init();
for(;;)
{
AT32_LEDn_Toggle(LED2);//OK
// Delay_ms(100);
Delay_TMR3_us(100);
// AT32_LEDn_Toggle(LED3);
// Delay_ms(200);
// AT32_LEDn_Toggle(LED4);
// Delay_ms(200);
}
}
void Delay_Timer3_Init(void)
{
TMR_TimerBaseInitType TMR_TimerBaseInitStructure = {0};
/* TMR3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR3, ENABLE);
TMR_TimeBaseStructInit(&TMR_TimerBaseInitStructure);
TMR_TimerBaseInitStructure.TMR_ClockDivision = TMR_CKD_DIV1;
TMR_TimerBaseInitStructure.TMR_CounterMode = TMR_CounterDIR_Down;
TMR_TimerBaseInitStructure.TMR_Period = 100;
TMR_TimerBaseInitStructure.TMR_DIV = 238;
// TMR_TimerBaseInitStructure.TMR_Period = 100-1;
// TMR_TimerBaseInitStructure.TMR_DIV = (84-1);
TMR_TimerBaseInitStructure.TMR_RepetitionCounter = 0x0000;
TMR_TimeBaseInit(TMR3, &TMR_TimerBaseInitStructure);
/*
while(TMR_GetFlagStatus(TMR3,TMR_FLAG_Update)==RESET);
TMR_ClearFlag(TMR3,TMR_FLAG_Update);
//STS & TMR_FLAG
*/
while((TMR3->STS & TMR_FLAG_Update)!=SET);
TMR3->STS = (uint16_t)~TMR_FLAG_Update;
}
//延时函数如下,对CNT寄存器赋延时值,然后通过判断Update标志位进行阻塞延时。
void Delay_TMR3_us(uint32_t us_cnt)
{
TMR3->CNT = us_cnt-1;
/* Enable the TMR Counter */
TMR3->CTRL1 |= TMR_CTRL1_CNTEN;
while((TMR3->STS & TMR_FLAG_Update)!=SET);
TMR3->STS = (uint16_t)~TMR_FLAG_Update;
/* Disable the TMR Counter */
TMR3->CTRL1 &= (uint16_t)(~((uint16_t)TMR_CTRL1_CNTEN));
}
|