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

* 33124: AVR定时问题[求助]

   fengsunping 
fengsunping发表的帖子 

 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时间才变化一次,我想问为什么?

发表时间:2008年3月6日11:08:52

  
回复该帖

本主题共有 1 帖,分页:>>>>>该主题的所有内容[1]条

 *树形目录 只列出部分跟帖的标题以及简单的摘要信息 该主题的部分跟帖如下:

[上一篇帖子]:熟悉STC单片机的大侠请进小匠入行不久,不懂规矩,如有不妥,请多谅解。 最近做一个项目,考虑
[下一篇帖子]:[原创]13760303660张工