PDA

View Full Version : Peek Poke pic16f870



Eric123
- 15th March 2007, 21:36
I am trying to take input off the Port a and port C pins but its not working. Here is my code. May be someone can tell me what I am doing wrong. When I run this code it alawys jumps to A and B weather the pins are high or low?

Thanks for any help?

symbol PortA = 5
symbol PortC = 6
symbol TrisA = $85
symbol TrisB = $86
symbol TrisC = $87
poke trisc,225
poke Trisa,225

symbol cmcon=$1f
poke cmcon,7

symbol adcon0=$9f
poke adcon0,7
symbol adcon1=$9f
poke adcon1,7

start:
peek 5,B0
if bit0=0 then A
A:gosub C
peek 6,B1
if bit8=0 then B
B: gosub D
goto start

C: low 6: high 7: pause 500: low 7: pause 500:
return

D: low 4: high 5: pause 500: low 5: pause 500
return

end

skimask
- 15th March 2007, 23:28
Is this even MeLabs PicBasic?
If it is, you really need to hit the books a bit and read on how the whole Basic thing works. I see a number of problems in your program....

symbol PortA = 5
symbol PortC = 6
symbol TrisA = $85
symbol TrisB = $86
symbol TrisC = $87
poke trisc,225
poke Trisa,225
symbol cmcon=$1f
poke cmcon,7
symbol adcon0=$9f
poke adcon0,7
symbol adcon1=$9f
poke adcon1,7

Why all the symbol references? PicBasic/MPASM already know the addresses and locations of the various registers. You can access them directly without using peeks and pokes.

start:
peek 5,B0
if bit0=0 then A
A:gosub C
peek 6,B1
if bit8=0 then B
B: gosub D
goto start

In the above chunk, why would the 'if bit0=0 then A' SKIP over the next line? It shouldn't and it won't. If it is 0, it'll go to A, if it isn't 0, it go directly to the next line. And in each of the 'if' statements, bit0 refers to what? The compiler (or me) can't tell. In the 2nd 'if' statement....bit8? Are you referring to a byte or a word. 'cause a byte may have 8 bits, but it doesn't have bit #8.

C: low 6: high 7: pause 500: low 7: pause 500:
return

D: low 4: high 5: pause 500: low 5: pause 500
return

These 2 lines also aren't written the best, but that's a personal preference.
What exactly are you trying to do? We'll get you there....but we're not going to write the code for you.

Eric123
- 16th March 2007, 02:43
I have made some changes to my code and am understanding why it was doing what it was doing. I am trying to use the examples from the PICBASIC compiler hand book page 47 to write my code. I am trying to use the first four pins on PortA and PortC to control two motors through an H bridge using port B pins 4,5 for motor 1 and 6,7 for motor 2. The example code says:

Loop: Peek PortA,B0 'get current portA pin states to variable B0
If bit0=1 then zerohigh
If bit1=0 then onelow
goto loop

zerohigh: high 0
gotoloop
onelow: low 1
goto loop
end


So this is why in my code I was using bits. According to the Pg 20 of this manual that came with my basic compilier it says that the first tow bytes , B0 and B1 may also be used as bit variables. Bit0...Bit7 for B0 and bit8...bit15 for B1. So this is why I had the use of bit8 in my orginal code. But may be there is a better way to do this and if so I would like to know. Here is my revised code.

symbol cmcon=$1f
poke cmcon,7
symbol adcon0=$9f
poke adcon0,7
symbol adcon1=$9f
poke adcon1,7

start:
peek 5, B0
if bit0=0 then A
if bit0=1 then D

peek 7, B1
if bit8=0 then B
if bit8=1 then C
goto start


A: low 6: high 7: Return
B: low 4: high 5: Return
C: low 7: Return
D: low 5 Return

Please tell me how I can fix my code It still seems like peek and poke arent working right for me.

Thanks for you Help.

paul borgmeier
- 16th March 2007, 06:57
Eric,

This will get you closer - I maintained your logic exactly, which, looks to me, to be a bit troubling as SKIMASK noted. You also will want to get a copy of the 16F870 datasheet to go with the manual. Email back if you want more.

;symbol cmcon=$1f ; 16F870 has no comparators and $1f is ADCON0 not CMCON
;poke cmcon,7 ; this line and above not needed for the 18F870
;symbol adcon0=$9f ; $9F is ADCON1 not ADCON0 – these two lines repeat the next two
;poke adcon0,7
symbol adcon1=$9f
poke adcon1,7 ; Make all of PORTA Digital rather than analog

Poke, $85, %00000011 ; make pins 0 and 1 on PORTA inputs, the rest outputs
Poke, $87, %00000011 ; make pins 0 and 1 on PORTC inputs, the rest outputs
Poke $86, %00000000 ; make all of PORTB outputs
Poke $05, 0 ; make all outputs of PORTA low
Poke $06, 0 ; make all outputs of PORTB low
Poke $07, 0 ; make all outputs of PORTC low

start:
peek 5, B0 ; read PORTA into predefined variable B0
if bit0=0 then A ; if B0.0 = 0 then GOTO A (no gosub in if statement)
ReturnFromA:
if bit0=1 then D ; if B0.1 = 1 then GOTO D (no gosub in if statement)
ReturnFromD:
peek 7, B1 ; Read PORTC into B1
if bit8=0 then B ; if B1.0 = 0 then GOTO B
ReturnFromB:
if bit8=1 then C ; if B1.0 = 1 then GOTO C
goto start

A: low 6: high 7 ; make PORTB pin 6 low and pin 7 high
Goto ReturnFromA

B: low 4: high 5 ; make PORTB pin 4 low and pin 5 high
ReturnFromB
C: low 7 ; make PORTB pin 7 low
Goto Start
D: low 5 ; make PORTB pin 5 high
Goto ReturnFromD
END

oh, I have not tried this - I do not use PBC anymore ... and you might want to start your project by trying to blink and LED or two rather than trying to code a complete program.

skimask
- 16th March 2007, 07:07
..........