Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <util/atomic.h>
- #include "lcd.h"
- static inline void LCDInitPorts(void)
- {
- #ifdef LCD4
- //<4-bit interface port initialization>
- DDRB|=_BV(1);
- DDRB|=_BV(0);
- DDRD|=_BV(7);
- DDRD|=_BV(6);
- #else
- //<8-bit interface port initialization>
- #endif
- RS_DDR|=_BV(RS_PIN);
- RW_DDR|=_BV(RW_PIN);
- EN_DDR|=_BV(EN_PIN);
- }
- static inline void LCDBus(unsigned char data)
- {
- #ifdef LCD4
- //<4-bit interface bus output>
- ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
- {
- unsigned char a=PORTD;
- a&=0x3F;
- a|=(data<<6)&0xC0;
- unsigned char b=PORTB;
- b&=0xFC;
- b|=(data>>2)&0x03;
- PORTD=a;
- PORTB=b;
- }
- #else
- //<8-bit interface bus output>
- #endif
- }
- static inline void RS(unsigned char value)
- {
- if(value)RS_PORT|=_BV(RS_PIN);
- else RS_PORT&=~_BV(RS_PIN);
- }
- static inline void RW(unsigned char value)
- {
- if(value)RW_PORT|=_BV(RW_PIN);
- else RW_PORT&=~_BV(RW_PIN);
- }
- static inline void EN(unsigned char value)
- {
- if(value)EN_PORT|=_BV(EN_PIN);
- else EN_PORT&=~_BV(EN_PIN);
- }
- #ifdef LCD4
- static void LCDWriteHalfByte(unsigned char data)
- {
- RS(0);
- RW(0);
- EN(1);
- LCDBus(data&0x0F);
- _delay_us(1);
- EN(0);
- }
- #endif
- static void LCDWriteByte(unsigned char data)
- {
- RW(0);
- #ifdef LCD4
- EN(1);
- LCDBus((data&0xF0)>>4); //high nibble
- _delay_us(1);
- EN(0);
- _delay_us(1);
- EN(1);
- LCDBus(data&0x0F); //low nibble
- _delay_us(1);
- EN(0);
- #else
- EN(1);
- LCDBus(data);
- _delay_us(1);
- EN(0);
- #endif
- }
- void LCDCommand(unsigned char cmd)
- {
- RS(0);
- LCDWriteByte(cmd);
- }
- void LCDData(unsigned char data)
- {
- RS(1);
- LCDWriteByte(data);
- }
- void LCDBusy(void)
- {
- _delay_us(70);
- }
- void LCDInit(void)
- {
- LCDInitPorts();
- #ifdef LCD4
- _delay_ms(20);
- LCDWriteHalfByte(0x03);
- _delay_ms(5);
- LCDWriteHalfByte(0x03);
- _delay_us(120);
- LCDWriteHalfByte(0x03);
- _delay_ms(5);
- LCDWriteHalfByte(0x02);
- _delay_ms(3);
- LCDCommand(0x28);
- _delay_ms(3);
- LCDCommand(0x08);
- _delay_ms(3);
- LCDCommand(0x01);
- _delay_ms(3);
- LCDCommand(0x06);
- _delay_ms(3);
- LCDCommand(0x0c);
- #else
- _delay_ms(20);
- LCDCommand(0x38);
- _delay_ms(5);
- LCDCommand(0x38);
- _delay_us(120);
- LCDCommand(0x38);
- _delay_ms(3);
- LCDCommand(0x38);
- _delay_ms(3);
- LCDCommand(0x08);
- _delay_ms(3);
- LCDCommand(0x01);
- _delay_ms(3);
- LCDCommand(0x06);
- _delay_ms(3);
- LCDCommand(0x0c);
- #endif
- }
- void LCDWriteString(char* str, unsigned char strLen)
- {
- for(unsigned char i=strLen;i;i--)
- {
- LCDBusy();
- LCDData(*str++);
- }
- }
- void LCDWriteStringZ(char* str)
- {
- while(*str)
- {
- LCDBusy();
- LCDData(*str++);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement