em viết chương trình thế này,ko chạy được. Ai chỉ cho em biết sai ở đâu vậy.
chương trình VB:
Private Sub chkled1_Click()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If chkled1.Value = vbChecked Then
led1.FillColor = vbRed
MSComm1.Output = "1" 'xuat qua cong Com gia tri 1
Else
led1.FillColor = vbBlack
MSComm1.Output = "4" 'xuat ra cong COM gia tri 4
End If
End Sub
Private Sub chkled2_Click()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If chkled2.Value = vbChecked Then
led2.FillColor = vbRed
MSComm1.Output = "2" 'xuat qua cong Com gia tri 2
Else
led2.FillColor = vbBlack
MSComm1.Output = "5" 'xuat ra cong COM gia tri 5
End If
End Sub
Private Sub chkled3_Click()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If chkled3.Value = vbChecked Then
led3.FillColor = vbRed
' neu check thi mo cong
MSComm1.Output = "3" 'xuat qua cong Com gia tri 3
Else
led3.FillColor = vbBlack
MSComm1.Output = "6" 'xuat ra cong COM gia tri 6
End If
End Sub
Private Sub Form_Load()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If MSComm1.PortOpen Then MSComm1.PortOpen = False
MSComm1.CommPort = 2
MSComm1.RThreshold = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InBufferCount = 0
MSComm1.InputLen = 0
MSComm1.PortOpen = True
End Sub
Chương trình VXL:
#include<avr/io.h>
#include<avr/iom16.h>
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int ubrr);
unsigned char USART_Receive( void );
void main(void)
{
char data;
DDRA=0xff;
PORTA=0x00;
USART_Init( MYUBRR );
while(1)
{
USART_Receive();
data=UDR;
switch (data)
{
case 0x31:
PORTA=PORTA|0x01;
break;
case 0x34:
PORTA=PORTA&0xfe;
break;
case 0x32:
PORTA=PORTA|0x02;
break;
case 0x35:
PORTA=PORTA&0xfd;
break;
case 0x33:
PORTA=PORTA|0x04;
break;
case 0x36:
PORTA=PORTA&0xfb;
break;
}
}
}
void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
return UDR;
}
mục đích của em là dùng VB điều khiển led thôi,nhưng ko chạy được. Mong được giải đáp sớm.
chương trình VB:
Private Sub chkled1_Click()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If chkled1.Value = vbChecked Then
led1.FillColor = vbRed
MSComm1.Output = "1" 'xuat qua cong Com gia tri 1
Else
led1.FillColor = vbBlack
MSComm1.Output = "4" 'xuat ra cong COM gia tri 4
End If
End Sub
Private Sub chkled2_Click()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If chkled2.Value = vbChecked Then
led2.FillColor = vbRed
MSComm1.Output = "2" 'xuat qua cong Com gia tri 2
Else
led2.FillColor = vbBlack
MSComm1.Output = "5" 'xuat ra cong COM gia tri 5
End If
End Sub
Private Sub chkled3_Click()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If chkled3.Value = vbChecked Then
led3.FillColor = vbRed
' neu check thi mo cong
MSComm1.Output = "3" 'xuat qua cong Com gia tri 3
Else
led3.FillColor = vbBlack
MSComm1.Output = "6" 'xuat ra cong COM gia tri 6
End If
End Sub
Private Sub Form_Load()
form1.BackColor = vbYellow
Frame1.BackColor = vbGreen
If MSComm1.PortOpen Then MSComm1.PortOpen = False
MSComm1.CommPort = 2
MSComm1.RThreshold = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InBufferCount = 0
MSComm1.InputLen = 0
MSComm1.PortOpen = True
End Sub
Chương trình VXL:
#include<avr/io.h>
#include<avr/iom16.h>
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int ubrr);
unsigned char USART_Receive( void );
void main(void)
{
char data;
DDRA=0xff;
PORTA=0x00;
USART_Init( MYUBRR );
while(1)
{
USART_Receive();
data=UDR;
switch (data)
{
case 0x31:
PORTA=PORTA|0x01;
break;
case 0x34:
PORTA=PORTA&0xfe;
break;
case 0x32:
PORTA=PORTA|0x02;
break;
case 0x35:
PORTA=PORTA&0xfd;
break;
case 0x33:
PORTA=PORTA|0x04;
break;
case 0x36:
PORTA=PORTA&0xfb;
break;
}
}
}
void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
return UDR;
}
mục đích của em là dùng VB điều khiển led thôi,nhưng ko chạy được. Mong được giải đáp sớm.
Comment