Trying to run a 16x2 through a shift register with wiringPi with no idea how to combine the two sets of functions.
All help appreciated
Martwana wrote:Anyone had any success in combining these two?
Trying to run a 16x2 through a shift register with wiringPi with no idea how to combine the two sets of functions.
All help appreciated
Martwana wrote:Thanks Gordon.
After looking at the lcd.c code, i can semi-understand it, but I need more help.
Read this and please correct me if you can.
In lcd.c, when initializing the lcd, in 4 bit mode 'func = LCD_FUNC | LCD_FUNC_DL', which according to the #defines is 0x20 and 0x10.
| is a bitwise OR, which when done is,
00100000 OR
00010000 =
00110000
The next line bitwise right shifts 00110000>>4 which i calculate as 00000011 ? Correct?
Now, if that is correct, 00000011 is sent to put4command, which takes rsPin low, then writes to each data pin.
It writes 'command & 1' which i calculate as
00000011 AND
00000001 =
00000001 correct?
Then, after doing each one, bitwise right shifts the 'command' which i think would change 00000001 to 10000000, 11000000 and 11100000. Correct?
I dont understand how to write these values out using shiftOut() because if I just ran the binary out to the shift register, it would send the binary to each pin of the register rather than sending it all to a specific pin.
I am totally new to C, so learning as I go, and I've never had any hardware interaction experience, before.
I CAN get the lcd working fine directly with the GPIO but I'm really struggling where to get started here.
Help me please.
writeDisplay(){
// Set lcd pins on the register
int d4 = 1;
int d5 = 0;
int d6 = 1;
int d7 = 1;
int e = 1;
int rs = 1;
// Put pins in array, with 2 extra blank values for the 2 unused pins on register
int displayPins[8] = {d4, d5, d6, d7, e, rs, 0, 0};
// SHIFT OUT TO REGISTER
clear();
// Convert the binary to a decimal value to shiftOut
for (i=7;i>=0;--i){
if (displayPins[i] == 1){
digitalWrite(inputB, HIGH);
} else {
digitalWrite(inputB, LOW);
}
tick();
}
delay(10000);
}
int main(){
setupPins();
writeDisplay();
}
#include <time.h>
#include <wiringPi.h>
#include <wiringShift.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int clockPin=14; #23
int clearPin=10; #24
int dataPin=11; #26
int i;
int LCD_CHR=16;
int LCD_CMD=0;
int LCD_ENB=32;
int DELAY=500;
int LCD_LN1=0x80;
int LCD_LN2=0xC0;
int LEFT=0;
int CENTER=1;
int RIGHT=2;
void setup(){
pinMode(clockPin,OUTPUT);
pinMode(clearPin,OUTPUT);
pinMode(dataPin,OUTPUT);
digitalWrite(clearPin,HIGH);
digitalWrite(clockPin,LOW);
digitalWrite(dataPin,LOW);
}
void clear(){
digitalWrite(clearPin,LOW);
digitalWrite(clearPin,HIGH);
}
void lcd_byte(value,mode){
int highBits=(value >>4)+mode+LCD_ENB;
int lowBits=(value & 0xF)+mode+LCD_ENB;
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,highBits);
usleep(DELAY);
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,lowBits);
usleep(DELAY);
clear();
usleep(DELAY);
}
void lcd_init(){
lcd_byte(0x33,LCD_CMD);
lcd_byte(0x32,LCD_CMD);
lcd_byte(0x28,LCD_CMD);
lcd_byte(0x0C,LCD_CMD);
lcd_byte(0x06,LCD_CMD);
lcd_byte(0x01,LCD_CMD);
}
void lcd_string(char displayStr[50], int just){
int extra=0;
if (just==CENTER) {
extra=(40-strlen(displayStr))/2;
} else if (just==RIGHT) {
extra=(40-strlen(displayStr));
}
for(i=1;i<=extra;i++){
lcd_byte(32,LCD_CHR);
}
for(i=0;i<strlen(displayStr);i++){
lcd_byte(displayStr[i],LCD_CHR);
//printf("%d ",displayStr[i]);
}
}
int main(){
if (wiringPiSetup()==-1){
exit(1);
}
char timeStr[40];
setup();
clear();
lcd_init();
time_t rawtime;
struct tm * timeinfo;
while (1==1){
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(timeStr,40,"%I:%M:%S %p",timeinfo);
lcd_byte(LCD_LN1,LCD_CMD);
lcd_string(timeStr,CENTER);
strftime(timeStr,40,"%B %d, %Y",timeinfo);
lcd_byte(LCD_LN2,LCD_CMD);
lcd_string(timeStr,CENTER);
}
}
rickseiden wrote:Here's my code for running an lcd display off a shift register. I hook the data pins of the lcd to the first four outputs of the shift registers, the register select pin to the fifth output, and the enable pin to the sixth output. That way enable is triggered only when everything else is already setup (more or less--it works, leave me alone!).
- Code: Select all
#include <time.h>
#include <wiringPi.h>
#include <wiringShift.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int clockPin=14; #23
int clearPin=10; #24
int dataPin=11; #26
int i;
int LCD_CHR=16;
int LCD_CMD=0;
int LCD_ENB=32;
int DELAY=500;
int LCD_LN1=0x80;
int LCD_LN2=0xC0;
int LEFT=0;
int CENTER=1;
int RIGHT=2;
void setup(){
pinMode(clockPin,OUTPUT);
pinMode(clearPin,OUTPUT);
pinMode(dataPin,OUTPUT);
digitalWrite(clearPin,HIGH);
digitalWrite(clockPin,LOW);
digitalWrite(dataPin,LOW);
}
void clear(){
digitalWrite(clearPin,LOW);
digitalWrite(clearPin,HIGH);
}
void lcd_byte(value,mode){
int highBits=(value >>4)+mode+LCD_ENB;
int lowBits=(value & 0xF)+mode+LCD_ENB;
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,highBits);
usleep(DELAY);
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,lowBits);
usleep(DELAY);
clear();
usleep(DELAY);
}
void lcd_init(){
lcd_byte(0x33,LCD_CMD);
lcd_byte(0x32,LCD_CMD);
lcd_byte(0x28,LCD_CMD);
lcd_byte(0x0C,LCD_CMD);
lcd_byte(0x06,LCD_CMD);
lcd_byte(0x01,LCD_CMD);
}
void lcd_string(char displayStr[50], int just){
int extra=0;
if (just==CENTER) {
extra=(40-strlen(displayStr))/2;
} else if (just==RIGHT) {
extra=(40-strlen(displayStr));
}
for(i=1;i<=extra;i++){
lcd_byte(32,LCD_CHR);
}
for(i=0;i<strlen(displayStr);i++){
lcd_byte(displayStr[i],LCD_CHR);
//printf("%d ",displayStr[i]);
}
}
int main(){
if (wiringPiSetup()==-1){
exit(1);
}
char timeStr[40];
setup();
clear();
lcd_init();
time_t rawtime;
struct tm * timeinfo;
while (1==1){
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(timeStr,40,"%I:%M:%S %p",timeinfo);
lcd_byte(LCD_LN1,LCD_CMD);
lcd_string(timeStr,CENTER);
strftime(timeStr,40,"%B %d, %Y",timeinfo);
lcd_byte(LCD_LN2,LCD_CMD);
lcd_string(timeStr,CENTER);
}
}
tick(){
digitalWrite(clockPin, HIGH);
usleep(DELAY);
digitalWrite(clockPin, LOW);
}
#include <time.h>
#include <wiringPi.h>
#include <wiringShift.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int clockPin=14; //23
int clearPin=10; //24
int dataPin=11; //26
int i;
int LCD_CHR=16;
int LCD_CMD=0;
int LCD_ENB=32;
int DELAY=500;
int LCD_LN1=0x80;
int LCD_LN2=0xC0;
int LEFT=0;
int CENTER=1;
int RIGHT=2;
void setup(){
pinMode(clockPin,OUTPUT);
pinMode(clearPin,OUTPUT);
pinMode(dataPin,OUTPUT);
digitalWrite(clearPin,HIGH);
digitalWrite(clockPin,LOW);
digitalWrite(dataPin,LOW);
}
void clear(){
digitalWrite(clearPin,LOW);
digitalWrite(clearPin,HIGH);
}
void lcd_byte(value,mode){
int highBits=(value >>4)+mode+LCD_ENB;
int lowBits=(value & 0xF)+mode+LCD_ENB;
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,highBits);
usleep(DELAY);
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,lowBits);
usleep(DELAY);
clear();
usleep(DELAY);
}
void lcd_init(){
lcd_byte(0x33,LCD_CMD);
lcd_byte(0x32,LCD_CMD);
lcd_byte(0x28,LCD_CMD);
lcd_byte(0x0C,LCD_CMD);
lcd_byte(0x06,LCD_CMD);
lcd_byte(0x01,LCD_CMD);
}
void lcd_string(char displayStr[50], int just){
int extra=0;
if (just==CENTER) {
extra=(40-strlen(displayStr))/2;
} else if (just==RIGHT) {
extra=(40-strlen(displayStr));
}
for(i=1;i<=extra;i++){
lcd_byte(32,LCD_CHR);
}
for(i=0;i<strlen(displayStr);i++){
lcd_byte(displayStr[i],LCD_CHR);
//printf("%d ",displayStr[i]);
}
}
int main(){
if (wiringPiSetup()==-1){
exit(1);
}
char timeStr[40];
setup();
clear();
lcd_init();
time_t rawtime;
struct tm * timeinfo;
while (1==1){
lcd_byte(LCD_LN1,LCD_CMD);
lcd_string("HELLO",LEFT);
lcd_byte(LCD_LN2,LCD_CMD);
lcd_string("WORLD!",RIGHT);
}
}
gcc helloWorld.c -o helloWorld -L/usr/local/lib -lwiringpirickseiden wrote:The code doesn't compile because I used Python comments to indicate the GPIO physical pin numbers. The code should read:
- Code: Select all
#include <time.h>
#include <wiringPi.h>
#include <wiringShift.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int clockPin=14; //23
int clearPin=10; //24
int dataPin=11; //26
int i;
int LCD_CHR=16;
int LCD_CMD=0;
int LCD_ENB=32;
int DELAY=500;
int LCD_LN1=0x80;
int LCD_LN2=0xC0;
int LEFT=0;
int CENTER=1;
int RIGHT=2;
void setup(){
pinMode(clockPin,OUTPUT);
pinMode(clearPin,OUTPUT);
pinMode(dataPin,OUTPUT);
digitalWrite(clearPin,HIGH);
digitalWrite(clockPin,LOW);
digitalWrite(dataPin,LOW);
}
void clear(){
digitalWrite(clearPin,LOW);
digitalWrite(clearPin,HIGH);
}
void lcd_byte(value,mode){
int highBits=(value >>4)+mode+LCD_ENB;
int lowBits=(value & 0xF)+mode+LCD_ENB;
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,highBits);
usleep(DELAY);
clear();
usleep(DELAY);
shiftOut(dataPin,clockPin,MSBFIRST,lowBits);
usleep(DELAY);
clear();
usleep(DELAY);
}
void lcd_init(){
lcd_byte(0x33,LCD_CMD);
lcd_byte(0x32,LCD_CMD);
lcd_byte(0x28,LCD_CMD);
lcd_byte(0x0C,LCD_CMD);
lcd_byte(0x06,LCD_CMD);
lcd_byte(0x01,LCD_CMD);
}
void lcd_string(char displayStr[50], int just){
int extra=0;
if (just==CENTER) {
extra=(40-strlen(displayStr))/2;
} else if (just==RIGHT) {
extra=(40-strlen(displayStr));
}
for(i=1;i<=extra;i++){
lcd_byte(32,LCD_CHR);
}
for(i=0;i<strlen(displayStr);i++){
lcd_byte(displayStr[i],LCD_CHR);
//printf("%d ",displayStr[i]);
}
}
int main(){
if (wiringPiSetup()==-1){
exit(1);
}
char timeStr[40];
setup();
clear();
lcd_init();
time_t rawtime;
struct tm * timeinfo;
while (1==1){
lcd_byte(LCD_LN1,LCD_CMD);
lcd_string("HELLO",LEFT);
lcd_byte(LCD_LN2,LCD_CMD);
lcd_string("WORLD!",RIGHT);
}
}
I've also updated it to do just HELLO on one line and WORLD! on the other.
Save it to helloWolrd.c, then compile using
- Code: Select all
gcc helloWorld.c -o helloWorld -L/usr/local/lib -lwiringpi
Martwana wrote:It had loads of random symbols flashing on the screen.
Heres a video of it, any ideas?
http://www.youtube.com/watch?v=RzwllKSmu2I
Martwana wrote:NopeStill no joy.
The leds all flicker as they run high and low.
Heres a few pics of the circuit, but i think its right. I've replaced the shift register since i ordered 3 but still no joy.
http://postimage.org/gallery/10tuftpi/f08ab16e/
Martwana wrote:I set the pins in the code correctly.
The photos you seen were like my 10th attempt at wiring it. is it maybe the type of shift register?
It's a 74HC164N register. I dont know if different kinds wont work with your code.
I have no idea where to go from here, I DID want to end up using the shift register to reduce the GPIO pins used for the display, since I was going to have another few devices hooked up, but maybe ill need to rethink it all.
#include <time.h>
#include <wiringPi.h>
#include <wiringShift.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int clockPin=10;
int clearPin=11;
int dataPin=6;
int i;
int LCD_CHR=16;
int LCD_CMD=0;
int LCD_ENB=32;
int DELAY=1;
int LCD_LN1=0x80;
int LCD_LN2=0xC0;
int LEFT=0;
int CENTER=1;
int RIGHT=2;
void setup(){
pinMode(clockPin,OUTPUT);
pinMode(clearPin,OUTPUT);
pinMode(dataPin,OUTPUT);
digitalWrite(clearPin,HIGH);
digitalWrite(clockPin,LOW);
digitalWrite(dataPin,LOW);
}
void clear(){
digitalWrite(clearPin,LOW);
delay(1);
digitalWrite(clearPin,HIGH);
}
newShiftOut(int dPin, int cPin, uint8_t val){
int8_t i;
for(i = 7; i >= 0; --i){
digitalWrite(dPin, val & (1<<i));
digitalWrite(cPin, HIGH);
delay(1);
digitalWrite(cPin, LOW);
}
}
void lcd_init(){
lcd_byte(0x33,LCD_CMD);
lcd_byte(0x32,LCD_CMD);
lcd_byte(0x28,LCD_CMD);
lcd_byte(0x0C,LCD_CMD);
lcd_byte(0x06,LCD_CMD);
lcd_byte(0x01,LCD_CMD);
}
void lcd_string(char displayStr[32], int just){
int extra=0;
if (just==CENTER) {
extra=(16-strlen(displayStr))/2;
} else if (just==RIGHT) {
extra=(16-strlen(displayStr));
}
for(i=1;i<=extra;i++){
lcd_byte(16,LCD_CHR);
}
for(i=0;i<strlen(displayStr);i++){
lcd_byte(displayStr[i],LCD_CHR);
//printf("%d ",displayStr[i]);
}
}
int main(){
if (wiringPiSetup()==-1){
exit(1);
}
char timeStr[16];
setup();
clear();
lcd_init();
time_t rawtime;
struct tm * timeinfo;
while (1==1){
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(timeStr,16,"%H:%M:%S %p",timeinfo);
lcd_byte(LCD_LN1,LCD_CMD);
lcd_string(timeStr,CENTER);
strftime(timeStr,16,"%x",timeinfo);
lcd_byte(LCD_LN2,LCD_CMD);
lcd_string(timeStr,CENTER);
}
}
Martwana wrote:SOLVED. ...
One last thanks to rickseiden for the original code and your help!