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.

Inchworm simple project flash an LED

Status
Not open for further replies.
Here's the code from the Inchworm quick start guide coming really soon.
Although this ones written for the Fireflys 16F88, the quick start will show code and schematics for the 12F509, 16F84A and 16F628A

It's designed simply to be your first "hello world" style program. The Firefly User Guide will describe how the very simple program works.

It flashes LED1 Green using the WDT.

Code:
;*** WDT reset toggles RA.0
         list    p=16F88
         include <p16F88.inc>
         __CONFIG _CONFIG1, 0x2F74
         org      0
         bsf      STATUS, RP0    
         movlw    b'00001110'
         movwf    OPTION_REG
         movlw    7
         movwf    CMCON
         clrf     ANSEL
         movlw    b'10111110'
         movwf    TRISA
         bcf      STATUS, RP0
         bcf      PORTA, 6
         movlw    1
         xorwf    PORTA, f
         sleep
         end
 
Last edited:
Wow, sounds great, can't wait to see this guide.
Would this code adapt directly to the 16F628A, given the first 2 lines of code are changed?

If so, I'll try that as soon as I get my serial cable. I found some IDC connectors and I'm pretty confident I've got everything else setup to get this chip programmed.
 
Here's the 16F628A version. An LED and 220ohm resistor on RA.0 should flash every 1.2sec
Hey the beauty of a WDT reset program is in theory it will never crash :p (cause it crashes all the time)
Code:
;*** WDT reset toggles RA.0
         list    p=16F628A
         include <p16F628A.inc>
         __CONFIG 0x3F34
         org      0
         bsf      STATUS, RP0    
         movlw    b'00001110'
         movwf    OPTION_REG
         movlw    7
         movwf    CMCON
         movlw    b'11111110'
         movwf    TRISA
         bcf      STATUS, RP0
         movlw    1
         xorwf    PORTA, f
         sleep
         end
 
Last edited:
Nigel Goodwin said:
One obvious omission is the interrupt vector, I would suggest a label at 0x0005 and a goto at 0x0000 - as you would normally.
It's designed to be as short a program as possible, easy to type in. It's not even commented.

Isn't WDT wakeup from sleep vectored to reset 0x000 and the INT vector 0x004?
 
Last edited:
William At MyBlueRoom said:
It's designed to be as short a program as possible, easy to type in. It's not even commented.

Isn't wakeup from sleep vectored to reset 0x000

Depends on the part - on old PIC's it was, and you had to check if it was a reset or a wakeup - but some modern PIC's simply continue on from where the sleep instruction was.

PS isn't the INT vector 0x004?

Yes, that's why you have a goto to jump past it, 0x0005 would obviously be the minimum, and it would move higher if an ISR was used - it's probably a good idea to have an RETFIE at 0x0004 just in case an interrupt should happen to occur?.
 
If you want to make it even shorter, you can get rid of the write to CMCON as the POR value is 7 on the 16f88. If you change it to flash LED 2 you can also get rid of the clr ANSEL.

Mike.
 
Nigel Goodwin said:
it's probably a good idea to have an RETFIE at 0x0004 just in case an interrupt should happen to occur?.

Interrupts don't just happen and if a stray interrupt occurred then it would keep executing the retfie because the offending interrupt bit isn't cleared. Do you also put 2 returns at the end of subroutines just in case it misses the first?

Mike.
 
Pommie said:
If you want to make it even shorter, you can get rid of the write to CMCON as the POR value is 7 on the 16f88. If you change it to flash LED 2 you can also get rid of the clr ANSEL.

Mike.
By gum you're right. That's a first the CMCON at 0x7 on power up. Thanks. It works :)
Code:
        ;*** WDT reset toggles RA.7
         list    p=16F88
         include <p16F88.inc>
         __CONFIG _CONFIG1, 0x2F74
         org      0
         bsf      STATUS, RP0    
         movlw    b'00001110'
         movwf    OPTION_REG
         movlw    b'00111111'
         movwf    TRISA
         bcf      STATUS, RP0
         movlw    b'10000000'
         xorwf    PORTA, f
         sleep
         end
 
Last edited:
Pommie said:
Interrupts don't just happen and if a stray interrupt occurred then it would keep executing the retfie because the offending interrupt bit isn't cleared. Do you also put 2 returns at the end of subroutines just in case it misses the first?

No, but by accidentally setting a bit wrong in a register you could inadvertently cause an interrupt - as already shown in a post elsewhere in this forum - an RETFIE would help prevent disaster!.

I thought the idea was to generate a general purpose header?, to base further programming on - it makes good sense to have an RETFIE and a jump past the interrupt vector.
 
Nigel Goodwin said:
No, but by accidentally setting a bit wrong in a register you could inadvertently cause an interrupt - as already shown in a post elsewhere in this forum - an RETFIE would help prevent disaster!.

Not really as the unserviced interrupt will cause the the retfie to be called for ever more and therefore result in a crash anyway.

I thought the idea was to generate a general purpose header?, to base further programming on - it makes good sense to have an RETFIE and a jump past the interrupt vector.

I thought this was a check it's working type program. For a general template I use the ones supplied by Microchip such as you find in C:\Program Files\Microchip\MPASM Suite\Template\Code\f88temp.asm

Mike.
 
This is not the master header version, it's the lazy typer program for the impatient first time PIC programmer to see that silly little LED blink. Not even good programming practice, just quick and dirty. It's only 15 bytes long... Heck the 12F509 / 16F84 version is even smaller.
 
Last edited:
William At MyBlueRoom said:
This is not the master header version, it's the lazy typer program for the impatient first time PIC programmer to see that silly little LED blink. Not even good programming practice, just quick and dirty. It's only 15 bytes long... Heck the 12F509 / 16F84 version is even smaller.

If you're giving a simple minimum program as an example, you may as well make it right! - rather than start people on bad practices. What's the difference between 15 bytes and 20 bytes when you're not even having to type them in?.
 
Let him rant. He should probably take a close look at some of his own examples before lecturing on "good programming practices" (grin).

Happy Holidays. Michael
 
I tried the flash a LED code for the 16F628A. When I "Release from Reset" the PIC, RA.0 is turned high and thus it lights up my LED.. but it never turns off, it just stays high all the time.

Are you sure the code is correct?
Also, do I have to set some configuration bits to some values in MPLAB in order to have it work or leaving it to defaults should be ok?
 
Try this version, uses RB0 instead. (doesn't use the PORT A analog stuff)

Code:
;*** WDT reset toggles RB0
    list     p=16F628A
    include <p16F628A.inc>
    __CONFIG 0x3F34
    org      0
    bsf      STATUS, RP0    
    movlw    b'00001110'
    movwf    OPTION_REG
    movlw    b'11111110'
    movwf    TRISB
    bcf      STATUS, RP0
    movlw    1
    xorwf    PORTB, f
    sleep
    end
 
That one works well, thanks :)

By the way.. there doesn't appear to be any loop control in the code.. do the program simply ends after the sleep instruction, and then, on a PIC, when the program has ended.. it just starts again from the beginning?
And the "sleep" command make it sleep for about 1.2 seconds and the xorwf instruction inverts the status of RB0?
 
Last edited:
Hedos said:
That one works well, thanks :)

By the way.. there doesn't appear to be any loop control in the code.. do the program simply ends after the sleep instruction, and then, on a PIC, when the program has ended.. it just starts again from the beginning?
And the "sleep" command make it sleep for about 1.2 seconds and the xorwf instruction inverts the status of RB0?

That's right, it uses the WDT timer and prescaler. The speed is determined by the value used in the OPTION_REG (look at the data sheet, you can go as long as 2.5sec or much shorter)

The program doesn't care how fast the clock is, 32KHz or 4MHz still runs the same. The sleep command just stops the program till an event occurs.

PS a neat trick (it's in one of the Microchip app notes) is the WDT timer is temperature sensitive. You can make a simple thermometer with it...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top