Go Down

Topic: How to lcd print the individual bits char val (Read 1 time) previous topic - next topic

markov85

Oct 07, 2010, 07:29 pm Last Edit: Oct 07, 2010, 07:35 pm by markov85 Reason: 1
Code: [Select]

#include <LiquidCrystal.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define RS 14
#define E 4
#define DB0 5
#define DB1 6
#define DB2 7
#define DB3 8
#define DB4 9
#define DB5 10
#define DB6 11
#define DB7 12
LiquidCrystal lcd(RS,E,DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7);


char MF=127;
char itoaMFBuffer[20];
int itoaBase=2;
void setup()
{
 lcd.begin(16,2);
 lcd.clear();
lcd.print(itoa(MF,itoaMFBuffer,itoaBase));

 
}


void loop()
{
 
 
 
}


Well, on my display it shows "111 1111", which is kind of correct, but I want it to show "01111111".For what I undestand the itoa function in this aplication takes the char(byte format)  of 127(which is in dec format) converts it to binary(itoaBase) which is 0111 1111 and then takes every number/character(every individual bit) of this value and converts it to ASCII

              ----->to ASCII----->
bit7 - 0                                       0x30
bit6 - 1                                       0x31
bit5 - 1                                       0x31
bit4 - 1                                       0x31

bit3 - 1                                       0x31
bit2 - 1                                       0x31
bit1 - 1                                       0x31
bit0 - 1                                       0x31

then every ASCII value (as you can see is in HEX format) is send to a buffer (itoaMFBuffer) and then from the buffer every ASCII value is printed to the LCD in respective order.
Every ASCII value should be printed, but that is not the case as you can see i don't see the the zero, it is just skipped.
I want to see the 0111 1111 on my LCD, not 111 1111.
For what I understand the high bits that are 0 are not shown if they are not followed before by even one  bit that is 1(set).
:-/

dcb

one option is to prepend 7 zeros to it in a string

i.e. 11 becomes 000000011

then grab the right 8 characters

00000011



markov85

Don't understand you :-?
itoa creates a string itself in the buffer and then that string is printed.
give some code?
:-[

dcb

Here is a java example, don't know the c version off the top of my head

String b = "101";
String tmp = "0000000"+b;
System.out.println(tmp.substring(tmp.length() -8, tmp.length()));

output:
00000101

markov85

:-[ java is no go here
String b = "101"; is not right for me
I need a value
char b =101; in my case
I need a C function of something :-/


PaulS

#6
Oct 11, 2010, 10:51 pm Last Edit: Oct 11, 2010, 10:52 pm by PaulS Reason: 1
You could use the bitRead() function, and construct the char array one bit at a time.

Code: [Select]
byte byteSizeVal = 45;
char byteAsString[10];

for(byte b=7; b>=0; b--)
{
  byteAsString[b] = bitRead(byteSizeVal, b) + '0';
  byteAsString[b+1] = '\0';
}

Msquare

Look at strcpy, strncpy and strlen (standard C functions)
Code: [Select]
char OutBuf[20] ;
itoa(MF,itoaMFBuffer,itoaBase) ;
strcpy(strncpy(OutBuf,"0000000",8-strlen(itoaMFBuffer),itoaMFBuffer) ;

I've written this straight in the editor, so minor syntax/coding errors possible  :) but this should lead you to a possible solution

BTW, Are you or the code putting the space " " between the nibbles?

markov85

#8
Oct 11, 2010, 11:30 pm Last Edit: Oct 12, 2010, 03:57 am by markov85 Reason: 1
Ohh this might be disleading
the space between the numbers like 1111 1111 is put by me manually, I mean the display doesn't show those "nibbles", it shows "11111111"(without the nibble), but that's okey I don't want that nibble anyway.

Will try the code tomorrow.
TNX

Go Up