stm32 analog output PPM signal

The PPM signal period is 20ms, divided into 10 points to represent 10 channel signals, that is, 2ms represents one signal.

0.5ms represents the start of a channel signal, so 0.5ms-2ms is the channel range control.

	LED p1('A',8); //IO port initialization, not introduced here, push-pull output

u16 count=0;
u16 pwm_count=1000; //Total count period 20ms, 20us advance
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 come in once
{ 		    		  			    
	if(TIM4->SR&0X0001)//Overflow interrupt
	{ 	
		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);//Clear the interrupt flag 	    
}
//Enable timer 4, enable interrupt.
void Timer1_Init(u16 arr,u16 psc)
{
	RCC->APB1ENR|=1<<2; //TIM4 clock enable    
 	TIM4->ARR=arr; //Set the automatic reload value of the counter  
	TIM4->PSC=psc; //Prescaler 71, get 1Mhz count clock	
	TIM4->DIER|=1<<0; //Allow update interrupt			  							    
	TIM4->CR1|=0x01; //Enable timer 2
  MY_NVIC_Init(1,1,TIM4_IRQn,2);//Preempt 1, sub-priority 1, group 2 (the highest priority in group 2)									 
}


Initialization: Timer1_Init(19,71);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325585646&siteId=291194637