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