AVR與VB通信模擬量輸入簡單程序
void uart_init()
{
UCSRB=0x00; //串口不使能
UCSRA=0x02; //串口倍速模式
UCSRC=0x06; //數(shù)據(jù)長度設(shè)置為8位
UBRRL=0x67; //串口設(shè)置為9600波特率
UBRRH=0x00;
UCSRB=0x98; //串口接受和發(fā)送使能,接受中斷使能
}
void uart_send(uchar k) //串口發(fā)送函數(shù)
{
while(!(UCSRA&(1< UDR=k;
}
uchar uart_receive()
{
while(!(UCSRA&(1<return UDR;
}
void send_number(uint k)
{
uart_send((uchar)(k>>8));
uart_send((uchar)(k));
}
#pragma vector = 0x2c
__interrupt void uart_rxc()
{
datt=UDR;
if(datt==a)PORTB=0xff;
else PORTB=0x00;
}
void main()
{
port_init();
uart_init();
time0_init();
adc_init();
SREG=0x80;
while(1)
{
adc_val=adc_vert();
delay(50000);
delay(50000);
delay(50000);
send_number(adc_val);
}
}
//----------------------VB代碼----------------------------
Private Sub Command1_Click()
MSComm1.CommPort = 4
MSComm1.PortOpen = True
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0
Private Sub MSComm1_OnComm()
Dim bytereceive() As Byte 定義字節(jié)數(shù)組
Dim strbuf As String 定義字符串變量
Select Case MSComm1.CommEvent
Case comEvReceive 接受事件發(fā)生
Text2 = MSComm1.InBufferCount 顯示接受緩沖區(qū)字節(jié)數(shù)
strbuf = MSComm1.Input 從輸入緩沖區(qū)讀取字符
bytereceive() = strbuf 字符串賦值給字符數(shù)組,低字節(jié)在前,高字節(jié)在后
Dim i As Integer
For i = 0 To UBound(bytereceive) UBound返回數(shù)組可用的最大長度
If Len(Hex(bytereceive(i))) = 1 Then
strdata = strdata & "0" & Hex(bytereceive(i))
Else
strdata = strdata & Hex(bytereceive(i))
End If
Next
Text1.Text = strdata 十六進制顯示
Text3.Text = Val("&H" & strdata) 十進制顯示
strdata = ""
End Select
評論