|
| |||||||
|
Опубликовано 2010-05-07 11:30:31 автором Ruslan Data transfer via the UART AVR. Lesson 9 AVRMicrocontroller to communicate with his older brother - the PC - you can use the data interfaces , but since I'm training on AVR I try to build on the microcontroller mega8 , it is preferable to communicate with all PC use interface USART . No, you can certainly zayuzat USB, but USART in mega8 implemented in hardware , which means that we do not have to manually implement logic data , we simply write the data into the register , but the process itself is already sending steer MK hardware . Protocol UART (Universal asynchronous receiver / transmitter) is one of the most common protocols for data transfer between devices. At one Atmega8 USART, he launched on legs PD0 - Rx, receiver ( receiver) and PD1 - Tx, transmitter ( transmitter) .
# include <mega8.h>
void main (void)
{
char date = 0 ;
PORTC = 0x00;
DDRC = 0x01;
/ / 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 = 0x18;
UCSRC = 0x86;
UBRRH = 0x00;
UBRRL = 0x33;
while ( 1 )
{
date = UDR;
if (date == '1 ') PORTC.0 = 1 ;
if (date == '0 ') PORTC.0 = 0 ;
} ;
}
Do AVR have register UDR . In fact , these are two different registers , which have one address . When the write data, then they get to the register of the transmitter , and when we read - taken from the receive register . To send , we just write the data into the register and they will fly to the receiver , and if something came to us - we read them from the register . Follow UART no need to turn all the service you can hang on interruption. When we come to the data , an interrupt is generated on completion of reception, and when the data will go - will be caused devastation interrupt Compile program throws in microns in Proteus . To send data from the terminal should tick near:
Enter "0" - LED goes
Job interruptAs you have noticed , our program infinitely waits for bytes. Impractical to do so because the main loop loaded sending and receiving data via uart and nothing else can not do. Of course , you can not get hung up , and periodically for example every 10 milliseconds to check if we have not come data for the rest of their time doing something . But then, if the channel is set at high speed , you can skip bytes . Best of all this stir raspberries interrupt : the data came - volunteered to interrupt handler and all . For this CodeWizard AVR USART tab should tick near Rx Interraptgenerates code and get
# include <mega8.h>
# ifndef RXB8
# define RXB8 1
# endif
# ifndef TXB8
# define TXB8 0
# endif
# ifndef UPE
# define UPE 2
# endif
# ifndef DOR
# define DOR 3
# endif
# ifndef FE
# define FE 4
# endif
# ifndef UDRE
# define UDRE 5
# endif
# ifndef RXC
# define RXC 7
# endif
# define FRAMING_ERROR ( 1 < < FE)
# define PARITY_ERROR ( 1 < < UPE)
# define DATA_OVERRUN ( 1 < < DOR)
# 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 ;
};
};
if (data == '1 ') PORTC.0 = 1 ;
if (data == '0 ') PORTC.0 = 0 ;
}
# include <stdio.h>
void main (void)
{
/ / Port C initialization
PORTC = 0x00;
DDRC = 0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA = 0x00;
UCSRB = 0x90;
UCSRC = 0x86;
UBRRH = 0x00;
UBRRL = 0x33;
/ / Global enable interrupts
# asm ("sei")
while ( 1 )
{
};
}
Here codewizard generated different parity, and a buffer for the data records that came from. Us to light the LED this is not necessary , so remove unnecessary and get
# include <mega8.h>
# include < stdio.h >
/ / USART Receiver interrupt service routine interrupt [USART_RXC] void usart_rx_isr (void)
{
char data;
data = UDR;
if (data == '1 ') PORTC.0 = 1 ;
if (data == '0 ') PORTC.0 = 0 ;
}
void main (void)
{
// Port C initialization
PORTC = 0x00;
DDRC = 0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA = 0x00;
UCSRB = 0x90;
UCSRC = 0x86;
UBRRH = 0x00;
UBRRL = 0x33;
// Global enable interrupts
# asm ("sei")
while ( 1)
{
};
}
As you can see , the main loop is free. Now we do not need to be checked regularly , and do not come any data on uart. When will byte uart, then the interrupt handler is invoked , and we will make it necessary for us action . Proceed to iron If your PC has COM port you to interface with the microcontroller will need a level converter TTL - RS232. It is easy to assemble on-chip MAX232. It is needed because the output voltage data from COM- port of the PC :
If your PC has no COM ports , you need juzat adapter chip FT232 - it connects to USB your PC and appears in the system virtual COM port , and on the findings of the appropriate signals FT232 UART.
Комментарии - (2)
Добавить комментарийДля отправки комментария вы должны авторизоваться. |
|||||||