新手上路
- 积分
- 48
- 金钱
- 48
- 注册时间
- 2016-11-22
- 在线时间
- 12 小时
|
1金钱
定时器是10mS中断,长时间搁置后,timer变成了200uS的更新中断
相关代码如下:
void sysclk_init(void)
{
//主时钟选择
{
CLK_SWR = 0xE1; //内部16MHSI为主时钟源
CLK_ICKR |= 0x05; //从halt模式唤醒使能、使能HSI时钟
while((CLK_ICKR & 0x02) == 0x00); //等待HSI稳定就绪
}
//16MRC时钟1:2输出 即Fmaster = Fhsi / 2 (00-1:1 01-1:2 10-1:4 11-1:8)
{
CLK_CKDIVR &= ~(1 << 4);
CLK_CKDIVR |= (1 << 3);
}
//Fcpu = Fmaster = 8M 这个才是真正的供CPU使用的时钟
CLK_CKDIVR &= ~((1 << 2) | (1 << 1) | (1 << 0));
//Fcpu = Fmaster / 2 = 4M
CLK_CKDIVR |= (1 << 0);
// CLK_CKDIVR = 0x00; //16MHz主频
CLK_PCKENR1 = 0x00; //关闭所有外设时钟
CLK_PCKENR2 = 0x00;
}
void awu_init(void)
{
CLK_PCKENR2 |= (1 << 2); //使能AWU时钟
//唤醒时间2.00S
AWU_TBR = 0x0D;
AWU_APR = 0x3E; //分频Fls
AWU_CSR1 |= 0x10; //AWU使能
CLK_ICKR |= (1 << 5); //活跃停机模式(AWU使能情况)下,关闭电压调节器节省功耗
FLASH_CR1 |= (1 << 2); //活跃停机模式下Flash掉电,默认只有停机模式才掉电,代价是唤醒时间增加至微秒级别
}
void tim1_init(void)
{
CLK_PCKENR1 |= (1 << 7);
//定时器时钟fck_cnt = Fck_psc / ( PSCR[15:0]+1); Fck_psc = Fmaster = 8M
//fck_cnt = 8M / (799 + 1) = 10KHz 周期100us
TIM1_PSCRH = 0x03;
TIM1_PSCRL = 0x1F; //(fCK_CNT)等于fCK_PSC/( PSCR[15:0]+1)。
// TIM1_CNTR寄存器计数到0x0064(100)为一次溢出:10ms
TIM1_ARRH = 0x00;
TIM1_ARRL = 0x64;
TIM1_IER = 0x01; //允许更新中断
//重复计数寄存器复位值为0,溢出一次即产生中断
// TIM1_RCR = 50; //重复计数寄存器值,这样timer1 2次50ms即100ms产生一次中断
TIM1_CR1 = 0x01;
}
void main(void)
{
sysclk_init();
adc_init();
timer1_init();
awu_init();
while(1)
{
//关ADC
ADC_CR1 &= ~(1 << 0);
//关timer
TIM1_CR1 &= ~0x01;
CLK_PCKENR1 &= ~(1 << 7);
asm("halt"); //进入睡眠模式
asm("nop");
asm("nop");
enter_int(); //关全局中断
CLK_PCKENR2 |= (1 << 3);
ADC_CR1 &= ~(1 << 0);
Delay_ms(10);
ADC_CR1 |= (1 << 0);
CLK_PCKENR1 |= (1 << 7);
TIM1_SR1 &= ~0x01;//清除中断标志
TIM1_CR1 = 0x01;
exit_int(); //开全局中断
}
}
|
|