Exact match. Not showing close matches.
PICList
Thread
'[PIC] Help on PIC16F628A - blinking LED'
2008\08\05@054707
by
Daniele Salatti
|
Hi all!
I'm a beginner in the PIC world, hope this is the right place for my
question.
I'm trying to blink a LED with a PIC16F628A. I'm on Linux, using Piklab as
IDE.
I wrote some code but it doesn't work...can someone help me finding where's
the problem?
Here is my code:
; -----------------------------------------------------------------------
; Template source file generated by piklab
#include <p16f628a.inc>
; -----------------------------------------------------------------------
; Configuration bits: adapt to your setup and needs
__CONFIG _WDT_ON & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT & _MCLRE_ON &
_BOREN_ON & _LVP_OFF & _DATA_CP_OFF & _CP_OFF
; -----------------------------------------------------------------------
; Variables declaration
INT_VAR UDATA_SHR
w_saved RES 1 ; variable used for context saving
status_saved RES 1 ; variable used for context saving
pclath_saved RES 1 ; variable used for context saving
count1 RES 1 ; used in delay routine
counta RES 1 ; used in delay routine
countb RES 1 ; used in delay routine
; -----------------------------------------------------------------------
; reset vector
STARTUP CODE 0x000
nop ; needed for ICD2 debugging
movlw high start ; load upper byte of 'start' label
movwf PCLATH ; initialize PCLATH
goto start ; go to start of main code
; interrupt vector
INT_VECTOR CODE 0x004
goto interrupt ; go to start of interrupt code
; relocatable code
PROG CODE
interrupt
movwf w_saved ; save context
swapf STATUS,w
movwf status_saved
movf PCLATH,w ; only required if using more than first page
movwf pclath_saved
clrf PCLATH
; << insert interrupt code >>
movf pclath_saved,w ; restore context
movwf PCLATH
swapf status_saved,w
movwf STATUS
swapf w_saved,f
swapf w_saved,w
retfie
start
;The following code is used to set all I/O pins as output
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
bsf STATUS, RP0 ;select bank 1
movlw b'00000000' ;set PortB all outputs
movwf TRISB
movwf TRISA ;set PortA all outputs
bcf STATUS, RP0 ;select bank 0
Loop
movlw 0xff
movwf PORTA ;set all bits on
movwf PORTB
nop ;the nop's make up the time taken by the goto
nop ;giving a square wave output
call Delay ;this waits for a while!
movlw 0x00
movwf PORTA
movwf PORTB ;set all bits off
call Delay
goto Loop ;go back and do it again
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
END
Thank you in advance...
Dan
2008\08\05@060317
by
Tamas Rudnai
Hi Dan,
You have the watchdog turned on which makes the chip reset about every 16ms
if you do not clear the timer with the CLRWDT instruction from time to time:
> _WDT_ON
Try your code with _WDT_OFF and will se if that works, if not, you can use
GPSIM to debug your code and tell us where is the code fails.
Tamas
On Tue, Aug 5, 2008 at 10:46 AM, Daniele Salatti <spam_OUTsaleTakeThisOuT
salatti.net> wrote:
{Quote hidden}>
> Hi all!
> I'm a beginner in the PIC world, hope this is the right place for my
> question.
> I'm trying to blink a LED with a PIC16F628A. I'm on Linux, using Piklab as
> IDE.
> I wrote some code but it doesn't work...can someone help me finding where's
> the problem?
>
> Here is my code:
>
> ; -----------------------------------------------------------------------
> ; Template source file generated by piklab
> #include <p16f628a.inc>
>
> ; -----------------------------------------------------------------------
> ; Configuration bits: adapt to your setup and needs
> __CONFIG _WDT_ON & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT & _MCLRE_ON &
> _BOREN_ON & _LVP_OFF & _DATA_CP_OFF & _CP_OFF
>
>
>
> ; -----------------------------------------------------------------------
> ; Variables declaration
> INT_VAR UDATA_SHR
> w_saved RES 1 ; variable used for context saving
> status_saved RES 1 ; variable used for context saving
> pclath_saved RES 1 ; variable used for context saving
>
> count1 RES 1 ; used in delay routine
> counta RES 1 ; used in delay routine
> countb RES 1 ; used in delay routine
>
> ; -----------------------------------------------------------------------
> ; reset vector
> STARTUP CODE 0x000
> nop ; needed for ICD2 debugging
>
> movlw high start ; load upper byte of 'start' label
> movwf PCLATH ; initialize PCLATH
> goto start ; go to start of main code
>
> ; interrupt vector
> INT_VECTOR CODE 0x004
> goto interrupt ; go to start of interrupt code
>
> ; relocatable code
> PROG CODE
> interrupt
> movwf w_saved ; save context
> swapf STATUS,w
> movwf status_saved
>
> movf PCLATH,w ; only required if using more than first page
> movwf pclath_saved
> clrf PCLATH
> ; << insert interrupt code >>
> movf pclath_saved,w ; restore context
> movwf PCLATH
> swapf status_saved,w
> movwf STATUS
> swapf w_saved,f
> swapf w_saved,w
> retfie
>
> start
>
> ;The following code is used to set all I/O pins as output
> movlw 0x07
> movwf CMCON ;turn comparators off (make it like
> a 16F84)
>
> bsf STATUS, RP0 ;select bank 1
> movlw b'00000000' ;set PortB all outputs
> movwf TRISB
> movwf TRISA ;set PortA all outputs
> bcf STATUS, RP0 ;select bank 0
>
> Loop
> movlw 0xff
> movwf PORTA ;set all bits on
> movwf PORTB
> nop ;the nop's make up the time taken by
> the goto
> nop ;giving a square wave output
> call Delay ;this waits for a while!
> movlw 0x00
> movwf PORTA
> movwf PORTB ;set all bits off
> call Delay
> goto Loop ;go back and do it again
>
> Delay movlw d'250' ;delay 250 ms (4 MHz clock)
> movwf count1
> d1 movlw 0xC7
> movwf counta
> movlw 0x01
> movwf countb
> Delay_0
> decfsz counta, f
> goto $+2
> decfsz countb, f
> goto Delay_0
>
> decfsz count1 ,f
> goto d1
> retlw 0x00
>
> END
>
> Thank you in advance...
>
> Dan
>
> -
2008\08\05@063624
by
Jinx
Daniele, do you have /mclr pulled up ? 10k to 5V
2008\08\05@080611
by
olin piclist
Daniele Salatti wrote:
> ; interrupt vector
> INT_VECTOR CODE 0x004
> goto interrupt ; go to start of interrupt code
You were doing well using relocatable mode, RES, UDATA_SHR, different CODE
sections etc. That already puts you ahead of 99% of other newbies.
However, don't put a GOTO at the interrupt vector. Put the interrupt
routine there directly instead. You don't know how PCLATH is set on entry
to the interrupt routine, and the GOTO wastes two cycles in any case.
********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014. Gold level PIC consultants since 2000.
2008\08\05@083458
by
Jan-Erik Soderholm
Olin Lathrop wrote:
> Daniele Salatti wrote:
>> ; interrupt vector
>> INT_VECTOR CODE 0x004
>> goto interrupt ; go to start of interrupt code
>
> You were doing well using relocatable mode, RES, UDATA_SHR, different CODE
> sections etc. That already puts you ahead of 99% of other newbies.
>
> However, don't put a GOTO at the interrupt vector. Put the interrupt
> routine there directly instead. You don't know how PCLATH is set on entry
> to the interrupt routine, and the GOTO wastes two cycles in any case.
I'm not saying that you're wrong, but... :-)
Note that older MPLAB's had a protected section at the
start, like this (probably a late 7.x version) :
...
CODEPAGE NAME=vectors START=0x0 END=0x4 PROTECTED
CODEPAGE NAME=page0 START=0x5 END=0x7FF
...
This might explain why you see a lot of GOTO's at the
ISR vector. Yes, PCLATH is a problem on > 2KWord devices,
but not on the 16F628A, at least...
Later MPLAB's has this small section removed.
Jan-Erik.
2008\08\05@115632
by
olin piclist
Jan-Erik Soderholm wrote:
> Note that older MPLAB's had a protected section at the
> start, like this (probably a late 7.x version) :
>
> ...
> CODEPAGE NAME=vectors START=0x0 END=0x4 PROTECTED
> CODEPAGE NAME=page0 START=0x5 END=0x7FF
> ...
>
> This might explain why you see a lot of GOTO's at the
> ISR vector.
Possibly, but the right solution is to fix the linker file, not convolute
the code to fit the broken linker conventions. You can get away with a GOTO
at 4 on a one-page PIC, but this is definitely a bug on larger 14 bit core
PICs. It's better never to put the GOTO at 4 in the first place, even when
it's only a waste of 2 cycles instead of outright bug.
> Later MPLAB's has this small section removed.
Yes, they *finally* fixed it after many years and much nagging.
********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014. Gold level PIC consultants since 2000.
2008\08\06@043327
by
Daniele Salatti
Hi!
Thanks to everyone!
I have turned off the watchdog, but now I have the opposite problem: the
LED is always on!
I'm going to try GPSim...and then I'll move the interrupt routine! :)
Dan
> Hi Dan,
>
> You have the watchdog turned on which makes the chip reset about every
16ms
> if you do not clear the timer with the CLRWDT instruction from time to
time:
>
> > _WDT_ON
>
> Try your code with _WDT_OFF and will se if that works, if not, you can
use
> GPSIM to debug your code and tell us where is the code fails.
>
> Tamas
2008\08\06@071709
by
Richard Seriani, Sr.
----- Original Message -----
From: "Daniele Salatti" <.....saleKILLspam
@spam@salatti.net>
To: <piclist
KILLspammit.edu>
Sent: Wednesday, August 06, 2008 4:32 AM
Subject: Re: [PIC] Help on PIC16F628A - blinking LED
{Quote hidden}>
> Hi!
> Thanks to everyone!
> I have turned off the watchdog, but now I have the opposite problem: the
> LED is always on!
> I'm going to try GPSim...and then I'll move the interrupt routine! :)
>
> Dan
>
>
>> Hi Dan,
>>
>> You have the watchdog turned on which makes the chip reset about every
> 16ms
>> if you do not clear the timer with the CLRWDT instruction from time to
> time:
>>
>> > _WDT_ON
>>
>> Try your code with _WDT_OFF and will se if that works, if not, you can
> use
>> GPSIM to debug your code and tell us where is the code fails.
>>
>> Tamas
>
> --
Dan,
How is the LED connected and to which pin of the PIC?
Richard
>
2008\08\06@121853
by
Matthew Miller
On Tue, Aug 05, 2008 at 05:46:37AM -0400, Daniele Salatti wrote:
> movlw 0x07
> movwf CMCON ;turn comparators off (make it like a 16F84)
>
> bsf STATUS, RP0 ;select bank 1
> movlw b'00000000' ;set PortB all outputs
> movwf TRISB
> movwf TRISA ;set PortA all outputs
> bcf STATUS, RP0 ;select bank 0
>
> Loop
> movlw 0xff
> movwf PORTA ;set all bits on
> movwf PORTB
I think the problem is that you're not changing banks before accessing a
register. Here is how you correct this:
> movlw 0x07
banksel CMCON
> movwf CMCON ;turn comparators off (make it like a 16F84)
>
> bsf STATUS, RP0 ;select bank 1
> movlw b'00000000' ;set PortB all outputs
banksel TRISB ; TRISA is in the same bank as TRISB.
> movwf TRISB
> movwf TRISA ;set PortA all outputs
> bcf STATUS, RP0 ;select bank 0
>
> Loop
> movlw 0xff
banksel PORTA
> movwf PORTA ;set all bits on
> movwf PORTB
Matthew
2008\08\06@185516
by
Daniele Salatti
Hi,
> How is the LED connected and to which pin of the PIC?
I'm working with a breadboard and I have tried different PINs of the
PIC...but the result is always the same. The LED is always on. There's a
150 ohm resistor in series with the LED.
I have also corrected the code as Matthew suggested, but the LED is still
always on...
Dan
2008\08\06@190717
by
Jinx
> the LED is still always on...
Is it a solid on ? Might there be a fast on/off loop ? How does its
brightness compare with a steady 5V supply ?
2008\08\06@211054
by
Mike Hagen
Get it going in MPLAB Sim before even doing the hardware?
>> the LED is still always on...
>>
>
> Is it a solid on ? Might there be a fast on/off loop ? How does its
> brightness compare with a steady 5V supply ?
>
>
2008\08\07@150736
by
Daniele Salatti
Hi again!
> Is it a solid on ? Might there be a fast on/off loop ? How does its
> brightness compare with a steady 5V supply ?
Compared with a steady 5V supply the brightness is lower... This could
be a symptom of a too fast on/off loop?
> Get it going in MPLAB Sim before even doing the hardware?
MPLAB doesn't work on Linux, but I can try to install it with WINE...
Dan
2008\08\07@152111
by
Erik Reynolds
On Thu, Aug 7, 2008 at 3:06 PM, Daniele Salatti <.....saleKILLspam
.....salatti.net> wrote:
> Hi again!
>
>> Is it a solid on ? Might there be a fast on/off loop ? How does its
>> brightness compare with a steady 5V supply ?
>
> Compared with a steady 5V supply the brightness is lower... This could
> be a symptom of a too fast on/off loop?
>
Quite possibly, it would seem you're creating some PWM by accident?
>> Get it going in MPLAB Sim before even doing the hardware?
>
> MPLAB doesn't work on Linux, but I can try to install it with WINE...
>
MPLAB should run fine in Wine. I don't recall it using any crazy
libraries, but I'd be interested to hear about regarding whether or
not it runs...
> Dan
>
> -
2008\08\07@160401
by
Richard Seriani, Sr.
|
----- Original Message -----
From: "Daniele Salatti" <EraseMEsalespam_OUT
TakeThisOuTsalatti.net>
To: <piclist
spam_OUTmit.edu>
Sent: Thursday, August 07, 2008 3:06 PM
Subject: Re: [PIC] Help on PIC16F628A - blinking LED
{Quote hidden}> Hi again!
>
>> Is it a solid on ? Might there be a fast on/off loop ? How does its
>> brightness compare with a steady 5V supply ?
>
> Compared with a steady 5V supply the brightness is lower... This could
> be a symptom of a too fast on/off loop?
>
>> Get it going in MPLAB Sim before even doing the hardware?
>
> MPLAB doesn't work on Linux, but I can try to install it with WINE...
>
> Dan
>
Dan,
If your delay really is 250ms, you are turning the the LED on and off twice
each second. It may appear to be always on.
Try lengthening your delay (maybe change the movlw 0x01 to between 0x02 and
0x04) to see if that gives you an observable pattern. The 0x02 should give
you a .5 second delay; 0x04 about 1 second.
Good luck,
Richard
2008\08\07@191323
by
Jinx
> If your delay really is 250ms
Daniele's code does run in simulation as 250.256ms. That equates to
a 2Hz LED blink, which wouldn't appear as 'always on', so there must
be something else wrong
Hmmm, no sh** Sherlock
<Doffs deer-stalker hat, puffs on Meerschaum, idly pokes Watson with cane>
The very first operation at 'start' is
movlw 0x07
movwf CMCON
According to the d/s, reset state of STATUS is 00011xxx, ie
RP0=RP1=0 which is bank 0, so that appears OK
Maybe add clrf intcon ;no interrupts
<Watson seriously niggled, throws pipe out window, hides morphine>
2008\08\07@195424
by
Mike Hagen
Working without Sim would kill me! It is your first step so you don't
have to spend hours on stupid or hidden mistake.
If you can't get it to work with Wine, get a dedicated XP machine to
develop on. I keep all my files one one machine that does not go on the
internet!
PC are cheap! Old ones laying all over the place here!
Daniele Salatti wrote:
{Quote hidden}> Hi again!
>
>
>
>
>> Get it going in MPLAB Sim before even doing the hardware?
>>
>
> MPLAB doesn't work on Linux, but I can try to install it with WINE...
>
> Dan
>
>
2008\08\09@170722
by
Daniele Salatti
|
Hi!
>>> Is it a solid on ? Might there be a fast on/off loop ? How does its
>>> brightness compare with a steady 5V supply ?
>>
>> Compared with a steady 5V supply the brightness is lower... This could
>> be a symptom of a too fast on/off loop?
>>
>
> Quite possibly, it would seem you're creating some PWM by accident?
Uhm...not sure, but I don't think...
> MPLAB should run fine in Wine. I don't recall it using any crazy
> libraries, but I'd be interested to hear about regarding whether or
> not it runs...
Yes, it works! Now I have to find out how the MPLAB Simulator works...
> The very first operation at 'start' is
>
> movlw 0x07
> movwf CMCON
>
> According to the d/s, reset state of STATUS is 00011xxx, ie
> RP0=RP1=0 which is bank 0, so that appears OK
>
> Maybe add clrf intcon ;no interrupts
Tried...still the same... :(
> Working without Sim would kill me! It is your first step so you don't
> have to spend hours on stupid or hidden mistake.
I'm using GPSim for simulations...
> If you can't get it to work with Wine, get a dedicated XP machine to
> develop on. I keep all my files one one machine that does not go on the
> internet!
> PC are cheap! Old ones laying all over the place here!
PC are cheap, but I don't have room for another PC in my house. If I'd
really need Windows XP (I hope not) I'll use VMWare... ;)
Can someone suggest me a place (link) where to find a working code to
make a LED blink with a PIC16F628A? Maybe it would be usefull to compare
it with my code...
Dan
2008\08\10@054255
by
Justin Richards
Hi,
if the LED is always on, perhaps comment out the lines that turn it on
and see if it is then always off.
At least this way you will be sure the PIC is running and something is
executing. If that works then it must be timing.
If it doesnt then the PIC may not be wired up right, bypass caps ?, mclr?
Oh, and what joy when I got my first LED to blink.
Justin
On Sun, Aug 10, 2008 at 5:06 AM, Daniele Salatti <@spam@saleKILLspam
salatti.net> wrote:
{Quote hidden}> Hi!
>
> >>> Is it a solid on ? Might there be a fast on/off loop ? How does its
> >>> brightness compare with a steady 5V supply ?
> >>
> >> Compared with a steady 5V supply the brightness is lower... This could
> >> be a symptom of a too fast on/off loop?
> >>
> >
> > Quite possibly, it would seem you're creating some PWM by accident?
>
> Uhm...not sure, but I don't think...
>
> > MPLAB should run fine in Wine. I don't recall it using any crazy
> > libraries, but I'd be interested to hear about regarding whether or
> > not it runs...
>
> Yes, it works! Now I have to find out how the MPLAB Simulator works...
>
> > The very first operation at 'start' is
> >
> > movlw 0x07
> > movwf CMCON
> >
> > According to the d/s, reset state of STATUS is 00011xxx, ie
> > RP0=RP1=0 which is bank 0, so that appears OK
> >
> > Maybe add clrf intcon ;no interrupts
>
> Tried...still the same... :(
>
> > Working without Sim would kill me! It is your first step so you don't
> > have to spend hours on stupid or hidden mistake.
>
> I'm using GPSim for simulations...
>
> > If you can't get it to work with Wine, get a dedicated XP machine to
> > develop on. I keep all my files one one machine that does not go on the
> > internet!
> > PC are cheap! Old ones laying all over the place here!
>
> PC are cheap, but I don't have room for another PC in my house. If I'd
> really need Windows XP (I hope not) I'll use VMWare... ;)
>
> Can someone suggest me a place (link) where to find a working code to
> make a LED blink with a PIC16F628A? Maybe it would be usefull to compare
> it with my code...
>
> Dan
> -
2008\08\12@175727
by
brucebeaty
|
----- Original Message -----
From: "Daniele Salatti" <KILLspamsaleKILLspam
salatti.net>
To: <RemoveMEpiclistTakeThisOuT
mit.edu>
Sent: Saturday, August 09, 2008 2:06 PM
Subject: Re: [PIC] Help on PIC16F628A - blinking LED
{Quote hidden}> Hi!
>
> >>> Is it a solid on ? Might there be a fast on/off loop ? How does its
> >>> brightness compare with a steady 5V supply ?
> >>
> >> Compared with a steady 5V supply the brightness is lower... This could
> >> be a symptom of a too fast on/off loop?
> >>
> Can someone suggest me a place (link) where to find a working code to
> make a LED blink with a PIC16F628A? Maybe it would be usefull to compare
> it with my code...
Hi, I can't help you with the code but if you want high (apparent)
brightness and high efficiency from your LED then drive it via a transistor
(bi-polar or MOSFET) at about 5 times the rated current on a 10% duty cycle.
Athough you only need about a 40 Hz repetition rate to avoid visible flicker
you should use a minimum 1 KHz repetition rate to be safe. I would be
inclined to use a 10 to 100 KHz cycle. You can breadboard a test using a
good old 555 to find your operating parameters. Your LED datasheet will
tell you what duty cycles and currents are acceptable, although if you
understand what you're doing you can safety exceed the specs. I've driven
LEDs directly from 5V using a 1 MHz repetition rate and a < 1% duty cycle.
Although the peak power was approaching 10 W the average power disipation
was <100mW.
Best of Luck, Bruce
2008\08\12@184215
by
olin piclist
brucebeaty wrote:
> Hi, I can't help you with the code but if you want high (apparent)
> brightness and high efficiency from your LED then drive it via a
> transistor (bi-polar or MOSFET) at about 5 times the rated current on
> a 10% duty cycle.
This may be useful depending on the power supply you have available to
minimize dissipation driving the LED. However, this will make the apparent
brightness about half that of driving the LED steady at full current. Some
say that with just the right blink sequence the eye will preceive a higher
brightness than the true average, but this is disputed and certainly not a
factor of 2 in any case.
> Athough you only need about a 40 Hz repetition rate
> to avoid visible flicker
You need considerably more than that in my experience, especially if using
low duty cycle. I consider 200Hz the minimum for such things. Even 60Hz
will be very noticable to many people.
********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014. Gold level PIC consultants since 2000.
2008\08\12@202143
by
Apptech
> Your LED datasheet will
> tell you what duty cycles and currents are acceptable,
> although if you
> understand what you're doing you can safety exceed the
> specs. I've driven
> LEDs directly from 5V using a 1 MHz repetition rate and a
> < 1% duty cycle.
> Although the peak power was approaching 10 W the average
> power disipation
> was <100mW.
And if you don't know what you are doing you can SEEM to be
doing OK exceeding the specs, and get early mortality. The
LEDs may die too :-). Some modern LEDs have far lower
Absolute max to average ratings than older ones typically
did. IR LEDs in eg TV remote controls are typically driven
at very high peak to average currents. Do that with a white
"phosphor LED" and you may find the lifetime is vanishingly
short.
R
2008\08\12@202144
by
Apptech
> ... Some
> say that with just the right blink sequence the eye will
> preceive a higher
> brightness than the true average, but this is disputed and
> certainly not a
> factor of 2 in any case.
Yes.
Also, actual LED efficiency generally falls with increasing
current. So any gains due to eye/brain response need to at
least exceed such losses.
R
2008\08\12@215938
by
brucebeaty
----- Original Message -----
From: "Apptech" <spamBeGoneapptechspamBeGone
paradise.net.nz>
To: "Microcontroller discussion list - Public." <TakeThisOuTpiclistEraseME
spam_OUTmit.edu>
Sent: Tuesday, August 12, 2008 5:02 PM
Subject: Re: [PIC] Help on PIC16F628A - blinking LED
{Quote hidden}> And if you don't know what you are doing you can SEEM to be
> doing OK exceeding the specs, and get early mortality. The
> LEDs may die too :-). Some modern LEDs have far lower
> Absolute max to average ratings than older ones typically
> did. IR LEDs in eg TV remote controls are typically driven
> at very high peak to average currents. Do that with a white
> "phosphor LED" and you may find the lifetime is vanishingly
> short.
>
>
> R
>
> --
More... (looser matching)
- Last day of these posts
- In 2008
, 2009 only
- Today
- New search...