cancel
Showing results for 
Search instead for 
Did you mean: 

Ideeas for encoding PPM signal to SPI data

Bogdan
Senior
Posted on June 28, 2015 at 19:28

Hello,  i started working with some texas instruments rf modules, namely cc1120 and cc1190 wireless ic's.

My idea is to build a rc remote control based on a ppm input signal.

I want this signal to be procesed via my MCU (stm32f103 or stm32f407), but i was curious if there is a standard way to encode ppm into SPI data.

Any ideas? Of course i know this requires use of timers

Thank you
3 REPLIES 3
Bogdan
Senior
Posted on July 02, 2015 at 22:05

Hello all,

i started working on the timer code . My first thing i want to make is trying to count the duration of the high pulse. I have a 50hz signal with 50% duty, that means for my high period i expect 10ms

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2); // setup timer2 channel1 on PA0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;//| GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 1;
TIM_TimeBaseStructure.TIM_Period = 60000; // timer runing somewhere at 699 HZ !!
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* configure Timer2 Channel 1 */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //Capture performed each time an edge is detected on the capture input. */
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); // enable Channel 1 capture interupt
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //enable interupt events
TIM_SelectInputTrigger(TIM2,TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); 
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE); // Start the Timer

And the interrupt routine is bellow

uint32_t capture; 
char txt[20];
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == 1)// If compare capture has occured
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
capture = TIM_GetCapture1(TIM2); 
GPIO_ToggleBits(GPIOD,GPIO_Pin_5);
sprintf(txt,''%d'',capture);
USART_Puts(txt);
USART_Puts(''

'');
capture=0; // reset the variable
}
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // Counter overflow, reset interrupt
GPIO_ToggleBits(GPIOD,GPIO_Pin_13);
}
}

In my channel1 interrupt handler the

GPIO_ToggleBits(GPIOD,GPIO_Pin_5);

togles high/low every 40ms like expected, since is set to trigger on every rising edge of my 50hz signal ( once every 20ms a rising edge apear) But my capture value with the current code setup is somewhere at 6500 - 6700 (unstable variations) My clock is 168MHZ runing on stm32f407VE, so normaly my timer AHPB is 84 mhz I wonder where i did wrong?!?
Posted on July 02, 2015 at 22:18

> I wonder where i did wrong?!?

Here:

capture=0; // reset the variable

Store the old capture value and calculate difference (subtract new value from old). JW
Bogdan
Senior
Posted on July 03, 2015 at 06:34

I am bit confuse, tryied to google`it but no luck so far.

I tryied your solution to do a substracion, namely this code

if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == 1)// If compare capture has occured
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
capture= TIM_GetCapture1(TIM2); // Get timer counts for Period
myc= capture-last_capture; // do a substraction
last_capture = capture; // store variable as last input value
capture = 0; // reset the variable
GPIO_ToggleBits(GPIOD,GPIO_Pin_5); // just toogle a pin
sprintf(txt,''%d'',myc);
USART_Puts(txt);
USART_Puts(''

'');
}

but this way, i get even negative values , and my range varies from -65 to 300