PDA

View Full Version : New At PIC's, need some help with code



SOHCKing03
- 22nd September 2006, 19:21
Hi everyone,

I am currently in the works of making a car product where I need to make LED's sequence down a line. i.e 0=off 1=on so...

0000
1000
1100
1110
1111
0111
0011
0001
0000

I think I have come up with a good code but I don't know how to increment the PIN number in my code during a DO LOOP. Each LED is driven by one output and so I need to increment the outputs to sequence them. Will I have turn on the LED from one output in one LOOP then do another in another LOOP and so on?

If I'm not clear then I'll try to explain it better.

-Brad

sayzer
- 23rd September 2006, 07:50
Hi Joe,

Take a look at this.

http://www.picbasic.co.uk/forum/showthread.php?t=3753



Regards.

paul borgmeier
- 23rd September 2006, 12:39
BRAD,

What PIC? You posted in the PBC section and not PBP - is this correct? If you are using PBC, try



'Example for 16F628A with PBC (Not PBP)

POKE $86, 0 ' all of PORTB output (optional line but good programming practice)
POKE $06, 0 ' all of PORTB low (optional line but good programming practice)

MAIN:
PAUSE 500 ' no LED on

FOR B0 = 0 to 3
HIGH B0 ' turn ON RB0, then RB1, then RB2, then RB3
PAUSE 500 ' pause half a second
NEXT B0

FOR B0 = 0 to 3
LOW B0 ' turn OFF RB0, then RB1, then RB2, then RB3
PAUSE 500 ' pause half a second
NEXT B0

PAUSE 500 ' no LED on

GOTO MAIN

END


You might also want to check out Melanie's excellent post here

http://tinyurl.com/gpt6b

and search the forum for "rider" (as in knight rider) for more visual ideas.

Any questions?

SOHCKing03
- 25th September 2006, 19:14
Thanks for the replies...

I think I will test out a code I created using some of the elements from the code you guys gave me...

Hopefully it'll work and I can get this thing going...

-Brad

SOHCKing03
- 26th September 2006, 19:33
Okay well I will use the code from the link Sayzer gave me...

I am making an LED Underbody Kit though and so one PIC will control the front left, one will control the left side, one will control the the back left, and the same for the right side. So I will need my PIC's to communicate. When the last LED on the front left is turned on I want the side to start sequencing and same goes for the transition from side to back.

To do this will I have to set the final output on the front left to go high and then have it connected to an input on the side PIC? That way I could tell the side PIC not to do anything until that input receives a "high" signal from the other PIC. I could do that correct? If so, how would I program it?

Thanks again for all your help guys.

-Brad

sayzer
- 26th September 2006, 20:04
If I understood correct, this should be it. Just an example though.





Wait:

IF PORTA.1 = 1 THEN GOTO Loop
'This is the pin you get the input signal from the other PIC.

GOTO Wait


Loop:

'Your loop code should be here....
'When the last loop index is achieved,
'you will HIGH a pin to activate the next PIC
'and you will go back to "Wait"


GOTO loop

SOHCKing03
- 27th September 2006, 18:50
Here is the code I came up with...

------------------------------------------------------

SYMBOL PORT_PIN = PORTA
X VAR BYTE ' For loop & bit index pointer

Wait:

IF PORTA.1 = 1 THEN GOTO Main 'checks if input recieves high signal
GOTO Wait

On:

ADCON1 = 7 ' All digital
PORTA = 0 ' Clear all port pins
PORTB = 0
PORTC = 0

TRISA = 0 ' Make them all outputs
TRISB = 0
TRISC = 0

FOR X = 0 TO 23
PORT_PIN.0[X] = 1 ' Set all porta, portb, and portc pins high
PAUSE 200
NEXT X

GOTO Off

Off:

ADCON1 = 7 ' All digital
PORTA = 0 ' Clear all port pins
PORTB = 0
PORTC = 0

TRISA = 0 ' Make them all outputs
TRISB = 0
TRISC = 0

FOR X = 0 TO 23
PORT_PIN.0[X] = 0 ' Set all porta, portb, and portc pins low
PAUSE 200
NEXT X

GOTO On

---------------------------------------------------

My only question now is, how do I set PORTA.1 to be an input? In my code I set all PORTA pins to be outputs. Is there a way I can set just PORTA.1 to be input?

As always, I appreciate your help.

-Brad

sayzer
- 27th September 2006, 19:28
1. You first need to set the TRIS register for a port. Having TRISA = %00000000 makes all pins output. From right to left they are arranged as porta.0 to porta.7. (F877 has 6 pins on PORTA). If you want to make one of them an input, say you need PORTA.1 as input, then you state TRISA = %00000010. Now, all of them are outputs except for PORTA.1 (also for some PICs, you need to watch for MCLR pin which is an input-only pin).
OR, you can just say INPUT PORTA.1

2. You should keep all registers at the beginning. No need to put them into each subroutine.

3. ON is a reserved word. Thus, you can not use it for another purpose.

4. In your code you have "goto Main" but there is no Main. I think you meant "goto ON", but since you can not use "ON", just change ON to Main.

5. For the FOR loop you have, you will not have 8-bits on PORTA. Thus you need to have a look at there. Check Bruce's example on the link above.


Check the datasheet of the PIC you will use. Check which pin can be used as an input-only and/or output-only etc.

Also, check how many pins you have on each port.


----------

------------

SOHCKing03
- 28th September 2006, 02:45
Okay so then here is an updated code:

'----------Code for the Front Left, Front Right, Back Left, and Back Right

SYMBOL PORT_PIN = PORTB
X VAR BYTE ' For loop & bit index pointer


ADCON1 = 7 ' All digital

PORTB = 0 ' Clear port pins

TRISB = 0 ' Make them all outputs

Main:

FOR X = 0 TO 7
PORT_PIN.0[X] = 1 ' Set all pins high
PAUSE 200
NEXT X

GOTO Off

Off:

ADCON1 = 7 ' All digital

PORTB = 0 ' Clear port pins
PORTC = 0

TRISB = 0 ' Make them all outputs
TRISC = 0

FOR X = 0 TO 7
PORT_PIN.0[X] = 0 ' Set all pins low
PAUSE 200
NEXT X

GOTO Main

'----------Code for Both Sides

SYMBOL PORT_PIN = PORTB
X VAR BYTE ' For loop & bit index pointer


ADCON1 = 7 ' All digital
PORTA = 0
PORTB = 0 ' Clear port pins
PORTC = 0

TRISB = 0 ' Make them all outputs
TRISC = 0

Wait:

INPUT PORTA.0
IF PORTA.0 = GOTO Main

GOTO Wait

Main:

FOR X = 0 TO 15
PORT_PIN.0[X] = 1 ' Set all pins high
PAUSE 200
NEXT X

GOTO Off

Off:

ADCON1 = 7 ' All digital

PORTB = 0 ' Clear port pins
PORTC = 0

TRISB = 0 ' Make them all outputs
TRISC = 0

FOR X = 0 TO 15
PORT_PIN.0[X] = 0 ' Set all pins low
PAUSE 200
NEXT X

GOTO Main

-------------------------------------------------

How does that look? For the front and back ones I only need 6 outputs but I need 16 for the sides. I hope I did that INPUT thing right. I assumed that PORT_PIN would still equal PORTB since I'm only using PORTA for the INPUT. Did I do everything correct? Also, I am using a 16F876 so all PORTB and PORTC pins can be outputs, however PORTA.6 and PORTA.7 are unimplemented so that is why I am starting with PORTB.

Thanks again.

-Brad

mister_e
- 28th September 2006, 02:55
IF PORTA.0 = GOTO Main
there's a missing value in that one, 0 or 1

Also, as you're using PORTB and PORTC, there's no special need to use the array method. Just use
HIGH x (from 0 to 15)=> refer to section 4.11 of your PBP manual

something like


X VAR BYTE ' For loop & bit index pointer
ADCON1 = 7 ' All digital
'
' Clear portB&C pins
PORTB = 0
PORTC = 0
'
' Make them all outputs
TRISB = 0
TRISC = 0
'
' Set PORTA.1 as input
TRISA.1=1

Main:
FOR X = 0 TO 7
HIGH X ' Set all pins high
PAUSE 200
NEXT X
Off:
FOR X = 0 TO 7
LOW X ' Set all pins low
PAUSE 200
NEXT X

GOTO Main

SOHCKing03
- 28th September 2006, 14:06
Alright here is an updated code as of 10:12 last night




'---------- FRONT TWO PICS' CODE
X VAR BYTE
ADCON1 = 7 'sets all as digital
PORTB = 0 'clears port pins
TRISB = 0 'sets as outputs

Main: 'turns each LED on
FOR X = 0 TO 7
HIGH X
PAUSE 200
NEXT X

PAUSE 7000 'waits for sequence to finish

GOTO Off

Off: 'turns each LED off
FOR X = 0 TO 7
LOW X
PAUSE 200
NEXT X

PAUSE 6000

GOTO Main






'---------- SIDE TWO PICS' CODE
X VAR BYTE
ADCON1 = 7 'sets all as digital
PORTB = 0 'clears all port pins
PORTC = 0
TRISB = 0 'sets all as outputs
TRISC = 0
TRISA.0 = 1 'set as input

Wait: 'wait for high signal
INPUT PORTA.0
IF PORTA.0 = 1 THEN GOTO Main
GOTO Wait

Main: 'turns each LED on
FOR X = 0 TO 15
HIGH X
PAUSE 200
NEXT X

GOTO Wait_Again

Wait_Again: 'waits for low signal
INPUT PORTA.0
IF PORTA.0 = 0 THEN GOTO Off

GOTO Wait_Again

Off: 'turns each LED off
FOR X = 0 to 15
LOW X
PAUSE 200
NEXT X

GOTO Wait 'restarts






'---------- BACK TWO PICS' CODE
X VAR BYTE
ADCON1 = 7 'sets all as digital
PORTB = 0 'clears all port pins
TRISB = 0 'sets all as outputs
TRISA.0 = 1 'set as input

Wait: 'wait for high signal
INPUT PORTA.0
IF PORTA.0 = 1 THEN GOTO Main
GOTO Wait

Main: 'turns each LED on
FOR X = 0 TO 7
HIGH X
PAUSE 200
NEXT X

GOTO Wait_Again

Wait_Again: 'waits for low signal
INPUT PORTA.0
IF PORTA.0 = 0 THEN GOTO Off

GOTO Wait_Again

Off: 'turns each LED off
FOR X = 0 to 7
LOW X
PAUSE 200
NEXT X

GOTO Wait 'restarts




How does that look? I went and double checked everything so I think it is near completion. Are there any mistakes or better ways to code it?

mister_e
- 29th September 2006, 06:21
First code:
You don't need the GOTO Off... it will jump there anyway

Second code:
Wait and Off are reserved word, so you need to change them


GOTO Wait_Again '******* not needed

Wait_Again: 'waits for low signal



Wait: 'wait for high signal
INPUT PORTA.0 '***** this one you don't need it as it's
' already defined above



Wait_Again: 'waits for low signal
INPUT PORTA.0 '******* same thing here

another method


'---------- SIDE TWO PICS' CODE
X VAR BYTE
ADCON1 = 7 'sets all as digital
PORTB = 0 'clears all port pins
PORTC = 0
TRISB = 0 'sets all as outputs
TRISC = 0
TRISA.0 = 1 'set as input

Start:
While PORTA.0=0 : Wend ' wait 'till PORTA.0=1

FOR X = 0 TO 15
HIGH X
PAUSE 200
NEXT X

While PORTA.0=1 : Wend 'waits for low signal

FOR X = 0 to 15
LOW X
PAUSE 200
NEXT X

goto start

sayzer
- 29th September 2006, 08:21
....
Wait and Off are reserved word, so you need to change them



Who/what reserves the "wait" Steve?

I did not know that!

SOHCKing03
- 29th September 2006, 13:55
Okay I updated it again...



'---------- FRONT TWO PICS' CODE
X VAR BYTE
ADCON1 = 7 'sets all as digital
PORTB = 0 'clears port pins
TRISB = 0 'sets as outputs

Turn_On: 'turns each LED on
FOR X = 0 TO 7
HIGH X
PAUSE 200
NEXT X

PAUSE 7000 'waits for sequence to finish


Turn_Off: 'turns each LED off
FOR X = 0 TO 7
LOW X
PAUSE 200
NEXT X

PAUSE 6000

GOTO Turn_On




'---------- SIDE TWO PICS' CODE
X VAR BYTE
ADCON1 = 7 'sets all as digital
PORTB = 0 'clears all port pins
PORTC = 0
TRISB = 0 'sets all as outputs
TRISC = 0
TRISA.0 = 1 'set as input

Waiting: 'wait for high signal

IF PORTA.0 = 1 THEN GOTO Turn_On

GOTO Waiting

Turn_On: 'turns each LED on
FOR X = 0 TO 15
HIGH X
PAUSE 200
NEXT X


Wait_Again: 'waits for low signal

IF PORTA.0 = 0 THEN GOTO Turn_Off

GOTO Wait_Again

Turn_Off: 'turns each LED off
FOR X = 0 to 15
LOW X
PAUSE 200
NEXT X

GOTO Waiting 'restarts




'---------- BACK TWO PICS' CODE
X VAR BYTE
ADCON1 = 7 'sets all as digital
PORTB = 0 'clears all port pins
TRISB = 0 'sets all as outputs
TRISA.0 = 1 'set as input

Waiting: 'wait for high signal

IF PORTA.0 = 1 THEN GOTO Turn_On

GOTO Waiting

Turn_On: 'turns each LED on
FOR X = 0 TO 7
HIGH X
PAUSE 200
NEXT X


Wait_Again: 'waits for low signal

IF PORTA.0 = 0 THEN GOTO Turn_Off

GOTO Wait_Again

TURN_Off: 'turns each LED off
FOR X = 0 to 7
LOW X
PAUSE 200
NEXT X

GOTO Waiting 'restarts


I changed the names to unreserved names and I took out my INPUT statements.

sayzer
- 29th September 2006, 14:13
PAUSE 7000 'waits for sequence to finish

This line will execute AFTER the sequence is finished.

So you wait 7 seconds additionaly; and while waiting nothing will take place.

SOHCKing03
- 29th September 2006, 18:21
No, the first PIC does its sequencing then it waits 7 seconds for the next two PICs to sequence. Cause after an LED is turned on a 200ms pause happens so I have the first PIC waiting for all this to get done. The lights will stay on for another second or two then after the first PIC's pause is over the lights will begin to turn off.

sayzer
- 29th September 2006, 18:34
I see.

Why Don't you do it by sending a kind of signal to each other?
Just like making a pin high etc...?

Would be safer I think.


---------------------

mister_e
- 30th September 2006, 02:09
Who/what reserves the "wait" Steve?

I did not know that!
wait is also a modifier for Hserin, Serin2, etc etc.

sayzer
- 30th September 2006, 04:25
"There's no problem. Only learning opportunities."


--------------

SOHCKing03
- 30th September 2006, 04:44
Why Don't you do it by sending a kind of signal to each other?

---------------------

I am sending a signal from PIC to PIC. Pic one sends the high signal to PIC two to start the side sequencing and after that the second PIC sends a high signal to PIC three to start the back sequencing. I just didnt wanna have to run a wire all the way from the back to the front just to send a high signal. Plus, if I have a PAUSE then I can control how long I want the LEDS to stay on when they are all lit.

But other than that, does it seem okay?