/* io.c * * Copyright (c) 2003-2004 Susan Cotterell. * Permission to copy is granted provided that this header remains * intact. This software is provided with no warranties. * * Version : 1.0 * * 4-Bit LCD Interface * */ /*-------------------------------------------------------------------------*/ #include #include "io.h" #include "delay.h" /*-------------------------------------------------------------------------*/ #define DATA_BUS PORTB #define RS RA2 #define E RA3 #define RS_BIT 2 #define E_BIT 3 /*-------------------------------------------------------------------------*/ unsigned char tempA = 0x00; /*-------------------------------------------------------------------------*/ void EnableLCD(void) { bitclr(tempA, PORTA, RS_BIT); DelayUs(1); //RS = 0 bitset(tempA, PORTA, E_BIT); DelayUs(1); //E = 1 bitclr(tempA, PORTA, E_BIT); DelayUs(1); //E = 0 } /*-------------------------------------------------------------------------*/ void EnableLCD2(void) { bitset(tempA, PORTA, RS_BIT); DelayUs(1); bitset(tempA, PORTA, E_BIT); DelayUs(1); bitclr(tempA, PORTA, E_BIT); DelayUs(1); } /*-------------------------------------------------------------------------*/ void InitIO(void) { DATA_BUS = (DATA_BUS & 0x0F) | 0x30; EnableLCD(); DATA_BUS = (DATA_BUS & 0x0F) | 0x30; EnableLCD(); DelayMs(5); DATA_BUS = (DATA_BUS & 0x0F) | 0x30; EnableLCD(); DATA_BUS = (DATA_BUS & 0x0F) | 0x20; EnableLCD(); // set 4-bit interface DelayUs(100); DATA_BUS = (DATA_BUS & 0x0F) | 0x20; EnableLCD(); // FUNCTION SET DATA_BUS = (DATA_BUS & 0x0F) | 0x80; EnableLCD(); // FUNCTION SET DelayUs(100); DATA_BUS = (DATA_BUS & 0x0F) | 0x00; EnableLCD(); // DISPLAY OFF DATA_BUS = (DATA_BUS & 0x0F) | 0x80; EnableLCD(); // DISPLAY OFF DelayUs(100); DATA_BUS = (DATA_BUS & 0x0F) | 0x00; EnableLCD(); // CLEAR SCREEN DATA_BUS = (DATA_BUS & 0x0F) | 0x10; EnableLCD(); // CLEAR SCREEN DelayMs(2); DATA_BUS = (DATA_BUS & 0x0F) | 0x00; EnableLCD(); // ENTRY MODE SET DATA_BUS = (DATA_BUS & 0x0F) | 0x60; EnableLCD(); // ENTRY MODE SET DelayUs(40); DATA_BUS = (DATA_BUS & 0x0F) | 0x00; EnableLCD(); // DISPLAY ON DATA_BUS = (DATA_BUS & 0x0F) | 0xD0; EnableLCD(); // DISPLAY ON DelayUs(40); } /*-------------------------------------------------------------------------*/ void ClearScreen(void) { DATA_BUS= (DATA_BUS & 0x0F) | 0x00; EnableLCD(); // CLEAR SCREEN DATA_BUS= (DATA_BUS & 0x0F) | 0x10; EnableLCD(); // CLEAR SCREEN DelayMs(2); } /*-------------------------------------------------------------------------*/ void GotoXY(unsigned char r, unsigned char c) { DATA_BUS= (DATA_BUS & 0x0F) | 0x00; EnableLCD(); DATA_BUS= (DATA_BUS & 0x0F) | 0x20; EnableLCD(); DelayMs(2); for(r=r*40+c, c=0; c