Skip to content

Commit

Permalink
first version of project11
Browse files Browse the repository at this point in the history
first iteration of project11 with first version of the various
source files to access wiringPi functionality for digitalWrite()
and digitalRead() of pins as well as 1602 LCD screen interface.
  • Loading branch information
RichardChambers committed Jan 6, 2018
1 parent 0eea48f commit 298c06d
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 0 deletions.
136 changes: 136 additions & 0 deletions project11/lcd1602.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/********************************************************************************************
*Filename : i2c1602_lcd.c
*Description : test iic 1602 lcd
*Author : Alan
*Website : www.osoyoo.com
*Update : 2017/07/03
********************************************************************************************/

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>

static int LCDAddr = 0x27; // I2c LCD address, use sudo i2cdetect -y 1 after hooking up 1602
static int BLEN = 1; // 1--open backlight. 0--close backlight
static int fd; // I2C device linux file descriptor

static void sendNibble (int bl, 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 |= (bl) ? 0x08 : 0; // set backlight on/off value
buf |= 0x04; // EN = 1, RW = 0, RS = 0
buf |= (rw) ? 0x02 : 0; // set RW value
buf |= (rs) ? 0x01 : 0; // set RS value
wiringPiI2CWrite (fd, buf);

delay (2);

buf &= 0xFB; // Make EN = 0 to turn off latch
wiringPiI2CWrite (fd, buf);
}

//send command to lcd
static void send_command(int comm)
{
// Send upper nibble, bit 7-4 firstly
sendNibble (BLEN, 0, 0, comm >> 4);

// Send lower nibble, bit 3-0 secondly
sendNibble (BLEN, 0, 0, comm);
}

//send data (text character) to lcd
static void send_data(int data){
// Send upper nibble, bit 7-4 firstly
sendNibble (BLEN, 1, 0, data >> 4);

// Send lower nibble, bit 3-0 secondly
sendNibble (BLEN, 1, 0, data);
}

//clear screen
void lcd1602Clear(void)
{
send_command (0x01); //clear display, set cursor to home position
}

//Print the message on the lcd
//initialize the lcd
static 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
delay (5);

send_command (0x28); // 4 bit mode, 2 Lines and 5x7 pixels character size
delay (5);

send_command (0x0C); // Enable display without cursor
delay (5);

lcd1602Clear ();
wiringPiI2CWrite (fd, 0x08); // turn on backlight of LCD
}

void lcd1602Write (int xCol, int yRow, const char data[])
{
int len = strlen (data);

// 16 columns per row
if (xCol < 0) xCol = 0;
if (xCol > 15) xCol = 15;

// only two rows or lines, row 0 and row 1
if (yRow < 0) yRow = 0;
if (yRow > 1) yRow = 1;

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

int i;
for (i = 0; i < len; i++){
send_data (data[i]);
}
}

void lcd1602Writef (int xCol, int yRow, const char *format, ... )
{
char buff[256];

va_list args;
va_start (args, format);
vsprintf (buff, format, args);
va_end (args);

lcd1602Write (xCol, yRow, buff);
}

int lcd1602Open(int lcdAddr)
{

//init I2C
fd = wiringPiI2CSetup (lcdAddr);
if (fd < 0) {
return 1;
}

init1602 ();

return 0;
}

int lcd1602Close (void)
{
lcd1602Clear ();
close (fd);
}
8 changes: 8 additions & 0 deletions project11/lcd1602.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

//clear screen
void lcd1602Clear(void);
void lcd1602Write (int xCol, int yRow, const char data[]);
void lcd1602Writef (int xCol, int yRow, const char *format, ... );
int lcd1602Open(int lcdAddr);
int lcd1602Close(void);

49 changes: 49 additions & 0 deletions project11/pinreadwrite.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#include <wiringPi.h>

#include "pinreadwrite.h"

void pinSetupWrite (int iPin, int iLevel)
{
pinMode (iPin, OUTPUT); // set the pin function
digitalWrite (iPin, iLevel); // make sure the buzzer is off
}

void pinWriteHiLo (int iPin, int iCount, int iDelayHigh, int iDelayLow)
{
for ( ; iCount > 0; iCount--) {
digitalWrite (iPin, HIGH);
delay (iDelayHigh);
digitalWrite (iPin, LOW);
delay (iDelayLow);
}
digitalWrite (iPin, LOW);
}

void pinWriteLoHi (int iPin, int iCount, int iDelayLow, int iDelayHigh)
{
for ( ; iCount > 0; iCount--) {
digitalWrite (iPin, LOW);
delay (iDelayHigh);
digitalWrite (iPin, HIGH);
delay (iDelayLow);
}
digitalWrite (iPin, HIGH);
}


int pinRead (int iPin)
{
return digitalRead (iPin);
}

void pinSetupRead (int iPin)
{
pinMode (iPin, INPUT); // set the pin function
}

void pinCleanup (int iPin)
{
pinMode (iPin, INPUT); // set the pin function
digitalWrite (iPin, LOW); // make sure the buzzer is off
}
26 changes: 26 additions & 0 deletions project11/pinreadwrite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// functions to set a pin to INPUT or to OUTPUT and to set
// the output level, HIGH or LOW.

// set the specified pin as an OUTPUT pin for writing and initialize
// the pin to the specified level, HIGH or LOW, for its initial conditions.
void pinSetupWrite (int iPin, int iLevel);

// for the number of times specified by the value of iCount, vary
// the signal level of the specified pin.
// These two functions have a loop where the output level of the pin
// is set then a delay then the output level of the pin is set followed
// by another delay.
//
// Output level is: HIGH, delay, LOW, delay.
// pin is left at LOW when function returns.
void pinWriteHiLo (int iPin, int iCount, int iDelayHigh, int iDelayLow);

// Output level is: LOW, delay, HIGH, delay.
// pin is left at HIGH when function returns.
void pinWriteLoHi (int iPin, int iCount, int iDelayLow, int iDelayHigh);

void pinSetupRead (int iPin);
int pinRead (int iPin);

void pinCleanup (int iPin);
43 changes: 43 additions & 0 deletions project11/tiltbuzz_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>

#include "lcd1602.h"
#include "pinreadwrite.h"

int main (int argc, char *argv[])
{
// BUZZPIN is wiringPi Pin #1 or physical pin #12 or GPIO #18
int BUZZPIN = 0;

if (wiringPiSetup() == -1) {
printf ("Setup wiringPi Failed!\n");
return -1;
}

lcd1602Open (0x27);

pinSetupWrite (BUZZPIN, LOW);

lcd1602Write (0, 0, "Welcome tilter");
delay (2000);

lcd1602Clear ();

int iCount = 20;
for ( ; iCount > 0; iCount--) {
int iDelay = ((iCount % 5) + 1) * 400;

lcd1602Writef (0, 0, "tiltbuzz_1 %2.2d", iCount);
pinWriteHiLo (BUZZPIN, 1, iDelay, iDelay);
}

// clean up the GPIO resources we have been using.
// make sure that things are back to a good state before exit.
pinCleanup (BUZZPIN);
lcd1602Close();

delay (500);
return 0;
}

0 comments on commit 298c06d

Please sign in to comment.