mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-07-10 06:18:40 +02:00
Updated Drivers
This commit is contained in:
+24
-10
@@ -45,20 +45,34 @@
|
||||
#define ADC_GPIO 26
|
||||
#define ADC_CHANNEL 0
|
||||
|
||||
|
||||
/**
|
||||
* @brief Read and print ADC voltage and chip temperature over UART
|
||||
*
|
||||
* Samples the ADC channel for voltage in millivolts and reads the
|
||||
* on-chip temperature sensor, then prints both values.
|
||||
*/
|
||||
static void _print_adc_readings(void) {
|
||||
uint32_t voltage_mv = adc_driver_read_mv();
|
||||
float temp_c = adc_driver_read_temp_celsius();
|
||||
printf("ADC0: %4lu mV | Chip temp: %.1f C\r\n", voltage_mv, temp_c);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Application entry point for the ADC voltage and temperature demo
|
||||
*
|
||||
* Initializes the ADC on GPIO26 channel 0 and prints readings
|
||||
* every 500 ms over UART.
|
||||
*
|
||||
* @return int Does not return
|
||||
*/
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
adc_driver_init(ADC_GPIO, ADC_CHANNEL);
|
||||
|
||||
printf("ADC driver initialized: GPIO%d (channel %d)\r\n",
|
||||
ADC_GPIO, ADC_CHANNEL);
|
||||
|
||||
printf("ADC driver initialized: GPIO%d (channel %d)\r\n", ADC_GPIO, ADC_CHANNEL);
|
||||
while (true) {
|
||||
uint32_t voltage_mv = adc_driver_read_mv();
|
||||
float temp_c = adc_driver_read_temp_celsius();
|
||||
|
||||
printf("ADC0: %4lu mV | Chip temp: %.1f C\r\n",
|
||||
voltage_mv, temp_c);
|
||||
|
||||
_print_adc_readings();
|
||||
sleep_ms(500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
|
||||
static uint8_t active_channel = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Convert a raw 12-bit ADC value to millivolts
|
||||
*
|
||||
@@ -44,10 +45,11 @@ static uint8_t active_channel = 0;
|
||||
* @param raw 12-bit ADC conversion result (0 - 4095)
|
||||
* @return uint32_t Equivalent voltage in millivolts (0 - 3300)
|
||||
*/
|
||||
static uint32_t raw_to_mv(uint16_t raw) {
|
||||
static uint32_t _raw_to_mv(uint16_t raw) {
|
||||
return (uint32_t)raw * ADC_VREF_MV / ADC_FULL_SCALE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Convert a raw temperature-sensor ADC value to degrees Celsius
|
||||
*
|
||||
@@ -57,11 +59,12 @@ static uint32_t raw_to_mv(uint16_t raw) {
|
||||
* @param raw 12-bit ADC result from the internal temperature sensor (channel 4)
|
||||
* @return float Die temperature in degrees Celsius
|
||||
*/
|
||||
static float raw_to_celsius(uint16_t raw) {
|
||||
static float _raw_to_celsius(uint16_t raw) {
|
||||
float voltage = (float)raw * 3.3f / (float)ADC_FULL_SCALE;
|
||||
return 27.0f - (voltage - 0.706f) / 0.001721f;
|
||||
}
|
||||
|
||||
|
||||
void adc_driver_init(uint32_t gpio, uint8_t channel) {
|
||||
active_channel = channel;
|
||||
adc_init();
|
||||
@@ -70,13 +73,15 @@ void adc_driver_init(uint32_t gpio, uint8_t channel) {
|
||||
adc_select_input(channel);
|
||||
}
|
||||
|
||||
|
||||
uint32_t adc_driver_read_mv(void) {
|
||||
return raw_to_mv(adc_read());
|
||||
return _raw_to_mv(adc_read());
|
||||
}
|
||||
|
||||
|
||||
float adc_driver_read_temp_celsius(void) {
|
||||
adc_select_input(4);
|
||||
float result = raw_to_celsius(adc_read());
|
||||
float result = _raw_to_celsius(adc_read());
|
||||
adc_select_input(active_channel);
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user