通过STM32解析SBUS然后完成混控
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "timer.h"
-
- extern int usart1_stop_flag;
- u16 channel[8];
- extern int sbus[200];
- int main(void)
- {
-
- uart_init(100000);
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- TIM3_PWM_Init(7199,199); //不分频。PWM频率 50hz 7200/50
- TIM4_PWM_Init(7199,199); //不分频。PWM频率 50hz 7200/50
- TIM_SetCompare2(TIM3,539);
- TIM_SetCompare3(TIM3,539);
- TIM_SetCompare4(TIM3,539);
- TIM_SetCompare1(TIM4,359);
- // TIM_SetCompare2(TIM4,539);
- // TIM_SetCompare3(TIM4,539);
- while(1)
- {
- if(usart1_stop_flag==1)
- {
- usart1_stop_flag=0;
- channel[0] = (((sbus[1] & 0x7) << 8) | sbus[0])*0.5+30;//*0.77+767;
- channel[1] = (((sbus[2] & 0x3f) << 5) | (sbus[1] >> 3))*0.5+30;//*0.77+767;
- channel[2] = (((sbus[4] & 0x1)<<10) | (sbus[3] << 2) | (sbus[2] >> 6))*0.261+279.27;//*0.77+767;
- channel[3]= (((sbus[5] & 0xf) << 7) | (sbus[4] >> 1))*0.5+30;//*0.77+767;
- channel[4]= (((sbus[6] & 0x7f) << 4) | (sbus[5] >> 4))*0.5+30;//*0.77+767;
- channel[5]= (((sbus[8] & 0x2) << 9) | (sbus[7] << 1) | (sbus[6] >> 7))*0.5+30;//*0.77+767;
- channel[6] = (((sbus[9] & 0x1f) << 6) | (sbus[8] >> 2))*0.5+30;//*0.77+767;
- channel[7] = ((sbus[10] << 3) | (sbus[9] >> 5))*0.5+30;//*0.77+767;
-
-
- TIM_SetCompare2(TIM3,channel[0]-channel[1]+539);
- TIM_SetCompare3(TIM3,channel[0]+channel[1]-539);
- TIM_SetCompare4(TIM3,channel[0]+channel[3]-539);
-
-
- TIM_SetCompare1(TIM4,channel[2]);
- // TIM_SetCompare2(TIM4,a);
- // TIM_SetCompare3(TIM4,a);
-
- printf("1=%d\t",channel[0]);
- printf("2=%d\t",channel[1]);
- printf("3=%d\t",channel[2]);
- printf("4=%d\t",channel[3]);
- printf("5=%d\t",channel[4]);
- printf("6=%d\t",channel[5]);
- printf("7=%d\t",channel[6]);
- printf("8=%d\r\n ",channel[7]);
- }
- }
- }
复制代码
所有资料51hei提供下载:
AT9S混控.7z
(195.14 KB, 下载次数: 70)
|