PDA

View Full Version : managing the I/O ports of the 16F84A



skyler_91600
- 24th April 2005, 19:57
I'm trying to write a program to have port A and Port B light up seven segment displays at the same time. The question I had was how does the compiler distinguish between a port A I/O or a port B I/O? I'm including some of my code to demonstrate what I'm talking about.

loop1: if bit0 = 1 then loopx 'if true program ends
if 7 = 1 then loop1 'if beam isn't broken endless loop

what's happening here is I have a seperate counter set-up and when the counter equals 0, it will send out a logic 1 to PortA bit 0 in the shown code. this in turn will end this program. then the second line is if bit 7 of portB is a logic 1 then this will create an endless loop until the pin turns low. At the of a low state the program will continue execution.

Now onto the question: How does the compiler distinguish between a portA command (line 1) and a portB command (line2)? when compiling I receive a "variable expected (token '7') error. and this is probably due to my lack in knowledge in managing the I/O ports of the 16F84A. Any help would be greatly appreciated.

mister_e
- 24th April 2005, 20:10
PIC16F84A.. great PIC choice :)

if you want to acces to a specific PORT pin you must use the according PIC PIN name.

something like

read a pin
IF PORTA.0=1 then DoSomething

write to a pin
HIGH PORTA.0

or

PORTA.0=1


Write to a whole PORT

PORTB=%11100110


don't forget to set the according TRIS register.

skyler_91600
- 24th April 2005, 21:52
thanks for the reply. I tried doing what you said but I get an illegal character error. It doesn't like the period in between the A and the 0 (PortA.0).

mister_e
- 25th April 2005, 18:00
DOH, just notice we are in the PBC section. You have to use POKE and PEEK with the according RAM address.

something like

POKE $86,0 ' set PORTB as output
POKE $06,255 ' set all PORTB pins to 1

try this and post your results...

skyler_91600
- 25th April 2005, 23:40
Here's what I'm trying to accomplish: I have a loop set-up where the only time the program can come out of the loop is when Port A bit 0 OR RA0 goes low. It then makes other pins go high and/or low to make a certain number out of a seven segment dieplay. What I thought is that I could just make the PIC do this on the fly where when RA0 goes low the program will instantly come out of the loop and continue on with the code. What you're describing is that when the pin goes low you would have to store that informaiton into ram and then retrive the data so that the PIC can compare the two results, as in the IF...Than statement. I"m assuming that you're correct since you're all over this forum! I'm just trying to learn how to do certain things rather than being told how to do it, and not learning anything! I really appreicate you helping me out.

mister_e
- 26th April 2005, 06:02
IN PBP we will write something like this...


TRISB=0 ' set PORTB as output
TRISA=%11111101 ' set PORTA as input except PORTA.1

a VAR BYTE
a=1


Loop:
Toggle PORTA.1
IF PORTA.0=0 then Gosub DoSomething ' testing PORTA.0 status
goto Loop

DoSomething:
PORTB=a ' send to PORTB
if a<255 then
a=a+1
else
a=1
endif
return


IN PBC, IMO it will look something like.. please correct me someone if i'm wrong




POKE $86,0 ' set PORTB as output
POKE $85,253 ' set PORTA.1 as output other as input
b0=0

loop:
toggle 0
PEEK $05,b1 ' read PORTA status
b1=b1 & 1 ' keep only bit 0 of PORTA
' well i don't know if you can use something like
' if b1.0=0 then gosub DoSomething... worth to give a try
if b1=0 then gosub DoSomething
goto loop

DoSomething:
POKE $06,b0 ' send to PORTB
if b0<255 then
b0=b0+1
else
b0=1
endif
return


this will toggle the PORTA.1 pin untill PORTA.0 go low, once it go to low, it will send the value of b0 var to PORTB.... well it's suppose to ;)

skyler_91600
- 28th April 2005, 02:16
I guess I just don't understand the principals of the IF..Then statement. IN the manual there are some examples for the statement, but when I tried to mimic the example it didn't work.

The example is:

If Pin0 = 0 Then pushd

now does Pin0 need to be equated to something? IN the beginning of the code do I need something like this?

Pin0 VAR PortA.0?
or
Pin0 = PortA.0?

when I type in PortA.0, the compiler gives me an illegal character error because of the period but I do not know how to otherwise specify any pin of either portA or PortB (RA0,RA1.....RB0, RB1.....)

I really appreciate all your help, the problem is that I don't have much programming knowledge, and when I try to understand your replies it goes over my head.

mister_e
- 28th April 2005, 03:52
well unfortunately you don't have PicBasic PRO where everything is so much simple.

your example IF PORTA.0= will work in PBP.

Can you try this and post your results



' alias definition
' ================
'
Symbol PORTA = 5
Symbol TRISA = $85
Symbol PORTB = 6
Symbol TRISB = $86

' I/O setting
' ==========
'
POKE TRISA=255 ' set input to all PORTA pins
POKE TRISB=0 ' set output to all PORTB pins

' Main Loop
' =========
'
START:
PEEK PORTA,B0 ' read the whole PORTA and place result into B0
IF B0.0=1 THEN ' read bit 0 of B0 wich is the PORTA status
'
' If PORTA.0 is 1 then place all PORTB pins to 1
'
POKE PORTB,255
'
ELSE
'
' If PORTA.0 is 0 then place all PORTB pins to 0
'
POKE PORTB,0
'
ENDIF
GOTO START