cancel
Showing results for 
Search instead for 
Did you mean: 

Pull down PA1

Ala
Senior

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?

11 REPLIES 11
Javier1
Principal

think about the ADC as a high impedance input.

If the capacitor is connected only to the ADC and your GPIO, your cap is not charging/discharging, youre not closing the cricuit.

Could you show us a schematic/drawing or picture of your setup?

>>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?

Thats strange, is it 2030 every time you test?

we dont need to firmware by ourselves, lets talk
Ala
Senior

here is my schematics

0693W00000QMcmBQAT.jpgit is quit simple, I wired up the very same circuit on Arduino and it worked just fine. that's why I think the setup is OK but how can I discharge the 12pF correctly?

yes, actually I see 2030 count in every loop

MM..1
Chief II

Your ADC idea is ???

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;	

line 3 generate rectangle pulse on PA2 and next line start conversion.

Result ? You dont show PA2 config.

ADC have own C and internal discharging logic, for what this your setup?

yes because in main() PA2 is been initiated by MX_GPIO_Init(); using CubeMX

I am using this setup to measure 12pF cap. as I said the setup is fine as I have tested it on Arduino first

Seems you name function OUTPUT but config pin as INPUT usw.

Your code do what you write. PA2 go HIGH and if is configured as drived OUTPUT then C is Xc and transfer pulse to ADC = 1,7V

yes, sorry bad name choosing but in last attempts I tried to make the pin INPUT to pull it down to GND in order to discharge the 12pF cap.

shouldnt C just be discharged using pull down resistor which is ~40K ohm as mentioned in datasheet?

I dont think thats the case, as all of ADC circuit should not work if we think they are high impedance and therefore the circuit is open. high impedance simply means that the input resistor is too high so no current is passing and so the voltage which we are trying to measure using ADC is not perturbed.

yo do you

we dont need to firmware by ourselves, lets talk

which arduino?

>>yes, actually I see 2030 count in every loop thats weird

we dont need to firmware by ourselves, lets talk