1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

[B]How to show 2bytes value on a display with two 1 byte variables?[/B]

Discussion in 'Microcontrollers' started by masoud23, Oct 21, 2011.

  1. masoud23

    masoud23 New Member

    Joined:
    Jan 29, 2010
    Messages:
    36
    Likes:
    0
    Hi
    How can I write a value on a display example: 257 when i can only use 1 byte variables that goes only to 255.
    Ex:
    Code (text):

    Char x1 = LSB;
    Char x2 = MSB;
    write(x2); write(x1);  //this should show 257 decimal
     
    code examples would be most helpful!

    Tanks in advance
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Do you have to use a char... why not an int?
     
  3. Robin2

    Robin2 New Member

    Joined:
    Aug 1, 2011
    Messages:
    47
    Likes:
    0
    I don't understand your question. If the display is limited to a single-byte value aren't you limited to a maximum of 255? If the display is not a limitation then every computer makes large numbers from single-byte values and I don't understand what the problem is.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. masoud23

    masoud23 New Member

    Joined:
    Jan 29, 2010
    Messages:
    36
    Likes:
    0

    This is a SW that I am trying to modify and it is not writen by me. The processor is a PIC18F26K20. The function used in the SW for writing on display handel only char. SW uses PORTB 0 to 7 as inputs. So I wonder is there anyway to do this using only char.

    void send_(){Lcd_send_data(8); Lcd_send_data(8); Lcd_send_data(8); Lcd_send_data(0);}

    #define LcdCtrlOut PORTB

    void Lcd_send_data(char data_in)
    { RS =1;
    if ( bInvertLcd) data_in= data_in ^ 255; DataOut(data_in); LcdLine++;
    }

    void DataOut(char inData)
    { CS2 =0;
    SCL =0; SI=0;
    if( inData.7) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.6) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.5) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.4) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.3) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.2) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.1) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.0) SI=1; SCL =1; SI=0; SCL =0;
    CS2 =1;

    }
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    This is incorrect.

    The if statement will cancel at the first semi colon even though the desired effect will be the same.

    it should be encapsulated in brackets like this
    Code (text):

    if( inData.7)
       {
       SI=1;
       SCL =1;
       SI=0;
       SCL =0;
       }
     
    All that said.. I don't know what LCD your writing to!!
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    This can be changed to,
    Code (text):

    void DataOut(char inData)
    { CS2 =0;
    SCL =0; SI=0;
    SI=1; SCL =1; SI=0; SCL =0;
    CS2 =1;
    }
     
    because all the if statements are the same.

    Or, in other words, the code is nonsense.

    Mike.
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Mike I think the object is to shift 8 bits of data out of SI.. If this be the case It should really work, because only SI = 1 is in the if statement the rest is clocking!!
     
  9. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    Actually, it will work as is but it's a very strange way to write it. The lack of braces makes it work. Adding braces will stop it working as it relies on SI being zero from the previous statements. Very confusing but there again, I have been in a bar for the last 3 hours.:D

    Mike.
     
  10. masoud23

    masoud23 New Member

    Joined:
    Jan 29, 2010
    Messages:
    36
    Likes:
    0
    Yes it works. The display do comunicate with the master unit. It is not a standard LCD. It is a costum made grafic LCD driven with a PIC18F26K20 which is a slave to a master unit that also have a PIC18. Comunication protocol is modbus.
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Either way.. to send a value to an LCD screen which is bigger than one byte.. you need to send two...But!!! you will need the ASCII code for three.. ie hundreds, tens and units

    For example... 265 is bigger than 255.. if you send 255 ton LCD the character for 255 is 5 bars ( filled cursor )
    We need to send 0x32, 0x36 then 0x35. ASCII characters for numbers required..
    (unless your serial screen can accept decimal numbers, but I doubt it)
     
  12. masoud23

    masoud23 New Member

    Joined:
    Jan 29, 2010
    Messages:
    36
    Likes:
    0
    Suppose I can write an integer on the display. The void DataOut(char inData) function is for char. How can I rewrite it to work with 24bits integer?

    #define U8 char
    #define S8 int8
    #define S16 int16
    #define U16 uns16
    #define S24 int24
    #define U24 uns24
    #define S32 int32
    #define U32 uns32

    S16 button_click1;
    S16 button_click2;
    S16 button_click3;
    S16 button_click;
    S24 button_click_total;

    button_click1 = (sensor[RELAY_WRITE_START+2].high8);
    button_click2 = (sensor[RELAY_WRITE_START+3].low8)*256;
    button_click3 = (sensor[RELAY_WRITE_START+3].high8)*256*256;
    button_click = (button_click1 + button_click2);
    button_click_total = (S24)button_click + (S24)button_click3;
    DataOut(button_click_total);

    void DataOut(char inData)
    { CS2 =0;
    SCL =0; SI=0;
    if( inData.7) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.6) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.5) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.4) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.3) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.2) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.1) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.0) SI=1; SCL =1; SI=0; SCL =0;
    CS2 =1;

    }
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    I use my own print screen decimal conversion routine.

    Code (text):

    void printdec(int value)
       {
       lcdout((value/1000) + 48);          // hundreds
       lcdout(((value/100) %10) + 48);  // tens
       lcdout((value % 10) + 48);         // units
       }
     
     
  14. mynameisdan

    mynameisdan New Member

    Joined:
    Dec 17, 2006
    Messages:
    37
    Likes:
    0
    Location:
    United Kingdom
    If I'm not worried about bloating my code then I always use sprintf.
    Code (text):

    #include "stdio.h"

    unsigned char buffer[16];
    unsigned long var = 12345678;
    sprintf(buffer, "%u", var);
     
    I usually format the whole line using sprintf and then send it as a string.
     
  15. masoud23

    masoud23 New Member

    Joined:
    Jan 29, 2010
    Messages:
    36
    Likes:
    0
    Suppose I can write an integer on the display. The void DataOut(char inData) function is for char. How can I rewrite it to work with 24bits integer?

    #define U8 char
    #define S8 int8
    #define S16 int16
    #define U16 uns16
    #define S24 int24
    #define U24 uns24
    #define S32 int32
    #define U32 uns32

    S16 button_click1;
    S16 button_click2;
    S16 button_click3;
    S16 button_click;
    S24 button_click_total;

    button_click1 = (sensor[RELAY_WRITE_START+2].high8);
    button_click2 = (sensor[RELAY_WRITE_START+3].low8)*256;
    button_click3 = (sensor[RELAY_WRITE_START+3].high8)*256*256;
    button_click = (button_click1 + button_click2);
    button_click_total = (S24)button_click + (S24)button_click3;
    pHex(button_click_total);

    void DataOut(char inData)
    { CS2 =0;
    SCL =0; SI=0;
    if( inData.7) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.6) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.5) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.4) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.3) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.2) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.1) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.0) SI=1; SCL =1; SI=0; SCL =0;
    CS2 =1;

    }
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Right.. Lets get something straight.. Who built the custom display? Have you got a copy of the protocol it uses? what works at the moment?

    If you send the value "245" (byte size) hat is displayed?
     
  17. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Ok like Ian asked if you do something like:

    DataOut(0x36);

    What shows on the LCD ? Would a 6 pop up or would 54 pop up?

    If its a 6 then try the below:
    Code (text):

    void DataOut(int inData,char len)
    {
      int hundreds,tens,ones=0;
      char loopBits;

      thousands = (inData / 1000) + 0x30;
      hundreds = (inData / 100) + 0x30;
      tens = (inData / 10) + 0x30;
      ones = (inData % 10) + 0x30;
      if(len == 4)
      {
        len--;
        for(loopBits=0;loopBits<8;loopBits++)
        {
            CS2 =0;
            SCL =0; SI=0;

            if( thousands & 0x80 == 1)
            {
              SI=1; SCL =1; SI=0; SCL =0;
            }
            thousands <<= 1;
            CS2 =1;
        }
      }
      if(len == 3)
      {
        len--;
        for(loopBits=0;loopBits<8;loopBits++)
        {
            CS2 =0;
            SCL =0; SI=0;
     
            if( hundreds & 0x80 == 1)
            {
              SI=1; SCL =1; SI=0; SCL =0;
            }
            hundreds <<= 1;
            CS2 =1;
        }
      }
        if(len == 2)
      {
        len--;
        for(loopBits=0;loopBits<8;loopBits++)
        {
            CS2 =0;
            SCL =0; SI=0;
     
            if( tens & 0x80 == 1)
            {
              SI=1; SCL =1; SI=0; SCL =0;
            }
            tens <<= 1;
            CS2 =1;
        }
      }
        if(len == 1)
      {
        len--;
        for(loopBits=0;loopBits<8;loopBits++)
        {
            CS2 =0;
            SCL =0; SI=0;
     
            if( ones & 0x80 == 1)
            {
              SI=1; SCL =1; SI=0; SCL =0;
            }
            ones <<= 1;
            CS2 =1;
        }
      }
    }
     
    Use it like... DataOut(400,3);

    That should display 400 on the LCD, The 3 signifies the max number of digits to show. So you expect only 3 then use 3. If you are unsure use 4...
    This should allow you to send DataOut(9999,4); which would show 9999 on LCD...

    note using DataOut(400,4); would show 0400 on lcd
     
    Last edited: Oct 26, 2011
  18. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    You could also leave it as is and make a new WriteInt function that will in turn use dataout:

    Code (text):

    void DataOut(char inData)
    { CS2 =0;
    SCL =0; SI=0;
    if( inData.7) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.6) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.5) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.4) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.3) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.2) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.1) SI=1; SCL =1; SI=0; SCL =0;
    if( inData.0) SI=1; SCL =1; SI=0; SCL =0;
    CS2 =1;

    }

    void WriteInt(int intData,char len)
    {
      char hundreds,tens,ones=0;

      thousands = (inData / 1000) + 0x30;
      hundreds = (inData / 100) + 0x30;
      tens = (inData / 10) + 0x30;
      ones = (inData % 10) + 0x30;

      if(len == 4)
      {
          DataOut(thousands);
          len--;
      }
      if(len == 3)
      {
          DataOut(hundreds);
          len--;
      }
      if(len == 2)
      {
          DataOut(tens);
          len--;
      }
      if(len == 1)
      {
          DataOut(ones);
      }
    }
     
    Ex:
    WriteInt(100,4); //Would show 0100 on LCD
    WriteInt(100,3); //Would show 100 on LCD
     
    Last edited: Oct 26, 2011

Share This Page