新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR與VB通信模擬量輸入簡單程序

AVR與VB通信模擬量輸入簡單程序

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏

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返回?cái)?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 十六進(jìn)制顯示

Text3.Text = Val("&H" & strdata) 十進(jìn)制顯示
strdata = ""

End Select

End Sub

上一頁 1 2 下一頁

關(guān)鍵詞: AVRVB通信模擬量輸

評(píng)論


技術(shù)專區(qū)

關(guān)閉