Changeset - d98fd1240e20
[Not reviewed]
tip default
0 3 0
Nick Orlando (NEO) - 9 years ago 2016-11-03 22:59:01
nick.orlando67@gmail.com
fixed bug that caused all analogs to read same ADC value.
3 files changed with 12 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/adc.c
Show inline comments
 
/*
 
 * adc.c
 
 *
 
 *  Created on: Aug 9, 2016
 
 *      Author: Nicholas Orlando
 
 */
 
 
/* Includes ------------------------------------------------------------------*/
 
#include "adc.h"
 
 
#include "gpio.h"
 
#include "dma.h"
 
 
/* USER CODE BEGIN 0 */
 
 
/* USER CODE END 0 */
 
 
// Peripheral Handles
 
ADC_HandleTypeDef hadc1;
 
DMA_HandleTypeDef hdma_adc1;
 
 
 
// Private Variables
 
static uint16_t adc_readings[3];
 
 
 
uint16_t get_viout_counts(void)
 
{
 
	return adc_readings[ADC_CHAN_VIOUT];
 
}
 
 
uint16_t get_vgiout_counts(void)
 
{
 
	return adc_readings[ADC_CHAN_GAIN_VIOUT];
 
}
 
 
uint16_t get_vbat_counts(void)
 
{
 
	return adc_readings[ADC_CHAN_VBAT];
 
}
 
 
/* ADC1 init function */
 
void adc_init(void)
 
{
 
	/* Peripheral clock enable */
 
	__HAL_RCC_ADC1_CLK_ENABLE();
 
	__HAL_RCC_DMA1_CLK_ENABLE();
 
	__HAL_RCC_GPIOA_CLK_ENABLE();
 
 
	GPIO_InitTypeDef GPIO_InitStruct;
 
 
	/**ADC1 GPIO Configuration
 
	PA0     ------> ADC1_IN1
 
	PA1     ------> ADC1_IN2
 
	PA2     ------> ADC1_IN3
 
	*/
 
	GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
 
	GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
	GPIO_InitStruct.Pull = GPIO_NOPULL;
 
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
 
 
	/**Common config
 
	*/
 
	hadc1.Instance = ADC1;
 
	hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
 
	hadc1.Init.Resolution = ADC_RESOLUTION_12B;
 
	hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
 
	hadc1.Init.ContinuousConvMode = ENABLE;
 
	hadc1.Init.DiscontinuousConvMode = DISABLE;
 
	hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 
	hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 
	hadc1.Init.NbrOfConversion = 1;
 
	hadc1.Init.NbrOfConversion = 3;
 
	hadc1.Init.DMAContinuousRequests = ENABLE;
 
	hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
 
	hadc1.Init.LowPowerAutoWait = DISABLE;
 
	hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
 
	HAL_ADC_Init(&hadc1);
 
 
 
	/* Peripheral DMA init*/
 
 
	hdma_adc1.Instance = DMA1_Channel1;
 
	hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
 
	hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
 
	hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
 
	hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
 
	hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
 
	hdma_adc1.Init.Mode = DMA_CIRCULAR;
 
	hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
 
	HAL_DMA_Init(&hdma_adc1);
 
 
	__HAL_LINKDMA(&hadc1,DMA_Handle,hdma_adc1);
 
 
 
	ADC_ChannelConfTypeDef sConfig;
 
 
	// Configure VIOUT channel
 
	sConfig.Channel = ADC_CHANNEL_1;
 
	sConfig.Rank = ADC_REGULAR_RANK_1;
 
	sConfig.SamplingTime = ADC_SAMPLETIME_601CYCLES_5;
 
	HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
 
	// Configure VIOUT-GAINS channel
 
	sConfig.Channel = ADC_CHANNEL_2;
 
	sConfig.Rank = ADC_REGULAR_RANK_2;
 
	sConfig.SamplingTime = ADC_SAMPLETIME_601CYCLES_5;
 
	HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
 
	// Configure VBAT channel
 
	sConfig.Channel = ADC_CHANNEL_3;
 
	sConfig.Rank = ADC_REGULAR_RANK_3;
 
	sConfig.SamplingTime = ADC_SAMPLETIME_601CYCLES_5;
 
	HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
 
 
	HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_readings, 3);
 
 
 
//  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
 
//  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
 
 
}
 
 
 
src/buttons.c
Show inline comments
 
/*
 
 * buttons.c
 
 *
 
 *  Created on: Oct 22, 2016
 
 *      Author: Nicholas Orlando
 
 */
 
 
#include "stm32f3xx_hal.h"
 
#include "stm32f3xx.h"
 
#include "gpio.h"
 
#include "ssd1306.h"
 
#include "buttons.h"
 
#include <math.h>
 
 
 
uint32_t last_button_debounce_time = 0;		// the system time at which the button values were last checked
 
uint32_t debounce_resolution = 5;	// period in which to check pin state in milliseconds
 
float filter_fraction = 0.2;	// a number between 0 and 1, which adjusts the heaviness of the filter.
 
								// 0 = never changes and 1 = no filter at all.
 
uint8_t l_thresh = 20; //lower threshold for when then button is considered pressed vs not pressed.
 
uint8_t u_thresh = 80; //upper threshold for when then button is considered pressed vs not pressed.
 
 
uint8_t temp_counter = 0; // temporary counter for testing purposes
 
 
float sw_btn_avg = 0; // some sort of running average
 
uint8_t sw_btn_state = NOT_PRESSED; // the state of the pin after filtering
 
uint8_t sw_btn_old_state = NOT_PRESSED; // the state of the pin after filtering
 
 
void freaking_debounce(void)
 
{
 
	if(HAL_GetTick() - last_button_debounce_time > debounce_resolution)
 
	{
 
		char buffer[256]; // needed for writing stuff to screen
 
 
		//averaging to filter button presses
 
		if(HAL_GPIO_ReadPin(SW_BTN) == GPIO_PIN_RESET)
 
		{
 
			sw_btn_avg = sw_btn_avg + filter_fraction * (100 - sw_btn_avg);
 
		}
 
		else
 
		{
 
			sw_btn_avg = sw_btn_avg + filter_fraction * (0 - sw_btn_avg);
 
		}
 
 
		// check to see if the btn average value has crossed a threshold
 
		if(sw_btn_avg < l_thresh)
 
		{
 
			sw_btn_state = NOT_PRESSED;
 
//			snprintf(buffer, 256, "NOT_PRESSED");
 
//			ssd1306_drawstring(buffer, 1, 0);
 
		}
 
		if(sw_btn_avg > u_thresh)
 
		{
 
			sw_btn_state = PRESSED;
 
//			snprintf(buffer, 256, "PRESSED    ");
 
//			ssd1306_drawstring(buffer, 1, 0);
 
		}
 
 
		// do something when state has changed
 
		if((sw_btn_state == PRESSED) && (sw_btn_old_state == NOT_PRESSED))
 
		{
 
			temp_counter++;
 
			snprintf(buffer, 256, "%i", temp_counter);
 
			ssd1306_drawstring(buffer, 1, 0);
 
//			temp_counter++;
 
//			snprintf(buffer, 256, "%i", temp_counter);
 
//			ssd1306_drawstring(buffer, 1, 0);
 
			HAL_GPIO_TogglePin(LED_RED);
 
			HAL_GPIO_TogglePin(GATE_DRIVE);
 
		}
 
 
 
//		snprintf(buffer, 256, "sw-btn-avg: %.1f", sw_btn_avg);
 
//		ssd1306_drawstring(buffer, 2, 0);
 
 
		// save previous button states
 
		sw_btn_old_state = sw_btn_state;
 
 
		last_button_debounce_time = HAL_GetTick();
 
	}
 
}
src/main.c
Show inline comments
 
//
 
// STM32F103 Template Firmware
 
// Copyright 2016 SeaLandAire Technologies
 
// Author(s): Ethan Zonca
 
//
 
 
#include "stm32f3xx_hal.h"
 
#include "config.h"
 
 
#include "watchdog.h"
 
#include "system.h"
 
#include "gpio.h"
 
#include "error.h"
 
#include "flash.h"
 
#include "ssd1306.h"
 
#include "stdio.h"
 
#include "adc.h"
 
#include "dma.h"
 
#include "interrupts.h"
 
#include "buttons.h"
 
 
int main(void)
 
{
 
	sysclock_init();
 
	hal_init();
 
	gpio_init();
 
	ssd1306_init();
 
	ssd1306_drawlogo();
 
	ssd1306_clearscreen();
 
	adc_init();
 
 
	flash_init();
 
	watchdog_init();
 
 
	//just some example code for getting flash values
 
//	flash_getsettings()->values.can_id = 67;
 
//
 
//	if(flash_getsettings()->values.can_id == 12);
 
 
//	ssd1306_drawstring(const char *dataPtr, unsigned char row, unsigned char xPos)
 
//	ssd1306_drawstring("[ ProtoFuse ]", 0, 0);
 
 
	float temp_counter = 0;
 
 
	// Software timers
 
	uint32_t last_screen_update_time = HAL_GetTick();
 
 
 
	while (1)
 
	{
 
		// function that checks all the buttons
 
		freaking_debounce();
 
 
		// Grab and transmit data
 
		if(HAL_GetTick() - last_screen_update_time > 100)
 
		{
 
			char buffer[256];
 
			snprintf(buffer, 256, "Counter: %.1f", temp_counter);
 
			ssd1306_drawstring(buffer, 0, 0);
 
//			snprintf(buffer, 256, "VBATT CNTS: %u", get_vbat_counts());
 
//			ssd1306_drawstring(buffer, 1, 0);
 
//			snprintf(buffer, 256, "IADC CNTS: %u", get_viout_counts());
 
//			ssd1306_drawstring(buffer, 2, 0);
 
//			snprintf(buffer, 256, "GIADC CNTS: %u", get_vgiout_counts());
 
//			ssd1306_drawstring(buffer, 3, 0);
 
			snprintf(buffer, 256, "VBATT CNTS: %u", get_vbat_counts());
 
			ssd1306_drawstring(buffer, 1, 0);
 
			snprintf(buffer, 256, "IADC CNTS: %u", get_viout_counts());
 
			ssd1306_drawstring(buffer, 2, 0);
 
			snprintf(buffer, 256, "GIADC CNTS: %u", get_vgiout_counts());
 
			ssd1306_drawstring(buffer, 3, 0);
 
 
 
 
			temp_counter = temp_counter + 0.1;
 
			last_screen_update_time = HAL_GetTick();
 
		}
 
 
		watchdog_feed();
 
	}
 
}
 
 
0 comments (0 inline, 0 general)