View previous topic :: View next topic |
Author |
Message |
teccs
Joined: 14 Oct 2006 Posts: 69
|
Posted: Wed Apr 08, 2020 7:05 pm Post subject: Representing ports numerically? |
|
|
In my code, I receive a number via serial port. Based on that number the code will turn a port on or off. For example, if 1 is received, turn port G.1 off. If 2 is received, turn port G.1 on.
I have to handle about 30 ports like the above with different numbers coming from the serial port to tell what port to do what with.
My current method to handle this is using a SELECT CASE statement with about 60 cases.
Is there any way to handle this numerically or with an array()?
It would clean up the code a bunch if I could get rid of the huge SELECT CASE setup.
Thank you!
Tim.
(BASCOM-AVR version : 2.0.8.2 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Apr 08, 2020 7:10 pm Post subject: |
|
|
that can only be done when there is some logic to the system.
for example, all even numbers do something and all odd numbers do something else.
then it depends on the processor used if it can be done simple or not. unfortunately normal AVR processors do not have a logic to the port numbering. Xmega do have.
so as always, you need to show processor and some code in order somebody can help you. _________________ Mark |
|
Back to top |
|
|
teccs
Joined: 14 Oct 2006 Posts: 69
|
Posted: Wed Apr 08, 2020 7:23 pm Post subject: Processor is ATMEGA 2560 |
|
|
Thank you for the reply.
I can use any numbering scheme or logic that would work well.
Code is below.
Code: |
DO
FOR X = 0 to 7
Call ReadADC(X)
ADCValue(X) = ADCCount
NEXT
A = Ischarwaiting(#2)
LOOP UNTIL A = 1
INPUT #2, InstructionReceived 'Get Instruction from PC.
WaitMS 500
SELECT CASE B
CASE = 1
PORTG.0 = 0
CASE = 2
PORTG.0 = 1
'... That example would be continued below with other ports.
CASE = 3
CASE = 4
CASE = 5
CASE = 6
CASE = 7
CASE = 8
CASE = 9
CASE = 11
CASE = 12
CASE = 13
CASE = 14
CASE = 15
CASE = 16
CASE = 17
CASE = 18
CASE = 19
CASE = 21
CASE = 22
CASE = 23
CASE = 24
CASE = 25
CASE = 26
CASE = 27
CASE = 28
CASE = 29
CASE = 31
CASE = 32
CASE = 33
CASE = 34
CASE = 35
CASE = 36
CASE = 37
CASE = 38
CASE = 39
CASE = 40
CASE = 41
CASE = 42
CASE = 43
CASE = 111
CASE = 112
CASE = 113
CASE = 114
CASE = 115
CASE = 116
CASE = 117
CASE = 118
CASE = 119
CASE = 121
CASE = 122
CASE = 123
CASE = 124
CASE = 125
CASE = 126
CASE = 127
CASE = 128
CASE = 129
CASE = 131
CASE = 132
CASE = 133
CASE = 134
CASE = 135
CASE = 136
CASE = 137
CASE = 138
CASE = 139
CASE = 140
CASE = 141
END SELECT |
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Apr 08, 2020 7:42 pm Post subject: |
|
|
if you are free to use anything you can consider the following : let the on/off be part of the number. meaning that even numbers means off. and odd numbers mean ON.
but you can also encode things. for example you need 1 bit to turn on/off. then you have port A,B,C,D,E,F,G which are 7 ports. meaning that you need 3 bits to specify the port.
and you have 8 bits which mean you need 3 bits again. thus using 7 bits you can set everything.
0xxx Z yyy
xxx=0- porta, 1-portb etc
yyy=0 bit 0, 7-bit 7
z=0 is off, 1=on
you can work that out on the sending side.
then on the decode :
bState=B.3 ' get state where B is the coded byte, bstate will be on/off
index=b and 7 'get index
prt= b and &HF0
swap prt 'get the port number
select case prt
case 0 : portA.index=bstate
case 1 : portB ....
that is it.
it might have some errors since i did not test and wrote it down. but it is simple to test in the sim. _________________ Mark |
|
Back to top |
|
|
teccs
Joined: 14 Oct 2006 Posts: 69
|
Posted: Wed Apr 08, 2020 9:36 pm Post subject: Thanks Albert. |
|
|
I will give this a try. |
|
Back to top |
|
|
|