109 lines
2.6 KiB
C
109 lines
2.6 KiB
C
/**
|
|
* Lab 2
|
|
* This is configured with GPIO15 as a pseudo button with a pull up resistor
|
|
* Since the button is active low, the program will send '1' when the button
|
|
* is not pressed (high) and send 'A' to 'Z' when the button is pressed (low)
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include "pico/stdlib.h"
|
|
#include "hardware/uart.h"
|
|
|
|
#define UART_ID uart0
|
|
#define BAUD_RATE 115200 // Baud rate of UART
|
|
|
|
#define UART_TX_PIN 16 // GPIO for UART TX pin
|
|
#define UART_RX_PIN 17 // GPIO for UART RX pin
|
|
#define PSEUDO_BTN 15 // GPIO for pseudo button
|
|
|
|
#define SLEEP_TIME 1000 // 1000ms is based on the lab requirements
|
|
|
|
/**
|
|
* Sends a character through UART0 based on the state of GPIO15 (Pseudo button)
|
|
* @param uart_input Pointer to a character to be sent through UART0
|
|
* @return void Prints the character in uart_receive()
|
|
*/
|
|
static void uart_transmit(u_char *uart_input) {
|
|
if (gpio_get(PSEUDO_BTN))
|
|
{
|
|
uart_putc(UART_ID, '1');
|
|
}
|
|
else
|
|
{
|
|
uart_putc(UART_ID, *uart_input);
|
|
|
|
// Increment character and wrap around if necessary
|
|
*uart_input = (*uart_input - 'A' + 1) % 26 + 'A';
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Receives a character through UART0 and prints it out
|
|
* NOTE: Flipping bit 5 effectively converts a character to lowercase
|
|
* EXAMPLE: 'A' = 0b01000001, 'a' = 0b01100001
|
|
* @param uart_output Pointer to a location to store the received character
|
|
* @return void Character is read and printed
|
|
*/
|
|
static void uart_receive(u_char *uart_output) {
|
|
if (uart_is_readable(UART_ID))
|
|
{
|
|
*uart_output = uart_getc(UART_ID);
|
|
|
|
if ((*uart_output >= 'A') && (*uart_output <= 'Z'))
|
|
{
|
|
*uart_output ^= (1 << 5); // This operation flips bit 5
|
|
}
|
|
else if (*uart_output == '1')
|
|
{
|
|
*uart_output = '2';
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
|
|
printf("%c\n", *uart_output);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initializes UART0 and GPIO pins
|
|
* @return void
|
|
*/
|
|
static void pin_init() {
|
|
// UART init
|
|
uart_init(UART_ID, BAUD_RATE);
|
|
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
|
|
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
|
|
|
|
// Disable UART FIFO for data to be tx/rx immediately
|
|
uart_set_fifo_enabled(UART_ID, false);
|
|
|
|
// Pseudo button init
|
|
gpio_set_dir(PSEUDO_BTN, GPIO_IN);
|
|
|
|
// Enable pull up resistor
|
|
gpio_set_pulls(PSEUDO_BTN, true, false);
|
|
|
|
}
|
|
|
|
int main() {
|
|
// Initialize stdio and pins first
|
|
stdio_init_all();
|
|
pin_init();
|
|
|
|
u_char uart_input = 'A';
|
|
u_char uart_output;
|
|
|
|
while (1) {
|
|
uart_transmit(&uart_input);
|
|
|
|
uart_receive(&uart_output);
|
|
|
|
sleep_ms(SLEEP_TIME);
|
|
}
|
|
|
|
}
|