Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Including a Watchdog

Status
Not open for further replies.

asilva

New Member
Hi all,
I have a simple project/code which the micro keeps flashing a LED.
Now I'm trying to add a Watchdog Timer to the code but I'm really stuck.

The code I have to flash the LED is below, and it works perfectly:

Code:
list P = 16F877
;
        include "P16f877.inc"  ; use definition file for 16F877
;
; --------------------
; USER RAM DEFINITIONS
; --------------------
;
        CBLOCK 0x20   ; RAM starts at address 20h
NaHi
NaLo
NbHi
NbLo
        ENDC
;
        org 0x0000      ; start address = 0000h

; INITIALISE PORTS
; binary used to see individual pin level

        movlw b'00000000'       ; all port pins = low
        movwf PORTA
        movlw b'00000000'
        movwf PORTB
        movlw b'00000000'
        movwf PORTC
        movlw b'00000000'
        movwf PORTD
        movlw b'00000000'
        movwf PORTE

        bsf STATUS,RP0  ; set RAM Page 1 for TRIS registers

; INITIALISE PORTS
; binary used to see individual pin IO status

        movlw b'00000000'       ; all IO pins = outputs
        movwf TRISA
        movlw b'00000000'
        movwf TRISB
        movlw b'00000000'
        movwf TRISC
        movlw b'00000000'
        movwf TRISD
        movlw b'00000000'
        movwf TRISE

        movlw b'00000110'       ; all analog pins = digital
        movwf ADCON1

        bcf STATUS,RP0  ; back to RAM page 0

; LED FLASH LOOP

Loop    bsf PORTC,4     ; RC4 = high = led on
        call Delay

        bcf PORTC,4     ; RC4 = low = led off
        call Delay
        goto Loop

; 1/2 SEC DELAY SUBROUTINE WITH 4MHz CLOCK

Delay   movlw 01h
        movwf NbHi
        movlw 03h
        movwf NbLo
        movlw 8Ah
        movwf NaHi
        movlw 5Bh
        movwf NaLo

DeLoop0 decfsz NaLo,F
        goto DeLoop0
        decfsz NaHi,F
        goto DeLoop0
        decfsz NbLo,F
        goto DeLoop0
        decfsz NbHi,F
        goto DeLoop0
        ;
        return

        end

So, could anyone show me how to add the WDT?
I've seen other posts and tried some bits of code but I can't really understand.
Thanks in advance.
 
Because I have another project to do that requires a Watchdog.
So firstly I'm trying to add it in a simple program. Then after I understand how it works I'll move on to the more complicated project.
 
Hi

On the PIC you are using the Watchdog timer configurations is 'shared' with that of timer0.
To enable the Watchdog you need to turn it on the the configuration bits.
You can extend the watchdog interval by changing the Pre-scalar in the 'option-register'.
Then in your code you need to clear the Watchdog by using 'CLRWDT' - if you do not clear it (or your application hangs) then a restart will be issued.

Although brief I hope this helps.
 
Hi gaspode42,
I have now added __CONFIG _WDT_ON at the top
and CLRWDT in the loop:
Code:
; LED FLASH LOOP

Loop    bsf PORTC,4     ; RC4 = high = led on
        call Delay

        bcf PORTC,4     ; RC4 = low = led off
        call Delay
        clrwdt
        goto Loop

But how and where should I configure the WDT?

Thank you.
 
Last edited:
Asilva

Sorry I am missing something, what do you want to configure.
If you want to change the duration of the Watchdog you can set the pre-scalar in the option_reg[3:0].
Have a look at the attached app-note it may help.
 

Attachments

  • 00828a-1.pdf
    144.3 KB · Views: 204
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top