CS代考 4CCE1PHC – cscodehelp代写

4CCE1PHC
1 Introduction
in §2.
2 Preparation
Materials
Make sure you have everything you need to com- plete this lab.
The hardware items should be included in your lab kit.
Ensure that you have the Atmel ATmega328P Datasheet, and the Arduino Schematic Circuit Di- agram to hand.
Item Qty
1 USB Type A – Type B cable 1
Serial Communications
November 15, 2021
This lab introduces you to serial communications with the Arduino board. You will learn to use the USART interface to communicate between your computer and the microcontroller, and use this to create a simple debug interface for your embedded platform.
At the end of the lab, you should be able to:
• Configure and activate the USART interface to enable serial communications from a microcontroller.
• Set up two way commnication between your a microcontroller and a PC.
Before starting work you should read through this document and complete the preparatory tasks detailed
Ensure that you have the following software in-
stalled on your computer: (i) a terminal program
(e.g., xterm or similar on Linux/Mac OS, cygwin
on Windows), (ii) a text editor (e.g., gedit or similar on Linux/Mac OS, notepad++ on Windows), (iii) avr-libc (including avr-gcc, avr-objcopy and avrdude) (iv) the arduino IDE(optional), and (v) scopy.
Self-study
Read §3 below, read up on any new material (in textbooks and/or online) that you need to, to accomplish the tasks. Remember to make notes of where you got the information, so that you can quickly go there again if something does not work as expected.
Study and familiarise yourself with the data sheet for the ATmega328P and the avr-libc documentation related to serial communications. It is not necessary to understand all the details, but it is important that you familiarise yourself with the functions and configuration parameters available.
Dr Matthew Howard 1 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
Answer the following questions:
1. What does the acronym UART stand for? Universal asynchronous receiver/transmitter.
2. What baud rates are supported by the UART for serial communication? Any, but the most common
rates used are 9600, 1200, 2400, 4800, 19200, 38400, 57600, and 115200 bps.
3. How many devices can be connected to the UART simultaneously? Two, one transmitter, one
receiver.
4. When an 8-bit ASCII character is transmitted over the UART how many bits are sent, and what does each one represent? One start bit, eight data bits (can have between 5-9 data bits), optionally, one parity bit, and one or two stop bits. So at least 10 bits.
5. What two macros does the util/setbaud.h header require to be defined in advance? F_CPU and BAUD.
Dr Matthew Howard 2 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
3 Laboratory Work
3.1 Transmitting data from a Microcontroller using UART
In this part of the lab, you will create a program to transmit data from the microcontroller through the UART interface. This can be helpful for debugging programs or logging statistics as your embedded program executes, for instance, by displaying output or error messages. To complete this exercise, take the following steps:
1. Create a new file uart_tx.c and open it up in a text editor. We will start by writing skeleton code and add functionality as we go. Implement two functions in your skeleton code: (i) an empty function uart_init() that takes no arguments and returns no value (in the below, we will use this function to configure the UART interface), and (ii) a main() function that calls uart_init() and then exits. Include the headers avr/io.h, util/delay.h and util/setbaud.h. Make sure to define any macros that these headers require by referring to the avr-libc documentation. Verify that your program compiles without errors. Sample skeleton code is below.
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include /* Include IO header. */
#ifndef F_CPU
#define F_CPU 16000000UL /* Specify the CPU frequency
#endif
#include
#ifndef BAUD #define BAUD 9600
for delay.h. */
/* Specify the desired baud
rate for communication. */
#endif
#include
void uart_init(void) { /* Empty function, to be implemented as we go. */
}
int main(void) { /* Main function*/
uart_init(); /* Call UART intialisation function. */ return 0;
}
2. The next step is to configure the Arduino’s baud rate generator to ensure the timing of pulses corresponds to the expected bit rate. This requires us to set up the registers UBRR0H, UBRR0L appropriately. Fortunately, the util/setbaud.h library makes this easy, as the calculations are defined in the UBRRH_VALUE and UBRRL_VALUE macros. We will use the baud rate 9600bps. Adjust your code to set this baud rate, then use the following lines to include this configuration step in the uart_init() function:
UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE;
3. The next step is to configure the data transmission format. This is done by setting bits in the USART control and status registers A B & C (i.e., UCSR0A, UCSR0B and UCSR0C, respectively).
Dr Matthew Howard 3 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
We wish to transmit char data, so we need to set the number of data bits as 8 bits. Write a statement in the uart_init() function that uses the UCSR0C register to specify the number of data bits (i.e., character size) as 8 bits.
4. To send data from the Arduino, we also need to enable the UART transmitter. Write a statement in the uart_init() function that uses the UCSR0B register to do this as the last step of the initialisation. The function should now look like this:
1 2 3 4 5 6 7 8 9
10 11 12 13
void uart_init(void) {
UBRR0H = UBRRH_VALUE; /* Use the setbaud.h library to
set baud rate. */
}
UBRR0L = UBRRL_VALUE;
UCSR0C = (1< 2
3 #ifndef F_CPU
4 #define F_CPU 16000000UL
5 #endif
6
7 #ifndef BAUD
8 #define BAUD 9600
9 #endif
10 #include
11
12 void uart_init(void) {
13
14
15
16
17
18
19
20
21
22
23
24
25 } 26
27 int 28
29
30
31
32
33
34
35 }
Dr Matthew Howard © 2020
UBRR0H = UBRRH_VALUE; /* Use the setbaud.h library to set baud rate. */
UBRR0L = UBRRL_VALUE;
UCSR0C = (1< /* Include IO library. */ 2
3 #ifndef F_CPU
4 #define F_CPU 16000000UL /* Define CPU frequency. */
5 #endif
6 #ifndef BAUD
7 #define BAUD 9600
8 #endif
9 #include
10
11 void uart_init(void) {
/* Set baud rate.
*/
Specify number of data bits. For 8-bit data , need UCSZ02=0, UCSZ01=1 and UCSZ00=1, see Table 19-7 of the data sheet. */
Enable receiver. */
12
13
14
15
16
17
18
19
20
21
22
23
24 } 25
26 int 27
28
29
30
31 }
Dr Matthew Howard © 2020
UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE;
UCSR0C = (1< /dev/ttyXXX
where /dev/ttyXXX is the device identifier that you noted above. This will cause the character A to
be sent to the Arduino UART. Test your program’s behaviour by sending different characters.
Dr Matthew Howard 7 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
4 Optional Additional Work
4.1 Printing the Alphabet
Extend your program from §3.1 to print the alphabet in lower or upper case, followed by a new line, in an endless loop.
Hint: You may need to insert a delay in the loop to ensure that the data in UDR0 is not overwritten before it can be transmitted to the host computer. Use the function _delay_ms() from the util/delay.h library to do this.
4.2 Send/Acknowledge
Modify your program from §3.2 so that when a letter of the alphabet is sent to the Arduino, it sends back the same letter in the opposite case. For example, if A (upper case A) is sent, the letter a is returned.
Dr Matthew Howard 8 Deparment of Engineering © 2020 King’s College London

Leave a Reply

Your email address will not be published. Required fields are marked *