标题STM32 通过定时器产生PPM信号
void ppm_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Timer_PPM_Tim5_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM5, ENABLE);
}
u16 ppm_value = 0;
void Timer_PPM_Tim5_Time(u16 *timer)
{
ppm_value = *timer;
TIM_SetAutoreload(TIM5,*timer);
}
void Timer_PPM_Tim5_Start(void)
{
TIM_ClearFlag(TIM5, TIM_FLAG_Update);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM5, ENABLE);
}
void Timer_PPM_Tim5_Stop(void)
{
TIM_ITConfig(TIM5, TIM_IT_Update, DISABLE);
TIM_Cmd(TIM5, DISABLE);
}
void TIM_PPM_Init(void)
{
ppm_GPIO_Init();
Timer_PPM_Tim5_Init();
}
void TIM5_IRQHandler(void)
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
ppm_out_process();
TIM_ClearITPendingBit(TIM5, TIM_FLAG_Update);
}
}
#ifndef __TIM_PPM_H
#define __TIM_PPM_H
#define PPM_PIN PAout(0)
void TIM_PPM_Init(void);
void Timer_PPM_Tim5_Start(void);
void Timer_PPM_Tim5_Stop(void);
void Timer_PPM_Tim5_Time(u16 *timer);
#endif
#include "includes.h"
#define PPM_OUT_L 0
#define PPM_OUT_H 1
#define PPM_CH_NUB 8
#define PPM_FLAG_SPOT_NUB 18
#define PPM_CYCLE_TIME 20000
#define PPM_LOW_TIME 400
#define LOCK 1
#define UNLOCK 0
u8 Lock = UNLOCK;
u16 ppm_data_flag[PPM_FLAG_SPOT_NUB];
PPM_Struct PPM = {
ppm_init,
ppm_out_process,
ppm_MarkProcess,
NULL,
NULL,
};
PPM_Bottom_Struct PPM_Bottom = {
&PPM_PIN,
TIM_PPM_Init,
Timer_PPM_Tim5_Time,
Timer_PPM_Tim5_Start,
Timer_PPM_Tim5_Stop,
};
void ppm_init(void)
{
u8 i;
for (i = 0; i < PPM_CH_NUB + 1; i ++)
{
ppm_data_flag[i * 2 + 1] = PPM_LOW_TIME;
}
PPM_Bottom.Init();
*(PPM_Bottom.PPM_OUT_PIN) = PPM_OUT_L;
Delay_ms(1000);
}
void ppm_out_process(void)
{
static u8 state = 0;
PPM_Bottom.Time(&ppm_data_flag[state]);
*(PPM_Bottom.PPM_OUT_PIN) = ~*(PPM_Bottom.PPM_OUT_PIN);
state += 1;
state %= PPM_FLAG_SPOT_NUB;
}
void ppm_MarkProcess(u16 *buf)
{
u16 *p = &ppm_data_flag[2];
u32 cycleTime_flag_time = 0;
u8 i;
Lock = LOCK;
for(i = 0; i < PPM_CH_NUB; i ++)
{
cycleTime_flag_time += buf[i];
p[i * 2] = buf[i] - PPM_LOW_TIME;
}
ppm_data_flag[0] = PPM_CYCLE_TIME - cycleTime_flag_time - PPM_LOW_TIME;
Lock = UNLOCK;
p = NULL;
}
#ifndef __PPM_H_
#define __PPM_H_
typedef struct
{
void (*Init)(void);
void (*Out_process)(void);
void (*MarkProcess)(u16 *buf);
void (*Start)(void);
void (*Stop)(void);
}PPM_Struct;
typedef struct
{
volatile unsigned long *PPM_OUT_PIN;
void (*Init)(void);
void (*Time)(u16 *timer);
void (*Start)(void);
void (*Stop)(void);
}PPM_Bottom_Struct;
extern PPM_Struct PPM;
void ppm_init(void);
void ppm_out_process(void);
void ppm_MarkProcess(u16 *buf);
#endif
|