0% found this document useful (0 votes)
15 views8 pages

ES Lab4 105

The document is a lab report for an Embedded Systems course, detailing Lab 04 focused on USART (Universal Synchronous and Asynchronous serial Receiver and Transmitter). It includes code examples for initializing USART, transmitting and receiving characters and strings, and reading analog values using ADC. The report is authored by Rayyan Khan and includes group members, submission details, and various code snippets demonstrating USART functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

ES Lab4 105

The document is a lab report for an Embedded Systems course, detailing Lab 04 focused on USART (Universal Synchronous and Asynchronous serial Receiver and Transmitter). It includes code examples for initializing USART, transmitting and receiving characters and strings, and reading analog values using ADC. The report is authored by Rayyan Khan and includes group members, submission details, and various code snippets demonstrating USART functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ES-Lab-4 Rayyan Khan EE-22105

EMBEDDED SYSTEMS
(ee-354)
Lab-04 (USART)

➢ Name: Rayyan Khan


➢ Roll No.: EE-22105
➢ Section: C
➢ Instructor: Miss Aiman Najeeb
➢ Submission: 21-April-2025
Group Members for Hardware:
➢ Rayyan Khan (EE-22105)
➢ Sajjad Raza (EE-22105)
➢ Yahya Amin (EE-22121)
➢ Bilal Ahmed (EE-22129)
ES-Lab-4 Rayyan Khan EE-22105
LAB # 04
Task: 1 & Example 1
Code:
1. #include <avr/io.h>
2. #include <util/delay.h>

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. }

13. // Send one character


14. void usart_putChar(char c)
15. {
16. while (!(UCSR0A & (1 << UDRE0))); // Wait for buffer to be empty
17. UDR0 = c;
18. }

19. // Send a string


20. void usart_putString(const char *str)
21. {
22. while (*str)
23. {
24. usart_putChar(*str++);
25. }
26. }

27. // Receive a character


28. char usart_getChar(void)
29. {
30. while (!(UCSR0A & (1 << RXC0))); // Wait for data
31. return UDR0;
32. }

33. // Receive a string


34. void usart_getString(char *buffer, uint8_t length)
35. {
36. for (uint8_t i = 0; i < length - 1; i++)
37. {
38. char c = usart_getChar();
39. if (c == '\r' || c == '\n') // Stop on newline or carriage return
40. break;
ES-Lab-4 Rayyan Khan EE-22105
41. buffer[i] = c;
42. }
43. buffer[length - 1] = '\0'; // Null-terminate the string
44. }

45. int main(void)


46. {
47. usart_init(); // initialize USART

48. // Transmitting and Receiving Character String


49. volatile char CNIC[13]; // Array to take input from received characters
50. uint8_t a = 13; // 13 char input is to be received (exact 13 digits)

51. usart_putString("Enter your 13-digit CNIC:\n\r");


52. usart_getString(CNIC, a); // receiving string

53. usart_putString("\n\rThe entered CNIC is: \n\r");


54. usart_putString(CNIC); // verify received characters by transmitting again

55. // Transmitting an Integer


56. usart_putString("\n\rDisplaying Integers:\n\r");
57. int num = 5555;
58. char buffer[sizeof(int) * 8 + 1];
59. itoa(num, buffer, 10); // convert integer to string
60. usart_putString(buffer);

61. // Transmitting a Float


62. usart_putString("\n\rDisplaying Float or Double:\n\r");
63. float temp = 23.51;
64. char str_temp[10];

65. dtostrf(temp, 5, 5, str_temp); // convert double to string


66. usart_putString(str_temp);

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

10. UCSR0A &= ~(1 << U2X0); // Single speed


11. UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Enable receiver and transmitter
12. UCSR0C = (0 << USBS0) | (1 << UCSZ01) | (1 << UCSZ00); // Async, No parity, 1 stop bit, 8 data bits
13. }

14. void usart_putChar(unsigned char data)


15. {
16. while (!(UCSR0A & (1 << UDRE0))) {}; // Wait for buffer to be empty
17. UDR0 = data; // Send data
18. while (!(UCSR0A & (1 << TXC0))) {}; // Wait until transmission complete
19. }

20. void usart_putString(char* StringPtr)


21. {
22. while (*StringPtr != 0x00)
23. {
24. usart_putChar((unsigned char)*StringPtr);
25. StringPtr++;
26. }
27. }

28. char usart_getChar()


29. {
30. while (!(UCSR0A & (1 << RXC0))) {}; // Wait for data reception
31. return UDR0; // Return received data
32. }

33. int main(void)


34. {
35. usart_init(); // Initialize USART

36. // Configure On-board LED pin (PB5 for Arduino Uno)


37. DDRB |= (1 << PB5); // Set PB5 (pin 13) as output

38. usart_putString("Send a character...\n\r");

39. while (1)


40. {
41. char received = usart_getChar(); // Receive a character from PC

42. // Display received character for confirmation


43. usart_putString("Received: ");
44. usart_putChar(received);
45. usart_putString("\n\r");
ES-Lab-4 Rayyan Khan EE-22105

46. if (received == 'A')


47. {
a. PORTB |= (1 << PB5); // Turn ON LED
48. }
49. else
50. {
a. PORTB &= ~(1 << PB5); // Turn OFF LED
51. }
52. }

53. return 0;
54. }

Letter other than ‘A’ Letter ‘A’


TASK: 3

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

You might also like