Chào mọi người !! Chả là mình đang làm 1 đề tài giao tiếp máy tính qua cổng com với PIC16F88 nhưng lại gặp vấn đề ở khâu lấy dữ liệu từ cổng COM để hiển thị lên PC.
Sau khi mình lập trình VB6 để nhận thì trên PC thu được như hình :
[url=http://anh.us/3bc9accfcc.html]
[/url
[code] #include <16f88.h>
#include <def_88.h>
#device *=16 ADC=10
#fuses NOWDT, HS, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bi ts=9)
#include <lcd_lib_4bit.c> // Thu vien ham cho LCD
int16 temp;
int8 nghin,tram,chuc,donvi;
int1 mili_volt;
float volt;
void convert_bcd(int16 x)
{
nghin = x / 1000 +0x30;
temp = x % 1000;
tram = temp / 100 +0x30;
temp = temp % 100;
chuc = temp / 10 +0x30;
donvi = temp % 10 +0x30;
}
void xuat_ra_lcd()
{
temp = read_adc() ;
volt=(float)(temp*5)/1023;
if(volt<1) mili_volt=1;
else mili_volt=0;
volt=volt*1000;
convert_bcd((int16)volt);
lcd_putcmd(0xc0);//con tro o dau hang 2
if(mili_volt==0)
{
lcd_putchar(nghin);
lcd_putchar(".");
}
lcd_putchar(tram);
lcd_putchar(chuc);
lcd_putchar(donvi);
if(mili_volt==1) printf(lcd_putchar," mV");
else printf(lcd_putchar," V");
}
void convert_pc(int16 x)
{
nghin = x / 1000 ;
temp = x % 1000;
tram = temp / 100 ;
temp = temp % 100;
chuc = temp / 10 ;
donvi = temp % 10 ;
}
void xuat_ra_pc() {
temp = read_adc() ;
volt=(float)(temp*5)/1023;
if(volt<1) mili_volt=1;
else mili_volt=0;
volt=volt*1000;
convert_pc((int16)volt);
printf("%u",nghin);
}
void main( ){
trisb=0x00;
LCD_init();
delay_us(100);
LCD_Putcmd(0X80);
printf(LCD_putchar,"DIEN AP U= ");
setup_adc( ADC_CLOCK_INTERNAL );
Setup_ADC_ports (0);
Set_ADC_channel (0) ;
Delay_us (300); // delay 10 us
while(1){
xuat_ra_lcd();
printf("%u\,%u\%u", nghin);
}
}
còn đây là đoạn code VB của mình:
Private Sub EXIT_Click()
End
End Sub
Private Sub Form_Load()
With MSComm1
MSComm1.CommPort = 1 ' Using Com1
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
End With
End Sub
Private Sub MSComm1_OnComm()
Dim s As String
Dim s1 As String
Dim s2 As String
s = MSComm1.Input
s1 = Left$(s, 4)
Text1.Text = "Va = " & s1 & " v"
s2 = Right$(s, 4)
Text2.Text = "Vb = " & s2 & " v"
End Sub
Private Sub on1_Click()
MSComm1.PortOpen = True
End Sub
************************************************** *******
nhờ các cao thủ chỉ giáo! Mong ACE nhanh nhanh hồi đáp sớm ! Thanks!!!!
************************************************** *******
do mới post bài lân đầu nên chưa biết cách post code! ACE thông cảm !!!
đây là sơ proteus