PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp cổng COM và LPT

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Giao tiếp cổng COM và LPT RS232, RS485 và LPT là những giao tiếp cơ bản và kinh điển khi mới học về vi điều khiển...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 11-07-2012, 09:54 AM   #1
khienpzo
Đệ tử 5 túi
 
Tham gia ngày: Sep 2010
Bài gửi: 107
:
Exclamation Cầu cứu gấp gấp (hệ thu thập nhiều kênh)

em đang làm hệ thu thập nhiều kênh dữ liệu nhiệt độ mà khi viết chương trình mô phỏng không thấy hồi âm gì mong các anh chỉ giáo giúp:
code vi điều khiển của em ạ:

#include <16f877a.h>
#device 16f877a*16ADC=10
#fuses hs,nolvp,nowdt,noprotect,put
#use delay(clock=4000000)

//==include thu vien==//

#include <string.h>
#include <stdlib.h>
#include <math.h>

//=====Khai bao I/O======//

#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)

#byte porta=0x5
#byte portb=0x6
#byte portc=0x7
#byte portd=0x8
#byte porte=0x9

#bit b0=portb.0
#bit b1=portb.1
#bit b2=portb.2
#bit b3=portb.3
#bit b4=portb.4
#bit b5=portb.5
#bit b6=portb.6
#bit b7=portb.7

#bit l10=porte.0
#bit l11=porte.1
#bit l12=porte.2
#bit l13=portc.3
#bit l14=portc.4
#bit l15=portc.5

//====Cac thanh ghi lien quan TIMER0-TIMER1====//

#byte timer0=0x1 // thanh ghi TIMER0
#byte option_reg=0x81 // thanh ghi Option_Reg
#byte PIR1=0x0c // thanh ghi co ngat PIR1

#bit TMR1IF=PIR1.0 // bit co bao tran TIMER1 :TMR1IF

#bit p0=option_reg.0 //khai bao cac bit trong thanh ghi OPTION_REG
#bit p1=option_reg.1 //1
#bit p2=option_reg.2 //2
#bit p3=option_reg.3 //3
#bit p4=option_reg.4 //4
#bit p5=option_reg.5 //5
//======================================//
//=====su dung truyen thong RS-232======//
//======================================//
#use rs232(baud=9600,parity=N,xmit=pin_C6,rcv=pin_C7)

//==Khai bien toan cuc==//
float x,y,z;
int m,n,k,chuc,tram,donvi,value,index,flag,duty;
// chuc - nhiet kenh 1, tram - nhiet kenh 2 , donvi - nhiet kenh 3
char mangdata[6];

//=======KHAI BAO SU DUNG NGAT TRUYEN THONG UART======//
#INT_RDA
void ngat_RDA()
{
char data;
if(kbhit())
{
data=getc();
if(data=='#')
{
mangdata[index]=data;
index=0;
flag=1;
}
else
{
mangdata[index]=data;
index++;
}
}
}
//========KHAI BAO SU DUNG NGAT TIMER-1========//
#INT_TIMER1
void ngat_timer1()
{
value=timer0; // vi ngat sau 100ms
set_timer1(28035); // Dat lai gia tri cho TIMER1
timer0=0; // Dat lai gia tri cho TIMER0
TMR1IF =0; // Xoa co tran timer1 TMR1IF
}
//============Chuong trinh chinh===================//
void doc_ADC_1();
void doc_ADC_2();
void doc_ADC_3();
void tinhtoan1(int m);
void tinhtoan2(int n);
void tinhtoan3(int k);
void xuat_pwm();
void xulykhung();
void main()
{
enable_interrupts(INT_RDA); //CHO PHEP NGAT TRUYEN THONG
enable_interrupts(INT_TIMER1); //CHO PHEP NGAT TIMER1
enable_interrupts(GLOBAL); //CHO PHEP NGAT TOAN CUC-GLOBAL
ext_int_edge(L_to_H); //CANH NGAT L_TO_H
setup_ADC_ports(AN0_AN1_AN3); //cau hinh AN0 AN1 AN3 nhan analog, Vref=Vdd
setup_ADC(ADC_clock_internal);
set_tris_a(0b111111);
set_tris_b(0b0);
set_tris_d(0b0);
set_tris_c(0x80);
set_tris_e(0);
porta=0;
portb=0b00001111;
portd=0;
portc=0;
porte=0;
mangdata[0]=0;
//bien nhiet//
tram=0;chuc=0;donvi=0;x=0,y=0,z=0;
//
index=0;
flag=0;
//
//CAU HINH CHO TIMER-1 VA TIMER-0,BAT DAU CHO TIMER HOAT DONG//
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //cau hinh cho TIMER 1-chia tan 1:8,timer1=28035
set_timer1(28035); //==>ngat sau 100mS.(1/3)*(65535-28035)*8=100ms
timer0=0; // Cai dat gia tri-cau hinh cho TIMER 0
p0=0;p1=1;p2=0; // Gia tri chia tan,mac dinh 1:8
p3=0; // chia tan
p4=1; // Chon canh tac dong L_TO_H
p5=1; // Cho TIMER0 bat dau hoat dong o che do Counter
value=0;
//================================================== =========//
while(1)
{
doc_ADC_1();
tinhtoan1(x);
//=============
doc_ADC_2();
tinhtoan2(y);
//=============
doc_ADC_3();
tinhtoan3(z);;
//==============
while(flag==1) //Chuong trinh se xu ly khung truyen duoc o day//
{
flag=0;
xulykhung();
mangdata==0;
index=0;
}
}
}
void doc_ADC_1()
{
set_ADC_channel(0);
x=read_ADC();
delay_us(10);
}
void doc_ADC_2()
{
set_ADC_channel(1);
y=read_ADC();
delay_us(10);
}
void doc_ADC_3()
{
set_ADC_channel(3);
z=read_ADC();
delay_us(10);
}
void tinhtoan1(int m)
{
x=x*2.049;
m=x;
chuc=m;
}
void tinhtoan2(int n)
{
y=y*2.049;
n=y;
tram=n;
}
void tinhtoan3(int k)
{
z=z*2.049;
k=z;
donvi=k;
}
void xuat_pwm()
{
setup_ccp2(ccp_pwm);
set_pwm2_duty(duty);
setup_timer_2(t2_div_by_16,255,1);
}
void xulykhung()
{
disable_interrupts(int_timer1); //Khong cho phep ngat timer-1 khi dang xu ly khung//

//==== khung xu ly du lieu cho kenh 1 xu ly dong ngat relay ngo ra B1 ========//
if((mangdata[0]==':')&&(mangdata[1]=='1')&&(mangdata[2]=='L')&&(mangdata[3]=='M'))
{
switch (mangdata[4])
{
case '1':
{b1=1;printf(":1D1M0#");
break;}
case '2':
{b1=0;printf(":1D2M0#");
break;}
}
}
//==== khung xu ly du lieu cho kenh 2 xu ly dong ngat relay ngo ra B2 ========//

if((mangdata[0]==':')&&(mangdata[1]=='1')&&(mangdata[2]=='L')&&(mangdata[3]=='T'))
{
switch (mangdata[4])
{
case '1':
{b2=1;printf(":1D1T0#");
break;}
case '2':
{b2=0;printf(":1D2T0#");
break;}
}
}
//==== khung xu ly du lieu cho kenh 3 xu ly dieu xung chan ngo ra cpp2 ========//

if((mangdata[0]==':')&&(mangdata[1]=='1')&&(mangdata[2]=='L')&&(mangdata[3]=='D'))
{
switch(mangdata[4])
{
case '0':
{duty=0;xuat_pwm();
break;}
case '1':
{duty=25;xuat_pwm();
break;}
case '2':
{duty=50;xuat_pwm();
break;}
case '3':
{duty=75;xuat_pwm();
break;}
case '4':
{duty=100;xuat_pwm();
break;}
case '5':
{duty=125;xuat_pwm();
break;}
case '6':
{duty=150;xuat_pwm();
break;}
case '7':
{duty=175;xuat_pwm();
break;}
case '8':
{duty=200;xuat_pwm();
break;}
case '9':
{duty=225;xuat_pwm();
break;}
}
}
if((mangdata[0]==':')&&(mangdata[1]=='1')&&(mangdata[2]=='G')&&(mangdata[3]=='T'))
{ // :1GTx# :khung giam sat nhiet do //
putc(':');putc('1');putc('T');putc(tram+48);putc(c huc+48);putc(donvi+48);putc('#');
}
if((mangdata[0]==':')&&(mangdata[1]=='1')&&(mangdata[2]=='0')&&(mangdata[3]=='0')&&(mangdata[4]=='0'))
{ // :1000# :khung thoat chuong trinh
b1=0;b2=0,duty=0;
}
enable_interrupts(int_timer1); //Cho phep ngat timer-1 hoat dong tro lai//
}



code visual basic ạ:


Option Explicit
Dim b1 As String
Dim b2 As String
Dim b3 As String
Dim b4 As String
Dim b5 As String
Dim b6 As String
Dim b7 As String
Dim i As Integer
Dim Temperature1 As Integer
Dim Temperature2 As Integer
Dim Temperature3 As Integer
Dim ts1 As Long
Dim ts2 As Long
Dim ts3 As Long
Dim tt1 As Long
Dim tt2 As Long
Dim tt3 As Long
Dim k_d As Long
Dim k_p As Long
Dim k_i As Long
Dim k As Long
Dim l As Long
Dim t As Long
Dim ss1 As Long
Dim ss2 As Long
Dim ss3 As Long
Dim pot1 As Long
Dim pot2 As Long
Dim pot3 As Long

Dim biendem As Long

Private Sub cmd_conect_Click()
On Error GoTo OPEN_FAIL
If cmd_conect.Caption = "CONNECT" Then
cmd_conect.BackColor = vbRed
With MSComm1
.Settings = cmb_baud.Text + "N,8,1"
.CommPort = Switch(cmb_port.Text = "COM 1", 1, cmb_port.Text = "COM 2", 2, cmb_port.Text = "COM 3", 3, cmb_port.Text = "COM 4", 4, cmb_port.Text = "COM 5", 5, cmb_port.Text = "COM 10", 10)
End With
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
'Frame1.Enabled = False
cmb_port.Enabled = False
cmb_baud.Enabled = False
'cmd_auto.Enabled = True
cmd_conect.Caption = "DISCONNECT"
ElseIf cmd_conect.Caption = "DISCONNECT" Then
cmd_conect.BackColor = vbGreen
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
'Frame1.Enabled = True
cmb_port.Enabled = True
cmb_baud.Enabled = True
cmd_conect.Caption = "CONNECT"
'cmd_auto.Enabled = False
Else
End If
Exit Sub
OPEN_FAIL:
If MsgBox("Cong COM khong hien dien", vbOKCancel, "ERROR !") = vbCancel Then
End
End If
End Sub

Private Sub Command9_Click()
On Error GoTo ERROR_EXIT
If MsgBox("Ban muon thoat khoi chuong trinh dieu khien ?", vbOKCancel + vbQuestion, "EXIT?") = vbOK Then
If MsgBox("Ban co muon tat tat ca cac thiet bi sau khi thoat ?", vbYesNo + vbQuestion, "WARNING !!!") = vbYes Then
MSComm1.Output = ":1000#"
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End If
End If
Exit Sub
ERROR_EXIT:
End
End Sub

Private Sub Form_Load()
biendem = 1
'cmd_auto.Enabled = False
Timer2.Enabled = False
Form1.Hide
Form3.Hide
Form2.Show
With MSComm1
.InBufferSize = 1024 'Bo dem 1024 byte thi day
.InputLen = 0 'Doc toan bo gia tri bo dem
.RThreshold = 7 'Cho phep ngat khi su kien Receiver o MSCOMM nhan duoc 7 byte
.SThreshold = 0 'Ko cho phep ngat khi su kien Send o MMSCOMM
.OutBufferSize = 512
.InputMode = comInputModeText
End With
End Sub

Private Sub MSComm1_OnComm()
Dim data As String
If MSComm1.CommEvent = comEvReceive Then ''SU KIEN NHAN DATA
data = MSComm1.Input
End If
If Len(data) = 7 Then ''NEU DA NHAN DU 7 BYTE DATA
''TACH DATA NHAN DUOC THANH 7 BYTE DE KIEM TRA
b1 = Mid(data, 1, 1)
b2 = Mid(data, 2, 1)
b3 = Mid(data, 3, 1)
b4 = Mid(data, 4, 1)
b5 = Mid(data, 5, 1)
b6 = Mid(data, 6, 1)
b7 = Mid(data, 7, 1)
' '- - - - - CHUONG TRINH KIEM TRA DU LIEU NHAN DUOC - - - - ''
'NHAN DUOC ":1Txxx#" KHUNG TRA VE NHIET DO '
ElseIf b1 = ":" And b2 = "1" And b3 = "T" And b7 = "#" Then
nhietdoht_tram1 = Asc(b4)
nhietdoht_tram2 = Asc(b5)
nhietdoht_tram3 = Asc(b6)
Temperature1 = Val(nhietdoht_tram1)
Temperature2 = Val(nhietdoht_tram2)
Temperature3 = Val(nhietdoht_tram3)
ss1 = (ts1 - b4) * (100 / ts1)
ss2 = (ts2 - b5) * (100 / ts2)
ss3 = (ts3 - b6) * (100 / ts3)
pot1 = (ts1 - b4) / (ts1 * 100)
pot2 = (ts2 - b5) / (ts2 * 100)
pot3 = (ts3 - b4) / (ts3 * 100)
Call Draw
Call PID
Call on_off
Else
b1 = 0
b2 = 0
b3 = 0
b4 = 0
b5 = 0
b6 = 0
b7 = 0
data = 0
End If
End If
End Sub

Private Sub reset_Click() ' truyen xuong vi dieu khien la thong so nhiet do dat ve gia tri 0
On Error GoTo send_fail
t1.Text = 0
t2.Text = 0
t3.Text = 0
t4.Text = 0
t5.Text = 0
t6.Text = 0
kp.Text = 0
kd.Text = 0
ki.Text = 0
Exit Sub
send_fail:
If MsgBox("CONG COM CHUA DUOC MO HOAC DANG HOAT DONG VOI THIET BI KHAC", vbRetryCancel, "ERROR ! ") = vbRetry Then
If MsgBox("Vui long chon cong COM-toc do Baud va nhan Connect de ket noi", vbOKOnly, "SUPPORT") = vbOK Then
End If
End If
End Sub

Private Sub save_Click() ' lay thong so nhiet do duoi may tinh len de tinh toan
On Error GoTo send_fail
MSComm1.Output = ":2GT0#"
Exit Sub
send_fail:
If MsgBox("CONG COM CHUA DUOC MO HOAC DANG HOAT DONG VOI THIET BI KHAC", vbRetryCancel, "ERROR ! ") = vbRetry Then
If MsgBox("Vui long chon cong COM-toc do Baud va nhan Connect de ket noi", vbOKOnly, "SUPPORT") = vbOK Then
End If
End If
End Sub


Private Sub Draw()
Dim Y() As Byte
Dim Mang1(1 To 50000) As Long
Dim Mang2(1 To 50000) As Long
Dim Mang3(1 To 50000) As Long
'''''''''''''''''''''''''''''''''''''''''''''''''' ''
Mang1(biendem) = Temperature1
Mang2(biendem) = Temperature2
Mang3(biendem) = Temperature3
'''''''''''''''''''''''''''''''''''''''''''''''''' ''
With Form1.TChart1.Series(0)
.AddXY biendem, Mang1(biendem), "", vbRed
.Marks.Visible = True
End With
With Form1.TChart1.Series(1)
.AddXY biendem, Mang2(biendem), "", vbGreen
.Marks.Visible = True
End With
With Form1.TChart1.Series(2)
.AddXY biendem, Mang3(biendem), "", vbYellow
.Marks.Visible = True
End With
'''''''''''''''''''''''''''''''''''''''''''''''''' ''
With Form1.TChart1.Series(0)
If .Count > 15 Then .Delete 0
End With
With Form1.TChart1.Series(1)
If .Count > 15 Then .Delete 0
End With
With Form1.TChart1.Series(2)
If .Count > 15 Then .Delete 0
End With
biendem = biendem + 1
End Sub

Private Sub PID()
ts3 = t6.Text
k_p = kp.Text
k_d = kd.Text
k_i = ki.Text
k = t1.Text
l = t2.Text
t = t3.Text
tt3 = b6
Dim a0 As Long
Dim a1 As Long
Dim a2 As Long
Dim u0 As Long
Dim u As Long
a0 = k_p + k_d / t + k_i * t
a1 = -k_p + k_i * t - 2 * k_d / t
a2 = k_d / t
u0 = ts3 - tt3
u = u0 + a0 * u0 + a1 * u0 + a2 * u0
If (u > 0 And u < 25) Then
MSComm1.Output = ":1MT00#"
End If
If (u > 25 And u < 50) Then
MSComm1.Output = ":1MT00#"
End If
If (u >= 25 And u < 50) Then
MSComm1.Output = ":1MT10#"
End If
If (u >= 50 And u < 75) Then
MSComm1.Output = ":1MT20#"
End If
If (u >= 75 And u < 100) Then
MSComm1.Output = ":1MT30#"
End If
If (u >= 100 And u < 125) Then
MSComm1.Output = ":1MT40#"
End If
If (u >= 125 And u < 150) Then
MSComm1.Output = ":1MT50#"
End If
If (u >= 150 And u < 175) Then
MSComm1.Output = ":1MT60#"
End If
If (u >= 175 And u < 200) Then
MSComm1.Output = ":1MT70#"
End If
If (u >= 200 And u < 225) Then
MSComm1.Output = ":1MT80#"
End If
If (u >= 225 And u < 255) Then
MSComm1.Output = ":1MT90#"
End If
End Sub
Private Sub on_off()
ts1 = t4.Text
ts2 = t5.Text
tt1 = b4
tt2 = b5
If (ts1 > tt1) Then
MSComm1.Output = ":1D2M0#"
End If
If (ts1 < tt1) Then
MSComm1.Output = ":1D1M0#"
End If
If (ts2 < tt2) Then
MSComm1.Output = ":1D1T0#"
End If
If (ts2 > tt2) Then
MSComm1.Output = ":1D2T0#"
End If
End Sub


mong các anh chị hỗ trợ giúp
khienpzo vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Tags
help help


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 01:17 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam