I am trying to interface 16*2 lcd with xmc4500 with 4 bit operation. But it's displaying garbage value and second line characters are not getting displayed. Below is the code, please tell me what modifications have to be done in
the code. P0--> data lines, P1.0 --RS, P1.1--> RW, P3.0--> E.
MOD note: Addded code tags.
the code. P0--> data lines, P1.0 --RS, P1.1--> RW, P3.0--> E.
C:
/*
* main.c
*
* Created on: 2016 Dec 09 19:23:38
* Author: DELL
*/
#include <DAVE.h> //Declarations from DAVE Code Generation (includes SFR declaration)
void LCD_init(void);
void DelayMs(int Ms);
void LCD_cmd4(unsigned char cmd);
void LCD_dat4(unsigned char byte);
/**
* @brief main() - Application entry point
*
* <b>Details of function</b><br>
* This routine is the application entry point. It is invoked by the device startup code. It is responsible for
* invoking the APP initialization dispatcher routine - DAVE_Init() and hosting the place-holder for user application
* code.
*/
void main(void)
{
DAVE_STATUS_t status;
status = DAVE_Init(); /* Initialization of DAVE APPs */
if(status == DAVE_STATUS_FAILURE)
{
/* Placeholder for error handler code. The while loop below can be replaced with an user error handler. */
XMC_DEBUG("DAVE APPs initialization failed\n");
while(1U)
{
}
}
unsigned int i;
const unsigned char Msg1[] = "WELCOME TO XMC";
const unsigned char Msg2[] = "LCD INTERFACING";
PORT0->IOCR4 = 0x80808080; //Defines P0.4 to P0.7 as output pins for data lines
PORT1->IOCR0 = 0x8080; //Defines P1.0 and P1.1 as output pins for control lines
PORT3->IOCR0 = 0x80; //Defines P3.0 as output pin for enable
DelayMs(100);
LCD_init();
DelayMs(50);
while(1U)
{
LCD_cmd4(0x80);
for(i=0;i<16;i++)
{
LCD_dat4(Msg1[I]);
DelayMs(5);
}
LCD_cmd4(0xC0);
for(i=0;i<16;i++)
{
LCD_dat4(Msg2[I]);
DelayMs(5);
}
}
}
void LCD_init(void) {
LCD_cmd4(0x33);
LCD_cmd4(0x22);
LCD_cmd4(0x22);
LCD_cmd4(0x22);
LCD_cmd4(0x28); // 28 for four bit mode
LCD_cmd4(0x0c);
LCD_cmd4(0x06);
LCD_cmd4(0x01);
}
void LCD_cmd4(unsigned char cmd) {
PORT1->OUT = 0x00; // RW=0,RS=0
PORT0->OUT = cmd;
PORT3->OUT = 0x01; // En = 1;
PORT3->OUT = 0x00; // En = 0;
cmd = (cmd<<4) & 0xF0;
PORT0->OUT = cmd;
PORT3->OUT = 0x01; // En = 1;
PORT3->OUT = 0x00; // En = 0;
DelayMs(3);
}
void LCD_dat4(unsigned char byte) {
PORT1->OUT = 0x01; // RW=0,RS=1
PORT0->OUT = byte;
PORT3->OUT = 0x01; // En = 1;
PORT3->OUT = 0x00; // En = 0;
byte = (byte<<4) & 0xF0;
PORT0->OUT = byte;
PORT3->OUT = 0x01; // En = 1;
PORT3->OUT = 0x00; // En = 0;
DelayMs(3);
}
void DelayMs(int Ms) {
long int k;
while(Ms>0) {
for(k=0;k<108*1200;k++);
Ms--;
}
}
Last edited by a moderator: