Full Version : Captain's Connecting MAX232 to AVR
avr >>COMMUNICATIONS & WEB PROJECTS >>Captain's Connecting MAX232 to AVR


AVR_Admin- 04-29-2006
Atmel ATmega (ATmega16) Serial Port Example Schematic and C program
by Web Captain

This is an example for connecting a MAX232 serial port level converter to an Atmel ATmega16 microcontroller for communication with i.e. an PC over the serial port (RS232).

The firmware:

Use avr-gcc or WinAVR to compile it and the Atmel ATmega16 Parallel Port Programmer to program the uC.

The firmware sends one character ("C") at startup and then returns any character sent via the serial port in the interrupt service routine (ISR).

Linux software (serterm2) for receiving the initial character can be found at the PIC - MMC (Multi Media Card) Flash Memory Extension page. Also on this page, the program "ser". It sends three characters ("ABC") via the PC serial port and tries to receive them. These three characters are returned by the MCU via the interrupt service routine.

Furthermore the firmware blinks a LED connected to pin 19 (similar to avrledtest.c at the Atmel ATmega16 Parallel Port Programmer page).

CODE

/*********************************************
* Chip type           : ATmega16
* Clock frequency     : 4,000000 MHz
*********************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <inttypes.h>
#include <avr/iom16.h>

#define F_OSC 4000000             /* oscillator-frequency in Hz */
#define UART_BAUD_RATE 9600
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_OSC)/((UART_BAUD_RATE)*16l)-1)

void delay_ms(unsigned short ms) {
unsigned short outer1, outer2;
outer1 = 200;
while (outer1) {
 outer2 = 1000;
 while (outer2) {
  while ( ms ) ms--;
  outer2--;
 }
 outer1--;
}
}

void usart_putc(unsigned char c) {
  // wait until UDR ready
while(!(UCSRA & (1 << UDRE)));
UDR = c;    // send character
}

void uart_puts (char *s) {
//  loop until *s != NULL
while (*s) {
 usart_putc(*s);
 s++;
}
}

void init(void) {
// set baud rate
UBRRH = (uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_OSC)>>8);
UBRRL = (uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_OSC);

// Enable receiver and transmitter; enable RX interrupt
UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);

//asynchronous 8N1
UCSRC = (1 << URSEL) | (3 << UCSZ0);
}

// INTERRUPT can be interrupted
// SIGNAL can't be interrupted
SIGNAL (SIG_UART_RECV) { // USART RX interrupt
unsigned char c;
c = UDR;
usart_putc(c);
}

int main(void) {
init(); // init USART
sei();  // enable interrupts

// send initial character
while(!(UCSRA & (1 << UDRE)));
UDR = 0x43; // "C"
while(!(UCSRA & (1 << UDRE)));
UDR = 0x0d;

// enable  PD5 as output
DDRD |= (1<<PD5);
while (1) {
 // PIN5 PORTD clear -> LED off
 PORTD &= ~(1 << PD5);
 delay_ms(500);
 // PIN5 PORTD set -> LED on
 PORTD |= (1 << PD5);
 delay_ms(500);
}
return 0;
}

Makefile:
MCU=atmega16
CC=avr-gcc
OBJCOPY=avr-objcopy
# optimize for size:
CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues
#-------------------
all: usart.hex
#-------------------
usart.hex : usart.out
$(OBJCOPY) -R .eeprom -O ihex usart.out usart.hex
usart.out : usart.o
$(CC) $(CFLAGS) -o usart.out -Wl,-Map,usart.map usart.o
usart.o : usart.c
$(CC) $(CFLAGS) -Os -c usart.c
# you need to erase first before loading the program.
# load (program) the software into the eeprom:
load: usart.hex
uisp -dlpt=/dev/parport0 --erase  -dprog=dapa
uisp -dlpt=/dev/parport0 --upload if=usart.hex -dprog=dapa  -v=3 --hash=32
#-------------------
clean:
rm -f *.o *.map *.out
#-------------------



Link to Site: http://www.captain.at/electronic-atmega16-serial-port.php


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.