AVR/Source/UART

From OmegaV-wiki
Jump to: navigation, search

This code can be used to transmit data through an RS232 cable. You can use UART_Tx to transmit data, and the function receive_data will be called when data is passed to the AVR. The keyword in the SIGNAL function is specific to the AVR used. In this example the ATMEGA168 is used.

uart.h

<source lang=c>

  1. ifndef _UART_H_
  2. define _UART_H_
  3. include <inttypes.h>

//Set this to your chips frequency

  1. define F_CPU 4000000

void UART_init(void); void UART_Tx(unsigned char); void UART_Tx_ASCII(uint8_t); uint8_t UART_Rx(void);

  1. endif

</source>

uart.c

<source lang=c>

  1. include "uart.h"
  2. include <avr/io.h>
  3. include <avr/interrupt.h>
  4. include <util/setbaud.h>
  1. define BAUD 9600
  2. define UBRRH_VALUE (F_CPU/8/BAUD-1)


/*****************************************************************************

  • Function name : UART_init
  • Returns : None
  • Parameters : None
  • Purpose : Initiate the UART
                                                                                                                                                          • /

void UART_init(void){

  UBRR0H = UBRRH_VALUE;
  UBRR0L = UBRRL_VALUE;
  // If you use 2X (if not you have to change UBRRH_VALUE to div by 16)
  UCSR0A |= (1 << U2X0);
  //Enable Receiving, transmitting and enable receive interupt.
  UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0)|(0<<UDRIE0)|(0<<UCSZ02);
  // Async. mode, 8N1
  UCSR0C = (0<<UMSEL01)|(0<<UMSEL00)|(0<<UPM01)|(0<<UPM00)|(0<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00)|(0<<UCPOL0);

}


/*****************************************************************************

  • Function name : UART_Tx
  • Returns : None
  • Parameters : char data: byte to send
  • Purpose : Send one byte through the UART
                                                                                                                                                          • /

void UART_Tx(unsigned char data) {

   while (!(UCSR0A & (1<<UDRE0)));
   UDR0 = data;

}


/*****************************************************************************

  • Function name : UART_Rx
  • Returns : char: byte received
  • Parameters : None
  • Purpose : Receives one byte from the UART
                                                                                                                                                          • /

uint8_t UART_Rx(void) {

   while (!(UCSR0A & (1<<RXC0)));
   return UDR0;

}

/*****************************************************************************

  • Function name : UART_Tx_ASCII
  • Returns : None
  • Parameters : uint8_t data: data to send
  • Purpose : Sends a number through the USART according to ASCII
                                                                                                                                                          • /

void UART_Tx_ASCII(uint8_t data){ uint8_t output[3] = {0,0,0};

output[2] = data%10; data = data/10; output[1] = data%10; data = data/10; output[0] = data%10;

UART_Tx(output[0]+48); UART_Tx(output[1]+48); UART_Tx(output[2]+48);


}


/****************************************************************************

  • Function Name : USART_RX_vect
  • Returns : None
  • Parameters : None
  • Purpose : Interrupt service routine for Data received from UART
  • The code in SIGNAL depends on your AVR
                                                                                                                                                          • /

SIGNAL(USART_RX_vect){ receive_data();

}


</source>

Personal tools