Full Version : 4-Bit LCD Routine (GCC)
avr >>C PROGRAMMING (AVR) >>4-Bit LCD Routine (GCC)


AVR_Admin- 04-24-2006
Should any other beginners have the same problem, this code works for me.

CODE

void main(void)
  {
  lcdsetup();
  interruptsetup();
  lcddisplay();
  while(1);
  }                                    

void interruptsetup(void)
  {
  DDRC=0xff;
  DDRD&=~(1<<PIND2);
   
  MCUCR=0x03;
  GICR|=(1<<INT0);
  sei();
  }

void lcdsetup(void)
  {
  DDRC=0xff;
  PORTC=0x00;
  DDRA=0xff;
   
  _delay_ms(20);
  PORTA=0x30;  //Initialization
  lcdenable();
   
  _delay_ms(5);
  PORTA=0x30;  //Initialization
  lcdenable();
   
  _delay_ms(1);
  PORTA=0x30; //Initialization
  lcdenable();
   
  PORTA=0x20; //Initialization
  lcdenable();
   
  cntrl=0b00101000;
  lcdcontrolsend(cntrl);
  cntrl=0b00001000;
  lcdcontrolsend(cntrl);
  cntrl=0b00001111;
  lcdcontrolsend(cntrl);
  cntrl=0b00000001;
  lcdcontrolsend(cntrl);
  cntrl=0b00000110;
  lcdcontrolsend(cntrl);
  }

void lcdenable(void)
  {
  PORTA &= ~(1 << PINA2);
  PORTA |= (1 << PINA2);
  _delay_ms(0.1);
  PORTA &= ~(1 << PINA2);
  }

void lcddatasend(int data)
  {
  PORTA |= (1 << PINA3);
   
  _delay_ms(0.07);
  PORTA = (PORTA&0x0f)|(data&0xf0);
  lcdenable();
   
  _delay_ms(0.07);
  PORTA = (PORTA&0x0f)|(data<<4);
  lcdenable();
  }

void lcdcontrolsend(int control)
  {
  PORTA &= ~(1<<PINA3);
   
  _delay_ms(0.07);
  PORTA = (PORTA&0x0f)|(control&0xf0);
  lcdenable();
   
  _delay_ms(0.07);
  PORTA = (PORTA&0x0f)|(control<<4);
  lcdenable();
  }




Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.