LCD code help
hello everyone, first post here hope i'm doing things right!
i've been learning arduino on , off few months, started major programming higher functions , stuff.
i got character lcd i've connected board (arduino uno) via parallel connection.
being curious, wanted know how pins on lcd worked display characters, down basic zero's , one's, , after few days of research able working without liquidcrystal library.
all working well, until decided wanted try , write in more efficient code , not take eleven lines of code each instruction/data wanted send.
what want able set each pin on arduino (5-12 data busses 0-7) either high or low, depending on status of each bit of hexadecimal character. hope making sense.
for example, if wanted display capital a, in ascii represented 0x41 (hexadecimal), want program break down hexadecimal character binary equivalent (0 1 0 0 0 0 0 1), read each bit of byte, , set cooresponding pin high or low.
this way can plug in hexadecimal representation of ever character want write instead of looking binary representation of character , manually change each pins output.
here's original code wrote basic commands:
and here's gets funky.
i tryed use arrays , loops perform each command in few lines of text.
this way seemed logical me, that's why i'm here, ask guys!
so set 2 arrays, 1 (a), contained output pins 5-12, or db0-db7, , other (b), 8 elements long, each element either high or low depending on bits of byte.
so here's wrote:
so think in loop not working correctly. , help/comments/sugestions/criticism appreciated!
thank you! xd
i've been learning arduino on , off few months, started major programming higher functions , stuff.
i got character lcd i've connected board (arduino uno) via parallel connection.
being curious, wanted know how pins on lcd worked display characters, down basic zero's , one's, , after few days of research able working without liquidcrystal library.
all working well, until decided wanted try , write in more efficient code , not take eleven lines of code each instruction/data wanted send.
what want able set each pin on arduino (5-12 data busses 0-7) either high or low, depending on status of each bit of hexadecimal character. hope making sense.

for example, if wanted display capital a, in ascii represented 0x41 (hexadecimal), want program break down hexadecimal character binary equivalent (0 1 0 0 0 0 0 1), read each bit of byte, , set cooresponding pin high or low.
this way can plug in hexadecimal representation of ever character want write instead of looking binary representation of character , manually change each pins output.
here's original code wrote basic commands:
code: [select]
/* "arduino pin, lcd pin - function"
2, pin1 - rs
3, pin2 - r/w
4, pin3 - e
5, pin4 - d0
6, pin5 - d1
7, pin6 - d2
8, pin7 - d3
9, pin8 - d4
10, pin9 - d5
11, pin10 - d6
12, pin11 - d7
*/
int lcdpins[12] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int x;
int rs = 2;
int rw = 3;
int e = 4;
int d7 = 5;
int d6 = 6;
int d5 = 7;
int d4 = 8;
int d3 = 9;
int d2 = 10;
int d1 = 11;
int d0 = 12;
int backlight = 13;
void setup()
{
for(x = 0; x < 11; x++){ //sets each pin (2-12), coorespond rs, rw, e, & db7-db0, outputs.
pinmode(lcdpins[x], output);
}
pinmode(backlight, output); //sets backlight output , turns on
digitalwrite(backlight, high);
delay(50);
/*
next groupings sending lcd instructions ready write characters it
*/
digitalwrite(rs, low); //clear home
digitalwrite(rw, low);
delay(50);
digitalwrite(e, high);
delay(50);
digitalwrite(d0, low);
digitalwrite(d1, low);
digitalwrite(d2, low);
digitalwrite(d3, low);
digitalwrite(d4, low);
digitalwrite(d5, low);
digitalwrite(d6, low);
digitalwrite(d7, high);
delay(50);
digitalwrite(e, low);
delay(50);
digitalwrite(rs, low); //function set
digitalwrite(rw, low);
delay(50);
digitalwrite(e, high);
delay(50);
digitalwrite(d0, low);
digitalwrite(d1, low);
digitalwrite(d2, high);
digitalwrite(d3, high);
digitalwrite(d4, high);
digitalwrite(d5, high);
digitalwrite(d6, low);
digitalwrite(d7, low);
delay(50);
digitalwrite(e, low);
delay(50);
digitalwrite(rs, low); //entry mode
digitalwrite(rw, low);
delay(50);
digitalwrite(e, high);
delay(50);
digitalwrite(d0, low);
digitalwrite(d1, low);
digitalwrite(d2, low);
digitalwrite(d3, low);
digitalwrite(d4, low);
digitalwrite(d5, high);
digitalwrite(d6, high);
digitalwrite(d7, low);
delay(50);
digitalwrite(e, low);
delay(50);
digitalwrite(rs, low); //display control
digitalwrite(rw, low);
delay(50);
digitalwrite(e, high);
delay(50);
digitalwrite(d0, low);
digitalwrite(d1, low);
digitalwrite(d2, low);
digitalwrite(d3, low);
digitalwrite(d4, high);
digitalwrite(d5, high);
digitalwrite(d6, high);
digitalwrite(d7, high);
delay(50);
digitalwrite(e, low);
delay(50);
digitalwrite(rs, low); //set ddram address
digitalwrite(rw, low);
delay(50);
digitalwrite(e, high);
delay(50);
digitalwrite(d0, high);
digitalwrite(d1, low);
digitalwrite(d2, low);
digitalwrite(d3, low);
digitalwrite(d4, low);
digitalwrite(d5, low);
digitalwrite(d6, low);
digitalwrite(d7, low);
delay(50);
digitalwrite(e, low);
delay(50);
/*
here i'm sending binary byte 0 1 0 0 0 0 0 1
lcd on db0 - db7 tell write capital a
*/
digitalwrite(rs, high); //write capital "a"
digitalwrite(rw, low);
delay(50);
digitalwrite(e, high);
delay(50);
digitalwrite(d0, low); // 0
digitalwrite(d1, high); // 1
digitalwrite(d2, low); // 0
digitalwrite(d3, low); // 0
digitalwrite(d4, low); // 0
digitalwrite(d5, low); // 0
digitalwrite(d6, low); // 0
digitalwrite(d7, high); // 1
delay(50);
digitalwrite(e, low);
delay(50);
}
void loop()
{
}
and here's gets funky.
i tryed use arrays , loops perform each command in few lines of text.
this way seemed logical me, that's why i'm here, ask guys!
so set 2 arrays, 1 (a), contained output pins 5-12, or db0-db7, , other (b), 8 elements long, each element either high or low depending on bits of byte.
so here's wrote:
code: [select]
int a[] = {5, 6, 7, 8, 9, 10, 11, 12}; //add these 2 arrays other variables in beginning of sketch
int b[7];
/* here's setup commands go, i'm leaving them out make shorter.
*/
char mychar = 0x41;
int y;
for(y = 0; y < 7; y++)
{
if(bitread(byte(mychar), y) == 1) //by doing these functions can change character want write changing hexadecimal character
{ //assigned variable mychar. byte() function changes byte, , bitread() reads each bit of
b[y] = high; //that byte, , if equal 1 sets cooresponding element of array b high, if equal 0, element
} //of array b becomes low.
else
{
b[y] = low;
}
}
digitalwrite(rs, high);
digitalwrite(rw, low);
delay(50);
digitalwrite(e, high);
delay(50);
digitalwrite(a[0], b[0]); //this saying each pin 5-12 high or low, depending on cooresponding element of array b.
digitalwrite(a[1], b[1]);
digitalwrite(a[2], b[2]);
digitalwrite(a[3], b[3]);
digitalwrite(a[4], b[4]);
digitalwrite(a[5], b[5]);
digitalwrite(a[6], b[6]);
digitalwrite(a[7], b[7]);
delay(50);
digitalwrite(e, low);
delay(50);
*/
so think in loop not working correctly. , help/comments/sugestions/criticism appreciated!
thank you! xd
your b array has 7 entries , not 8 access 8th (b[7]) when using digitalwrite().
this same without intermediate array b. high defined 0x01 , low 0x00.
additionally think have reverse array a, because in example d0 significant bit , set @ a[0] / bitread(..., 0) handles least significant bit.
code: [select]
for(byte nbitno= 0; nbitno < 8; nbitno++)
digitalwrite(a[nbitno], bitread((byte)mychar, nbitno));this same without intermediate array b. high defined 0x01 , low 0x00.
additionally think have reverse array a, because in example d0 significant bit , set @ a[0] / bitread(..., 0) handles least significant bit.
Arduino Forum > Using Arduino > Programming Questions > LCD code help
arduino
Comments
Post a Comment