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.

Program yeilds unexpected results

Status
Not open for further replies.

parts-man73

New Member
This little bit of code I wrote as a practice while teaching myself assembly. I programmed the PIC, with options for watchdog off, and using the internal oscillator, and put in a solderless breadboard. I connected 8 LEDS with current limiting resistors on RB0-RB7. VSS is connected to ground, VDD connected to 5V, and pin 4 pulled to 5V with a 4.7k resistor.

I expected to see a "bit" to scroll across the LED's, then start again from the other side. and continue until the cows come home.

What I get is a few of the LEDS in the first nibble light up in order, then nothing out of the second nibble, and nothing repeats.

Is this a code error that I'm overlooking? Sorry about the formatting in the code, I guess a subquestion is, how are people attaching code so it looks nice?

Thank you,
Brian

INCLUDE "P16F628A.inc"

COUNT1 equ 20h
COUNT2 equ 21h


bsf STATUS,RP0 ;bank 1
clrf TRISB
bcf STATUS,RP0 ;bank 0

Start CLRF PORTB
movlw 01h ; load 00000001 in W
movwf PORTB ; then move w to portb
Loop call Delay ; delay routine
rrf PORTB,1 ; rotate bits right
goto Loop

Delay

Loop1 decfsz COUNT1,1
goto Loop1
decfsz COUNT2,1
goto Loop1
return

end
 
I would suggest a shadow register for port B

Also you seem to have forgotten the carry bit on rrf, which is rotate right through carry. (Look it up in the data sheet)

Your line

rrf PortB, 1

needs to read Port B to work

I would suggest:-

;Initialise

PortBshadow equ 0x22

movlw 0x01
movwf PortBshadow

;This bit goes in the main loop instead of rrf Portb, 1

rrf PortBshadow, w ;gets bit 0 into carry bit
rrf PortBshadow, f ;actual rotate and puts carry bit (was bit0) into bit7

movf PortBshadow, w
movwf PortB ;put result in PortB, ignoring what was there before
 
I changed the code slightly to toggle all pins of PORTB high, then low, repeat forever...

All the leds except the one connected to RB4 are flashing. upon studying the datasheet some more, I found the following statement.

"LVP configuration bit sets RB4 functionality"

could this bit be the "brick wall" that stops the scrolling LEDS from entering the other nibble? I assume that LVP is Low Voltage Programming, I saw an option to enable or disable Low voltage programming in the programming software that I use (using winpic software, I'm using a PIC-PG2 from Olimex to program the PIC)

another odd thing that's going on, if I touch any of the bare resistor wires or LED leads with my finger, it stops the LEDs from flashing, and they are stuck on at half brightness until I reset the power.

Brian
 
parts-man73 said:
I changed the code slightly to toggle all pins of PORTB high, then low, repeat forever...

All the leds except the one connected to RB4 are flashing. upon studying the datasheet some more, I found the following statement.

"LVP configuration bit sets RB4 functionality"

could this bit be the "brick wall" that stops the scrolling LEDS from entering the other nibble? I assume that LVP is Low Voltage Programming, I saw an option to enable or disable Low voltage programming in the programming software that I use (using winpic software, I'm using a PIC-PG2 from Olimex to program the PIC)
Yes, disable the LVP fuse. Otherwise it will override the TRISB settings and RB4 will be an input; the rrf instruction will always read RB4 as '0' and only the first 4 LEDs will turn on once.


parts-man73 said:
Sorry about the formatting in the code, I guess a subquestion is, how are people attaching code so it looks nice?
Surrond the code with the code tags ([ C o d e ] and [ / C o d e ] without spaces). You can also upload the asm file as an attachment.
 
Last edited:
Ok, I disabled LVP, reprogrammed with the same code (the one that toggles the entire portB high and low) and all 8 LEDs are now happily flashing.

The issue of touching a bare wire has disappeared.

Now I'll follow your example Diver300 and see about making the LED shift.

From the diagram in the Datasheet, I assumed the carried bit automatically went into the other side. I will study your example and try again.

Thanks for the helps,
Brian
 
Thank you! The shadow register and carry fixes did the trick. Not only does the code now work..... I understand it also!

Also I learned about RB4, and disabling LVP. Many lessons learned from such a simple exercise.

Brian
 
I've been looking through those tutorials since you pointed them out, alot of really good information, ty!

The rrf method for the carry is slightly different then the one described, both get the job done in this example, different methods of handling the carry bit. Your example uses few instructions, so the execution time would be faster. Diver300's example would handle multiple bits (like scrolling a group of three bits) and seeing the 2 was of doing it has taught me something also.

I think my biggest problem was not disabling LVP.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top