Hic, đây là code của em, biên dịch thì ko lỗi, nhưng nạp vào thì chẳng thấy LCD viết gì anh ạ, chán thế .
Code:
#include <p30f4011.h>
#include <string.h>
_FOSC(CSW_FSCM_OFF & HS);
_FWDT(WDT_OFF);
_FBORPOR(MCLR_EN & PBOR_ON & BORV_27);
_FGS(CODE_PROT_OFF);
#define LCD_EN LATFbits.LATF2 //Tin hieu Enable cho LCD, chan RF2
#define LCD_RS LATFbits.LATF6 //Tin hieu Reset cho LCD, chan RF6
#define LCD_RW LATFbits.LATF3 //Tin hieu doc ghi cho LCD, chan RF3
#define RW_TRIS TRISFbits.TRISF3
#define RS_TRIS TRISFbits.TRISF6
#define E_TRIS TRISFbits.TRISF2
#define LCD_PORTB PORTB
#define LCD_DATA LATB //Cac tin hieu Data cho LCD
#define LCD_TRIS TRISB //Tristate cua cac tin hieu Data cho 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_8B2L 0x38 //Che do 2 dong, giao tiep 8 bit du lieu
#define LCD_HOMEL1 0x80 //Ve dau dong 1
#define LCD_HOMEL2 0xC0 //Ve dau dong 2
#define ShortDelay() {Nop() Nop() Nop() Nop() Nop() Nop() Nop() Nop() Nop()}
//***************************************************************************//
//***************************************************************************//
//
// Function: LCDdelayms
// Description: LCDdelayms for LCD
// Input: Number of ms
// Output: None
//
//***************************************************************************//
void LCDdelayms(unsigned int ms)
{
#define FCY 2500*4
unsigned int i=0;
while(ms--){while(++i<FCY/7); i=0;}
}
//*************************** END OF LCDDELAYMS ****************************//
void LCD_CMD(unsigned char CMD)
{
LCD_RW=0;
LCD_RS = 0;
LCD_DATA = CMD;
LCD_EN = 1; ShortDelay(); LCD_EN = 0;
}
//************************* End of LCD_CMD *********************************//
void LCD_DAT(unsigned char DATA)
{
LCD_RW=0;
LCD_RS = 1;
LCD_DATA = DATA;
LCD_EN = 1; ShortDelay(); LCD_EN = 0; //Xung Enable
}
//****************************** END OF LCD_DATA ****************************//
void InitLCD(void)
{
LCD_CMD(LCD_8B2L); //Dat che do giao tiep 8-bit, man hinh 2 dong
LCDdelayms(1);
LCD_CMD(LCD_8B2L); //Dat che do giao tiep 8-bit, man hinh 2 dong
LCDdelayms(1);
LCD_CMD(LCD_8B2L); //Dat che do giao tiep 8-bit, man hinh 2 dong
LCDdelayms(1);
LCD_CMD(LCD_OFF); //Tat man hinh
LCDdelayms(1);
LCD_CMD(LCD_CLEAR); //Xoa man hinh
LCDdelayms(1);
LCD_CMD(LCD_ON); //Bat man hinh
LCDdelayms(1);
LCD_CMD(LCD_NORMAL); //Che do nhap lieu binh thuong
//Cursor move Increment, Not to shift the display
LCDdelayms(1);
LCD_CMD(LCD_CLEAR); //Xoa man hinh
LCDdelayms(5);
}
//************************** END OF InitLCD *********************************//
void LCD_PutChar(unsigned char character)
{
LCD_DAT(character);
LCDdelayms(1);
}
//************************** END of LCD_PutChar *****************************//
void LCD_WriteString(const char *str)
{
// LCDdelayms(1);
char ps;
ps = *str;
while(ps>0)
{
str++;
if (ps==0) break;
LCDdelayms(1);
LCD_DAT(ps);
ps = *str;
}
}
//*********************** END OF LCD_Writestring ****************************//
void LCD_Gotoxy(char x, char y)
{
char tg;
LCDdelayms(5);
switch (y)
{
case 1:tg = 0x80+x;
break;
case 2:tg = 0xC0+x;
break;
}
LCD_CMD(tg);
}
//************************* END OF LCD_Gotoxy *******************************//
void LCD_Clear(void)
{
LCD_CMD(LCD_CLEAR);
}
void SystemInit(void)
{
//Init IO
TRISB = TRISB & 0xFF00; //RB0..RB7 Output.
TRISFbits.TRISF6 = 0;
TRISFbits.TRISF2 = 0;
TRISFbits.TRISF3 = 0;
LATB = 0xFF00;
LATFbits.LATF6 = 0;
LATFbits.LATF2 = 0;
LATFbits.LATF3 = 0;
//InitLCD
InitLCD();
}
//******************************* End of SystemInit *************************//
int main(void)
{
SystemInit();
LCD_Gotoxy(1,1); //Nhay den vi tri cot 1, dong 1.
LCD_WriteString("picvietnam");
LCDdelayms(10);
LCD_Gotoxy(3,2); //Nhay den vi tri cot 3, dong 2.
LCD_WriteString("dspic");
while(1); //Dung chuong trinh.
}
//********************************* End of Main *****************************//