|
CVAVR 软件中启动delay.h库,调用delay_ms()函数,自动带了喂狗程序
近期在学习中发现个问题,CVAVR 中启动delay.h库,调用delay_ms()函数延时,系统怎么都不复位重启,即使打开看门狗熔丝位,看门狗也不会重启,找了很久原因,发现是调用调用系统自身带的delay_ms()函数引起的,换成自己的简单延时函数,问题就解决,看门狗可以正常工作,后面附带我自己写的简单延时函数。
后来查找问题,发现系统中的delay_ms()函数自带了喂狗程序,所以不会自动的重启,请大家放心使用,用延时函数看门狗不溢出是正常的。后面附带软件编辑后生产的汇编程序,一看就知道确实带了喂狗。
今天写出来供大家注意,不要犯我同样的问题。
/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.9 Standard
Chip type : ATmega8L
Program type : Application
Clock frequency : 1.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/2048k
#pragma optsize-
//#asm("cli") //关中断
//#asm("WDR") //看门狗计数器清零==喂狗
WDTCR=0x1F; //启动看门狗第一步
WDTCR=0x0F; //启动看门狗第二步
//#asm("sei") //打开中断
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
PORTB.1=0;
DDRB.1=1;
while (1)
{
delay_ms(3000); //延时3秒,理论应该是会反复的重启,但是却没有重启。
//Delay1000ms(); //换用自己的简单延时函数,看门狗问题解决
//Delay1000ms();
//Delay1000ms();
PORTB.1=1 ;
#asm("WDR") //看门狗计数器清零==喂狗 在2.1秒溢出前必须清零
};
}
//之上是测试程序,下面是我写的一个简单的大约延时1秒函数,时间不精确,大家只可以借鉴一下。
//大约延时1秒函数
void Delay1000ms() //@1MHz大约1秒的延时程序
{
unsigned char i, j, k;
i = 4;
j = 166;
k = 210;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
//下面是我打开汇编码,看到的结果
; 31 while (1)
_0x7:
; 32 {
; 33
; 34 delay_ms(3000); //延时3秒,理论应该是会反复的重启,但是却没有重启。
LDI R30,LOW(3000)
LDI R31,HIGH(3000)
ST -Y,R31
ST -Y,R30
RCALL _delay_ms
;程序运行到这里执行了下面的_delay_ms程序,请继续往下看
; 35 PORTB.1=1 ;
SBI 0x18,1
; 36 #asm("WDR") //看门狗计数器清零==喂狗 在2.1秒溢出前必须清零,这条是36行,执行的喂狗程序,下面的WDR就是喂狗
WDR
; 37 };
RJMP _0x7
; 38 }
_0xC:
RJMP _0xC
_delay_ms:
ld r30,y+
ld r31,y+
adiw r30,0
breq __delay_ms1
__delay_ms0:
__DELAY_USW 0xFA
Wdr
;这里看到了没有,上面自动带了喂狗程序。。。。还是比较先进的啊!!
sbiw r30,1
brne __delay_ms0
__delay_ms1:
ret
;END OF CODE MARKER
__END_OF_CODE:
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|