





| Reset Your Arduino |
| 站長Blog文章單元 - AVR_Basic | |
| 作者是 xlinx | |
| 週日, 11 七月 2010 00:40 | |
|
在不使用AVR的WDT看門狗中斷下,我們必須借外部電路重新Reset ArduinoChip本身。WDT一般對瞬間干擾造成的問題有效,要是長時間的干擾或是軟硬件問題,看門Dog的意義不是很大。 For Arduino(testing) AVR IO=LOW => AVR RST=Hi For Arduino code
int PWMledPin = 9;
void setup() {
pinMode(7, OUTPUT);
}
void loop() {
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(PWMledPin, fadeValue);
delay(30);
}
digitalWrite(7, LOW); // send RST command
//digitalWrite(7, HIGH); // 依據9013 or 9012
//never run here
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(PWMledPin, fadeValue);
delay(30);
}
}
For AVR WDT C code(AVR分為軟體看門Dog和硬體Dog fuse)
#include
#include
void port_init(void){
PORTA = 0x03; //設置為輸出
DDRA = 0x03; //高電位,兩個LED都滅
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//Watchdog initialize
// prescale: 2048K //f越大,定時時間越長,也就是可以更長時間不餵
// 約為2.1s RST (根據spec,2048K,5V典型)
void watchdog_init(void){
WDR(); //this prevents a timout on enabling
WDTCR = 0x1F;
WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}//加入了餵食物的延時程式
void Delay(void)
{
unsigned char i,j;
for(i=200;i>0;i--){
for(j=200;j>0;j--)
;
}
WDR(); //這裡餵食物
}
//call this routine to initialize all peripherals
void init_devices(void){ //stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
watchdog_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void){
unsigned int i;
init_devices(); //初始化
for(i=10;i>0;i--){ //看到程式的閃動
PORTA = 0x02; //1腳為高,0腳為低,0腳燈亮
Delay(); //延時
PORTA = 0x01; //0腳為高,1腳為低,1腳燈亮
Delay(); //延時
}
while(1) //普通情況下,程式會陷入這裡一直循環。
; //看門狗能夠讓AVR復位,程式重新運行,我們看到LED閃爍。
//如果在這裡加入WDR(); 餵食物,AVR就不會復位了。
}
|
|
| 最近更新在 週日, 11 七月 2010 11:01 |