2006binghuo 发表于 2006-4-30 22:00:01

请精mega16的大侠进来-----一个非常奇怪的问题

熔丝位设置    高频石英/陶瓷振荡器(3.0-8.0MHZ)        16K CK + 0 ms        CKSEL=1111 SUT=01



板子用的是双龙SL-DIY02-9,外部8兆晶振



我写了这样一个程序

//ICC-AVR application builder : 2006-4-30 15:38:54

// Target : M16

// Crystal: 8.0000Mhz



#include <iom16v.h>

#include <macros.h>







void port_init(void)

{

PORTA = 0x00;

DDRA= 0x01;

PORTB = 0xFF;

DDRB= 0x00;

PORTC = 0xFF; //m103 output only

DDRC= 0x00;

PORTD = 0xFF;

DDRD= 0x00;

}



//TIMER1 initialisation - prescale:8

// WGM: 14) PWM fast, TOP=ICRn

// desired value: 50Hz

// actual value: 50.000Hz (0.0%)

void timer1_init(void)

{

TCCR1B = 0x00; //stop

TCNT1H = 0x00; //setup

TCNT1L = 0x00;

ICR1H= 0x4E;

ICR1L= 0x1F;

TCCR1A = 0x02;

TCCR1B = 0x1A; //start Timer

}



int flag = 0;



#pragma interrupt_handler timer1_ovf_isr:9

void timer1_ovf_isr(void)

{

//TIMER1 has overflowed

TCNT1H = 0x00; //reload counter high value

TCNT1L = 0x00; //reload counter low value

if (flag == 0)

        {

       PORTA = 0x01;       

       flag=1;

        }

else        

{PORTA = 0x00;

flag = 0;

}       

}



//call this routine to initialise all peripherals

void init_devices(void)

{

//stop errant interrupts until set up

CLI(); //disable all interrupts

port_init();

timer1_init();



MCUCR = 0x00;

GICR= 0x00;

TIMSK = 0x14; //timer interrupt sources

SEI(); //re-enable interrupts

//all peripherals are now initialised

}



void main(void)

{

init_devices();

while(1);

}



用示波器测试端口PA1的波形,始终是高电平,而且所有端口都一直是高电平,看起来就好像在下载后,程序根本不执行,而且可以肯定不是示波器的原因

其后我又写了几个带有中断的程序都出现同样的问题









然后我将串口接受部分注释起来,并改了相应的标志位运行通过

//ICC-AVR application builder : 2006-4-29 12:59:32

// Target : M16

// Crystal: 8.0000Mhz



#include <iom16v.h>

#include <macros.h>



void port_init(void)

{

PORTA = 0x00;                                //11000000

DDRA= 0x00;                                //00111111

PORTB = 0xFF;

DDRB= 0x00;

PORTC = 0xFF; //m103 output only

DDRC= 0xFF;

PORTD = 0xFF;

DDRD= 0xFF;

}



//call this routine to initialise all peripherals

void init_devices(void)

{

//stop errant interrupts until set up

CLI(); //disable all interrupts

port_init();

MCUCR = 0x00;

GICR= 0x00;

TIMSK = 0x00; //timer interrupt sources

SEI(); //re-enable interrupts

//all peripherals are now initialised

}



void main(void)

{



       

        int i=0;

        int j=0;

       

        init_devices();

       

       

        //insert your functional code here...

        while (1)

        {

                for(i=0;i<100;i++)

                        for(j=0;j<1500;j++);

                PORTC = 0xFF;       

                for(i=0;i<100;i++)

                        for(j=0;j<1500;j++);

                PORTC = 0x00;       

               



        }



}



此程序是让C0---C7的七个灯不停的闪烁,程序运行通过,实现预定功能,说明问题不是出在下载设备或软件上



熔丝位设置    高频石英/陶瓷振荡器(3.0-8.0MHZ)        16K CK + 0 ms        CKSEL=1111 SUT=01



板子用的是双龙SL-DIY02-9,外部8兆晶振

lzhwbd 发表于 2006-5-11 20:37:29

让我来告诉你吧。

你这里面一个严重的问题是开了输出比较1的中断,而却没有输出比较1的中断程序。

而OCR1A又等于0,因此在你执行完溢出程序后,实际上紧接是进入了输出比较1中断入口。因为你没有在中断入口写程序,所以单片机执行了0XFFFF的空代码。恰好溢出中断入口在输出比较1中断入口的后面,这时候单片机实际上又一次执行了溢出中断程序。

所以,在示波器上显示的波形是一个个的短脉冲。至于你说的端口PA1的波形,始终是高电平,我想应该是你看的不够认真。
页: [1]
查看完整版本: 请精mega16的大侠进来-----一个非常奇怪的问题