Skip to content

Commit 56c9ecd

Browse files
Source code upload for AVR USART TX RX
1 parent fc7253c commit 56c9ecd

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

ATmega328-328P_Datasheet_Full.pdf

5.17 MB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
avrdude -c usbasp -p m328p -U lfuse:w:0xFF:m

_1_Serial_Transmit/main.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
3+
4+
// AVR Send Character string "Hello from ATmega328p" Continously to the PC using Serial port.
5+
// PC receives the data and displays on terminal.
6+
// External Oscillator = 11.0592MHz
7+
8+
//+------------------------------------------------------------------------------------------------+
9+
//| Compiler : AVR GCC (WinAVR) |
10+
//| Microcontroller : ATmega328p |
11+
//| Programmer : Rahul.Sreedharan |
12+
//| Date : 16-July-2019 |
13+
//+------------------------------------------------------------------------------------------------+
14+
15+
//(C) www.xanthium.in
16+
// Visit to Learn More
17+
18+
#include <stdint.h>
19+
#include <avr/io.h>
20+
#include <util/delay.h>
21+
22+
// +-----------------------------------------------------------------------+ //
23+
// | ATmega328p Baudrate values for UBRRn for external crystal 11.0592MHz | //
24+
// +-----------------------------------------------------------------------+ //
25+
26+
#define BAUD_RATE_4800_BPS 143 // 4800bps
27+
#define BAUD_RATE_9600_BPS 71 // 9600bps
28+
29+
#define BAUD_RATE_14400_BPS 47 // 14.4k bps
30+
#define BAUD_RATE_19200_BPS 35 // 19.2k bps
31+
#define BAUD_RATE_28800_BPS 23 // 28.8k bps
32+
#define BAUD_RATE_38400_BPS 17 // 38.4k bps
33+
#define BAUD_RATE_57600_BPS 11 // 57.6k bps
34+
#define BAUD_RATE_76800_BPS 8 // 76.8k bps
35+
36+
#define BAUD_RATE_115200_BPS 5 // 115.2k bps
37+
#define BAUD_RATE_230400_BPS 2 // 230.4k bps
38+
39+
// +-----------------------------------------------------------------------+ //
40+
41+
42+
43+
int main()
44+
{
45+
int i = 0;
46+
unsigned int ubrr = BAUD_RATE_230400_BPS;
47+
48+
unsigned char data[] = "Hello from ATmega328p ";
49+
50+
/* Set Baudrate */
51+
UBRR0H = (ubrr>>8); // Shift the 16bit value ubrr 8 times to the right and transfer the upper 8 bits to UBBR0H register.
52+
UBRR0L = (ubrr); // Copy the 16 bit value ubrr to the 8 bit UBBR0L register,Upper 8 bits are truncated while lower 8 bits are copied
53+
54+
55+
56+
UCSR0C = 0x06; /* Set frame format: 8data, 1stop bit */
57+
UCSR0B = (1<<TXEN0); /* Enable transmitter */
58+
59+
while(1) /* Loop the messsage continously */
60+
{
61+
i = 0;
62+
while(data[i] != 0) /* print the String "Hello from ATmega328p" */
63+
{
64+
while (!( UCSR0A & (1<<UDRE0))); /* Wait for empty transmit buffer */
65+
66+
/* When UDRE0 = 0,data transmisson ongoing. */
67+
/* So NOT{[UCSR0A & (1<<UDRE0)] = 0} = 1 ,While(1) loop stays there */
68+
69+
/* When UDRE0 = 1,data transmisson completed. */
70+
/* So NOT{[UCSR0A & (1<<UDRE0)] = 1} = 0 ,While(0) loop fails */
71+
72+
UDR0 = data[i]; /* Put data into buffer, sends the data */
73+
i++; /* increment counter */
74+
}
75+
76+
77+
/* Sending '\n' '\r' Character pair helps to format the output properly on console putty Screen */
78+
/*************************************************************************************************/
79+
/* Send "\n" Character */
80+
while (!( UCSR0A & (1<<UDRE0))); /* Wait for empty transmit buffer */
81+
UDR0 = '\n'; /* Put data into buffer, sends the data */
82+
83+
/* Send "\r" Character */
84+
while (!( UCSR0A & (1<<UDRE0))); /* Wait for empty transmit buffer */
85+
UDR0 = '\r'; /* Put data into buffer, sends the data */
86+
/*------------------------------------------------------------------------------------------------*/
87+
88+
_delay_ms(100);
89+
90+
91+
}
92+
93+
}//end of main
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+

_2_Serial_Receive/main.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// AVR Serial Reception @ 230.4k bps
2+
// AVR Receives character 'A' Blinks the LED on PC4
3+
// 'B' Beeps the Buzzer on PC5
4+
// External Oscillator = 11.0592MHz
5+
// Controller ATmega328p
6+
7+
8+
//+------------------------------------------------------------------------------------------------+
9+
//| Compiler : AVR GCC (WinAVR) |
10+
//| Microcontroller : ATmega328p |
11+
//| Programmer : Rahul.Sreedharan |
12+
//| Date : 16-July-2019 |
13+
//+------------------------------------------------------------------------------------------------+
14+
15+
//(C) www.xanthium.in
16+
// Visit to Learn More
17+
18+
19+
#include <stdint.h>
20+
#include <avr/io.h>
21+
#include <util/delay.h>
22+
23+
// +-----------------------------------------------------------------------+ //
24+
// | ATmega328p Baudrate values for UBRRn for external crystal 11.0592MHz | //
25+
// +-----------------------------------------------------------------------+ //
26+
27+
#define BAUD_RATE_4800_BPS 143 // 4800bps
28+
#define BAUD_RATE_9600_BPS 71 // 9600bps
29+
30+
#define BAUD_RATE_14400_BPS 47 // 14.4k bps
31+
#define BAUD_RATE_19200_BPS 35 // 19.2k bps
32+
#define BAUD_RATE_28800_BPS 23 // 28.8k bps
33+
#define BAUD_RATE_38400_BPS 17 // 38.4k bps
34+
#define BAUD_RATE_57600_BPS 11 // 57.6k bps
35+
#define BAUD_RATE_76800_BPS 8 // 76.8k bps
36+
37+
#define BAUD_RATE_115200_BPS 5 // 115.2k bps
38+
#define BAUD_RATE_230400_BPS 2 // 230.4k bps
39+
40+
// +-----------------------------------------------------------------------+ //
41+
42+
43+
int main()
44+
{
45+
46+
unsigned int ubrr = BAUD_RATE_230400_BPS;
47+
48+
PORTC = 0x00; //All LED's OFF
49+
PORTD = 0x00;
50+
51+
/* Set Baudrate @ 230.4k bps */
52+
UBRR0H = (ubrr>>8);
53+
UBRR0L = (ubrr);
54+
55+
/*Enable receiver */
56+
UCSR0B = (1<<RXEN0);
57+
58+
/* Set frame format: 8data, 1stop bit */
59+
UCSR0C = 0x06;
60+
61+
while(1)
62+
{
63+
while ( !(UCSR0A & (1<<RXC0)) ); /* Wait for data to be received */
64+
65+
switch(UDR0) // using the switch() statement to control the LED and Buzzer
66+
{
67+
case 'A' : DDRC |= (1<<PC4); // Blinks LED
68+
PORTC |= (1<<PC4);
69+
_delay_ms(500);
70+
PORTC &= ~(1<<PC4);
71+
break;
72+
73+
case 'B' : DDRC |= (1<<PC5); // Beeps Buzzer
74+
PORTC |= (1<<PC5);
75+
_delay_ms(500);
76+
PORTC &= ~(1<<PC5);
77+
break;
78+
79+
default :
80+
break;
81+
}//end of Switch()
82+
}//end of While(1)
83+
}//end of main
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+

0 commit comments

Comments
 (0)