Mấy a kiểm tra giúp e cái code hiển thị LCD của e nó sai những đâu ak. Em mô phỏng thì chạy mà nạp vào KIT thì lúc chạy lúc ko
Code:
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,HS,NODEBUG,NOPROTECT,NOWRT,NOPUT,NOBROWNOUT,NOCPD
#use delay(clock=20000000)
#use fast_io(D)
#use fast_io(A)
#use fast_io(E)
#byte PORTA = 0x05
#byte PORTD = 0x08
#byte PORTE = 0x09
#bit RE1 = PORTE.1
#bit RE2 = PORTE.2
#bit RA2 = PORTA.2
#bit RA3 = PORTA.3
#bit RA4 = PORTA.4
#bit RD4 = PORTD.4
#bit RD5 = PORTD.5
#bit RD6 = PORTD.6
#bit RD7 = PORTD.7
#bit RD0 = PORTD.0
#bit RD1 = PORTD.1
#bit RD2 = PORTD.2
#bit RD3 = PORTD.3
#define E PORTA
#define RS RE1
#define RW RE2
#define Data PORTD
#define LS138_A RA2
#define LS138_B RA3
#define LS138_C RA4
///Khai bao cac ham con
void LCD_enable();
void LCD_Send_4bit();
void LCD_SendCommand();
void LCD_Putchar();
void LCD_Putschar();
void LCD_gotoxy();
void LCD_clear();
void LS138_Put();
//===========Tao xung enable==========//
void LS138_Put( int8 point)
{
if(point&0x01)
LS138_A=1;
else
LS138_A=0;
if(point&0x02)
LS138_B=1;
else
LS138_B=0;
if(point&0x04)
LS138_C=1;
else
LS138_C=0;
}
void LCD_Enable(void)
{
LS138_Put(7);
delay_us(3);
LS138_Put(0)=0;
delay_us(50);
}
//=====Ham gui 4bit=====//
void LCD_Send_4bit(unsigned char data)
{
RD4=data & 0x01;
RD5=(data>>1) & 0x01; //dich sang trai 1 bit va so sanh
RD6=(data>>2) & 0x01;
RD7=(data>>3) & 0x01;
}
//========Ham gui lenh========//
void LCD_Sendcommand(unsigned char command)
{
RS=0; //chon thanh ghi lenh
RW=0; //cho phep ghi
LCD_Send_4bit(command>>4); //gui 4 bit cao
LCD_enable();
LCD_Send_4bit(command); //gui 4 bit thap
LCD_enable();
}
void LCD_Senddata(unsigned char data)
{
RS=1; //chon thanh ghi data
RW=0; //cho phep ghi
LCD_Send_4bit(data>>4); //gui 4 bit cao
LCD_enable();
LCD_Send_4bit(data); //gui 4 bit thap
LCD_enable();
}
//========Ham khoi tao LCD============//
void LCD_inits(void)
{
LCD_Send_4bit(0x00);
delay_ms(20); //cho cho LCD hoat dong
LCD_Sendcommand(0x03);//
LCD_Sendcommand(0x02); //tro ve dau dong
LCD_SendCommand( 0x28 ); // giao thuc 4 bit, hien thi 2 hang, ki tu 5x8
LCD_SendCommand( 0x0c); // cho phep hien thi man hinh
LCD_SendCommand( 0x06 ); // tang ID, khong dich khung hinh
LCD_SendCommand( 0x01 ); // xoa toan bo khung hinh
}
//=========void thiet lap vi tri======//
void LCD_gotoxy(unsigned char x , unsigned char y)
{
unsigned char address;
if(y==1)
address = (0x80+x);
else
address = (0xC0+x);
delay_us(1000);
LCD_SendCommand(address);
delay_us(50);
}
//===Ham xoa man hinh===//
void clear(void)
{
LCD_SendCommand(0x01); //xoa man hinh
delay_ms(10);
}
//=======Ham gui 1 ky tu len LCD==//
void LCD_Putchar(unsigned char text)
{
LCD_senddata(text);
}
//=======Ham gui 1 chuoi ky tu=====//
void LCD_PutsChar(char *s)
{
while(*s)
{
LCD_Putchar(*s);
s++;
}
}
void main()
{
int8 str[16]; //16 la so ki tu
SET_TRIS_A(0);
SET_TRIS_D(0);
SET_TRIS_E(0);
delay_ms(200);
LCD_inits(); //khoi tao LCD
delay_ms(100);
LCD_Gotoxy(0,1);
sprintf(str,".....BKSTAR.....");
LCD_Putschar(str);
delay_ms(100);
LCD_gotoxy(0,2);
sprintf(str,"...TECHNOLOGY...");
LCD_putschar(str);
while(1){};
}

Comment