打印
[AVR单片机]

T1的中断问题,请教各位了!!!急急

[复制链接]
1481|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yaoji104|  楼主 | 2007-3-27 14:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
设计思路:我想用Mega64 T1的比较匹配中断,产生固定的定时间隔。比如1ms或是10ms。
设置T1的工作模式(WGMn3:0 = 4) 设置OCR1A = 2000;打开了匹配中断 TIMSK |= (1<<OCIE1A);附上源码

#include <iom64v.h>
#include <macros.h>

#pragma interrupt_handler isr_timer1:15
unsigned int  counter;

void main()
{
       counter   = 300;
      TCCR1B |= (1<<WGM12);
      OCR1A  = 2000;
      DDRB     = 0xF0;
      
      SEI();
      TIMSK     |= (1<<OCIE1A);
     
      TCCR1B |= 1<<CS10;
           
      while(1);
}

void   isr_timer1()
{
      if(counter != 0)
    {
       counter--;
    }
    else
    {
        counter = 300;
        PORTB ^= 1<<PB7;
    }
}

但是接在PB7的LED并没有按照600ms闪烁一次。不知道是什么原因,还望高手指点。

相关帖子

沙发
kennyzhao| | 2007-4-3 14:01 | 只看该作者

回复

注意对定时器输入时钟的设置,CS12,CS11,CS10位,默认是没有CLOCK的

使用特权

评论回复
板凳
kennyzhao| | 2007-4-3 17:14 | 只看该作者

呵呵

刚看错了,

使用特权

评论回复
地板
avrvi| | 2007-4-4 00:27 | 只看该作者

中断向量不对,程序也不明了

timer1_compa_isr:13

中断向量都没有搞对哦,好像。

//ICC-AVR application builder : 2007-4-4 0:44:06
// Target : M64
// Crystal: 4.0000Mhz

#include <iom64v.h>
#include <macros.h>

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0xF0;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;
}

//TIMER1 initialize - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 10mSec
// actual value:  9.984mSec (0.2%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFF; //setup
 TCNT1L = 0x64;
 OCR1AH = 0x00;
 OCR1AL = 0x9C;
 OCR1BH = 0x00;
 OCR1BL = 0x9C;
 OCR1CH = 0x00;
 OCR1CL = 0x9C;
 ICR1H  = 0x00;
 ICR1L  = 0x9C;
 TCCR1A = 0x00;
 TCCR1B = 0x04; //start Timer
}

#pragma interrupt_handler timer1_compa_isr:13
void timer1_compa_isr(void)
{
 //compare occured TCNT1=OCR1A
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 port_init();
 timer1_init();

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x10; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

相关链接:http://www.avrvi.com/

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

9

帖子

0

粉丝