/**
* ECE 315 Assignment 3 - main.c
* Purpose: Samples ADC0 every 1 second and sends the value to an SPI peripheral.
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/spi.h"
#define ADC_PIN 26
#define SPI_PORT spi0
#define PIN_SCK 18
#define PIN_MOSI 19
#define PIN_CS 17
int main() {
stdio_init_all();
// Initialize ADC
adc_init();
adc_gpio_init(ADC_PIN); // GP26
adc_select_input(0); // ADC0
// Initialize SPI at 0.5 MHz
spi_init(SPI_PORT, 500 * 1000);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Initialize chip select
gpio_init(PIN_CS);
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1); // High = Deselected
char msg_buffer[32];
while (true) {
// Read from the noise-maker chip
// 12-bit ADC: 0 to 4095. With 3.3V ref, 1V is roughly 1241
uint16_t raw_value = adc_read();
float voltage = raw_value * (3.3f / 4095.0f);
// Print sampled voltage to main console
printf("Pico: Sampled Voltage = %.3f V\n", voltage);
// // Format string for the SPI chip
// snprintf(msg_buffer, sizeof(msg_buffer), "Value: %.3f V\n", voltage);
// // Send to my-output-display chip via SPI
// gpio_put(PIN_CS, 0); // Select chip (Active Low)
// spi_write_blocking(SPI_PORT, (uint8_t *)msg_buffer, strlen(msg_buffer));
// gpio_put(PIN_CS, 1); // Deselect chip
// Wait 1 second before sampling again
sleep_ms(1000);
}
return 0;
}