STM32F303VC - Gated Mode - Problem - Need Help - Page 1
Forum section: electronics / general

author: mzdenkov
author_rank: Regular Contributor
timestamp: November 01, 2019, 07:55:27 pm
content: Hello,Im having problems with gated mode ofSTM30F303VCuC and i'm programming it inmirkoC for ARMIDE.I did everything as it was said inRM0316 datasheet, page: 683/1141-Look at the picture in attachmentThe only thing i've changed is, that i use TIM2_CH2 and TI2. so TS-bit(Trigger selection) in TIM2_SMCR is set to " 110: Filtered Timer Input 2 (TI2FP2) ".The uC jumps in the interrupt routine, but theTIM2_CNTregister is always at zero.Results are displayed on 0802-LCDhere is my prototype code:Code:sbit LCD_RS at GPIOE_ODR.B8;sbit LCD_EN at GPIOE_ODR.B9;sbit LCD_D4 at GPIOE_ODR.B10;sbit LCD_D5 at GPIOE_ODR.B11;sbit LCD_D6 at GPIOE_ODR.B12;sbit LCD_D7 at GPIOE_ODR.B13;char TIM2_cnt_values;int val1;int myVal;int cnt = 0;void TIM2_TRIG_IV() iv IVT_INT_TIM2 ics ICS_AUTO {if(TIM2_SRbits.TIF == 1){TIM2_SRbits.TIF = 0;/* GPIOE_ODR = 0x0F << 8;Delay_ms(500);GPIOE_ODR = 0x00 << 8;Delay_ms(500); */IntToStr(TIM2_CNT,TIM2_cnt_values);Lcd_Out(1, 1, TIM2_cnt_values);}}void TIM2_init(int prescaler_val){RCC_APB1ENRbits.TIM2EN = 1; // EN tim2 clkTIM2_CR1bits.CMS = 0b00; //edge aligned modeTIM2_CR1bits.DIR_ = 0; //upcounterRCC_CFGRbits.PPRE1 = 0b111;TIM2_PSC = prescaler_val; // set tim2 pscTIM2_CCMR1_Inputbits.IC2F = 0b0000;TIM2_CCMR1_Inputbits.CC2S = 1; //01: CC2 channel is configured as input, IC2 is mapped on TI2//TIMx capture/compare enable register (TIMx_CCER)TIM2_CCERbits.CC2NP = 0;TIM2_CCERbits.CC2P = 0; //00: noninverted/rising edge//Circuit is sensitive to TI2FP2 rising edge (capture, trigger in reset, external clock or trigger//mode), TI2FP2 is not inverted (trigger in gated mode, encoder mode).TIM2_SMCRbits.SMS = 0b101; //2:0 0101: Gated Mode - The counter clock is enabled when the trigger input (TRGI) is high. The//counter stops (but is not reset) as soon as the trigger becomes low. Both start and stop of//the counter are controlled.TIM2_SMCRbits.TS = 0b110; //110: Filtered Timer Input 2 (TI2FP2)TIM2_DIERbits.TIE = 1; //1: CC2 interrupt enabled.TIM2_CR1bits.CEN = 1; // TIM2 starts counting}void main() {TIM2_init(35000);//Enable clock for PORTD and PORTEGPIO_Clk_Enable(&GPIOD_BASE);GPIO_Clk_Enable(&GPIOE_BASE);//Set PD4 to its alternative function as TIM2_CH2 input// GPIO_Config(&GPIOD_BASE, _GPIO_PINMASK_4, _GPIO_CFG_PULL_DOWN);GPIO_Alternate_Function_Enable(&_GPIO_MODULE_TIM2_CH2_PD4);//Set PE8-PE15 to output & PE0GPIO_Config(&GPIOE_BASE,_GPIO_PINMASK_HIGH | _GPIO_PINMASK_0,_GPIO_CFG_DIGITAL_OUTPUT);Lcd_Init();Lcd_Out(2, 1, "Hello!");Delay_ms(1000);NVIC_IntEnable(IVT_INT_TIM2);while(1){GPIOE_ODR = 1 << B0;Delay_ms(1000);GPIOE_ODR = 0 << B0;Delay_ms(1000);};}Thank you for your help.Best Regards.

author: ozcar
author_rank: Frequent Contributor
timestamp: November 03, 2019, 03:57:15 am
content: You said you were following page 683 of RM0316, however, the jpg you attached does not show that page as far as I can see. In fact the jpg looks like it maybe came from RM0368 for STM32F4. I did not try to compare the two in detail, maybe they say the same thing with slightly different words, but maybe not.Other than that, what sort of debugging can you do with mikroC? Can you stop your code and confirm the counter value and check that the other timer registers are as expected?

author: mzdenkov
author_rank: Regular Contributor
timestamp: November 03, 2019, 06:53:30 pm
content: QuoteOh, my mistake the page is638/1141.Well at the end i took a break from theGated Modeand changed it forInput Capture Modefor measuring time of HI-logic level of the signalby adding two NOR gates for delay combined with XOR to make impulse when the change of logic level happens.If you still have any idea how to make Gated Mode work, please suggest your option to success.EDIT:As far as i know, there are no methods for me to debug code.I've tired to stop timer and then print values on the LCD, this can be seen in the prototype code.Other than that i dont know if there are any methods to do debugging in mikroC with STLink V2Thank you.Best Regards.Matija

author: ozcar
author_rank: Frequent Contributor
timestamp: November 04, 2019, 06:28:08 am
content: Wild guess:It is maybe behaving like it would if ARR was zero - causing CNT to get reset to zero as soon as CNT > 0. It looks like you don't touch ARR, and reset is supposed to set it to max 0xFFFFFFFF, but maybe 'something' has thoughtfully changed it for you. Either display the ARR value, or just try setting it yourself to something sensible.As for the debugging, I don't know mikroC at all, but from what I can find is seems maybe it does support SWD debugging using STLink.