超声波模块+步进电机做雷达的童鞋们.来公开源码讨论下吧
[复制链接]
本帖最后由 paulhyde 于 2014-9-15 09:13 编辑
很多人说类似于雷达的东西今年必须弄一个.我弄了个.超声波模块效果不大好.总体效果就没出来.源码上了.明天新的超声波模块到.说实话.国赛弄成这个样子真没多大意思了.我现在只求速度比完...
#include "stm32f10x_lib.h" #include "ili9320.h" #include "math.h"
/* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define ADC1_DR_Address ((u32)0x4001244C) #define X0 80 //80为显示180°,R=150 120在中央.显示360°,R=100 #define Y0 160 #define kmax 120 // 方向最大值 #define rmax 150 //最大显示长度的坐标数 #define appscale 10 //小于接近尺度的点将被合并.单位为mm #define pi 3.14//15926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure;
u16 dutytime =0x3ff ; vu16 T2value; u16 distance[kmax]; u16 drawlenth; int rotateflag; int k,ktemp,ksum;//共200,步进电机的200个方向
double xlen,ylen; //~
volatile short TIM1_Period=35;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void TIM_Configuration(void);
void SetAxis(void);
/* Private functions ---------------------------------------------------------*/ u8 AsciiBuff[5]; void HexToASCII(u16 data) { AsciiBuff[0] = data/1000%10 + 0x30; AsciiBuff[1] = data/100%10 + 0x30; AsciiBuff[2] = data/10%10 + 0x30; AsciiBuff[3] = data%10 + 0x30; AsciiBuff[4] = 0; } /******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif
int i,t; /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* GPIO Configuration */ GPIO_Configuration();
ili9320_Initializtion(); //LCD初始化 ili9320_Clear(White); //清屏 ili9320_Clear(White); //清屏 TIM_Configuration();
GPIO_ResetBits(GPIOC, GPIO_Pin_0); GPIO_ResetBits(GPIOC, GPIO_Pin_1);
// WriteString(30,70,"启动",Blue); SetAxis(); k=1; //防止第一次k-1无效
while (1) { for(i=0;i<5;i++) { GPIO_SetBits(GPIOC, GPIO_Pin_7); for(t=0;t<100;t++); GPIO_ResetBits(GPIOC, GPIO_Pin_7); while(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)); //等待高电平开始 TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE); TIM_Cmd(TIM8, ENABLE); while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)); //计高电平时间 TIM_ITConfig(TIM8, TIM_IT_Update, DISABLE); TIM_Cmd(TIM8, DISABLE); if(T2value<1000)distance[k]+=T2value/5; //判断取值是否有效 else i--; } distance[k]=(distance[k]*2/58); if(1) rotateflag=1; //此处判断是否返回了有效值 //新绘图方式 if((distance[k]-distance[k-1])<appscale) { ksum++; } else { if(ksum==0) { drawlenth=distance[k]/1; //显示drawlenth与距离distance的比例 xlen=sin(pi*k/kmax); ylen=cos(pi*k/kmax); Draw_Circle(X0+drawlenth*xlen,Y0-drawlenth*ylen,2); //~ Draw_Circle(120,160,2); HexToASCII(distance[k]); WriteString(X0+drawlenth*xlen-40,Y0-drawlenth*ylen,AsciiBuff,Blue); } else { for(i=0;i<ksum;i++) drawlenth=distance[k-i];
drawlenth =drawlenth/1; ktemp=k-ksum/2; xlen=sin(pi*ktemp/kmax); ylen=cos(pi*ktemp/kmax); Draw_Circle(X0+drawlenth*xlen,Y0-drawlenth*ylen,2); //~ Draw_Circle(120,160,2); HexToASCII(distance[ktemp]); WriteString(X0+drawlenth*xlen-40,Y0-drawlenth*ylen,AsciiBuff,Blue);
} } T2value=0; //distance=0; if(!GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_0)) k++; else if(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_0)) k--; if(k==kmax) { GPIO_SetBits(GPIOC, GPIO_Pin_0); //步进电机方向改变 ili9320_Clear(White); //清屏.以下为画图 SetAxis(); } else if(k==0) { GPIO_ResetBits(GPIOC, GPIO_Pin_0); ili9320_Clear(White); //清屏.以下为画图 SetAxis(); } rotateflag=1; // TIM_CtrlPWMOutputs(TIM1,ENABLE); GPIO_SetBits(GPIOA, GPIO_Pin_11); //400Hz for(t=0;t<15000;t++); GPIO_ResetBits(GPIOA, GPIO_Pin_11); // for(t=0;t<5000;t++); // while(1); } }
/*自写函数*/ void SetAxis(void) { LCD_DrawLine(0, Y0, 240, Y0); LCD_DrawLine(X0, 0, X0, 320); LCD_DrawLine(X0-5, Y0-rmax, X0+5, Y0-rmax); LCD_DrawLine(X0-5, Y0+rmax, X0+5, Y0+rmax); WriteString(X0-40,Y0-rmax-10,"1.5m",Red); WriteString(X0-40,Y0+rmax-10,"1.5m",Red); LCD_DrawLine(X0-5, Y0-rmax*2/3, X0+5, Y0-rmax*2/3); LCD_DrawLine(X0-5, Y0+rmax*2/3, X0+5, Y0+rmax*2/3); WriteString(X0-40,Y0-rmax*2/3-10,"1.0m",Red); WriteString(X0-40,Y0+rmax*2/3-10,"1.0m",Red); LCD_DrawLine(X0-5, Y0-rmax/3, X0+5, Y0-rmax/3); LCD_DrawLine(X0-5, Y0+rmax/3, X0+5, Y0+rmax/3); WriteString(X0-40,Y0-rmax/3-10,"0.5m",Red); WriteString(X0-40,Y0+rmax/3-10,"0.5m",Red); }
[ 本帖最后由 astwyg 于 2011-8-27 16:58 编辑 ]
|