vutranthelan
25-11-2009, 06:28 AM
Cho em hỏi là tại sao khi em gửi 1 số 2 byte từ VB xuống PIC thì lần đầu tiên luôn ko truyến đúng. Sau đó em phải thoát VB ra rồi vào lại 1 lần nữa thì mới bắt đầu truyền đúng???
Đây là Code VB:
Private Sub Command1_Click()
Dim delay As Integer
Dim low_byte As Byte
Dim high_byte As Byte
delay = Text1.Text
low_byte = delay Mod 256
high_byte = delay \ 256
MSComm1.Output = Chr$(low_byte) + Chr$(high_byte)
Đây là code bên PIC:
char count,low_byte,high_byte;
int16 delay;
char value=0xff;
#int_RDA
RDA_isr()
{
if (kbhit())
{
switch (count)
{
case 0:low_byte=getc();
break;
case 1:high_byte=getc();
break;
default:break;
}
count++;
if(count=2)
{
count=0;
delay=(high_byte*256)+(low_byte);
}
}
}
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
delay=200;
while(1)
{
if(count=2)
{
count=0;
delay=(delay2*256)+(delay1);
}
output_b(value);
delay_ms(delay);
output_b(!value);
delay_ms(delay);
}
}
Cho em hỏi thêm là có cách nào truyền nhiều byte từ VB sang PIC và truyền nhiều byte từ PIC sang VB hay hơn không, em thấy cách này hơi "gà" thì phải,:D
Đây là Code VB:
Private Sub Command1_Click()
Dim delay As Integer
Dim low_byte As Byte
Dim high_byte As Byte
delay = Text1.Text
low_byte = delay Mod 256
high_byte = delay \ 256
MSComm1.Output = Chr$(low_byte) + Chr$(high_byte)
Đây là code bên PIC:
char count,low_byte,high_byte;
int16 delay;
char value=0xff;
#int_RDA
RDA_isr()
{
if (kbhit())
{
switch (count)
{
case 0:low_byte=getc();
break;
case 1:high_byte=getc();
break;
default:break;
}
count++;
if(count=2)
{
count=0;
delay=(high_byte*256)+(low_byte);
}
}
}
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
delay=200;
while(1)
{
if(count=2)
{
count=0;
delay=(delay2*256)+(delay1);
}
output_b(value);
delay_ms(delay);
output_b(!value);
delay_ms(delay);
}
}
Cho em hỏi thêm là có cách nào truyền nhiều byte từ VB sang PIC và truyền nhiều byte từ PIC sang VB hay hơn không, em thấy cách này hơi "gà" thì phải,:D