mayur_bankhele
New Member
I want C code for Pic microcontroller in microc compiler for LCD driving . i tried to right down the code but it is working fine but sometimes it fails displaying gaurbage vaule on lcd what ever code i write it work only this pattern only if i interchanged the line below fuction set code it will not work
#include "built_in.h"
#define fset 0x28
#define clr_dsp 0x01
#define e_mode 0x06
#define dis_on 0x0f
#define LINE1 0x80
#define LINE2 0xC0
#define RS PORTD.F3
#define WR PORTD.F2
#define EN PORTD.F1
int i;
void lccd_toggle(void);
void lccd_cmd(unsigned char cmd);
void lccd_data(unsigned char data) ;
void lccd_Init(void);
void lccd_str(unsigned char str[32]);
void lccd_num(unsigned int n);
void lccd_wait ();
void main()
{ unsigned int cnt=0;
TRISA=0x07;
TRISB=0x10;
TRISC=0x87;
TRISD=0x00;
TRISE=0x00;
ADCON0=0x01;
ADCON1=0x0A;
ADCON2=0x8D;
CMCON =0x07;
// CVRCON=0x00;
PORTA=PORTB=PORTC=PORTD=PORTE=0 ;
// Lcd_Custom_Config(&PORTD,7,6,5,4,&PORTD,3,2,1);/*==============*/
// Lcd_Custom_Cmd(LCD_CURSOR_OFF);
// Lcd_Custom_Out(1, 3, "Hello!");
lccd_Init();
//lccd_cmd(LINE1);
// delay_ms(500);
while(1){
// Lcd_Custom_Out(1, 3, "Hello!");
// Lcd_Custom_Out(2, 3, "Hello!");
//lccd_data('A');//
lccd_str("mayurbankhele");
lccd_cmd(0xC0);
lccd_str("mayurbankhele");
lccd_cmd(0x80);
}
}
void lccd_cmd(unsigned char cmmd)
{
RS=0; EN=0;
PORTD =(cmmd & 0xF0); //send upper 4 bits of given command
EN=1; delay_ms(20);EN=0;
delay_ms(10);
i=cmmd & 0x0F; //send lower 4 bits of given command
RS=0; PORTD=(i<<4);
EN=1; delay_ms(20); EN=0;
delay_ms(10);
}
void lccd_data(unsigned char data)
{
unsigned int may=0,hit=0;
EN=0;
may= ((data & 0xF0)|(0x08));
PORTD =may;
EN=1; delay_us(500); EN=0;
hit=(data & 0x0F);
may=((hit<<4)|(0x08));
PORTD =(may);
EN=1; delay_us(500); EN=0;
}
void lccd_Init(void)
{
delay_ms(15);
PORTD=0x30;RS=0;
delay_ms(10);
PORTD=0x30;RS=0;
delay_us(500);
PORTD=0x30;RS=0;
delay_us(500);
PORTD=0x20;RS=0;
delay_ms(10);
lccd_cmd(0x28); //function set
lccd_cmd(0x06); // set cursor move direction 110 1,id,s
lccd_cmd(0x08); //Enable display/cursor 1100 1,D,C,B display,cursor,blink ON/OFF
lccd_cmd(0x0C);
lccd_cmd(0x80);
}
void lccd_str(unsigned char str[32])
{
unsigned char j=0;
RS=1; EN=0;
while(str[j]!='\0')
{
if(j==16)
lccd_cmd(LINE2);
lccd_data(str[j]);
j++;
}
}
void lccd_num(unsigned int n)
{
unsigned char b[10],f=0;
int i=0,j=0;
if(n==0)
{
lccd_data('0'+f);
}
else
{
while(n>0)
{
b[j]=n%10;
j++;
n=n/10;
if(n>0)
f=1;
else
f=0;
}
for(i=j-1;i>=0;i--)
{
lccd_data('0'+b);
}
}
}
#include "built_in.h"
#define fset 0x28
#define clr_dsp 0x01
#define e_mode 0x06
#define dis_on 0x0f
#define LINE1 0x80
#define LINE2 0xC0
#define RS PORTD.F3
#define WR PORTD.F2
#define EN PORTD.F1
int i;
void lccd_toggle(void);
void lccd_cmd(unsigned char cmd);
void lccd_data(unsigned char data) ;
void lccd_Init(void);
void lccd_str(unsigned char str[32]);
void lccd_num(unsigned int n);
void lccd_wait ();
void main()
{ unsigned int cnt=0;
TRISA=0x07;
TRISB=0x10;
TRISC=0x87;
TRISD=0x00;
TRISE=0x00;
ADCON0=0x01;
ADCON1=0x0A;
ADCON2=0x8D;
CMCON =0x07;
// CVRCON=0x00;
PORTA=PORTB=PORTC=PORTD=PORTE=0 ;
// Lcd_Custom_Config(&PORTD,7,6,5,4,&PORTD,3,2,1);/*==============*/
// Lcd_Custom_Cmd(LCD_CURSOR_OFF);
// Lcd_Custom_Out(1, 3, "Hello!");
lccd_Init();
//lccd_cmd(LINE1);
// delay_ms(500);
while(1){
// Lcd_Custom_Out(1, 3, "Hello!");
// Lcd_Custom_Out(2, 3, "Hello!");
//lccd_data('A');//
lccd_str("mayurbankhele");
lccd_cmd(0xC0);
lccd_str("mayurbankhele");
lccd_cmd(0x80);
}
}
void lccd_cmd(unsigned char cmmd)
{
RS=0; EN=0;
PORTD =(cmmd & 0xF0); //send upper 4 bits of given command
EN=1; delay_ms(20);EN=0;
delay_ms(10);
i=cmmd & 0x0F; //send lower 4 bits of given command
RS=0; PORTD=(i<<4);
EN=1; delay_ms(20); EN=0;
delay_ms(10);
}
void lccd_data(unsigned char data)
{
unsigned int may=0,hit=0;
EN=0;
may= ((data & 0xF0)|(0x08));
PORTD =may;
EN=1; delay_us(500); EN=0;
hit=(data & 0x0F);
may=((hit<<4)|(0x08));
PORTD =(may);
EN=1; delay_us(500); EN=0;
}
void lccd_Init(void)
{
delay_ms(15);
PORTD=0x30;RS=0;
delay_ms(10);
PORTD=0x30;RS=0;
delay_us(500);
PORTD=0x30;RS=0;
delay_us(500);
PORTD=0x20;RS=0;
delay_ms(10);
lccd_cmd(0x28); //function set
lccd_cmd(0x06); // set cursor move direction 110 1,id,s
lccd_cmd(0x08); //Enable display/cursor 1100 1,D,C,B display,cursor,blink ON/OFF
lccd_cmd(0x0C);
lccd_cmd(0x80);
}
void lccd_str(unsigned char str[32])
{
unsigned char j=0;
RS=1; EN=0;
while(str[j]!='\0')
{
if(j==16)
lccd_cmd(LINE2);
lccd_data(str[j]);
j++;
}
}
void lccd_num(unsigned int n)
{
unsigned char b[10],f=0;
int i=0,j=0;
if(n==0)
{
lccd_data('0'+f);
}
else
{
while(n>0)
{
b[j]=n%10;
j++;
n=n/10;
if(n>0)
f=1;
else
f=0;
}
for(i=j-1;i>=0;i--)
{
lccd_data('0'+b);
}
}
}