Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending hex code

I'm new to C programming, and I want to send a hex code(38) to 8 pins (on a parallax propeller micro-controller) so that the first pin gets a 0, and the next pin gets a 0 and the next pin gets a 1, etc. This would be easier then sending a binary code to each pin.

By the way this is for C code. The code I'm using so far, that works by using binary is:

//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);
}

What I want to do is send a single hex code (38) to pins 20 to 27.

like image 388
Gregfox Avatar asked Dec 25 '15 03:12

Gregfox


People also ask

How do I send hex?

Open the event where you want to send the hex command. Go to Event Properties > Advanced. Click Add Command and set the Command to Send and the Command Parameters to Serial bytes (comma separated). Set the Port to 0 (this is the case for most standard serial devices).

How can I send hex through UART?

What is the proper way to send this? Usually when people say "hex data", they mean the value converted to two ASCII characters to represent the hexadecimal value. e.g. the value 0xD5 would be sent as 'D' (character 0x44), and '5' (character 0x35). You are sending the raw binary value.


2 Answers

The loop should be a bit 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 start from port 0x20 and bit 0 of val. If that bit is 1, it does a high else it does a low on that port.

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

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

Note that if you have to start from the top port, this different version should fit

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
}
like image 178
Déjà vu Avatar answered Sep 22 '22 12:09

Déjà vu


Firstly, when working with micro-controller, typically there is a way to specify the value of the 8 pins belong to the same port at once.

You should try to figure it out from the Microcontroller (uC) manual/datasheet by searching its keyword!! Otherwise you will find a hellish time (really, I am NOT kidding! At least I did!) to search for it scattered in but a few of the hundred/thousand pages of the manual/datasheet.

Try this first and it should get a lot easier.

Secondly, in uC, assigning high and low on pin typically do not need to take different functions since they simply assigning value (high or low) to the same pin. Thus, whenever possible, you should combine the high & low functions into single function (since I do not know what is inside high & low, I cannot really help further on this, but you should note this possibility to ease your task)

Finally, however, in case it cannot be done 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, simply do

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

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 a lot easier in calling high/low functions and val variable in the assignPort above since you simply 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! Try it out!

like image 20
Ian Avatar answered Sep 22 '22 12:09

Ian