ES Lab4 105
ES Lab4 105
EMBEDDED SYSTEMS
(ee-354)
Lab-04 (USART)
3.
4. // Function to initialize USART
5. void usart_init(void)
6. {
7. // Set baud rate to 9600 for 16 MHz clock
8. unsigned int ubrr = 103;
9. UBRR0H = (unsigned char)(ubrr >> 8);
10. UBRR0L = (unsigned char)ubrr;
11.
12. // Enable transmitter
13. UCSR0B = (1 << TXEN0);
14.
15. // Set frame format: 8 data bits, 1 stop bit, no parity
16. UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
17. }
18.
19. // Function to transmit a single character
20. void usart_putChar(char c)
21. {
22. // Wait for empty transmit buffer
23. while (!(UCSR0A & (1 << UDRE0)));
24.
25. // Put data into buffer, sends the data
26. UDR0 = c;
27. }
28.
29. int main(void)
30. {
31. usart_init(); // initialize USART
32.
33. while (1)
34. {
35. usart_putChar('A'); // Send character 'A'
36. usart_putChar('\r'); // Carriage return
37. usart_putChar('\n'); // New line
38. _delay_ms(1000); // Delay of 1 second
39. }
ES-Lab-4 Rayyan Khan EE-22105
40.
41. return 0;
42. }
Example: 2
Code:
1. #include <avr/io.h>
2. #include <util/delay.h>
3. #include <stdlib.h>
4. // Initialize USART
5. void usart_init(void)
6. {
7. uint16_t ubrr = 103; // Baud rate = 9600 for 16 MHz clock
8. UBRR0H = (unsigned char)(ubrr >> 8);
9. UBRR0L = (unsigned char)ubrr;
10. UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Enable receiver and transmitter
11. UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8 data bits, 1 stop bit, no parity
12. }
67. return 0;
68. }
ES-Lab-4 Rayyan Khan EE-22105
Task: 2
Code:
1. #include <avr/io.h>
2. #include <util/delay.h>
3. #include <stdlib.h>
4. void usart_init(void)
5. {
6. // Set baud rate to 14400
7. // UBRR = (F_CPU / (16 * BAUD)) - 1
8. // For F_CPU = 16MHz and BAUD = 14400: UBRR = 69
9. UBRR0 = 69; // 14400 baud @ 16MHz
53. return 0;
54. }
Code:
ES-Lab-4 Rayyan Khan EE-22105
1. #include <avr/io.h>
2. #include <util/delay.h>
3. #include <stdlib.h> // for dtostrf()
4.
5. #define F_CPU 16000000UL
6. #define BAUD 14400
7. #define UBRR_VALUE ((F_CPU / (16UL * BAUD)) - 1)
8.
9. void usart_init(void)
10. {
11. UBRR0H = (UBRR_VALUE >> 8);
12. UBRR0L = (UBRR_VALUE & 0xFF);
13. UCSR0B = (1 << TXEN0); // Enable transmitter only
14. UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8-bit data
15. }
16.
17. void usart_putChar(char data)
18. {
19. while (!(UCSR0A & (1 << UDRE0)));
20. UDR0 = data;
21. }
22.
23. void usart_putString(const char* str)
24. {
25. while (*str)
26. {
27. usart_putChar(*str++);
28. }
29. }
30.
31. void ADC_init(void)
32. {
33. ADMUX = (1 << REFS0); // AVcc as reference, ADC0
34. ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); // Enable ADC, prescaler 64
35. }
36.
37. uint16_t ADC_read(void)
38. {
39. ADCSRA |= (1 << ADSC); // Start conversion
40. while (ADCSRA & (1 << ADSC)); // Wait until done
41. return ADC;
42. }
43.
44. int main(void)
45. {
46. char adcStr[10];
47. char voltStr[10];
48. uint16_t adc_value;
49. float voltage;
50.
51. usart_init();
52. ADC_init();
53.
54. usart_putString("ADC and Voltage Reader Using dtostrf()\r\n");
55.
56. while (1)
57. {
58. adc_value = ADC_read();
59. voltage = (adc_value * 5.0) / 1023.0;
60.
61. itoa(adc_value, adcStr, 10); // Convert ADC int to string
62. dtostrf(voltage, 5, 2, voltStr); // Convert float to string: width 5, precision 2
ES-Lab-4 Rayyan Khan EE-22105
63.
64. usart_putString("ADC: ");
65. usart_putString(adcStr);
66. usart_putString(", Voltage: ");
67. usart_putString(voltStr);
68. usart_putString(" V\r\n");
69.
70. _delay_ms(1000);
71. }
72.
73. return 0;
74. }
To transmit
the analog
voltage
across a
potentiometer
read by the
ATmega328P
ADC to PC