USART

Elliot Williams' Make: AVR Programming was helpful for this code.

Don’t forget to plug Rx to Tx and Tx to Rx! This is the only protocol I know of where this reversal is important.

I am using the Attiny2313 because it is, as far as I know, the only chip in the Attiny family that supports USART in hardware.

To view your Serial output in AVR Studio,

  1. Click Tools > Data Visualizer
  2. Under Configuration > Modules, click External Connection.
  3. Double click Serial Port
  4. Configure your port. e.g. Set the Baud Rate to 9600
  5. Select your COM port
  6. Click Connect.
  7. Enter in ‘j’ to see if the LED on pin PA0 blinks

A reminder, this will loop until a bit is set:

while (!(BIT & (1<<BIT)));  

Here is how to hook up the USART to your Attiny2313. Your chip should be powered by a 5V adapter. I have a status LED on PA0.

/*
 * ATTINY 2313 USART.c
 *
 * Created: 2/7/2019 6:04:16 PM
 * Author : FablabDigiscope

USB - FTDI 5V:
Rx to Tx
Tx to Rx
Gnd to Gnd

(NO LED ON chip's RX, but the one on TX doesn't seem to interfere)

To view your Serial output in AVR Studio,
1. Click Tools > Data Visualizer
2. Under Configuration>Modules, click External Connection.
3. Double click Serial Port
4. Configure your port. e.g. Set the Baud Rate to 9600
5. Select your COM port
6. Click Connect.

appears to send ASCII from AVR Studio terminal

 */

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

#define BAUD 9600                                   // define baud
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)            // set baud rate value for UBRR. I copied the UL and don't understand it!

// function to initialize UART
void uart_init (void)
{
	UBRRH = (BAUDRATE>>8);                      // shift the register right by 8 bits
	UBRRL = BAUDRATE;                           // set baud rate
	UCSRB|= (1<<TXEN)|(1<<RXEN);                // enable receiver and transmitter
	UCSRC|= (1<<UCSZ0)|(1<<UCSZ1);			   // 8bit data format + asynchronous mode
}

// function to send data
void uart_transmit (unsigned char data)
{
	while (!(UCSRA & (1<<UDRE)));                // wait while register is free
	UDR = data;                                   // load data in the register
}

unsigned char uart_recieve (void)
{
	while(!(UCSRA) & (1<<RXC));           // wait while data is being received
	return UDR;                             // return 8-bit data
}

int main(void)
{

DDRA = 0b00000001;
uart_init();

unsigned char a;

    while (1)
    {
		//transmit
// 	uart_transmit('alpha');
// 	PORTA = 0b00000001;
// 	_delay_ms(1000);
// 	PORTA = 0b00000000;
// 	_delay_ms(1000);

		//recieve
	a = uart_recieve();
	_delay_ms(100);


	if (a == 'j') //corresponds to a 'j' for instance, but not a 'c'
		{
			PORTA = 0b00000001;
			_delay_ms(1000);
			PORTA = 0b00000000;
			_delay_ms(1000);
			PORTA = 0b00000001;
			_delay_ms(1000);
			PORTA = 0b00000000;
			_delay_ms(1000);	//some kind of local echo thing makes it repeat?? Or else the register is not being cleared
		}
    }
}



You might also want to check out an ASCII character to hex/binary table.

Here’s the scope while sending different characters from AVR Studio’s serial monitor.