문제 설명
// timer.c
void TIM2_Delay(int time)
{
Macro_Set_Bit(RCC->APB1ENR, 0);
// TIM2 CR1 설정: down count, one pulse
// PSC 초기값 설정 => 20usec tick이 되도록 설계 (50KHz)
// ARR 초기값 설정 => 요청한 time msec에 해당하는 초기값 설정
// UG 이벤트 발생
// Update Interrupt Pending Clear
// Update Interrupt Enable
// TIM2 start
// Wait timeout
// Stop and Power off
Macro_Clear_Bit(TIM2->CR1, 0);
Macro_Clear_Bit(TIM2->DIER, 0);
}
정답 코드
#include "device_driver.h"
#define TIM2_TICK (20) // usec
#define TIM2_FREQ (1000000/TIM2_TICK) // Hz
#define TIME2_PLS_OF_1ms (1000/TIM2_TICK)
#define TIM2_MAX (0xffffu)
#define TIM4_TICK (20) // usec
#define TIM4_FREQ (1000000/TIM4_TICK) // Hz
#define TIME4_PLS_OF_1ms (1000/TIM4_TICK)
#define TIM4_MAX (0xffffu)
void TIM2_Stopwatch_Start(void)
{
Macro_Set_Bit(RCC->APB1ENR, 0);
TIM2->CR1 = (1<<4)|(1<<3);
TIM2->PSC = (unsigned int)(TIMXCLK/50000.0 + 0.5)-1;
TIM2->ARR = TIM2_MAX;
Macro_Set_Bit(TIM2->EGR,0);
Macro_Set_Bit(TIM2->CR1, 0);
}
unsigned int TIM2_Stopwatch_Stop(void)
{
unsigned int time;
Macro_Clear_Bit(TIM2->CR1, 0);
time = (TIM2_MAX - TIM2->CNT) * TIM2_TICK;
return time;
}
#if 1
/* Delay Time Max = 65536 * 20use = 1.3sec */
void TIM2_Delay(int time)
{
Macro_Set_Bit(RCC->APB1ENR, 0);
TIM2->CR1 = (1<<4)|(1<<3);
TIM2->PSC = (unsigned int)(TIMXCLK/(double)TIM2_FREQ + 0.5)-1;
TIM2->ARR = TIME2_PLS_OF_1ms * time;
Macro_Set_Bit(TIM2->EGR,0);
Macro_Clear_Bit(TIM2->SR, 0);
Macro_Set_Bit(TIM2->DIER, 0);
Macro_Set_Bit(TIM2->CR1, 0);
while(Macro_Check_Bit_Clear(TIM2->SR, 0));
Macro_Clear_Bit(TIM2->CR1, 0);
Macro_Clear_Bit(TIM2->DIER, 0);
}
#endif
#if 0
/* Delay Time Extended */
void TIM2_Delay(int time)
{
int i;
unsigned int t = TIME2_PLS_OF_1ms * time;
Macro_Set_Bit(RCC->APB1ENR, 0);
TIM2->PSC = (unsigned int)(TIMXCLK/(double)TIM2_FREQ + 0.5)-1;
TIM2->CR1 = (1<<4)|(1<<3);
TIM2->ARR = 0xffff;
Macro_Set_Bit(TIM2->EGR,0);
Macro_Set_Bit(TIM2->DIER, 0);
for(i=0; i<(t/0xffffu); i++)
{
Macro_Set_Bit(TIM2->EGR,0);
Macro_Clear_Bit(TIM2->SR, 0);
Macro_Set_Bit(TIM2->CR1, 0);
while(Macro_Check_Bit_Clear(TIM2->SR, 0));
}
TIM2->ARR = t % 0xffffu;
Macro_Set_Bit(TIM2->EGR,0);
Macro_Clear_Bit(TIM2->SR, 0);
Macro_Set_Bit(TIM2->CR1, 0);
while (Macro_Check_Bit_Clear(TIM2->SR, 0));
Macro_Clear_Bit(TIM2->CR1, 0);
Macro_Clear_Bit(TIM2->DIER, 0);
}
#endif
메모
printf 내부의 \n 습관화 필요