PPM信号周期为20ms,分成10分代表10个通道信号,也就是2ms代表一个信号。
0.5ms代表一个通道信号的开始,所以0.5ms-2ms为通道范围控制。
LED p1('A',8); //IO口初始化,这里就不介绍了,推挽输出
u16 count=0;
u16 pwm_count=1000; //总计数 周期20ms,20us进次
u16 pwm1=25; //0.5ms
u16 pwm2=30;
u16 pwm3=40;
u16 pwm4=50;
u16 pwm5=75;
u16 pwm6=75;
u16 pwm7=75;
u16 pwm8=75; //2ms
//u16 pwm9=100;
//u16 pwm10=25; //2ms
extern "C" void TIM4_IRQHandler(void)//1ms进来1次
{
if(TIM4->SR&0X0001)//溢出中断
{
count++;
if(count>0&&count<=100) //ch1
{
if(count<=25)
{
PAout(8)=0;
}
else
{
if(count<=pwm1+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>100&&count<=200) //ch2
{
if(count<=100+25)
{
PAout(8)=0;
}
else
{
if(count<=pwm2+100+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>200&&count<=300) //ch3
{
if(count<=200+25)
{
PAout(8)=0;
}
else
{
if(count<=pwm3+200+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>300&&count<=400) //ch4
{
if(count<=300+25)
{
PAout(8)=0;
}
else
{
if(count<=pwm4+300+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>400&&count<=500) //ch5
{
if(count<=400+25)
{
PAout(8)=0;
}
else
{
if(count<=pwm5+400+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>500&&count<=600) //ch6
{
if(count<=500+25)
{
PAout(8)=0;
}
else
{
if(count<=pwm6+500+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>600&&count<=700) //ch7
{
if(count<=600+25)
{
PAout(8)=0;
}
else
{
if(count<=pwm7+600+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>700&&count<=800) //ch8
{
if(count<=700+25)
{
PAout(8)=0;
}
else
{
if(count<=pwm8+700+25)
{
PAout(8)=1;
}
else
{
PAout(8)=0;
}
}
}
if(count>800&&count<=900) //ch9
{
PAout(8)=1;
}
if(count>900&&count<=1000) //ch10
{
PAout(8)=1;
}
if(count==pwm_count)count=0;
}
TIM4->SR&=~(1<<0);//清除中断标志位
}
//使能定时器4,使能中断.
void Timer1_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<2; //TIM4时钟使能
TIM4->ARR=arr; //设定计数器自动重装值
TIM4->PSC=psc; //预分频器71,得到1Mhz的计数时钟
TIM4->DIER|=1<<0; //允许更新中断
TIM4->CR1|=0x01; //使能定时器2
MY_NVIC_Init(1,1,TIM4_IRQn,2);//抢占1,子优先级1,组2(组2中优先级最高的)
}
初始化:Timer1_Init(19,71);