FB Twitter Google Plus
My Basket:

Shopping Cart
Your basket is currently empty.

Click to view the full basket.
Search Site

Call +44 (0)1974 261273

change currency to dollars change currency to pounds sterling change currency to euros
view the basket

AVR UART Code for STK200 and STK300 in WinAVR C

This UART code for AVR will work on STK200 and STK300 AVR boards and on most other UART
circuits. An initial value is transmitted every 250mS. When a byte is received on UART, the interrupt is triggered and value received is transmitted instead. The value is displayed on LEDs as a test.

Have a look at our BLOG for more microcontroller information

Popular Products

avrisp programmer

Works standalone or from AVRStudio, Now reduced by 25%

STK200 AVR Dragon
STK200 Dragon Kit picture

Complete training kit for AVR microcontrollers with AVR Dragon ICE and Programmer

STK300 Kit
STK300 AVR Training Kit picture

Low cost training kit for AVR microcontrollers with USB AVRISP, board and training material

AVR ISP 1.27mm (0.05") Adapter
Atmel AVR ISP 1.27mm Adapter

Adapter to convert standard AVR ISP 2.54 (0.1") output to 1.27mm (0.05")

AVR JTAG 1.27mm (0.05") Adapter
Atmel AVR JTAG 1.27mm Adapter

Adapter to convert standard AVR JTAG 2.54 (0.1") output to 1.27mm (0.05")

AVR Studio

Create a C project in AVRStudio and make sure the AVR microcontroller is the one you want to use. WinAVR C is very fussy about the correct device and although the code will compile, it will not run properly unless the AVR microcontroller type is correct

To change AVR microcontroller, go to Project > Configuration Options and select correct AVR.
Note that only the C Compiler project changes, not main project device type. Also change
Optimization to -0s otherwise compiler has problems with delay routines.

Connect to PC via standard serial cable, not Nul-modem cable. Run Hyper Teminal (Programs > Accessories > Communications) and create New connection, with Connect Using set to COM1 (or other COM Port). In COM1 Properties set Baud Rate to 38400, Data bits to 8, no Parity, 1 stop bit
and no flow control.

#define F_CPU 8000000UL  // 8 MHz

/*Very Important - change F_CPU to match target clock 
  Note: default AVR CLKSEL is 1MHz internal RC
  This program transmits continously on USART. Interrupt is used for 
	Receive charactor, which is then transmitted instead. LEDs are used 
	as a test. Normal RX routine is included but not used.
  Change USART_BAUDRATE constant to change Baud Rate

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

// Define baud rate
#define USART_BAUDRATE 38400   
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

volatile unsigned char value;  
/* This variable is volatile so both main and RX interrupt can use it.
It could also be a uint8_t type */

/* Interrupt Service Routine for Receive Complete 
NOTE: vector name changes with different AVRs see AVRStudio -
Help - AVR-Libc reference - Library Reference - <avr/interrupt.h>: Interrupts
for vector names other than USART_RXC_vect for ATmega32 */

   value = UDR;             //read UART register into value
   PORTB = ~value;          // output inverted value on LEDs (0=on)

void USART_Init(void){
   // Set baud rate
   UBRRL = BAUD_PRESCALE;// Load lower 8-bits into the low byte of the UBRR register
	 /* Load upper 8-bits into the high byte of the UBRR register
    Default frame format is 8 data bits, no parity, 1 stop bit
  to change use UCSRC, see AVR datasheet*/ 

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

void USART_SendByte(uint8_t u8Data){

  // Wait until last byte has been transmitted
  while((UCSRA &(1<<UDRE)) == 0);

  // Transmit data
  UDR = u8Data;

// not being used but here for completeness
      // Wait until a byte has been received and return received data 
uint8_t USART_ReceiveByte(){
  while((UCSRA &(1<<RXC)) == 0);
  return UDR;

void Led_init(void){
   //outputs, all off
	 DDRB =0xFF;       
   PORTB = 0xFF;        

int main(void){
   USART_Init();  // Initialise USART
   sei();         // enable all interrupts
   Led_init();    // init LEDs for testing
   value = 'A'; //0x41;    
   PORTB = ~value; // 0 = LED on
   for(;;){    // Repeat indefinitely
     USART_SendByte(value);  // send value 
		         // delay just to stop Hyperterminal screen cluttering up    

If you found this information useful, please give us a mention or share it on Social media.

return to support

      Product News       Special Offers       Sena Products       A-Z Product Index       Manufacturer Index       Software Downloads       Contact