trinhdieuhvktqs
Newbie level 2
hello everybody !
I'm writing program display lcd 20x4, mode 4bit , used busy flag before communicating with lcd and not used Delay. I wrote according to the datasheet of lcd but not displayed at all. please help me
THANK YOU VERY MUCH.
I'm writing program display lcd 20x4, mode 4bit , used busy flag before communicating with lcd and not used Delay. I wrote according to the datasheet of lcd but not displayed at all. please help me
Code:
#include <p33FJ64MC802.h>
#include <math.h>
//----------------------------------------------------------------------------//
_FGS(GCP_OFF & GWRP_OFF);
_FOSCSEL(FNOSC_FRCPLL);
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON & POSCMD_NONE);
_FWDT(FWDTEN_OFF);
_FICD(ICS_PGD1 & JTAGEN_OFF);
//--------------------------------------------------------------------------------------
#define Fcy 40000000 //Instruction Cycle Frequency
#define BAUDRATE 9600
#define LED_READ LATAbits.LATA0
#define LED_BLUE LATAbits.LATA1
#define LCD_RS LATBbits.LATB15
#define LCD_RW LATBbits.LATB14
#define LCD_E LATBbits.LATB13
#define LCD_D4 LATBbits.LATB10
#define LCD_D5 LATBbits.LATB9
#define LCD_D6 LATBbits.LATB6
#define LCD_D7 LATBbits.LATB7
#define LCD_1_D7 PORTBbits.RB7
// DINH NGHIA CAC CHE DO HOAT DONG CUA LCD
#define LCD_clear 0x01 //Xoa man hinh LCD
#define LCD_home 0x02 //Tro ve dau dong
#define LCD_normal 0x06 //Che do nhap du lieu binh thuong
#define LCD_normal_s 0x07 //Che do nhap du lieu binh thuong, dich man hinh
#define LCD_off 0x08 //Tat man hinh
#define LCD_on 0x0C //Bat man hinh
#define LCD_on_crsr 0x0E //Bat man hinh va con tro
#define LCD_on_blink 0x0F //Bat man hinh va con tro (nhap nhay)
#define LCD_4b2l 0x28 //Che do 2 dong, giao tiep 4 bit du lieu
#define LCD_homeL1 0x80 //Ve dau dong 1
#define LCD_homeL2 0xC0 //Ve dau dong 2
#define lcd_line_two 0x40
//---------------------------------------------------------------------------------------------
unsigned long TM1,TM0;
unsigned int i;
long int EC;
//---------------------------------------------------------------------------------------------
void InitPort(void);
void Timer1_Init(void);
void OSOC_Init(void);
void Timer1_Init(void);
void Delay_us(unsigned int time);
void Delay_ms1(unsigned int time) ;
void Timer2_Init(void);
void Init_LCD(void);
void LCD_OK(void);
void LCD_CMD(unsigned char cmd);
void LCD_DATA (unsigned char dat);
void lcd_putstr(char *buffer);
void lcd_gotoxy(int x, int y);
void khoi_tao(void);
void LCD_IN_INTER(unsigned int d);
void LCD_IN_FLOAT(float d);
void LCD_busy();
//---------------------------------------------------------------------------------------------
int main (void)
{
OSOC_Init();
InitPort();
Timer1_Init();
Timer2_Init();
Init_LCD();
Delay_ms1(100);
lcd_gotoxy(4,1);
lcd_putstr("weather station");
LED_DO=1;
while(1)
{
if(IFS0bits.T2IF==1)
{
TM0++;
if(TM0==1000)
{LED_BLUE^=1;
TM0=0;
}
IFS0bits.T2IF=0;
}
}
return 0;
}
//---------------------------------------------------------------------------------------------
void InitPort(void)
{
//LATB = 0x0000; // Initialize Port-B
PMD1=0xffff;
PMD2=0xffff;
PMD3=0xffff;
TRISAbits.TRISA0 = 0;
TRISAbits.TRISA1 = 0;
TRISBbits.TRISB10 = 0; //LCD_D4
TRISBbits.TRISB9 = 0; //LCD_D5
TRISBbits.TRISB6 = 0; //LCD_D6
TRISBbits.TRISB7 = 0; //LCD_D7
TRISBbits.TRISB15 = 0; //LCD_RS
TRISBbits.TRISB14 = 0; //LCD_RW
TRISBbits.TRISB13 = 0; //LCD_E
LCD_D4=0;
LCD_D5=0;
LCD_D6=0;
LCD_D7=0;
LCD_RW=0;
LCD_RS=0;
LCD_E=0;
}
//---------------------------------------------------------------------------------------------------------
void OSOC_Init(void)
{
//Config FCy=40MHz
PLLFBD=41; // M=43
CLKDIVbits.PLLPOST=0; // N2=2
CLKDIVbits.PLLPRE=0; // N1=2
OSCTUN=0;
// Disable Watch Dog Timer
RCONbits.SWDTEN=0; // N1=2
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(0x01);
while(OSCCONbits.COSC != 0b001); // Wait for PLL to lock
while(OSCCONbits.LOCK!=1) {};
}
//------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
void Init_LCD(void)
{
LCD_CMD(0x33);
Delay_ms1(10);
LCD_CMD(0x33);
Delay_us(200);
LCD_CMD(0x33);
Delay_us(100);
LCD_CMD(0x28); // che do giao tiep 4 bit 2 dong
Delay_ms1(10);
LCD_CMD(0x08); // off man hinh
Delay_ms1(10);
LCD_CMD(LCD_normal); //Tat man hinh
Delay_ms1(10);
LCD_CMD(LCD_on);
Delay_ms1(10);
LCD_CMD(0x01); //Xoa man hinh
Delay_ms1(10);
}
//---------------------------------------------------------------------------------------------------------
void LCD_OK(void)
{
//unsigned u;
LCD_E = 1; //Tao xung Enable
Delay_us(2);
LCD_E = 0;
}
//---------chuong trinh xuat lenh che do 4 bit--------------------------------------------------------------------------------
void LCD_CMD(unsigned char cmd)
{
unsigned temp1;
LCD_busy();
LCD_RW = 0;
LCD_RS = 0; //Dat che do xuat lenh
temp1 = cmd >> 4; //Lay 4 bit cao
LCD_D4 = temp1 & 1; //Xuat 4 bit cao
LCD_D5 = (temp1 & 2) >>1;
LCD_D6 = (temp1 & 4) >>2;
LCD_D7 = (temp1 & 8) >>3;
LCD_OK();
temp1 = cmd & 0x0F; //Lay 4 bit thap
LCD_D4 = temp1 & 1; //Xuat 4 bit thap
LCD_D5 = (temp1 & 2) >>1;
LCD_D6 = (temp1 & 4) >>2;
LCD_D7 = (temp1 & 8) >>3;
LCD_OK();
}
//----------Chuong trinh con ghi mot ky tu o che do 4 bit--------------------------
void LCD_DATA (unsigned char dat)
{
unsigned temp2;
LCD_busy();
LCD_RW = 0;
LCD_RS = 1; //Dat che do xuat du lieu
temp2 = dat >> 4; //Lay 4 bit cao
LCD_D4 = temp2 & 1; //Xuat 4 bit cao
LCD_D5 = (temp2 & 2) >>1;
LCD_D6 = (temp2 & 4) >>2;
LCD_D7 = (temp2 & 8) >>3;
LCD_OK();
temp2 = dat & 0x0F; //Lay 4 bit thap
LCD_D4 = temp2 & 1; //Xuat 4 bit thap
LCD_D5 = (temp2 & 2) >>1;
LCD_D6 = (temp2 & 4) >>2;
LCD_D7 = (temp2 & 8) >>3;
LCD_OK();
}
//-----------Chuong trinh con hien thi 1 chuoi ky tu--------------------------------------------------------------------
void lcd_putstr(char *buffer)
{
while(*buffer != '\0')
{
LCD_DATA(*buffer); /* calling another function */
/* to write each char to the lcd module */
buffer++;
//Delay_us(50);
}
}
//-----------Chuong trinh con nhay den cot x, dong y-------------------------------------------------------------------
void lcd_gotoxy(int x, int y)
{
int address = 0x00;
switch(y) {
case 1 : address=0x00;break;
case 2 : address=0x40;break;
case 3 : address=0x14;break;
case 4 : address=0x54;break;
}
address +=x-1;
LCD_CMD(0x80|address);
// Delay_us(5);
}
//===========================================
void LCD_IN_INTER(unsigned int d)
{
unsigned i,j,k,l;
l=d/1000;//nghin
k=(d%1000)/100;//tram
j=((d%1000)%100)/10;//chuc
i=((d%1000)%100)%10;//donvi
LCD_DATA(0x30+l);
LCD_DATA(0x30+k);
LCD_DATA(0x30+j);
LCD_DATA(0x30+i);
}
// ========khoi tao timer 2
void Timer2_Init(void)
{
PMD1bits.T2MD = 0; // Enable Timer1 module
T2CONbits.TON = 0; // Disable Timer
T2CONbits.TCS = 0; // Select Internal Clock for Timer1
T2CONbits.TGATE = 0; // Disable Gated Timer Mode
T2CONbits.TCKPS = 0b00; // Set Timer1 Prescaler = 1:1
TMR2 = 0x00; // Clear Timer1 counter
PR2 = 40000; // 20ms period for keyboard detect ;3125
//IPC0bits.T1IP = 0x01; // Timer1 interrupt Priority Level 1
IFS0bits.T2IF = 0; // Clear Timer1 Interrupt Flag
IEC0bits.T2IE = 0; // Diasble Timer1 Interrupt
T2CONbits.TON = 1; // Enable Timer1
}
//----- khoi tao timer1---------------------------------------------------------------------
void Timer1_Init(void)
{
PMD1bits.T1MD = 0; // Enable Timer1 module
T1CONbits.TON = 0; // Disable Timer
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TGATE = 0; // Disable Gated Timer mode
TMR1 = 0x00; // Clear timer register
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
IEC0bits.T1IE = 0; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Start Timer
}
//------ham tao tre 1us (khi PR1=40)-------------------------------------------
void Delay_us(unsigned int time)
{
T1CONbits.TCKPS = 0b00; // Set Timer1 Prescaler = 1:1
PR1 = 40*time ; // Timer1 period set
while(IFS0bits.T1IF==0); // While the interupt flag is not set
IFS0bits.T1IF = 0;
}
//------ham tao tre 1ms (khi PR1=625) t?o tre toi da 100ms-------------------------------------------
void Delay_ms1(unsigned int time)
{ T1CONbits.TCKPS = 0b10; // Set Timer1 Prescaler = 64:1
PR1 = 625*time ; // Timer1 period set
while(IFS0bits.T1IF==0); // While the interupt flag is not set
IFS0bits.T1IF = 0;
}
//====== kiem tra co ban cua lcd============================================
void LCD_busy()
{ TRISBbits.TRISB7 = 1; //LCD_D7 : luc nay D7 la chan vao
LCD_D7 = 1; //Make D7th bit of LCD as i/p
LCD_E = 1; //Make port pin as o/p
LCD_RS = 0; //Selected command register
LCD_RW = 1; //We are reading
while(LCD_1_D7){ //read busy flag again and again till it becomes 0
LCD_E = 0; //Enable H->L
LCD_E = 1;
}
TRISBbits.TRISB7 = 0;
}
Last edited by a moderator: