You are required to write a C program to: • Initialize GPIO peripherals • Initialise UART peripheral for receiving ASCII characters ‘A’ to ‘Z’ at baud 9600 • Initialise an internal array to hold 10 characters with head and tail: CharBuff • Repeat the following:o When data is received on the serial communication port, read ASCII character X, o If received character X is a capital letter add it to CharBuff, else ignore. o While CharBuff is not empty, transmit the morse code of the oldest stored character by blinking the LED (code provided for you). o When CharBuff is full, disable UART RX. o If UART RX is disabled, pushing the button P_B1 will activate it; otherwise, pushing the button does not affect your programme. You are recommended to use interrupt to control UART receiving data and coordinate the operation between CharBuff and P_LD2.
时间: 2024-02-10 12:06:13 浏览: 91
Sure, I can help you with that. This program requires a good understanding of C programming and experience with GPIO and UART peripherals. Here is an outline of the program:
1. Initialize GPIO peripherals for LED and button.
2. Initialize UART peripheral for receiving ASCII characters 'A' to 'Z' at baud 9600.
3. Initialize an internal array to hold 10 characters with head and tail: CharBuff.
4. Set up an interrupt to control UART receiving data and coordinate the operation between CharBuff and P_LD2.
5. When data is received on the serial communication port, read ASCII character X.
6. If received character X is a capital letter add it to CharBuff, else ignore.
7. While CharBuff is not empty, transmit the morse code of the oldest stored character by blinking the LED.
8. When CharBuff is full, disable UART RX.
9. If UART RX is disabled, pushing the button P_B1 will activate it; otherwise, pushing the button does not affect your programme.
Here is a sample code snippet to get you started:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f4xx.h"
#define MAX_CHARACTERS 10
char CharBuff[MAX_CHARACTERS];
int head = 0;
int tail = 0;
int uart_rx_enabled = 1;
void initialize_gpio() {
// TODO: Initialize GPIO peripherals for LED and button
}
void initialize_uart() {
// TODO: Initialize UART peripheral for receiving ASCII characters 'A' to 'Z' at baud 9600
}
void initialize_interrupt() {
// TODO: Set up an interrupt to control UART receiving data and coordinate the operation between CharBuff and P_LD2
}
int is_full() {
return ((tail + 1) % MAX_CHARACTERS == head);
}
int is_empty() {
return (head == tail);
}
void add_to_buffer(char c) {
if (!is_full()) {
CharBuff[tail] = c;
tail = (tail + 1) % MAX_CHARACTERS;
}
else {
uart_rx_enabled = 0;
}
}
char remove_from_buffer() {
char c = CharBuff[head];
head = (head + 1) % MAX_CHARACTERS;
if (uart_rx_enabled == 0 && !is_full()) {
uart_rx_enabled = 1;
}
return c;
}
void transmit_morse_code(char c) {
// TODO: Transmit the morse code of the oldest stored character by blinking the LED
}
void uart_receive_data_handler() {
char c = USART_ReceiveData(USART1);
if (c >= 'A' && c <= 'Z') {
add_to_buffer(c);
}
}
void button_push_handler() {
if (!uart_rx_enabled) {
uart_rx_enabled = 1;
}
}
int main(void) {
initialize_gpio();
initialize_uart();
initialize_interrupt();
while (1) {
if (!is_empty()) {
char c = remove_from_buffer();
transmit_morse_code(c);
}
}
}
```
I hope this helps you get started on your program. Let me know if you have any questions or need further assistance.
阅读全文