导航: 老古网老古论坛XMOS公共讨论区XMOS开源项目区单片机程序设计嵌入式系统广告区域
→AVR定时问题[求助][fengsunping]

 *第18767篇: AVR定时问题[求助]

  
楼 主:fengsunping 2008年3月6日11:08
 AVR定时问题[求助]
//ICC-AVR application builder : 2008-3-6 9:46:31
// Target : M16
// Crystal: 6.0000Mhz

#include   <iom16v.h>  
#include   <macros.h>  

void port_init(void)
{
 PORTA = 0xff;
 DDRA  = 0xff;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//TIMER0 initialize - prescale:256
// WGM: CTC
// desired value: 10mSec
// actual value: 10.027mSec (-0.3%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 //TCNT0 = 0x16; //set count
 OCR0  = 0xEA;  //set compare
 TCCR0 = 0x0C; //start timer
}

#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
  static unsigned char i,count1s;
 count1s++;
 if(count1s==100)
 {
  count1s=0;
  i++;
 } 
 switch(i)
 {
  case 0:
  break;  
  case 1:
   PORTA=0XFE;
  break;
  case 2:
   PORTA=0XFF;
   i=0;
  break;
  default:
   i=0;
  break;
 }
 //compare occured TCNT0=OCR0
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer0_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x02; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

void main()
  {
   init_devices();
   while(1);
  }
此程序的意思是每个中断定时10MS,1S时间变化一次.
但实际结果,5,6S时间才变化一次,我想问为什么?


>>>>>>对该主题发表你的看法

本主题贴数1,分页: [第1页]


[上一篇主题]:熟悉STC单片机的大侠请进

[下一篇主题]:解密芯片