Giao tiếp pic18f4431 và SFR05 hiển thị LCD - Cần giúp đỡ !!!
tình hình em đang làm mạch pic18f4431 giao tiếp với SFR05 đo khoảng cách sau đó hiển thị lên LCD 16x2, nhưng em đang gặp rắc rối ở phần đo thời gian nhận xung phản hồi từ chân ECHO, đây là code của em, mọi người xem giúp e sai chỗ nào
#include <18F4431.h>
#FUSES NOWDT, NOPUT, HS, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=8000000)
#define LCD_EN PIN_D3
#define LCD_RS PIN_D1
#define LCD_RW PIN_D2
#define LCD_D4 PIN_D4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7
#define TRIGGER PIN_C0
#define ECHO PIN_C1
float dist,getvar;
int1 has_echo=0;
#int_TIMER1
void TIMER1_isr(void)
{
has_echo = 1;
dist = -1;
}
#int_CCP2
void CCP2_isr(void)
{
if(pin_c1)
{ // Neu là xung canh lên
set_timer1(0); // Reset timer1 ve 0
setup_ccp2(CCP_CAPTURE_FE); // Chuyen Capture 1 do canh xuong
}
if(!pin_c1) { // Neu là xung canh xuong
getvar=CCP_2;// Ðoc giá tri timer 1
dist=(getvar*2)/58; // Moi 1 nhip cua timer 0 tuong ung vs 2 us, lay us chia cho 58 ra cm
setup_ccp2(CCP_CAPTURE_RE); // Chuyen Capture 1 do xung lên tro lai
has_echo = 1; // Ðã nhan xong echo
}
}
//----15us cho trigger----
void trig()
{
output_high(TRIGGER);
delay_us(15);
output_low(TRIGGER);
}
//Tao Xung
void LCD_Enable(void)
{
output_high(LCD_EN);
delay_us(10);
output_low(LCD_EN);
delay_us(10);
}
//Ham Gui 4 Bit Du Lieu Ra LCD
void LCD_Send4Bit( unsigned char Data )
{
output_bit(LCD_D4,Data&0x01);
output_bit(LCD_D5,(Data>>1)&1);
output_bit(LCD_D6,(Data>>2)&1);
output_bit(LCD_D7,(Data>>3)&1);
}
// Ham Gui 1 Lenh Cho LCD
void LCD_SendCommand (unsigned char command )
{
LCD_Send4Bit ( command >>4 );/* Gui 4 bit cao */
LCD_Enable () ;
LCD_Send4Bit ( command ); /* Gui 4 bit thap*/
LCD_Enable () ;
}
// Ham Khoi Tao LCD
void LCD_Init ( void )
{
output_drive(LCD_D4);
output_drive(LCD_D5);
output_drive(LCD_D6);
output_drive(LCD_D7);
output_drive(LCD_EN);
output_drive(LCD_RS);
output_drive(LCD_RW);
LCD_Send4Bit(0x00);
delay_us(100);
output_low(LCD_RS);
output_low(LCD_RW);
LCD_Send4Bit(0x03);
LCD_Enable();
delay_us(500);
LCD_Enable();
delay_us(50);
LCD_Enable();
LCD_Send4Bit(0x02);
LCD_Enable();
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 LCD_Gotoxy(unsigned char x, unsigned char y)
{
unsigned char address;
if(!y)
address = (0x80+x);
else
address = (0xC0+x);
delay_us(500);
LCD_SendCommand(address);
delay_us(50);
}
// Ham Xoa Man Hinh LCD
void LCD_Clear()
{
LCD_SendCommand(0x01);
delay_ms(10);
}
// Ham Gui 1 Ki Tu Len LCD
void LCD_PutChar ( unsigned char Data )
{
output_high(LCD_RS);
LCD_SendCommand( Data );
output_low(LCD_RS);
}
void LCD_Puts (char *s)
{
while (*s)
{
LCD_PutChar(*s);
s++;
}
}
void main()
{
set_tris_B(0x00);
SET_TRIS_C(0xff);
output_bit(pin_D3,1);
output_bit(pin_D1,1);
LCD_init();
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); //32.7 ms overflow
setup_ccp2(CCP_CAPTURE_RE); // Thiet lap Capture 1 do canh lên
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
While (TRUE)
{
has_echo = 0; // Xác lap lai echo
trig(); // Trigger
while (has_echo == 0) { } // cho den khi nhan het echo
lcd_gotoxy(2,0);
printf(lcd_putchar,"dist=%6.1f",dist);
lcd_putchar("cm");
delay_ms(100); // 0.1 s do mot lan
}
|