Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <io.h>
#include <delay.h>
#include <mega32a.h>
/***************************************
Configure Connections
****************************************/
#define HC595_PORT PORTA
#define HC595_DDR DDRA
#define HC595_DS_POS PORTA.4 //Data pin (DS) pin location
#define HC595_SH_CP_POS PORTA.5 //Shift Clock (SH_CP) pin location
#define HC595_ST_CP_POS PORTA.6 //Store Clock (ST_CP) pin location
//#define LCD_DATA PORTA.4 // LCD data port
#define en PORTA.2 // enable signal
#define rw PORTA.1 // read/write signal
#define rs PORTA.0 // register select signal
/***************************************
Configure Connections ***ENDS***
****************************************/
//Initialize HC595 System
void HC595Init()
{
//Make the Data(DS), Shift clock (SH_CP), Store Clock (ST_CP) lines output
HC595_DDR|=((1<<HC595_SH_CP_POS)|(1<<HC595_ST_CP_POS)|(1<<HC595_DS_POS));
}
//Low level macros to change data (DS)lines
#define HC595DataHigh() (HC595_PORT|=(1<<HC595_DS_POS))
#define HC595DataLow() (HC595_PORT&=(~(1<<HC595_DS_POS)))
//Sends a clock pulse on SH_CP line
void HC595Pulse()
{
//Pulse the Shift Clock
HC595_PORT|=(1<<HC595_SH_CP_POS);//HIGH
delay_ms(20);
HC595_PORT&=(~(1<<HC595_SH_CP_POS));//LOW
}
//Sends a clock pulse on ST_CP line
void HC595Latch()
{
//Pulse the Store Clock
HC595_PORT|=(1<<HC595_ST_CP_POS);//HIGH
delay_ms(10);
HC595_PORT&=(~(1<<HC595_ST_CP_POS));//LOW
delay_ms(10);
}
/*
Main High level function to write a single byte to
Output shift register 74HC595.
Arguments:
single byte to write to the 74HC595 IC
Returns:
NONE
Description:
The byte is serially transfered to 74HC595
and then latched. The byte is then available on
output line Q0 to Q7 of the HC595 IC.
*/
//unsigned int data ;
void HC595Write(unsigned int data)
{
//Send each 8 bits serially
int i=0;
//Order is MSB first
for(i=0;i<8;i++)
{
//Output the data on DS line according to the
//Value of MSB
if(data & 0b10000000)
{
//MSB is 1 so output high
HC595DataHigh();
}
else
{
//MSB is 0 so output high
HC595DataLow();
}
HC595Pulse(); //Pulse the Clock line
delay_ms(10);
data=data<<1; //Now bring next bit at MSB position
}
//Now all 8 bits have been transferred to shift register
//Move them to output latch at one
HC595Latch();
}
/*
Simple Delay function approx 0.5 seconds
*/
void Wait()
{
int i=0;
for(i=0;i<30;i++)
{
delay_ms(10);
}
}
int i=0;
// uint_t data; //Initialize HC595 system
void main()
{
DDRB=0xff;
PORTB=0xff;
DDRA=0xff;
PORTA=0xff;
for(i=0;i<=3;i++) //Startup Led
{
PORTB.4=~PORTB.4;
delay_ms(200);
}
PORTB.4=1;
// PORTA.7=0;
PORTA.1=0;
HC595Init();
HC595Write(0x38); //Write the data to HC595
Wait(); //Wait
rs=0;rw=0;en=1; // RS and RW as LOW and EN as HIGH
delay_ms(10);
rs=0;rw=0;en=0;
delay_ms(20);
while(1)
{
PORTA.7=1; // back light on/off
delay_ms(1000);
rs=1;rw=0;en=1; // RS and RW as LOW and EN as HIGH
delay_ms(10);
HC595Write(0x0f) ; //Write the data to HC595
Wait(); //Wait
rs=1;rw=0;en=0;
delay_ms(20);
// rs=1;rw=0;en=1; // RS and RW as LOW and EN as HIGH
// delay_ms(1000);
// HC595Write(0x14) ; //Write the data to HC595
// Wait(); //Wait
//
// rs=1;rw=0;en=0;
// delay_ms(20);
// rs=1;rw=0;en=1; // RS and RW as LOW and EN as HIGH
// delay_ms(10);
// HC595Write(0xff) ; //Write the data to HC595
// Wait(); //Wait
//
// rs=1;rw=0;en=0;
// delay_ms(20);
PORTA.7=0;
delay_ms(1000);
}
}
This is displaying the 16th user defined graphic... But you haven't defined a graphic..HC595Write(0x0f) ; //Write the data to HC595
Well thak you for your time.this is a part of a larger pcb hardware which some time we have to disable the 595 and also some time need to Read data instead of writing to lcd.I get it, but I think that if a pin (an input pin ofc) is suposed to be always zero it should be tied to ground - it would be more readable and less error-prone.
Please post your current code, what exactly do you want to diplsay as a test and what data, byte by byte are you sending. I suppose you checked that 595 handling code by attaching some test leads or LEDs to the 595 output?
As I said it works ok on the set up but not on the other side.This is displaying the 16th user defined graphic... But you haven't defined a graphic..
Try HC595Write('A') ; and see if something happens..
Explain!! Do you mean the commands work but not the data?... If so how do you know??As I said it works ok on the set up but not on the other side.
Both command and data work.The only (or so far only) bug is that it would not go to the second line (0xC0) and there are one unwanted space between the characters.Explain!! Do you mean the commands work but not the data?... If so how do you know??
The code you posted will not show anything even if the LCD somehow boots to 8 bit...
There is another command in the set up that turns the display on/off , writing 0xC0 will turn it on..
the only (or so far only) bug is that it would not go to the second line (0xC0)
RS=0
RW=0
D[7-0]=0x0C
rs=1;rw=0;en=1;//ctrl = (1<<rs)|(0<<rw)|(1<<en); // RW as LOW and RS, EN as HIGH
The command is correct... In his LCDinit we have LCD_cmd(0x0E); which includes the display on...Ok, a bit of theory (please correct me if im wrong here), the command 0xC0 does not clear the display.
The proper byte is 0x0C which tells the display to do theese things: turn on the LCD, cursor off, cursor blink off. To send it, the following pins must have the following states:
#include<xc.h>
#define _XTAL_FREQ 4000000
#pragma config FOSC = INTRCIO
#pragma config MCLRE = OFF
#pragma config WDTE = OFF
#define HC595_PORT GPIO
#define HC595_DDR TRISIO
#define HC595_DS_POS GPIO2 //Data pin (DS) pin location
#define HC595_SH_CP_POS GPIO1 //Shift Clock (SH_CP) pin location
#define HC595_ST_CP_POS GPIO0 //Store Clock (ST_CP) pin location
//#define LCD_DATA PORTA.4 // LCD data port
#define E GPIO4 // enable signal
#define RW GPIO3 // read/write signal
#define RS GPIO5 // register select signal
#define HC595DataHigh() HC595_DS_POS = 1
#define HC595DataLow() HC595_DS_POS =0
void HC595Init()
{
//Make the Data(DS), Shift clock (SH_CP), Store Clock (ST_CP) lines output
HC595_DDR|=0;
}
void HC595Pulse()
{
//Pulse the Shift Clock
HC595_SH_CP_POS=1;//HIGH
__delay_us(20);
HC595_SH_CP_POS=0; //
}
void HC595Latch()
{
//Pulse the Store Clock
HC595_ST_CP_POS = 1;//HIGH
__delay_us(10);
HC595_ST_CP_POS = 0;//LOW
__delay_us(10);
}
void HC595Write(unsigned int data)
{
//Send each 8 bits serially
int i=0;
//Order is MSB first
for(i=0;i<8;i++)
{
//Output the data on DS line according to the
//Value of MSB
if(data & 0b10000000)
{
//MSB is 1 so output high
HC595DataHigh();
}
else
{
//MSB is 0 so output high
HC595DataLow();
}
HC595Pulse(); //Pulse the Clock line
__delay_us(10);
data=data<<1; //Now bring next bit at MSB position
}
//Now all 8 bits have been transferred to shift register
//Move them to output latch at one
HC595Latch();
}
void LCDcmd(unsigned char cmd)
{
RS = 0;
HC595Write(cmd);
E = 1;
__delay_us(100);
E = 0;
}
void LCDdata(unsigned char data)
{
RS = 1;
HC595Write(data);
E=1;
__delay_us(100);
E =0;
}
void LCDinit(void)
{
TRISIO = 0;
__delay_ms(15);
HC595Write(0x33);
RS = 0;
E = 1;
__delay_ms(5);
E = 0;
__delay_ms(5);
E = 1;
__delay_ms(5);
E = 0;
LCDcmd(0x38);
__delay_ms(5);
LCDcmd(0x38);
LCDcmd(0xC);
LCDcmd(0x6);
LCDcmd(0x1);
}
void LCDprint(const char* str)
{
while(*str != 0)
LCDdata(*str++);
}
void LCDgoto(char x, char y)
{
char addr = 0x80;
if(y==2) addr = 0xC0;
if(y==3) addr = 0x94;
if(y==4) addr = 0xD4;
addr+=x;
LCDcmd(addr);
}
void main(void)
{
ANSEL = 0;
LCDinit();
__delay_ms(10);
LCDprint("Hello World");
LCDgoto(0,2);
LCDprint("Serial LCD driver");
while(1);
}