Hallo,
ich habe ein Bascom Demo Programm gemacht. Leider bin ich in C nicht so fit, daß ich das schnell hinbekomme. Habe aber versucht, möglichst gut zu kommentieren. Programm ist auf Tiny44 getestet. Auf Tiny13 heißen, so meine ich, nur zwei relevante Bits anders. Ist im Programm kommentiert.
Gruß
Searcher
Code:
'###################################################
'
'ATtiny44
'
'WATCHDOG DEMO
'
'IMPORTANT prerequisite:
'WDTON fuse is default (unprogrammed = 1, watchdog timer interrupt function is enabled)
'
'three LEDs are used to visually monitor the success of sleep mode pauses
' 1. after start of program (or a reset) all three leds lit for one second
' 2. 8 seconds wdt pause
' 3. wdt led flash, life led flash
' 4. 8 seconds pause
' 5. wdt led flash, life led flash
' 6. 8 seconds pause
' 7. wdt led flash, life led flash
' 8. directly after 7. action led flash
' 9 5 seconds pause in action sub, another action led flash
' 10. continue step 2. with 8 sconds pause
'
' control overall sleep duration by value in watchdog_ovf
'
'#######################################################
$regfile = "attiny44.dat"
$framesize = 24
$swstack = 24
$hwstack = 38
$crystal = 8000000 'internal oscillator
Config Portb.0 = Output 'WDT LED, switched in watchdog isr (200ms flash)
Config Porta.3 = Output 'Action LED indicates action after watchdog controlled sleep
Config Porta.7 = Output 'Life LED, monitor wether sleep mode ist entered
Rem : all LEDs for 1 second ON (check for program begin and/or µC reset)
portb.0 = 1
porta.3 = 1
porta.7 = 1
wait 1
portb.0 = 0
porta.3 = 0
porta.7 = 0
Rem : check end for program begin and/or µC reset
dim watchdog_action_flag as byte 'set in watchdog interrupt, indicates end of sleep pause
dim watchdog_ovf as word 'counts watchdog timer overflows before action
set mcucr.sm1 'prepare sleepmode as power down, SM1 Bit in MCUcR
on wdt isr_watchdog 'watchdog interrupt vector to watchdog isr
wdtcsr = bits(wdp0 , wdp3) 'set wdp0,wdp3 bits, watchdog prescaler to 8 seconds
Rem : ### !wdr 'asm instruction "wdr" resets the watchdog timer
set WDTCSR.wdie 'watchdog interrupt enable
Rem attiny13 : set WDTCSR.wdtie 'watchdog interrupt enable
enable interrupts 'global interrupt enable (sei)
Rem : ###############################################################################
Rem : ### main loop
Do
set mcucr.se 'enable following sleep command, SE Bit in MCUCR
!sleep 'asm sleep command. enters sleepmode which is prepared in MCUCR by SM bits
reset mcucr.se 'disable sleep commands: recomended in datasheet
porta.7 = 1 'Life LED ON
waitms 200 '200ms flash, monitor sleep state
porta.7 = 0 'Life LED OFF
Rem : ### if sleepmode ist not entered , Life LED OFF is not noticable (to fast, LED appears continiously on exept during action)
if watchdog_action_flag = 1 then
gosub sub_watchdog_action
watchdog_action_flag = 0
end if
Loop
Rem : main loop end
Rem : ###############################################################################
Rem : ###############################################################################
Rem : ### in subroutine do whatever to be done after watchdog controlled long sleep break
sub_watchdog_action:
porta.3 = 1 'Action LED ON
waitms 200 '200ms flash
porta.3 = 0 'Action LED OFF
wait 5 '5 seconds do nothing, just wait
porta.3 = 1 'Action LED ON
waitms 200 '200ms flash
porta.3 = 0 'Action LED OFF
Rem : ### !wdr 'asm instruction "wdr" resets the watchdog timer
set WDTCSR.wdif 'clear eventually set interrupt flag before enabling wdt interrupt
set WDTCSR.wdie 'watchdog interrupt enable
Rem attiny13 : set WDTCSR.WDTIF 'clear eventually set interrupt flag before enabling wdt interrupt
Rem attiny13 : set WDTCSR.WDTIE 'watchdog interrupt enable
return
Rem : ###############################################################################
isr_watchdog: 'controls invoking of "sub_watchdog_action"
portb.0 = 1
waitms 200 '200ms flash - monitor watchdog interrupt
portb.0 = 0
watchdog_ovf = watchdog_ovf + 1 'count wdt timer overflows/interrupts
if watchdog_ovf >= 3 then
reset WDTCSR.wdie 'watchdog interrupt disable
Rem attiny13 : reset WDTCSR.wdtie 'watchdog interrupt disable
watchdog_action_flag = 1 'flag will be processed in main loop
watchdog_ovf = 0
end if
return
end 'end program
Lesezeichen