Shop OBEX P1 Docs P2 Docs Learn Events
Send hex to 8 pins — Parallax Forums

Send hex to 8 pins

Hi all,
I'm trying to send a hex code to 8 pins of a parallax propeller in c.
Send hex 38 to pins 27,26,25,24,23,22,21,20 which is 00111000 in binary, so that;
0 goes to pin27
0 goes to pin26
1 goes to pin25
1 goes to pin24
1 goes to pin23
0 goes to pin22
0 goes to pin21
0 goes to pin20
In stead of sending a binary to each pin I want to send a single hex code.
Thanks

Comments

  • Are you sure you even know what you are asking? Can you try to rephrase what you saying. Hex is a way of representing 4 bits as a single digit. A pin is high or low, that is binary.
  • Try
    outa[27..20] := $38
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-12-24 04:10
    Sapphire wrote: »
    Try
    outa[27..20] := $38
    

    This is my guess as well.

    As Peter points out, 'Hex" is a way of displaying numbers for us humans. All numbers are stored in binary inside the Propeller. Sometime us humans don't mind reading numbers in binary but we also like numbers displayed in other ways depending on the application. Hexadecimal is a convenient way of displaying a single byte as two digits.

    I'll add, Sapphire's code:
    outa[27..20] := $38
    

    Could also be written other ways with the exact some results.
      outa[27..20] := $38
      outa[27..20] := 56
      outa[27..20] := %111000
      outa[27..20] := %%320 ' Quaternary (I think I got that right)
      outa[27..20] := "8" ' an ASCII character
    

    (I left off the leading zeros in the binary and quaternary notation.)

    All the above numbers are stored inside the Propeller with the same pattern of ones and zeros.
  • AribaAriba Posts: 2,690
    In C you need to AND the OUTA first with a mask to clear the bits 27..20, and then you can OR the hex value to set the needed bits:
    OUTA = OUTA & ~(0xFF<<20) | (0x38<<20);  //0x38 = 00111000 binary
    

    Andy
  • gregfox wrote: »
    In stead of sending a binary to each pin I want to send a single hex code.
    Thanks

    This is just a poorly worded question, are you saying you want to send "hex code" to each pin?
    If I write $FA to P0..P7 then it is possible to feed P0..P3 into a "binary" to 7-segment decoder driving a 7-segment display to display "hex" and the same for the other 4 bits. But your question comes across as just pure nonsense.
  • Thank you all, but as I wrote in my question I'm trying to do this in C not Spin. I've done it in spin with no problem, by I'm now trying to work in C only.
  • gregfox wrote: »
    I'm now trying to work in C only.

    Sorry about that. Good thing Ariba was around.

  • Well, I guess my post was unclear.
    I want to send a hex code(38) to 8 pins 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
    Low(27);
    Low(26);
    High(25);
    I hope that’s clear, sorry for my poorly worded post.
    By the way this is C not spin.
    Thank you
  • 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.
    I hope that’s clear, sorry for my poorly worded post.
    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.
    Thank you
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2015-12-25 05:02
    Hey guys, I've been told off for being a little hard on poor Greg but be assured that it's not business and it's not personal. People who do nothing can't get it wrong but plenty of clever people do dumb things all the time. If we didn't know it was dumb then we wouldn't be clever, would we? That's how we get clever, doing dumb things.... but learning from our mistakes. So I'm pointing out that the wording is dumb, not the person. Sorry Greg if it seemed to come across that way, that directness I attribute to my German heritage.... :)

    P.S. come to think of it, I get in trouble with my wife all the time for this type of thing!
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-12-25 05:22
    Starting a second thread on the same topic usually isn't a great idea -- just a tip.

    I'm not a C programmer, but I understand the Propeller well enough to know that outputs require manipulation of the dira and outa registers. After installing SimpleIDE I poked around for a few minutes and found a function that I could adapt to your pin set.
    void wr8bits(int val)
    {
        DIRA |= 0xff << 20;
        OUTA = (OUTA & ~(0xff << 20)) | (val << 20);
    }
    
    Edit: I just saw in the other thread that Andy provided a solution; this one is a little more generic so you can change the values on the pins with the function call.
  • David BetzDavid Betz Posts: 14,516
    edited 2015-12-25 17:06
    Try these functions for getting or setting a range of pins:
    #include <propeller.h>
    
    void setPins(int low, int high, int value)
    {
        int bitCount = high - low + 1;
        int mask = ((1 << bitCount) - 1) << low;
        DIRA |= mask;
        OUTA = (OUTA & ~mask) | (value << low);
    }
    
    int getPins(int low, int high, int value)
    {
        int bitCount = high - low + 1;
        int mask = ((1 << bitCount) - 1) << low;
        DIRA &= ~mask;
        return ((INA & mask) >> low;
    }
    

    In your case you would do something like this:
    setPins(20, 27, value);
    
  • Thank you for the information. I found a very simple way to do what I wanted to do:
    set_directions	(23, 16, 0xFF);      //instead of 0xFF  0b11111111  can be used
    set_outputs (23, 16, 0XF0);
    

    The above will set pins 16-23 to output
    then pins 20-23 will go high.
    Thanks again.
  • Jon is correct, Cross posting is not permitted
    No Cross-Posting:
    Cross-posting is: 1) submitting a post to one forum or thread, and then submitting a second linking to the original post (without changing content or intent), or 2) creating an identical post in more than one forum or thread. When the same content is found in more than one post, even under different subject lines, the forum moderator will remove one (or more) of the redundant posts and the posting account may be subject to moderation.
  • gregfoxgregfox Posts: 68
    edited 2015-12-25 17:45
    Peter Jakacki I'm sure you're very clever, but it is very clear you do not know C very well if at all since you didn't respond to my second post when I tried to make my question more 'clear' You were right in saying what you did, but the way in which you said it was a bit hurtful.

    Hiding behind German heritage is no excuse for bellicose, pejorative, lilliputian behavior.

    Just so you know, I solved the problem with my own agog with the below code:

    set_directions (23, 16, 0xFF); //instead of 0b11111111 0xFF can be used
    set_outputs (23, 16, 0XF0); //set pin 20-23 high

    In any case I hold no umbrage.
  • sorry, did't know. Now I do.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-12-25 17:51
    I thought Ariba already answered this?

    I don't suppose we could get these threads merged?

    As has been said multiple times, you're not sending "hex", you're sending a number. Hex is one way to display the number.
  • gregfox wrote: »
    Thank you for the information. I found a very simple way to do what I wanted to do:
    set_directions	(23, 16, 0xFF);      //instead of 0xFF  0b11111111  can be used
    set_outputs (23, 16, 0XF0);
    

    The above will set pins 16-23 to output
    then pins 20-23 will go high.
    Thanks again.
    Are those functions from the Simple Library? I suspect they contain pretty much the same code that I posted earlier. :-)

  • PublisonPublison Posts: 12,366
    edited 2015-12-25 21:09
    Duane Degn wrote: »

    I don't suppose we could get these threads merged?

    I"m trying.

  • PropWare contains a great object for this, called the SimplePort. Here's how to write the code you're looking for with a SimplePort:
    #include <PropWare/port.h>
    
    using namespace PropWare;
    
    int main () {
        // Lowest pin is P20, the port is 8 pins wide, and they should be initialized as output
        SimplePort pins(Port::P20, 8, Port::OUT);
    
        // Send the hex value 38 to the port
        pins.write(0x38);
    
        return 0;
    }
    

    Full documentation on the SimplePort can be found here: http://david.zemon.name/PropWare/classPropWare_1_1SimplePort.xhtml
  • Poor wording was a bit hurtful? vs personal and insulting? I'm very busy myself but always stop to help if I can understand the question which you didn't bother to work on. Grow up Greg.
  • Gentleman, Peter already apologized. Let's keep it technical.

  • Hi I have the following code
    /code
    /*
    Blank Simple Project.c
    http://learn.parallax.com/propeller-c-tutorials
    */
    #include "simpletools.h" // Include simple tools

    //DataPins "20 21 22 23 24 25 26 27"
    #define RS 17 //Register Select
    #define EN 18 // Enable (Starts data read/write.)After E allows R/W it must then be turned off.
    #define RW 19 //Read/write
    #define DATA {27 26 25 24 23 22 21 20}

    void inst(void);
    void data(void);
    void latch(void);
    void Initialize(void);
    void setout(void);
    void setin(void);
    void wr8bits(int val);


    int main()
    { //main start

    initialize(); //initialize LCD display (low(RS), low(RW), high(EN)
    inst(); //Send Instruction Function Select routine (low(RS), low(RW))
    //DATA Send Data 0X38 (set 8 bit mode) Send This instruction on LCD databus
    set_outputs (27, 20, 0x38); //set 8 bit mode, 2-lines Font 5x7
    pause(2);
    latch();

    //DATA Send 0X0f Second Instruction on LCD databus
    inst();
    set_outputs (27, 20, 0x0F); //Display on, Cursor on, Cursor blink
    latch();

    inst(); //(low(RS), low(RW))
    set_directions (27, 20, 0xFF); //instead of 0b11111111 0xFF can be used (outputs)
    set_outputs (27, 20, 0x1); //CLEAR display 0b00000001
    latch();
    pause(2000);



    set_directions (27, 20, 0xFF); //instead of 0b11111111 0xFF can be used (outputs)
    data(); //Change the input type to Data.(before it was instruction input)


    set_outputs (27, 20, 0x47); //Send G send 47 HEX (G) to pins 20 to 27
    latch();
    set_outputs (27, 20, 0x52); //Send R
    latch();
    set_outputs (27, 20, 0x45); //Send E
    latch();
    set_outputs (27, 20, 0x47); //Send G
    latch();
    return(0);
    inst();
    set_outputs (27, 20, 0xC0); //Cusor on line 2
    latch();
    void inst(void) //Instruction select routine
    {
    low(RS); //RS low
    low(RW); //RW low
    }
    void data(void) // //Change the input type to Data.(before it was instruction input)
    {
    high(RS); //RS high
    low(RW); //RW low
    }

    void latch(void) //->EN<- Latch the above instruction once.
    {

    high(EN); //EN high
    pause(2); //wait 30mS
    low(EN); //EN low
    pause(2); //wait 30 mS
    high(EN); //EN high
    // p ause(50); //wait 50mS
    }

    void initialize(void)
    {
    low(RS);
    low(RW);
    high(EN);
    pause(2); //some LCDs takes time to initialize at startup
    }

    void setout(void)
    {
    set_directions (27, 20, 0xFF); //instead of 0b11111111 0xFF can be used (outputs)
    }



    void setin(void)
    {
    set_directions (27, 20, 0x0); //instead of 0b00000000 0xFF can be used (inputs)
    }
    void wr8bits(int val)
    {
    DIRA |= 0xff << 20;
    OUTA = (OUTA & ~(0xff << 20)) | (val << 20);
    }

    code/
    As can be seen (in red), I'm sending one character at a time to the LCD, I've been working on a way to sent a string, instead of one character at a time. Any help?
  • David Betz wrote: »
    Try these functions for getting or setting a range of pins:
    #include <propeller.h>
    
    void setPins(int low, int high, int value)
    {
        int bitCount = high - low + 1;
        int mask = ((1 << bitCount) - 1) << low;
        DIRA |= mask;
        OUTA = (OUTA & ~mask) | (value << low);
    }
    
    int getPins(int low, int high, int value)
    {
        int bitCount = high - low + 1;
        int mask = ((1 << bitCount) - 1) << low;
        DIRA &= ~mask;
        return (INA & mask) >> low;
    }
    
    Sorry, there was a bug in the code above. I've fixed it by removing an unnecessary left paren from the second function.

  • gregfoxgregfox Posts: 68
    edited 2016-01-04 06:22
    I've coded the following in c, what do you think?
    //code
    
    #include <simpletools.h>
    inst();
          set_outputs	(26,19, 0xC0);			//Cusor on line 2
          strobe();
          char a[20] = ("This is line 2 "); 		// Initialize the array
         for(int i = 0; i < 20; i++) 		        // Count i from 0 to 14
          {   
          pause(2);
          data();
          set_outputs	(26,19, (a[i]));			//Send LINE 2
          //pause(2);
          strobe();
    
    code//
    BTW, is this the proper way to display code?
  • Code blocks are:

    [ code ]

    And

    [ /code ]

    But without the spaces.
  • @gregfox Correct code symbols added. As Jason says- that's what you need use next time.
  • Thanks for the info.
Sign In or Register to comment.