2022-08-01 08:34 AM
hi there
I have a project with STM32f103c8t6 blue pill. here I connect a 12pF capacitor to pin PA2 and PA1. PA2 is GPIO_OUTPUT and PA1 is ADC.
first: PA2 is set HIGH to charge the capacitor and then goes LOW right afterwards.
here is my while(1) code
turn_PA1_ADC();
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET);
ADC1->CR2|=0x00400000;//start conversion on regular channel
while(!(ADC1->SR &2)){}
val=ADC1->DR;
//clear everything for next time
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET);
turn_PA1_OUTPUT();
HAL_Delay(500);
I use turn_PA1_OUTPUT() function to discharge PA1 by pulling it down. here are the codes
void turn_PA1_OUTPUT(void)
{
//PA1
GPIO_InitTypeDef GPIO_InitStruct1 = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
GPIO_InitStruct1.Pin = GPIO_PIN_1;
GPIO_InitStruct1.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct1.Pull = GPIO_PULLDOWN;
GPIO_InitStruct1.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct1);
}
void turn_PA1_ADC(void)
{
RCC->APB2ENR |=0x0204;//10 0000 0100..Enable Clock for ADC1 and GPIOA
RCC->CFGR=0x8000;//PCLK divided by 6
GPIOA->CRL=0x00004A00;//Configure GPIOA as analog input
ADC1->SMPR2=0x0038;//239.5 Cycle
ADC1->SQR3|=0x0001;//select channel 1 for sequence
ADC1->CR2=0x0003;//enable for first time
HAL_Delay(10);
ADC1->CR2=0x0003;//enable for second time
HAL_Delay(5);
ADC1->CR2=0x0007;//calibration
}
but in debug mode I see that val variable shows around 2030 counts which shows that around 1.7volts is present on PA1. why is PA1 not discharged? and how can I discharge it fully for next loop?
2022-08-02 01:16 AM
Arduino UNO
2022-08-02 04:31 AM
I make next look and your image show PA0 PA2 , but code PA1...
And when you start with ADC remove circouit and connect know voltage for example 1,5V battery to input...