/*
* File: main.c
* Author: Isaac Tong
* Course: ECE 315
* Assignment: Assignment 3
*
* Description:
* Samples analog noise generated by the noise-maker custom chip
* once per second using the Raspberry Pi Pico ADC. The sampled
* value is printed locally and transmitted over SPI to a custom
* SPI display chip that prints the received data.
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include <string.h>
#include "hardware/adc.h"
#include "hardware/spi.h"
#include "hardware/gpio.h"
#define SPI_PORT spi0
#define PIN_MISO 7
#define PIN_CS 5
#define PIN_SCK 6
int main() {
stdio_init_all();
// ADC setup
adc_init();
adc_gpio_init(26); // GP26 = ADC0
adc_select_input(0);
// SPI setup
spi_init(SPI_PORT, 100000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
spi_set_format(SPI_PORT, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
gpio_init(PIN_CS);
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1);
char msg[64];
while (true) {
uint16_t raw = adc_read();
float voltage = raw * 3.3f / 4095.0f;//converts into real voltage value
//sends message
printf("ADC Voltage: %.3f V\n", voltage);
sprintf(msg, "Voltage: %.3f\n", voltage);
gpio_put(PIN_CS, 0);
spi_write_blocking(SPI_PORT, (uint8_t *)msg, strlen(msg));
gpio_put(PIN_CS, 1);
sleep_ms(1000);
}
}