can I see a byte?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Olivia
Posts: 39
Joined: 12 Feb 2010 12:31

can I see a byte?

#1 Post by Olivia » 09 Sep 2010 23:04

Is there a way to display a byte on LCD, so you can actually see it?

it's not part of a project,
just for educational purposes

Thanks

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: can I see a byte?

#2 Post by drdoug » 10 Sep 2010 00:33

Yes.

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: can I see a byte?

#3 Post by drdoug » 10 Sep 2010 00:49

OK so you were probably expecting an explanation. I'll try, but some of the local experts can do a better job.
A byte is just 8 bits of data (1's and 0's).
The LCD typically requires you to send the data as a byte (an ASCII byte in most cases).
So if your byte is 0x21 that corresponds to the decimal number 33 or the ASCII character "!".
Now, lets assume that the byte you want to keep track of is some counting value and that value has reached 114. If you send that byte to the LCD it will show "r" on the LCD. This is probably not helpful. One of the cool functions that mE provides is ByteToStr(consult the help file). This allows you to take some byte and convert it to easily display the decimal value is ascii. So you will now have a string of characters that represents "114" .

Hopefully that helps.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: can I see a byte?

#4 Post by Dany » 11 Sep 2010 15:29

Olivia wrote:Is there a way to display a byte on LCD, so you can actually see it?

it's not part of a project,
just for educational purposes

Thanks
I have never seen a byte. I have seen the value of a byte in many formats: in binary, in hex, in decimal, in BCD and in ascii, so I know a byte actually exists.
Anyway, to see the value of a byte you will have to convert it to a format that the LCD can handle, and this is the character/string format.
In the library set of the different compilers one can fins several conversion routines to convert the value of a byte to a string that can be sent to the LCD, e.g. "bytetostr" (example from mikroPascal).

have fun! :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Olivia
Posts: 39
Joined: 12 Feb 2010 12:31

Re: can I see a byte?

#5 Post by Olivia » 12 Sep 2010 00:19

I was thinking if it is possible to put on dispaly a byte, let's say 0b10011011
I know you can put the same in hex on display 0x9B

But is there a function that will convert the hex being transmitted to the LCD so it will be displayed as 0b10011011?

And if the original message read from the chip was in binary (0b10011011) why should I tranform it into hex, then back into binary so I can see it on the LCD as I want?
I thought I can take the original binary reading and using a function or a magical crystall ball, I can put it on LCD to see it.

You know, like in the Matrix!!!
Well almost. Not top to bottom, but left to right.

Regards.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: can I see a byte?

#6 Post by Dany » 12 Sep 2010 14:06

Olivia wrote:I thought I can take the original binary reading and using a function or a magical crystall ball, I can put it on LCD to see it.
Yes, in the "Conversions" library of Yo2Lio (Florin) there is a "Byte2Bin" routine, which transforms the value of a byte to a string displayable on LCD. See http://www.mikroe.com/forum/viewtopic.php?t=21871 (for Pascal, but I think there is one for C too).

Otherwise you can use the following code: after translation to C:

Code: Select all

procedure ByteToBinStr(Value: byte; var S: string[8]);
var I: byte;
begin
  I := 0;
  while I < 8 do
  begin
    if Value.I = 0
    then S[7 - I] := '0'
    else S[7 - I] := '1';
    Inc(I);
  end;
  S[8] := 0; // terminator
end;
Success! :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: can I see a byte?

#7 Post by Dany » 12 Sep 2010 17:54

MikroC_888 wrote:Besides the fact that C code is easier-to-read and forces the programmer to think "structured code" ...
Thought you would say that. :mrgreen:

Please do not spoil this thread. :wink:
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

zan
Posts: 434
Joined: 15 May 2007 19:35

Re: can I see a byte?

#8 Post by zan » 12 Sep 2010 18:25

I have just written the same procedure in mikroBasic :D :D :D

Code: Select all

    sub procedure ByteToBin(dim InByte as byte, dim byref OutStr as string[8])
        dim cnt as byte

        for cnt = 0 to 7
            if InByte.cnt = 1 then
                OutStr[7 - cnt] = "1"
            else
                OutStr[7 - cnt] = "0"
            end if
        next cnt

        OutStr[8] = 0
    end sub

sly
Posts: 43
Joined: 13 Feb 2005 02:20

Re: can I see a byte?

#9 Post by sly » 12 Sep 2010 22:52

There you go :

Code: Select all

/*
 * Project name:
     Lcd_Test (Simple demonstration of the LCD Library functions)
 * Copyright:
     (c) Mikroelektronika, 2005.
 * Description:
     This is a simple demonstration of LCD library functions. LCD is first
     initialized (PORTD, 4-bit data interface, default pin settings), then some
     text is written at the first row.
 * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       EasyPIC4
     Oscillator:      HS, 08.0000 MHz
     Ext. Modules:    LCD 2x16
     SW:              mikroC v8.2
 * NOTES:
     None.
*/


//----------------------------------------------------------------------------

void lcd_out_bin (unsigned char value)
{
  char i,*text;
  
  for(i=0;i<=7;i++)
  {

  if(value&(1<<i))  text[7-i] = '1' ;
  else              text[7-i] = '0' ;

  }

  text[8] = 0;

  lcd_out(2,1,"0b");
  lcd_out(2,3,text);

}

//----------------------------------------------------------------------------

void main()
{

  LCD_Init(&PORTD);           // Initialize LCD connected to PORTD
  LCD_Cmd(LCD_CLEAR);         // Clear display
  LCD_Cmd(LCD_CURSOR_OFF);    // Turn cursor off

  lcd_out(1,1,"0xf0") ;
  lcd_out_bin(0xf0) ;
  
} 

Post Reply

Return to “mikroC PRO for PIC General”