EmbDev.net

Forum: µC & Digital Electronics LCD interface with Dev board


Author: Brandon S. (brandon_s)
Posted on:

Rate this post
0 useful
not useful
Hi,

I had this working LCD code that used Port 0. Now I tried to change to 
PORT 1.

Attached is my code. Is there something wrong in my code?

I am sure of my hardware. Checked every connection and I already tested 
my board with a simple LED blinky so I think my start up configuraton is 
oki.

I appreciate if you can guide me of any mistake. I also tried to 
simulate with ISIS however I am getting an error of Vin..

thanks
#include <lpc21xx.h>

#define buzz 1<<25 /*lcd rs pin*/

#define pin_rs 1<<21 /*lcd rs pin*/
#define pin_rw 1<<23 /*lcd rw pin*/
#define pin_en 1<<20 /*lcd en pin*/

#define pin_ctrl pin_rs|pin_rw|pin_en /*lcd control pins. pin_ctrl include all 4 [pin_rs, pin_rw, pin_en]*/

#define lcd_d4 1<<16 /*lcd data pins D4,D5,D6,D7*/
#define lcd_d5 1<<17
#define lcd_d6 1<<18
#define lcd_d7 1<<19

#define lcd_data lcd_d4|lcd_d5|lcd_d6|lcd_d7 /*lcd_data include all 4 [lcd_d4, lcd_d5, lcd_d6, lcd_d7]*/

#define lcd_rs(x) ((x)?(IOSET1 = pin_rs):(IOCLR1 = pin_rs));  /*lcd_rs(x) if x=1 will set the lcd_rs pin will clear if x=0*/
#define lcd_rw(x) ((x)?(IOSET1 = pin_rw):(IOCLR1 = pin_rw));  /*lcd_rw(x) if x=1 will set the lcd_rs pin will clear if x=0*/
#define lcd_en(x) ((x)?(IOSET1 = pin_en):(IOCLR1 = pin_en));  /*lcd_en(x) if x=1 will set the lcd_rs pin will clear if x=0*/

#define data_in ((IOPIN0>>10)&(0x0F)) /*direction of the data pins as input*/
#define data_out(x) IOCLR0 = lcd_data; IOSET0 = (x & 0x0F)<<10; /*direction of the data pins and control pins as output*/

#define lcd_all_dir_out IODIR0 |= lcd_data ; IODIR1 |= pin_ctrl; /*setting as output for both data and control pins*/

#define lcd_dir_data_out IODIR0 |= lcd_data     /*set*/
#define lcd_dir_data_in IODIR0 &= ~(lcd_data)   /*clear*/

#define lcd_clear lcd_cmd_write(0x01);



static void delay(int);


static void delay(int cnt)
{
  cnt <<= 4;
  while(cnt--);   //delay
}

static unsigned char busy_wait()
{
  unsigned char (status);
  do
  {
    lcd_dir_data_in;//set pins as inputs
    lcd_rs(0);  // rs=0 indecates that data is being passed
    lcd_rw(1);  // rw=0 indicates that Read function is enabled
    lcd_en(1);  //enabeled
    delay(10);
    status = (data_in << 4)&(0xF0); //data_in shift by 4 AND 0xF0
    lcd_en(0);  //disabled
    delay(10);
    lcd_en(1);  //enabeled
    delay(10);
    status |= data_in; //bit wise OR
    lcd_en(0);  //disabled
    lcd_dir_data_out;  //set pins as outputs
    delay(10);
  }
  while(status & 0x80);  //bit wise AND
  return (status);
}

void lcd_write_4bit(unsigned char c)
{
  lcd_rw(0);  // rw=0 indicates that Read function is enabled
  lcd_en(1);  // enables the lcd
  data_out(c & 0x0F);//bit wise AND
  data_out(c);
  delay(10);
  lcd_en(0);
  delay(10);
}

static void lcd_cmd_write(unsigned char c)
{
  busy_wait();
  lcd_rs(0); // 0 indecates that data is being passed
  delay(10);
  lcd_write_4bit(c >> 4);// c is shifted to the right by 4 and passed to lcd_write_4bit() 
  lcd_write_4bit(c);  // c is passed to lcd_write_4bit()
}

static void lcd_data_write(unsigned char c)
{
  busy_wait(); //
  lcd_rs(1);  // 1 indecates that a command is being passed
  delay(10);
  lcd_write_4bit(c >> 4);// c is shifted to the right by 4 and passed to lcd_write_4bit() 
  lcd_write_4bit(c); // c is passed to lcd_write_4bit()
}

static void lcd_init()
{
  lcd_all_dir_out; //set pins as outputs
  delay(10);
  lcd_rs(0);       // 0 indecates that data is being passed
  lcd_write_4bit(0x3);
  delay(10);
  lcd_write_4bit(0x3);
  delay(10);
  lcd_write_4bit(0x3);
  lcd_write_4bit(0x2);

  lcd_cmd_write(0x28); // 1 01000 = 1[(0)DL (1)N (0)F (0)* (0)*]   Sets interface data length (DL) number of display lines (L) and character font(F).
  lcd_cmd_write(0x0C); // 1 100 = 1[(1)Disp (0)Cursor (0)Blink] display ON curser OFF   blink OFF
  lcd_cmd_write(0x06); // courser auto increment
}

void lcd_print(unsigned char const *mess)
{
  while(*mess)
  {
    lcd_data_write(*mess++);//repeats this function for every constant in string mess
  }
}


int main(void)
{  
  PINSEL2 &= 0x0;//SET IO PORT FUNCTIONALITY
  IODIR1 = 0xFFFF; //set all as outpuT

  delay(1000);

  lcd_init();    //initilize lcd
  lcd_clear;    //clears lcd

  lcd_print("test");   //used for testing
  delay(1000);
  
  
while(1);
  
}


Author: Andreas (Guest)
Posted on:

Rate this post
0 useful
not useful
You are still using the IO registers for port 0 in some of the 
definitions. Change them all, then it should work.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.