| 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 #------------------- |