Continue to Site

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.

  • 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.

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

Status
Not open for further replies.

masoud23

New Member
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:
Char x1 = LSB;
Char x2 = MSB;
write(x2); write(x1);  //this should show 257 decimal

code examples would be most helpful!

Tanks in advance
 
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.
 
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;

}
 
This is incorrect.

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;

}

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:
if( inData.7) 
   {
   SI=1; 
   SCL =1; 
   SI=0; 
   SCL =0; 
   }

All that said.. I don't know what LCD your writing to!!
 
\

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;

}

This can be changed to,
Code:
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.
 
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.
 
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.
 
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)
 
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;

}
 
I use my own print screen decimal conversion routine.

Code:
void printdec(int value)
   {
   lcdout((value/1000) + 48);          // hundreds
   lcdout(((value/100) %10) + 48);  // tens
   lcdout((value % 10) + 48);         // units
   }
 
If I'm not worried about bloating my code then I always use sprintf.
Code:
#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.
 
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;

}
 
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:
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:
You could also leave it as is and make a new WriteInt function that will in turn use dataout:

Code:
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:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top