代码,电赛基本要求可以完成。
spi读取GY953数据,加上ANO6.5通讯协议,PWM_IN_OUT
单片机源程序如下:
- #include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
- #include "bsp_usart.h"
- #include "delay.h"
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include "bsp_servo.h"
- #include "bsp_daba.h"
- #include "bsp_keyscan.h"
- #include "bsp_led.h"
- #include "oled.h"
- #include "bsp_keyscan.h"
- int16_t tim = 0;
- uint16_t volatile receivebuff[1024]={0};
- extern uint16_t aim_x,aim_y,flag;
- extern volatile uint16_t num;
- u8 key = 16,key1=16,single=1,select_yaw=0,select_d=0;
- u16 yaw=0,d=0,v0=5;
- u16 single_index=0;
- double pitch=0;
- u16 SINGLE[20]={30,35,40,45,50,55,60,65,70};//????
- u8 pattern_flag=1;
- u16 temp=0;
- extern int16_t aim_distances,aim_yaw;
- int main(void)
- {
- LED_GPIO_Config();
- USART1_Config();
- printf("串口成功!\n");
- KeyBoard_Init();
- OLED_Init();
- Servo_Init(20000, 72-1,1500,1500);//20ms周期 头:600-2350(10-170度) 底座:500-2500(0-180度), 1500对应90度asddadasdsada
- printf("222");
-
- while(1)
- {
- oled_show();
- //printf("%d",d);
- //key=Read_KyeValue();
- //printf("%d",key);
- // delay_ms(100);
-
- if((Read_KyeValue()==14) && (pattern_flag==1))//??1???
- {
- delay_ms(10);
- //LED1_TOGGLE ;
- printf("123");
- LED1_OFF;//chongdianjidianqi
- LED2_ON;//fangdainjidianqi
- delay_ms(Tchong);//???
- LED1_ON;
- LED2_ON;
- //delay_ms(200);
- //printf("443");
- LED1_ON;
- LED2_OFF;
- delay_ms(100);
- LED1_ON;
- LED2_ON;
-
-
- }
- if((Read_KyeValue()==10))//????
- {
- delay_ms(5);
-
- ++pattern_flag;
- while(Read_KyeValue()!=16);
- if(pattern_flag==4)
- {
- pattern_flag=1;
- }
- }
- /************************************************/
- if((Read_KyeValue()==11) && (pattern_flag==2)) //??2,????
- {
- delay_ms(10);
- if(Read_KyeValue()==11)
- {
- d=0;
- while(Read_KyeValue()!=16);
- select_d=1;
- while(select_d)
- {
- key=Read_KyeValue();
- if(key!=16)
- {
- delay_ms(20);
- if(key!=16)
- {
- if(key!=12&&key!=13&&key!=11)//??0-9
- {
- if(d==0){d=key;}
- else if(d>0&&d<10){d=d*10+key;}
- else{d=d*10+key;}
- }
- if(key==13)
- {
- key=16;select_d=0;
- }//??
- if(key==11){key=16;d=0;}//??
- }
- delay_ms(20);
- while(Read_KyeValue()!=16);
- daba(d,1500);
- delay_ms(TG);
- LED1_OFF;//chongdianjidianqi
- LED2_ON;//fangdainjidianqi
- delay_ms(Tchong);//???
- LED1_ON;
- LED2_ON;
- //delay_ms(200);
- //printf("443");
- LED1_ON;
- LED2_OFF;
- delay_ms(100);
- LED1_ON;
- LED2_ON;
-
- }
- }
- }
- printf("%d",d);
- }
- /********************************/
-
-
- if((Read_KyeValue()==11) && (pattern_flag==3)) //3
- {
- delay_ms(100);
- if(Read_KyeValue()==11)
- {
- d=0;
- while(Read_KyeValue()!=16);
- select_d=1;
- select_yaw=1;
- printf("rrrr");
- while(select_d)
- {
- key=Read_KyeValue();
- //printf("%d",key);
- if(key!=16)
- {
- //printf("juli");
- delay_ms(5);
- if(key!=16)
- {
- if(key!=12&&key!=13&&key!=11)//??0-9
- {
- if(d==0){d=key;}
- else if(d>0&&d<10){d=d*10+key;}
- else if(d>10&&d<100){d=d*10+key;}
- else if(d>100&&d<1000){d=d*10+key;}
- else {temp=d*10+key;}
- }
- if(key==13)
- {
- key=16;
- select_d=0;
- //select_yaw=1;
- }//??
- //if(key==11){key=16;d=0;}//??
- }
- delay_ms(5);
- while(Read_KyeValue()!=16);
-
- }
- }
- }
- d=temp/100;
- yaw=temp%100;
-
- /****************************/
- /*if(USART_RXBUF[5] == '-')
- {
- aim_yaw = (angle*9.3333)+1500;
- }
- else
- {
- aim_yaw = 1500-(angle*12.3333);
- }*/
-
- /*******************************/
-
- if(yaw>=30 && yaw<=60)
- {
- aim_yaw=yaw;
- aim_yaw=(aim_yaw-30)*9.3333+1500;
- }
- else if(yaw<30 && yaw>=0)
- {
- aim_yaw=30-yaw;
- aim_yaw=1500-(aim_yaw*12.3333);
- }
- else
- {
- aim_yaw=1500;
- }
-
- daba(d,aim_yaw);
- delay_ms(TG);
- LED1_OFF;//chongdianjidianqi
- LED2_ON;//fangdainjidianqi
- delay_ms(Tchong);//???
- LED1_ON;
- LED2_ON;
- //delay_ms(200);
- //printf("443");
- LED1_ON;
- LED2_OFF;
- delay_ms(100);
- LED1_ON;
- LED2_ON;
-
- printf("%d\n",d);
- printf("%d\n",yaw);
- }
-
- // if(Read_KyeValue()==13)//???
- // {
- // delay_ms(5);
- // //printf("yaw:%d,d:%d\n",yaw,d);
- // //pitch=(0.5*(asin(d*9.8/(v0*v0*100)))*180/3.14);//pitch=0.5*arcsin(d*g/(v0*v0));
- // // printf("yaw:%d,pitch:%f,single:%d\n",yaw,pitch,single);
- // if(single==1)
- // {
- // Servo_PWM_Updata((pitch*10.9375)+1450,(yaw*11.1111)+1500);
- // }
- // else
- // {
- // Servo_PWM_Updata((pitch*10.9375)+1450,1500-(yaw*11.1111));
- // }
- // if(Read_KyeValue()==14)//chongdian
- // {
- // //LED1_TOGGLE ;
- // printf("123");
- // LED1_OFF;//chongdianjidianqi
- // LED2_ON;//fangdainjidianqi
- // delay_ms(4000);//刚刚好
- // LED1_ON;
- // LED2_ON;
- // //delay_ms(200);
- // printf("443");
- // LED1_ON;
- // LED2_OFF;
- // delay_ms(100);
- // LED1_ON;
- // LED2_ON;
- //
- //
- // }
- tim ++;
- if(tim%1==0)
- {
- //daba(aim_distances,aim_yaw);//正式程序恢复
- }
-
- if(tim ==10)
- {
- tim = 0;
- }
-
- }
- }
复制代码
所有资料51hei提供下载:
两轴云台2.0.7z
(204.31 KB, 下载次数: 133)
|