Beginner PIC program, confusing behavior?

Started by JKowalski, November 11, 2009, 10:33:16 PM

Previous topic - Next topic

JKowalski

Today I went on a PIC learning binge, and I think I've gotten pretty far. Mainly I used this tutorial:

http://www.mstracey.btinternet.co.uk/pictutorial/picmain.htm

I'm on page 7, still going through it. After figuring out how to adapt the 16f84 commands to one of the 16f684 chips I have laying around, I managed to get the tutorial's flashing light dealio working. I went back and refreshed my knowledge of the tutorial by designing a program that ran a 7-segment LED display either from 0 to 9, or flashing the letter "F", depending on the position of a switch. It worked great! However, I some weird issues.

The first issue is that the program, in F flashing mode, stays on slightly longer on every fifth flash..??? I wrote the flashing as a loop that goes through ON then OFF, so I don't see why the fifth time looping would be any different than any of the other loops???

The second issue is that when you switch BACK from the flashing F to the 0-9 sequence, occasionally the ensuing 0-9 sequence does not make it to 9 and loops beforehand. For example, you switch back to the 0-9, it resets to 0, counts to 5, goes to 0 again, then starts looping the 0-9 sequence normally.


Here's the code:
;7segment LED display flasher thingy

INCLUDE "p16f684.inc"

STATUS          equ       03h
TRISA           equ       85h
TRISC           equ       87h
PORTC           equ       07h 
PORTA           equ       05h
COUNT1          equ       20h
COUNT2          equ       21h                             

bsf                    STATUS,5       ;Switch to Bank 1
movlw              00h                 
movwf              TRISC             ;Set the Port C pins to output.
clrf ANSEL ;clear analog selects
movlw b'000010'
movwf TRISA ;bit 1 to input
bcf STATUS,5 ;Bank 0
clrf PORTA ;Init PORTA
movlw b'111111' ;Set RA<2:0> to
movwf CMCON0 ;digital I/O

movlw  b'11111111'                  ; put the value of 255 in the count registers
movwf  20h
movlw  b'11111111'
movwf  21h

Start     

BTFSS            PORTA,1             ;Get the value from RA1
goto Flash                           ;goto flashing sequence


;0-9 number sequence
                                                 
     
movlw              b'110111'   ;7 segment display number "0"               
movwf              PORTC         
movlw              b'000100'               
movwf              PORTA
call                   Delay

movlw              b'100001'      ;7 segment display number "1"             
movwf              PORTC           
movlw              b'000000'               
movwf              PORTA
call                   Delay

movlw              b'111110'     ;7 segment display number "2"           
movwf              PORTC           
movlw              b'000000'             
movwf              PORTA
call                   Delay


movlw              b'111011'       ;7 segment display number "3"       
movwf              PORTC         
movlw             b'000000'             
movwf              PORTA
call                   Delay

movlw              b'101001'     ;7 segment display number "4"             
movwf              PORTC           
movlw              b'000100'                 
movwf              PORTA
call                   Delay

movlw              b'011011'      ;7 segment display number "5"           
movwf              PORTC           
movlw              b'000100'             
movwf              PORTA
call                   Delay

movlw              b'011111'      ;7 segment display number "6"           
movwf              PORTC         
movlw              b'000100'                 
movwf              PORTA
call                   Delay

movlw              b'110001'      ;7 segment display number "7"       
movwf              PORTC           
movlw              b'000000'         
movwf              PORTA
call                   Delay

movlw              b'111111'      ;7 segment display number "8"           
movwf              PORTC           
movlw              b'000100'                 
movwf              PORTA
call                   Delay

movlw              b'111011'       ;7 segment display number "9"           
movwf              PORTC           
movlw              b'000100'             
movwf              PORTA
call                   Delay 
call                   Delay                   

goto                 Start     ;start over           


Flash           

movlw              b'011100'               
movwf              PORTC           
movlw              b'000100'               
movwf              PORTA
call                   Delay

movlw              b'000000'               
movwf              PORTC           
movlw              b'000000'               
movwf              PORTA
call                   Delay

goto Start                 ;start over




Delay

Loop1          decfsz              COUNT1,1     ;delay 255 clock 255 times
                     goto                  Loop1           
                     decfsz              COUNT2,1   
                     goto                  Loop1             
return


end                                 


pjwhite

I'm not familiar with that PIC, but it sounds to me like you could have issues with a watchdog timer causing the chip to reset itself periodically.  If that's the case, you would need to include an instruction to 'kick" the watchdog timer periodically to prevent the reset.  This instruction should go in your main loop somewhere.  Or, you could figure out how to just disable the watchdog timer and do that in your initialization code.

MoltenVoltage

It doesn't look like the problem is in the main loop

There are lots of different things it could be other than a main loop code error

Read the datasheet backward and forward then read it again
Go through each setup item and make sure its the way you need it.  You might not know how you need it at first then need to change one thing at a time to see if it works.

I haven't used that exact PIC either, but this issue comes up EVERY time I use a new chip.  You just need to slog through, there is no shortcut.

Did I mention you need to read the datasheet?

I could also be a hardware issue - not a big enough regulator - no decoupling cap - decoupling cap too far away - etc - etc
MoltenVoltage.com for PedalSync audio control chips - make programmable and MIDI-controlled analog pedals!

JKowalski

#3
Thanks for the replies, I've been slowly pouring over the datasheet along with the tut but it's slow going. I disabled the watchdog timer and made some adjustments but it's still having the same issues.

I realized while trying to figure out the problem that the F flashing stays on slightly longer on the fifth flash but the 0-9 sequence (I forgot to mention this) has a slight delay/weird skip/sputter thing at then end of every 0-9 count. In the program I posted, I added an extra delay at the end to make it so the 0-9 weird issue wouldn't be obvious (which made it instead of doing a weird skipping thing, kind of sputtering, it just delayed longer before looping)

But the strange thing is that the timing of the glitch is not the same for the flasher and the 0-9 loop. The flasher does it every five seconds, the 0-9 loop does it every loop restart, and their total times between glitches are not equal.

Also, the F glitch never makes the flashing OFF time delay extra, only the ON...

What I am trying to say is that it's very consistent with the main program loop, not at unique regular intervals independent of the program instructions, as evidenced by the differing lengths for each sequence (0-9 or F) and the end-of-loop timings.

I'm perplexed.

WLS

It looks like the glitch is in Loop1. The way the timers are setup. both refer to restarting the loop. I am not into ASM programming, I am more a C man, but it appears that timer one when activated will not allow the code to pass to the second timer except on the first pass.

Anyways, if you want a quick answer visit this site http://www.avrfreaks.net and post your code there. They are more software oriented and would not be guessing like myself. :)


Bill


Since I've breadboarded it I can only blame myself.

But It's Just A Chip!

potul

Hi

I don't see anything wrong in the code, although I 'm not familiar with this particular PIC.

The Loop1 seems to be ok, simply counting to 255x255, and never exits until finished.... Just be sure there's no interrupt messing with your timings, because the counters are never set (the code expects them to be 0 at the beginning)

Potul

MoltenVoltage

Quote from: JKowalski on November 13, 2009, 02:03:58 AM
Thanks for the replies, I've been slowly pouring over the datasheet along with the tut but it's slow going. I disabled the watchdog timer and made some adjustments but it's still having the same issues.

I realized while trying to figure out the problem that the F flashing stays on slightly longer on the fifth flash but the 0-9 sequence (I forgot to mention this) has a slight delay/weird skip/sputter thing at then end of every 0-9 count. In the program I posted, I added an extra delay at the end to make it so the 0-9 weird issue wouldn't be obvious (which made it instead of doing a weird skipping thing, kind of sputtering, it just delayed longer before looping)

But the strange thing is that the timing of the glitch is not the same for the flasher and the 0-9 loop. The flasher does it every five seconds, the 0-9 loop does it every loop restart, and their total times between glitches are not equal.

Also, the F glitch never makes the flashing OFF time delay extra, only the ON...

What I am trying to say is that it's very consistent with the main program loop, not at unique regular intervals independent of the program instructions, as evidenced by the differing lengths for each sequence (0-9 or F) and the end-of-loop timings.

I'm perplexed.

Putting in extra code to account for glitches is NEVER the right answer.  The problem is in your lack of setup - I just can't tell you exactly where.  You need to go through the datasheet three more times, then catch a fly with chopsticks - wax on wax off danielson.

I recently caught a fly with needlenose pliers...
MoltenVoltage.com for PedalSync audio control chips - make programmable and MIDI-controlled analog pedals!

JKowalski

#7
Sorry, I guess I should have posted earlier, but I've already figured it out. Yes, it was the CONFIG setup, I was going about that completely wrong (trying to move a binary into the config hex register...) and I read up on it and it's all good now.

Added in:
__CONFIG _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO


That was the source of all my problems, the code works perfectly now. A also added in a switch for my last extra I/O pin that adjusts the LED response from linear to logarithmic.... It's a nice replacement for those LM3914/LM3815 analog bar graph IC's which are obsolete and expensive nowadays.

Fried one of my chips when I stuck my 9V power supply wire into the breadboard +V strip rather than the 5V regulator I had on it... Dang.  :icon_rolleyes: Oh well, I've got ~10 of em!

Thanks for the help guys.

MoltenVoltage

MoltenVoltage.com for PedalSync audio control chips - make programmable and MIDI-controlled analog pedals!