PDA

View Full Version : Changing from PORTC to PORTA Problems



Tissy
- 8th December 2005, 18:47
I was using PORTC as inputs for switches, but as it appears that the PIC18F2550 has no PORTC.3, i've to to move all to PORTA. However, after changing all the PORTC in code to PORTA, the PIC doesn't perform correctly.

Am i missing something?

This is what i had.



' ---------- [ I/O Definition ] ----------
TRISA = %11111111 ' Set PORTA to all input
TRISB = %10000001 ' Set PORTB (0)INPUT (1-5)OUTPUTS
TRISC = %00000111 ' Set PORTC (0-2 input) rest Outputs

' ---------- [ System Inputs ] ----------
Circle Var PORTC.0 ' 1 - Circle Switch

MaskSwitch=PORTC & $07
Select Case MaskSwitch
Case 1 '%00000001 or Circle Switch
If Circle = 1 Then Gosub Procedure_Circle
If Circle = 0 THEN
EndIf
..........

Which i thought i'd simply changed to-


' ---------- [ System Inputs ] ----------
Circle Var PORTA.0 ' 1 - Circle Switch

MaskSwitch=PORTA & $07
Select Case MaskSwitch
Case 1 '%00000001 or Circle Switch
If Circle = 1 Then Gosub Procedure_Circle
If Circle = 0 THEN
EndIf
..........

Thank you,

Steve

Tissy
- 8th December 2005, 19:14
Second thoughts.....is there a way to use the Case Select option as above for PORTC.0-2 (as it currently is) for the first 3 switched inputs, but then use PORTB.0-2 for the remainding 3 swtched inputs?

I am still new to using Case Selects so it is a bit confusing.

Many thanks.

BigWumpus
- 8th December 2005, 20:51
You must switch PortA to digital-I/O, after a Reset it will be only analog-in !
Just see the docs !

Tissy
- 10th December 2005, 23:32
Thanks BigWumpus, i tried this, but still no luck.

Ideally i want to monitor lines RC0, RC1, RC2, RC6, RC7 and RA0 (can't use RC3 as there isn't one and RC4 & 5 are used for USB. When a switch is depressed on one of theses lines it jumps to a corresponding sub routine.

I've got the Case Select routine working for RC0-2 but am struggling with the rest, particulalry as it loks at a different PORT.

Can anyone help please?

Thanks,

BigWumpus
- 11th December 2005, 11:09
Check the documentation around ADCON1 to switch PortA from analog to digital.

Your Case-statement looks fine, but is very very big in code !
(You have to calculate the MaskSwitch out of 2 numbers!)

Whats about this:

-----
Button1 Var PortC.0
Button2 Var PortC.1
...
Button7 Var PortA.0

ADCON1=%00001111

If Button1 Then
If Circle = 1 Then Gosub Procedure_Circle
Endif
If Button2 Then
If ...
Endif
-----

Tissy
- 11th December 2005, 18:42
Thanks for you help BigWumpus. I wanted to avoid using multiple IF..THEN statements as the 'cleaner' way is supposed to be using a CASE SELECT.

However, this is where i fall down. I am having difficulty using the CASE SELECT with multiple PORTS.

Obvioulsy if it can't be done then i have the multiple IF...THENS but i think with 5 or 6 buttons (or HIGH inputs as they will be), the code will be much bigger then using a structured CASE SELECT.

Cheers,

Steve

BigWumpus
- 12th December 2005, 19:29
Hello,

the complicated way:

MaskSwitch=(PortC & %00000111) | ((PortC & %11000000) >> 3) | ((PortA.0 & %00000001) << 5)


The better way (using the bit-operators of PICs):

MaskSwitch=0
If PortC.0 Then MaskSwitch.0=1
If PortC.1 Then MaskSwitch.1=1
If PortC.2 Then MaskSwitch.2=1
If PortC.6 Then MaskSwitch.3=1
If PortC.7 Then MaskSwitch.4=1
If PortA.0 Then MaskSwitch.5=1


Check out the size !!!

But be aware to press more then ONE key !!!
Then you have to write multiple case-lines !!! ;-)

Tissy
- 12th December 2005, 22:31
OK, So the second options is still using the CASE SELECT method is it?

Does this mean that the statement will look as follows:


Button1 Var PORTC.0
Button2 Var PORTC.1
Button3 Var PORTC.2
Button4 Var PORTC.6

Main:
MaskSwitch=0
If PORTC.0 Then MaskSwitch.0=1
If PORTC.1 Then MaskSwitch.1=1
If PORTC.2 Then MaskSwitch.2=1
If PORTC.6 Then MaskSwitch.3=1
Select Case MaskSwitch
Case 0
If Button1 = 1 Then Gosub Procedure_Button1
If Button1 = 0 THEN
EndIf

Case 1
If Button2 = 1 Then Gosub Procedure_Button2
If Button2 = 0 THEN
EndIf

Case 2
If Button3 = 1 Then Gosub Procedure_Button3
If Button3 = 0 THEN
EndIf
.......

ie, does the MaskSwitch.1 represent Case 1 value, MaskSwitch.2 represent Case 2 etc etc?

Many thanks again,

Steve

BigWumpus
- 13th December 2005, 21:09
No,

like your first posting, the maskswitch is:

1, 2, 4, 8, 16, 32 ....

or binary:

%00000001, %00000010, %00000100, %00001000, ...

BigWumpus
- 13th December 2005, 21:11
...and:

why are you checking the buttons inside the case-staments, when the case-statements are only executed, if the Buttons are pressed ?

Do you think, this will be the optimal way ?

After programming several decades in assembler, i think it is better, to check the Port-Bits and call the subroutines...