PDA

View Full Version : Another simple program misbehaving



Luckyborg
- 15th June 2011, 22:13
I'm having problems with the below code. It is written for a 16F1829. It is a stripped down version of what I want. I am guesssing the problem is with not disabling something analog or the pull ups not working right. I have tried adding 4.7k external pull up resistors on both B4 and B5 to 5 volts as well. This is the first time I have tried using this part.


asm
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_OFF & _MCLRE_OFF & _IESO_OFF
__CONFIG _CONFIG2, _PLLEN_ON & _LVP_OFF & _STVREN_OFF
endasm

Clear
define OSC 32

OSCCON = %11110000 '32 MHz internal

ANSELA = 0 'set porta to digital
ANSELB = 0
ANSELC = 0

OPTION_REG.7 = 0 'enable weak pullups
WPUB = %00110000
INLVLB = 0

CM1CON0.7 = 0 'disable comparator
CM2CON0.7 = 0

TRISA = %011000 '0-2 tx pins
TRISB = %11001111 'red and green
TRISC = %11111111 '

Red var PORTB.5
Green var PORTB.4

PORTA = %000111
PORTB = 0
PORTC = 0

Pause 1000

mainloop:

green = 1
red = 0
pause 1000
green = 0
red = 1
pause 1000
goto mainloop

endThe basic program toggles a red and green line, one high and one low. If I comment out both green or both red lines the program behaves exactly as desired, with the un-commented line going on and off. When both red and green are in the code then the green light remains on and the red light will toggle. I have tried to look for every register I can find in the datasheet that may have anything to to with this, but no luck. I have also pulled the legs up from the circuit and checked with a volt meter just to make sure there aren't any physical problems with the board. I have also use multiple chips.

thanks in advance for the help.

mister_e
- 15th June 2011, 22:22
What happen if you change your High & red definition with


Red var LATB.5
Green var LATB.4

Luckyborg
- 15th June 2011, 22:50
What happen if you change your High & red definition with


Red var LATB.5
Green var LATB.4

...magically all my problems seem to go away now. I've never needed to use LATX before. Maybe you can educate me a little on why I needed to this time. The datasheet seems to imply that writing to the latch is just a more direct way of writing to the port, but maybe I'm not catching the nuance.

Thanks
David

mister_e
- 15th June 2011, 23:15
You should also do it with PIC18 and with all PIC having LATx register. This is to avoid the R-M-W issue. The datasheet explain it, like you said. For further details you may want to have a look at this document

http://www.cornerstonerobotics.org/curriculum/lessons_year2/erii_rmw_problem.pdf