我的目的是想利用定时器1比较输出500Khz的高低电平信号,也就是用定时器1产生500Khz的中断,
先试用500hz的程序如下:
void timer_init(void)
{
RCC->APB2ENR |= 1 << 11;
TIM1->ARR = 0xFFFF;//
TIM1->PSC = 71;//CPU工作频率72M,预分频72000000/(71+1)=1M,所以定时器1工作频率为1M
TIM1->CCR1 = 0xFFFF;
TIM1->RCR = 0x00;
TIM1->DIER = 0x03;//开启比较中断
TIM1->BDTR = 0x8000;
TIM1->CR1 = 0x81;定时器1使能
nvic_init(2,2,TIM1_CC_IRQn,2);
}
void TIM1_CC_IRQHandler(void) //定时器1中断
{
u16 temp;
if (TIM1->SR & 0x0002)
{
TIM1->SR &= 0xFFFD;
LED0 = !LED0;利用LED的高低电平测量验证
temp = TIM1->CCR1;
TIM1->CCR1 = temp + 2000; // 1M/2000=500HZ,用示波器测量是很标准的高电平2ms,低电平也是2ms
}
}
下面是500Khz的程序如下:
void timer_init(void)
{
RCC->APB2ENR |= 1 << 11;
TIM1->ARR = 0xFFFF;//
TIM1->PSC = 71;//CPU工作频率72M,预分频72000000/(71+1)=1M,所以定时器1工作频率为1M
TIM1->CCR1 = 0xFFFF;
TIM1->RCR = 0x00;
TIM1->DIER = 0x03;//开启比较中断
TIM1->BDTR = 0x8000;
TIM1->CR1 = 0x81;定时器1使能
nvic_init(2,2,TIM1_CC_IRQn,2);
}
void TIM1_CC_IRQHandler(void) //定时器1中断
{
u16 temp;
if (TIM1->SR & 0x0002)
{
TIM1->SR &= 0xFFFD;
LED0 = !LED0;利用LED的高低电平测量验证
temp = TIM1->CCR1;
TIM1->CCR1 = temp + 2; // 1M/2=500KHZ,用示波器测量是畸变的信号,且信号的频率不对,为50HZ
}
}
下面是1M的程序如下:
void timer_init(void)
{
RCC->APB2ENR |= 1 << 11;
TIM1->ARR = 0xFFFF;//
TIM1->PSC = 71;//CPU工作频率72M,预分频72000000/(71+1)=1M,所以定时器1工作频率为1M
TIM1->CCR1 = 0xFFFF;
TIM1->RCR = 0x00;
TIM1->DIER = 0x03;//开启比较中断
TIM1->BDTR = 0x8000;
TIM1->CR1 = 0x81;定时器1使能
nvic_init(2,2,TIM1_CC_IRQn,2);
}
void TIM1_CC_IRQHandler(void) //定时器1中断
{
u16 temp;
if (TIM1->SR & 0x0002)
{
TIM1->SR &= 0xFFFD;
LED0 = !LED0;利用LED的高低电平测量验证
temp = TIM1->CCR1;
TIM1->CCR1 = temp + 1; // 1M/1=1MHZ,用示波器测量是畸变的信号,且信号的频率不对,为15HZ
}
}
|