Captain's Universe Home
Captain's Universe Home
Cosmic Ray Muon DetectorTeleGarden Pages
Time on MarsBryophyllum Plants
Jupiter Radio AstronomyAncient Pages
Salzburg Tourist GuideEarth Magnetometer
  H O M E     AJAX & MORE     LINUX & MORE     RTAI     XENOMAI     ADEOS IPIPE      
    JAVA & BROWSERS     *NIX     ELECTRONICS     REVIEWS     ARTEMIA     FAIRY SHRIMP      



Atmel ATmega (ATmega16 / ATmega32) Serial Port Example Schematic and C program

UPDATE: Since the Atmega32 MCU is just an ATmega16 with more Flash, RAM etc. the ATmega16 can be replaced by the ATmega32.

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).



Here is the schematic (ATmega16 pins for the DIP packages):
Atmel ATmega16 Serial Port Example Schematic
Click on the schematic for a large view (opens in a new window)


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).
/*********************************************
* 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
#-------------------



Last-Modified: Mon, 19 Feb 2007 22:26:32 GMT

Google
 
Web www.captain.at
go to top
© 1996-2010 . All rights reserved.
No reproduction, distribution, publishing or transmission of the copyrighted materials at this site is permitted. Policy
go to top