Mindaugasu
New Member
The idea is to convert serial data coming to LCD using shift register 74HC164.
My routine tryout:
LIkely signals are OK by LCD giving me strange results few messy characters instead of one desired. I cant see where ismy mistake...
My routine tryout:
Code:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/delay.h>
#include <avr/iom8.h>
#define freq 8000000
#define datapin 0 // define datapin from atmega8 to 74HC164
#define clockpin 1 // define clock pin from atmega8 to 74HC164
#define register r16
/*
LCD pins connected to 74hc164:
Q0 ->LCD_D4
Q1 ->LCD_D5
Q2 ->LCD_D6
Q3 ->LCD_D7
Q4 ->NC
Q5 ->NC
Q6 ->LCD_RS
Q7 ->LCD_E
*/
void atmega8init(void)
{
DDRC=(1<<DDC0)|(1<<DDC1);
}
void sendByteToLCD(uint8_t LCDdata)
{
uint8_t i;
PORTC &= ~(1<<0); // sets datapin to output a LOW
for (i=0;i<=8;i++) //clear shift 74HC164 register
{
PORTC |= (1<<1); // sets clockpin to output a HIGH
PORTC &= ~(1<<1); // sets clockpin to output a LOW
}
for (i=0;i<=7;i++) //write 8bit LCDdata to 74HC164 register
{
if (LCDdata&&0x01)
{
PORTC |= (1<<0); // sets datapin to output a HIGH
PORTC |= (1<<1); // sets clockpin to output a HIGH
_delay_us(100);
PORTC &= ~(1<<1); // sets clockpin to output a LOW
PORTC &= ~(1<<0); // sets datapin to output a LOW
}
else
{
PORTC &= ~(1<<0); // sets datapin to output a LOW
PORTC |= (1<<1); // sets clockpin to output a HIGH
_delay_us(100);
PORTC &= ~(1<<1); // sets clockpin to output a LOW
}
LCDdata=LCDdata>>1;
if (i==7)
{
PORTC &= ~(1<<1);
PORTC |= (1<<0);
PORTC |= (1<<0);
}
}
}
void sendChar(uint8_t topack) //forms data ready to send to 74HC164
{
uint8_t packed;
/*first 4 bits of byte contains LCD D4-D7 bits
4 and 5 bits are not used
bit 6 is for LCD RS
bit 7 is for LCD E
*/
packed=(topack&0xF0)|0x03;
sendByteToLCD(packed);
_delay_us(100);
packed=(topack<<4)|0x03;
sendByteToLCD(packed);
_delay_us(100);
}
void sendCommand(uint8_t topack) //forms data ready to send to 74HC164
{
uint8_t packed;
/*first 4 bits of byte contains LCD D4-D7 bits
4 and 5 bits are not used
bit 6 is for LCD RS
bit 7 is for LCD E
*/
packed=(topack&0xF0)|0x01;
sendByteToLCD(packed);
_delay_us(100);
packed=(topack<<4)|0x01;
sendByteToLCD(packed);
_delay_us(100);
}
void LCDinit(void)
{
_delay_ms(30); //wait for poverup
sendByteToLCD(0x31);//1
_delay_ms(10);
sendByteToLCD(0x31);//2
_delay_ms(10);
sendByteToLCD(0x31);//3
_delay_ms(20);
sendByteToLCD(0x21);//sends 00100001 to enable 4 bit mode
_delay_ms(15);
sendByteToLCD(0x21);
_delay_ms(5);
sendByteToLCD(0x01);
_delay_ms(5);
sendByteToLCD(0x11);//clear display
_delay_ms(5);
sendByteToLCD(0x01);
_delay_ms(5);
}
int main (void)
{
atmega8init();
LCDinit();
sendChar(0x49);
/* loop forever, the interrupts are doing the rest */
for (;;) /* Note [6] */
return (0);
}