Mình chỉ làm test đơn giản như sau :
- Phần cứng : máy tính <--> max232 <--> 75176 <----> 75176 <---> atmega8.Trên chương trình VB sẽ gửi kí tự xuống và atmega8 sẽ nhận và gửi trở lại VB. Nhưng sửa rồi kiểm tra phần cứng mà không được, không thấy có kí tự gửi về Text box của VB. Bạn nào làm chạy rồi chỉ mình biết có thể sai do đâu :
- code vb đoạn gửi dữ liệu ra bus rs485 :
- code vb của cả chương trình :
- Phần chương trình cho atmega8 mình test với 1 con nên chỉ nhận data từ máy tính rồi gửi lại thôi . Bình thường vdk đưa pin_23_75176 (low để nhận dữ liệu), khi cần truyền dữ liệu về máy tính thì lại đưa chân này lên mức cao.
.
Mình dùng thạch anh 16Mhz , phần cứng có lẽ tạm nhưng phần mềm không biết sai chỗ nào nên không có dữ liệu gửi về . nhờ các bạn chỉ cho mình chỗ sai để khắc phục.
- Phần cứng : máy tính <--> max232 <--> 75176 <----> 75176 <---> atmega8.Trên chương trình VB sẽ gửi kí tự xuống và atmega8 sẽ nhận và gửi trở lại VB. Nhưng sửa rồi kiểm tra phần cứng mà không được, không thấy có kí tự gửi về Text box của VB. Bạn nào làm chạy rồi chỉ mình biết có thể sai do đâu :
- code vb đoạn gửi dữ liệu ra bus rs485 :
Code:
Private Sub cmdSend_Click()
MSComm1.RTSEnable = False ' Pin7_DB9 = 0 ( pin2+3_ic75176 = 1 ==> truyền data)
MSComm1.Output = Text2.Text ' đưa dữ liệu xuống bộ đệm phát
MSComm1.RTSEnable = True ' Disable pin7_DB9 = 1 (pin2+3_ic75176 = 0 ==> nhận data)
End Sub
Code:
Private Sub cmdClear_Click()
Text1.Text = ""
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdSend_Click()
MSComm1.RTSEnable = False ' Pin7_232 = 0
MSComm1.Output = Text2.Text
MSComm1.RTSEnable = True ' Disable pin7_rs232 = 1
End Sub
Private Sub D_RTS_Click()
MSComm1.RTSEnable = True ' Disable pin7_rs232 = 1
End Sub
Private Sub Form_Load()
cmdSend.Caption = "&Send"
Text1.Text = ""
Text2.Text = ""
Text1.Enabled = False
cmdExit.Caption = "&Exit"
With MSComm1
.Settings = "9600,N,8,1"
.CommPort = 1
.RThreshold = 1
.SThreshold = 0
.InputMode = comInputModeText
.InputLen = 0
.Handshaking = comNone
.InBufferSize = 1024
.OutBufferSize = 1024
If .PortOpen = False Then
.PortOpen = True
End If
End With
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
If MSComm1.CommEvent = comEvReceive Then
Text1.Text = Text1.Text + MSComm1.Input
End If
End Sub
Private Sub RTS_ENABLE_Click()
MSComm1.RTSEnable = False ' Pin7_232 = 0
End Sub
Code:
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 11/14/2009
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega8
Program type : Application
Clock frequency : 16.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];
#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
{
--tx_counter;
UDR=tx_buffer[tx_rd_index];
PORTB.0 = 1; // ------------------> pin 2+3_ic75176 de truyen du lieu ve lai may tinh
if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
{
tx_buffer[tx_wr_index]=c;
if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
++tx_counter;
}
else
{
UDR=c;
PORTB.0 = 1; // ------------------> pin 2+3_ic75176 de truyen du lieu ve lai may tinh
}
#asm("sei")
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
void main(void)
{
PORTB=0x00;
DDRB=0x03;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x67;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
PORTB.0 = 0; // chan pin2+3 _ic75176 = 0 (low) de cho nhan du lieu
if(rx_counter > 0)
{
putchar(getchar()); // gui ve lai may tinh
}
}
}
Mình dùng thạch anh 16Mhz , phần cứng có lẽ tạm nhưng phần mềm không biết sai chỗ nào nên không có dữ liệu gửi về . nhờ các bạn chỉ cho mình chỗ sai để khắc phục.

Comment