Skip to content

Commit

Permalink
updated sample source
Browse files Browse the repository at this point in the history
rewrote and simplified logic, added more interesting
text to be displayed which changes, added comments.
  • Loading branch information
RichardChambers committed Jan 3, 2018
1 parent 950a7e1 commit 30d8dab
Showing 1 changed file with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions project10/i2c_1602lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <wiringPiI2C.h>
#include <string.h>

int LCDAddr = 0x3f;//IIc LCD address
int BLEN = 1;//1--open backlight.0--close backlight
int fd;//linux file descriptor

Expand All @@ -24,48 +23,43 @@ void write_word(int data){
wiringPiI2CWrite(fd, temp);
}

//send command to lcd
void send_command(int comm){
int buf;
// Send bit7-4 firstly
buf = comm & 0xF0;
buf |= 0x04; // RS = 0, RW = 0, EN = 1
void sendNibble (int rs, int rw, int data)
{
int buf = (data & 0x0F) << 4; // data nibble in upper nibble of byte

// set the command pins data in the lower nibble of byte
buf |= 0x04; // EN = 1, RW = 0, RS = 0
buf |= (rs) ? 0x01 : 0; // set RS value
buf |= (rw) ? 0x02 : 0; // set RW value
write_word(buf);

delay(2);

buf &= 0xFB; // Make EN = 0
write_word(buf);
}

//send command to lcd
void send_command(int comm){
// Send bit7-4 firstly
sendNibble (0, 0, comm >> 4);

// Send bit3-0 secondly
buf = (comm & 0x0F) << 4;
buf |= 0x04; // RS = 0, RW = 0, EN = 1
write_word(buf);
delay(2);
buf &= 0xFB; // Make EN = 0
write_word(buf);
sendNibble (0, 0, comm);
}

//send data to lcd
void send_data(int data){
int buf;
// Send bit7-4 firstly
buf = data & 0xF0;
buf |= 0x05; // RS = 1, RW = 0, EN = 1
write_word(buf);
delay(2);
buf &= 0xFB; // Make EN = 0
write_word(buf);
sendNibble (1, 0, data >> 4);

// Send bit3-0 secondly
buf = (data & 0x0F) << 4;
buf |= 0x05; // RS = 1, RW = 0, EN = 1
write_word(buf);
delay(2);
buf &= 0xFB; // Make EN = 0
write_word(buf);
sendNibble (1, 0, data);
}

//initialize the lcd
void init(){
void init1602 (void)
{
send_command(0x33); // Must initialize to 8-line mode at first
delay(5);
send_command(0x32); // Then initialize to 4-line mode
Expand All @@ -79,21 +73,24 @@ void init(){
}

//clear screen
void clear(){
void clear1602(void)
{
send_command(0x01); //clear Screen
}

//Print the message on the lcd
void write(int x, int y, char data[]){
void write(int xCol, int yRow, const char data[])
{
int addr, i;
int tmp;
if (x < 0) x = 0;
if (x > 15) x = 15;
if (y < 0) y = 0;
if (y > 1) y = 1;
if (xCol < 0) xCol = 0;
if (xCol > 15) xCol = 15;
if (yRow < 0) yRow = 0;
if (yRow > 1) yRow = 1;

// Move cursor
addr = 0x80 + 0x40 * y + x;
// Move cursor to either line 1 or line 2
// line 1 -> 0x80 + xCol, line 2 -> 0xC0 + xCol
addr = 0x80 + 0x40 * yRow + xCol;
send_command(addr);

tmp = strlen(data);
Expand All @@ -120,19 +117,36 @@ void print_info()
printf("Program is running...\n");
printf("Press Ctrl+C to end the program\n");
}
int main(){

int main(int argc, char *argv[])
{
int LCDAddr = 0x27;//IIc LCD address

//init I2C
fd = wiringPiI2CSetup(LCDAddr);
init();
if (fd < 0) {
printf ("Error: I2C setup for 1602 LCD failed.\n");
return 1;
}

init1602();

print_info();

write(0, 0, "Hi man.Welcome ");
write(0, 1, "to osoyoo.com");
delay(3000);
clear();
clear1602();

int iCount = 0;
while(1){
write(0,0,"This is Lesson13");
write(0,1,"IIC LCD Test");
delay(1000);
char buf[24];

iCount++;
sprintf (buf, "Lesson13 cnt %d", iCount);
write(0, 0, buf);
write(0, 1, "IIC LCD Test");
delay(2000);
}
return 0;
}

0 comments on commit 30d8dab

Please sign in to comment.