Sending Hex Code

I'm new to C programming and I want to send a hex code (38) to 8 contacts (on the parallax propeller microcontroller) so that the first contact gets 0 and the next contact gets 0 and the next contact gets 1, etc. This would be easier than sending binary code to each output.

By the way, this is for C code. The code I use so far works with a binary:

//int port[] = {27,26,25,24,23,22,21,20}; int i = 8; while(i >0) { //while start --i; low(27); low(26); high(25); high(24); high(23); low(22); low(21); low(20); } 

I want to send a hexadecimal code (38) to contacts from 20 to 27.

+6
source share
2 answers

The cycle should be a little different

 int port = 0x20; // starting port int val = 0x38; // value int i; for(i=0 ; i<8 ; i++) { if (val & 1) high(port); else low (port); val >>= 1; // shift val one bit right port++; // increment port } 

The loop starts at port 0x20 and bit 0 from val. If this bit is 1, it performs high if it is less.

Then, val is shifted one bit to the right to use bit 1, and the port is incremented.

Etc ... this is done 8 times for eight bits of val (0x38).

Please note that if you need to start from the top port, this other version should match

 int val = 0x38; // value int port; for(port=0x27 ; port >= 0x20 ; port--) { // goes from port 0x27~20 if (val & 0x80) high(port); else low (port); val <<= 1; // shift val one bit left } 
+3
source

Firstly , when working with a microcontroller, as a rule, there is a way to indicate that the value of 8 contacts belongs to the same port at the same time.

You should try to figure this out from the Microcontroller (uC) manual / table by searching for a keyword ! Otherwise, you will find hellish time (indeed, I am NOT joking! At least I did it!) To find it, scattered in , but several of a hundred / thousand pages of manual / technical description.

Try it first and it will be much easier for you.

Secondly , in uC, when assigning high and low output, you usually do not need to take different functions, since they simply assign a value (high or low) to the same output. Thus, when possible, you should combine the high and low functions into a single function (since I do not know what is high and low inside, I can not help in the future, but you should mention this feature to facilitate your task)

Finally , however, if it cannot be executed for one reason or another, you can simplify your C code like this,

 void assignPort(char val, char portBase){ int i = 0; for (i = 0; i < 8; ++i) if (val & (1 << i)) //masked your val with shifting 1 to get the current bit high(portBase + i); //increase your port address by i else low(portBase + i); } 

And to call the function just do

 assignPort(0x38, 20); //notice the 38 using 0x for hex indicator while 20 is written as it is (assuming 20 is not hex) 

Please note that if your high and low functions can be combined

 void highLowCombined(char pinAddress, char highOrLow) { //highOrLow is simply non-zero (high) or zero (low) //do something based on pinAddress and highOrLow } 

your task is much simpler in calling high / low value functions and the val variable in assignPort above, since you just need to do it like this

 void assignPort(char val, char portBase){ int i = 0; for (i = 0; i < 8; ++i) highLowCombined(portBase + i, val & (1 << i)); //only one line is needed here! } 

Now you only need one line in the for-loop ! Give it a try!

+2
source

All Articles