View Single Post
Old 18-04-2012, 05:50 PM   #40
kyanh911
Đệ tử 1 túi
 
Tham gia ngày: Jan 2010
Bài gửi: 11
:
em có đoạn code này giao tiếp lcd 4bit đã chạy ổn định....chân rs,rw,e đã nói trong code. còn chân D7 của LCD nối chân RB0, D6 nối RB1, D5 nối RB2, D4 nối RB3

#include<18f4550.h>
#fuses nowdt,hs,noprotect,nolvp
#use delay(clock=20000000)
#use fast_io(b)
#define rs pin_b7
#define rw pin_b6
#define e pin_b5

int8 dao(int8 xx)
{
int8 buff;
buff=0;
buff+=(xx>>7)&0x01;
buff+=(xx>>5)&0x02;
buff+=(xx>>3)&0x04;
buff+=(xx>>1)&0x08;

buff+=(xx<<1)&0x10;
buff+=(xx<<3)&0x20;
buff+=(xx<<5)&0x40;
buff+=(xx>>7)&0x80;
return buff;
}

void ghilenh(int8 malenh)
{
int8 tam;
tam=dao(malenh);
output_b(tam&0xf); //xuat 4 bit cao truoc
output_low(rs); //chon thanh ghi lenh
output_low(rw); //cho phep ghi
delay_us(2);
output_high(e);
delay_us(5);
output_low(e);
delay_us(500);

output_b(tam>>4); //xuat 4 bit thap sau
output_low(rs);
output_low(rw);
delay_us(2);
output_high(e);
delay_us(5);
output_low(e);
delay_us(500);
}

void ghikytu(int8 makytu)
{
int8 tam;
tam=dao(makytu);
output_b(tam&0xf);
output_high(rs);
output_low(rw);
delay_us(2);
output_high(e);
delay_us(5);
output_low(e);
delay_us(500);

output_b(tam>>4);
output_high(rs);
output_low(rw);
delay_us(2);
output_high(e);
delay_us(5);
output_low(e);
delay_us(500);
}
void khoitaolcd()
{
ghilenh(0x28);
ghilenh(0x28); //mã 28 chu k phai 38
delay_ms(10);
ghilenh(0x28); //vi giao tiep 4 bit
delay_ms(10);

ghilenh(0x28);
delay_ms(1);
ghilenh(0x0c);
delay_ms(1);
ghilenh(0x01);
delay_ms(1);
ghilenh(0x06);
delay_ms(100);
}
main()
{
set_tris_b(0x00);
khoitaolcd();
ghikytu('N');
ghikytu('b');
}


Vấn đề thắc mắc ở đây là sao đưa qua port D thì lại hổng chạy?
kyanh911 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn