View previous topic :: View next topic |
Author |
Message |
Denny9167
Joined: 15 Feb 2021 Posts: 49
|
Switch Statement |
Posted: Fri Apr 16, 2021 9:22 am |
|
|
How would this switch statement look in CCS C? Thanks
Quote: |
// key1 0x50 to Toggle relay 1 // these are command of the IR remote control which i have
// key2 0xD8 to Toggle relay 2
// key3 0xF8 to Toggle relay 3
// key4 0x30 to Toggle relay 4
// key5 0xB0 to Turn off all the relays
switch(command) // swich on
{
case 0x50: RELAY1 = !RELAY1; //Toggle relay 1
break;
case 0xD8: RELAY2 = !RELAY2; //Toggle relay 2
break;
case 0xF8: RELAY3 = !RELAY3; //Toggle relay 3
break;
case 0x30: RELAY4 = !RELAY4; //Toggle relay 4
break;
case 0xB0: RELAY1 = 0; //Turn off all the relay
RELAY2 = 0;
RELAY3 = 0;
RELAY4 = 0;
break;
default :
break;
}
|
|
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Fri Apr 16, 2021 9:31 am |
|
|
From my experience, the CCS compiler handles switch/case like a normal C compiler.
The directives to toggle a relay might be different -- either mapping to a specific I/O pin (is that what your RELAY is?) or using output_high/output_low/etc. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Apr 16, 2021 9:38 am |
|
|
Assuming you are talking a PIC16/18, then if you don't have a 'default', and
there are a reasonable number of 'cases', it'll code as a jump table, rather
than tests. If you add a default, it switches to using tests, and will use tests
if there are less than about half a dozen cases. |
|
|
|