vb编的上位机,和mega8通信不正确,请求帮助。
花了一天把硬件弄好了,可是现在用vb写的程序和单片机无法正确接收。这个问题看了好多网页都没有弄懂。刚学,请高手们帮忙看看。谢谢单片机程序是:每隔一秒发送一个字符。vb是事件触发。输出:
0
0
152
230
24然后就不动了。。。
#include <iom8v.h>
#include <macros.h>
#define fosc 8000000
#define baud 9600
#define uchar unsigned char
#define uint unsigned int
/****************全局变量定义*******************/
unsigned char timeNum,count;
unsigned char TimeFlag,Num;
unsigned char a;
void putchar(unsigned char c){
while(!(UCSRA &(1<<UDRE)));
UDR=c;
}
unsigned char getchar(void){
while(!(UCSRA &(1<<RXC)));
return UDR;
}
void uart_init(void){
UCSRB=(1<<RXEN)|(1<<TXEN);
UBRRL=(fosc/16/baud-1)%256;
UBRRH=(fosc/16/baud-1)/256;
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}
void timer0_init(void)
{
TCCR0 = 0x00;//停止定时器
TIMSK |= 0x01;//中断允许
TCNT0 = 0x06;//初始值
TCCR0 = 0x03;//启动定时器 clk/64
}
#pragma interrupt_handler timer0_isr:10
void timer0_isr(void)
{
TCNT0 = 0x06; //重装初始值6
if(++count==5) //是否百分秒
{count=0;
timeNum++;
if(timeNum==100)//1秒
{timeNum=0;
if(Num<=17)putchar(a);
else {
putchar(0x0a);
putchar(0x0b);
Num=1;
TimeFlag=1;
}
}
}
}
void main(void){
unsigned char i;
timer0_init();
uart_init();
SEI();
DDRD = 0xFF; /* output */
PORTD = 0x00; /* all off */
a=0xaa; //开始位
for (i=2; i<18; i++)
a=i;
TimeFlag=0; //1s定时到,置为1
while(1)
{ if (TimeFlag==1)
for (i=2; i<18; i++)
a=a+1;
TimeFlag=0;
}
}
——————————————————————
Option Explicit
Dim out() As Byte
Dim buff As String
Private Sub Command1_Click()
MSComm1.PortOpen = False
Unload Me
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 1
MSComm1.InBufferSize = 60
MSComm1.InputMode = comInputModeBinary
'¶Ë¿Ú´ò¿ª
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
MSComm1.RThreshold = 1
MSComm1.SThreshold = 10
Timer1.Interval = 500
Timer1.Enabled = True
End Sub
Private Sub MSComm1_OnComm()
Dim i
Dim var As Variant, lngData() As Byte
With MSComm1
Select Case .CommEvent
Case comEvReceive
.RThreshold = 0
var = MSComm1.Input
lngData = var
For i = 0 To UBound(lngData)
Text1.Text = Trim(Text1.Text) + Str(lngData(i))
Next
Text1.Text = Trim(Text1.Text) + Chr(13) + Chr(10)
.RThreshold = 1
End Select
End With
End Sub 分步调试,先用串口调试助手检查单片机发出的数据是否正确,然后再调上位机程序.
页:
[1]