Skip to content

Commit

Permalink
added source for lcd project
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardChambers committed Jan 3, 2018
1 parent 37d3b10 commit 950a7e1
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
138 changes: 138 additions & 0 deletions project10/i2c_1602lcd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/********************************************************************************************
*Filename : i2c1602_lcd.c
*Description : test iic 1602 lcd
*Author : Alan
*Website : www.osoyoo.com
*Update : 2017/07/03
********************************************************************************************/
#include <stdio.h>
#include <wiringPi.h>
#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

//writ a word(16 bits) to LCD
void write_word(int data){
int temp = data;
if ( BLEN == 1 )
temp |= 0x08;
else
temp &= 0xF7;
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
write_word(buf);
delay(2);
buf &= 0xFB; // Make EN = 0
write_word(buf);

// 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);
}

//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);

// 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);
}

//initialize the lcd
void init(){
send_command(0x33); // Must initialize to 8-line mode at first
delay(5);
send_command(0x32); // Then initialize to 4-line mode
delay(5);
send_command(0x28); // 2 Lines & 5*7 dots
delay(5);
send_command(0x0C); // Enable display without cursor
delay(5);
send_command(0x01); // Clear Screen
wiringPiI2CWrite(fd, 0x08);
}

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

//Print the message on the lcd
void write(int x, int y, 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;

// Move cursor
addr = 0x80 + 0x40 * y + x;
send_command(addr);

tmp = strlen(data);
for (i = 0; i < tmp; i++){
send_data(data[i]);
}
}

void print_info()
{
printf("\n");
printf("|***************************|\n");
printf("| IIC 1602 LCD test |\n");
printf("| --------------------------|\n");
printf("| | LCD | | Pi |\n");
printf("| --------------------------|\n");
printf("| | GND | connect to | GND |\n");
printf("| | VCC | connect to | 5V |\n");
printf("| | SDA | connect to | SDA.1|\n");
printf("| | SCL | connect to | SCL.1|\n");
printf("| --------------------------|\n");
printf("| OSOYOO|\n");
printf("|***************************|\n");
printf("Program is running...\n");
printf("Press Ctrl+C to end the program\n");
}
int main(){
//init I2C
fd = wiringPiI2CSetup(LCDAddr);
init();
print_info();
write(0, 0, "Hi man.Welcome ");
write(0, 1, "to osoyoo.com");
delay(3000);
clear();
while(1){
write(0,0,"This is Lesson13");
write(0,1,"IIC LCD Test");
delay(1000);
}
return 0;
}
4 changes: 4 additions & 0 deletions project10/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

i2c_1602lcd: i2c_1602lcd.c
cc -o i2c_1602lcd i2c_1602lcd.c -lwiringPi

0 comments on commit 950a7e1

Please sign in to comment.