Difference between revisions of "LCD"

From wikipost
Jump to navigationJump to search
 
(No difference)

Latest revision as of 14:15, 26 January 2013

This page shows the code for directly accessing an attached LCD device (here the 2x16 character lcd device from Jaycar).


void Write_LCD_Nibble(unsigned short N, int RS)
{

  digitalWrite(STR,0);

  // ****** Write RS *********
  digitalWrite(CLK,0);
  digitalWrite(Dout,RS);  // Data_Pin
  digitalWrite(CLK,1);
  digitalWrite(CLK,0);


  // Shift in 4 bits
  int t = 0;
  int Flag = 0;
  int Mask = 8;

  for (t=0; t<4; t++)
  {
    Flag = N & Mask;
    if(Flag==0)
    {
      digitalWrite(Dout,0);  // Data_Pin     
    } else {
      digitalWrite(Dout,1);  // Data_Pin
    }
 
    digitalWrite(CLK,1);
    digitalWrite(CLK,0);

    Mask = Mask >> 1;
  }

  // One more clock because SC and ST clks are tied
  digitalWrite(CLK,1);
  digitalWrite(CLK,0);
  digitalWrite(Dout,0);  // Data_Pin
  digitalWrite(STR,1);   // Enable_Pin  (to LCD)
  digitalWrite(STR,0);   // Enable_Pin  (to LCD)

}


void Write_LCD_Data(unsigned short D)
{
  int RS = 1; // It is Data, not command
  int Low_Nibble = D & 15;
  int High_Nibble = D/16;
  Write_LCD_Nibble(High_Nibble,RS);
  Write_LCD_Nibble(Low_Nibble,RS);
}

void Write_LCD_Cmd(unsigned short C)
{

  int RS = 0; // It is command, not data
  int Low_Nibble = C & 15;
  int High_Nibble = C/16;
  Write_LCD_Nibble(High_Nibble,RS);
  Write_LCD_Nibble(Low_Nibble,RS);
}



void initialise_LCD() 
{

  pinMode(CLK,0);
  pinMode(STR,0);
  pinMode(Dout,0);


  _delay_ms(100); 

  Write_LCD_Cmd(0x03); // function set
  _delay_ms(50);

  Write_LCD_Cmd(0x03);
  _delay_ms(50);

  Write_LCD_Cmd(0x03);
  _delay_ms(50);

  Write_LCD_Cmd(0x02);
  _delay_ms(50);

  Write_LCD_Cmd(0x2C); // Display ON, No cursors
  _delay_ms(50);

  Write_LCD_Cmd(0x06); // Entry mode- Auto-increment, No Display shifting
  _delay_ms(50);

  Write_LCD_Cmd(0x0E);
  _delay_ms(50);

  Write_LCD_Cmd(0x01);
  _delay_ms(50);

  Write_LCD_Cmd(0x80);
  _delay_ms(50);


}




void Position_LCD(unsigned short x, unsigned short y)
{
  int temp = 127 + y;

  if (x == 2) 
  {
    temp = temp + 64;
  }

  Write_LCD_Cmd(temp);
}
 

void Clear_LCD()
{
   Write_LCD_Cmd(0x01);  // Clear LCD
   _delay_ms(5);

}

void Write_LCD_Text(char *StrData)
{

  int p = 0;
  int q = strlen(StrData);
  int temp = 0;

  for (p = 0; p < q; p++)
  {
    temp = StrData[p];
    Write_LCD_Data(temp);
  }
}



void Write_LCD_Number(char number)
{
  // writes a number's digits on the display

  int digits;

  if (number == 0)
  {
    digits = 0;
    int array[0];
    array[0]=0;
    Write_LCD_Data(array[0]+48);

  } else {
 
    // determine the number of digits 
    int digits = (int)(log(number)/log(10))+1;

    // split up the number to an array
    int i = digits - 1;
    int array[digits];
    while (number > 0)
    {
      array[i--] = number % 10;
      number /= 10;  
    }

    // display array on LCD
    for(i = 0; i <= digits-1; i++)
    {
      Write_LCD_Data(array[i]+48);
    }

  }

}