-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
56 lines (45 loc) · 1.38 KB
/
Copy pathmain.c
File metadata and controls
56 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* UART.c
*
* Created: 04-07-2018 13:26:25
* Author : Sahil
*/
#define F_CPU 14745600UL
#include <avr/io.h>
#include <avr/interrupt.h>
#define BAUD 9600 // UART baud rate
#define BAUDRATE ((F_CPU / (BAUD * 16UL)) - 1)
#define indicator_1 PORTB // Output port for indication
// Function prototypes
void port_init(void);
void serialstart_1(void);
// Global variable to hold received UART data
volatile int value_1 = 0;
int main(void)
{
sei(); // Enable global interrupts
port_init(); // Initialize ports
serialstart_1(); // Initialize UART
while (1)
{
indicator_1 = value_1; // Display received value on PORTB LEDs
}
}
// Initialize PORTB as output for indicator LEDs
void port_init(void)
{
DDRB = 0xFF; // Set PORTB as output
}
// Initialize UART1 for 9600 baud, 8 data bits, no parity, 1 stop bit
void serialstart_1(void)
{
UBRR1H = (BAUDRATE >> 8); // Set baud rate high byte
UBRR1L = BAUDRATE; // Set baud rate low byte
UCSR1B = (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1); // Enable RX, TX and RX interrupt
UCSR1C = (1 << UCSZ11) | (1 << UCSZ10); // 8-bit data frame
}
// UART RX Complete Interrupt Service Routine
ISR(USART1_RX_vect)
{
value_1 = UDR1; // Read received data from UART data register
}