基于stm32 的智能小车
单片机源程序如下:
- #include <stm32f10x_lib.h>
- #include "sys.h"
- #include "usart.h"
- #include "delay.h"
- #include "led.h"
- #include "key.h"
- #include "exti.h"
- #include "wdg.h"
- #include "timer.h"
- #include "lcd.h"
- #include "usmart.h"
- //===
- #include "adc.h"
- #include "dma.h"
- #include "24cxx.h"
- #include "flash.h"
- #include "touch.h"
- ////////////////////////////////////////////////////////////////////////////////////
- #include "Imageprocess.h"//图像处理
- #include "car_crtl.h"//车子的控制
- //全局变量
- #include "world.h"
-
- //全局变量
- int pinbi;
- int data00; //速度
- char jz_flag;
- char flag_show; // 一场数据接收完标志
- unsigned int ang; //转角
- int m; // 记录行中断次数
- int hang; // 存数组行标
- //==== 全局数组定义 ==========================
- int before[IMAGE_ROW]; // 上一场数据缓存数组
- signed int Line_center[IMAGE_ROW+1]; // 记录黑线中心数组
- //unsigned char uca_Buffer1[IMAGE_ROW][IMAGE_COLUMN]; // 数据数组//90,136
- unsigned char uca_pic[IMAGE_ROW*IMAGE_COLUMN]; // 数据数组//90,136
- unsigned char *p_addr_Buffer1[IMAGE_ROW]; // 等同于数据数组
- unsigned char *p_pic; // 等同于数据数组
- char chang_flag,lcd_flag;
- int speed_0,speed_now;
- int pwmval,duoval;
- void show_set(void)
- {
- LCD_ShowString(272,16*2,"- +");
- LCD_ShowNum(280,16*2,pwmval,4,16);//显示速度
- LCD_ShowString(272,16*3,"- +");
- LCD_ShowNum(280,16*3,pwmval*2,4,16);
- LCD_ShowString(272,16*4,"- +");
- LCD_ShowNum(280,16*4,pwmval*3,4,16);
- LCD_ShowString(272,16*5,"- +");
- LCD_ShowNum(280,16*5,pwmval*4,4,16);
- LCD_ShowString(272,16*6,"- +");
- LCD_ShowNum(280,16*6,pwmval*5,4,16);
- LCD_ShowString(272,16*7,"- +");
- LCD_ShowNum(280,16*7,pwmval*6,4,16);
- }
- void Load_Drow_Dialog(void)
- {
- LCD_Clear(WHITE);//清屏
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(296,0,"RST");//显示清屏区域
- LCD_ShowString(296,16,"SET");//显示清屏区域
-
- //画矩形
- POINT_COLOR=RED;
- LCD_DrawRectangle(64-1, 32-1, IMAGE_COLUMN+64, 32+IMAGE_ROW);
- show_set();
-
-
- }
- //==============================================
- void GAY03_init()
- {
- Stm32_Clock_Init(12);//系统时钟设置
- delay_init(96); //延时初始化
- uart_init(96,38400); //串口1初始化
- LED_Init(); //灯的初始化
- LCD_Init(); //彩色屏幕初始化
- usmart_dev.init(96); //初始化USMART//本来是100ms中断扫描
- EXTIX_Init(); //行场中断初始化
- //KEY_Init(); //PA0设置成输入,采集二值化数据 (上一句已经搞定)
- //Timerx_Init(5000,9599); //Timerx_Init(5000,7199);//10Khz 的计数频率,计数到 5000 为500ms
- //PWM_Init(900,0); //不分频。PWM 频率=72000/900=8Khz
- //====电机
- PWM11_Init(1000,31); //PA8 95---1K
- //====舵机 //60HZ
- PWM22_Init(16666,95); //PWM_Init(900,0); //不分频。PWM 频率=72000/900=8Khz
- DIAN_VAL=0; //0---1000
- DUO_VAL=straight;//1500---1.5ms //
- pwmval=0;
- duoval=straight;
-
- }
- //串口,状态设置函数
- void sset(u16 a,u16 b)
- {
- if(a==0)
- pwmval=b; //更改电机速度
- else if(a==1)
- duoval=b; //更改舵机角度
- else if(a==2)
- lcd_flag=b; //车载视频
- else if(a==3)
- pwmval=b; //
- else if(a==4)
- pwmval=b; //
- else if(a==5)
- pwmval=b; //
- else if(a==6)
- pwmval=b; //
- else if(a==99)
- {
- jz_flag=1;
- }
- }
- //函数参数调用测试函数
- void test_fun(void(*ledset)(u8),u8 sta)
- {
- ledset(sta);
- }
- //Mini STM32开发板扩展实验9
- //USMART应用 实验
- //正点原子@ALIENTEK
- //技术论坛:www.openedv.com
- int main(void)
- {
-
- signed int i;
- char lost_h;
- //unsigned int j;
- //unsigned int speed[8];
- //unsigned int j;
- //u16 led0pwmval=0;
- // u8 dir=1;
- //u8 dir=1;
-
- //=====================================================================//
- GAY03_init(); //超频到96M,串口工作在38400波特率,初始化了LED和液晶
- chang_disable();
- hang_disable();
- for(i = 0 ; i < IMAGE_ROW ; i++) // 图像采集数组赋地址
- p_addr_Buffer1[i] = &uca_pic[i*IMAGE_COLUMN];
- //p_addr_Buffer1[0]=&uca_Buffer1[i][0];
- //p_pic=uca_Buffer1;//取图像首地址!!!!
- flag_show=0;
- //===============================================================================//
- // 初始化结束开始办正事
- //===============================================================================//
- POINT_COLOR=RED;
- LED0=!LED0;
- //delay_ms(500);
- //开机画面
- /*for(i=68;i>0;i=i-10)
- {
- //LCD_Clear(WHITE);
- LCD_Init();
- LCD_chinese(20,i,0); LCD_chinese(120,i,1); LCD_chinese(220,i,2);
- LCD_chinese(20,i+100,3); LCD_chinese(120,i+100,4); LCD_chinese(220,i+100,5);
- delay_ms(80);
- }
- */
- POINT_COLOR=BLUE;
- LCD_ShowString(140,200,"Designed By E=mc^2.0");
- //画矩形
- POINT_COLOR=RED;
- LCD_DrawRectangle(64-1, 32-1, IMAGE_COLUMN+64, 32+IMAGE_ROW);
- //delay_ms(800);
-
- //===============================================================================//
- // 办正事
- //===============================================================================//
- //Timerx_Init(5000,7199);//10Khz的计数频率,计数到5000为500ms
- //PWM_Input_Init(50000,96);
- //PWM0_INPUT_INIT(50000,960); //测量频率越高,pcs要越小;测量频率越小,psc要越大,这样,精度才能跟上去
- // void PWM0_INPUT_INIT(u16 arr,u16 psc)
- //
- chang_enable();
- //hang_disable();
- //=============默认打开车载视频!!!==============
- lcd_flag=1;
- //================默认速度=======================
- pwmval=330;
- //初始化触摸屏
- POINT_COLOR=RED;//设置字体为蓝色
-
- Touch_Init();
-
- Load_Drow_Dialog();
- JTAG_Set(JTAG_SWD_DISABLE);
- while(1)
- {
- //如果速度发生了变化!!
- //LCD_ShowNum(156,130,TIM3->CCR1,5,16);//显示频率
- //LCD_ShowNum(156,150,TIM3->CCR2,5,16);//显示占空比
- /*if(speed[4]!=)
- {
- j=speed[0];
- speed[0]= speed[1];
- speed[1]= speed[2];
- speed[2]= speed[3];
- speed[3]= speed[4];
- speed[4]= speed[5];
- speed[5]= speed[6];
- speed[6]= speed[7];
- speed[7]= TIM3->CCR1;
- if(j<speed[1])
- j=speed[1];
- if(j<speed[2])
- j=speed[2];
- if(j<speed[3])
- j=speed[3];
- if(j<speed[4])
- j=speed[4];
- if(j<speed[5])
- j=speed[5];
- if(j<speed[6])
- j=speed[6];
- if(j<speed[7])
- j=speed[7];
- } */
- // delay_ms(10);
- //if(dir)pwmval+=10;
- // else pwmval-=10;
-
- // if(pwmval>1000)dir=0;
- //if(pwmval==0)dir=1;
- //DIAN_VAL=pwmval; //0---1000
- //DUO_VAL=duoval;//1500---1.5ms //
- //==========这个其实应该每隔一段时间执行一次1ms-2ms=========
- dianji();
- //===========是否一场数据???===========//
- if(flag_show == 1)
- {
- flag_show = 0;
- //LCD_ShowNum(156,128,speed_now,5,16);//显示车速
- LED1=!LED1; //注意这个绿色灯的的频率//控制频率
- // 标志清零,等待下一场数据
- //uart(3);//把图像通过串口发回来
- lost_h = Imageprocess();//图像处理函数
- duoji(lost_h);
-
- //chang_enable();
- //===============打开接收==============//
- chang_flag=1;
- //====================车载视频相关======================//
- if(lcd_flag==1)
- {
- LCD_pic();
- }
- }
- //===================触摸屏被按下==================//
- if(Pen_Point.Key_Sta==Key_Down)
- {
- Pen_Int_Set(0);//关闭中断
- do
- {
- Convert_Pos();
- Pen_Point.Key_Sta=Key_Up;
- //判断触摸屏位置
- if(Pen_Point.X0>296&&Pen_Point.Y0<16)
- {
- Load_Drow_Dialog();//清除
- LCD_LED=1;// 背光
- lcd_flag=1;// 车载视频
- }
- //===
- else if(Pen_Point.X0>296&&Pen_Point.Y0<32&&Pen_Point.Y0>16)
- {
- Load_Drow_Dialog();//清除
- LCD_LED=0;//关闭背光
- lcd_flag=0;//关闭车载视频
- }
- else if(Pen_Point.X0>270&&Pen_Point.X0<282&&Pen_Point.Y0>16*2&&Pen_Point.Y0<16*3)
- {
- pwmval++; //==1
- show_set();
- }
- else if(Pen_Point.X0>308&&Pen_Point.Y0>16*3&&Pen_Point.Y0<16*4)
- {
- pwmval--; //==2
- show_set();
- }
- /* else if(Pen_Point.X0>270&&Pen_Point.X0<282&&Pen_Point.Y0>16*2&&Pen_Point.Y0<16*3)
- {
- pwmval++; //==3
- show_set();
- }
- else if(Pen_Point.X0>270&&Pen_Point.X0<282&&Pen_Point.Y0>16*2&&Pen_Point.Y0<16*3)
- {
- pwmval++; //==4
- show_set();
- }
- else if(Pen_Point.X0>270&&Pen_Point.X0<282&&Pen_Point.Y0>16*2&&Pen_Point.Y0<16*3)
- {
- pwmval++; //==5
- show_set();
- }
- else if(Pen_Point.X0>270&&Pen_Point.X0<282&&Pen_Point.Y0>16*2&&Pen_Point.Y0<16*3)
- {
- pwmval++; //==6
- show_set();
- }*/
- else
- {
- //Draw_Big_Point(Pen_Point.X0,Pen_Point.Y0);//画图
- LCD_DrawPoint(Pen_Point.X0,Pen_Point.Y0);
- GPIOC->ODR|=1<<1; //PC1 上拉
- }
- }while(PEN==0);//如果PEN一直有效,则一直执行
- Pen_Int_Set(1);//开启中断
- }
- //else delay_ms(10);
- if(jz_flag==1)//则执行校准程序
- {
- jz_flag=0;
- LCD_Clear(WHITE);//清屏
- Touch_Adjust(); //屏幕校准
- Save_Adjdata();
- Load_Drow_Dialog();
- }
-
-
- }
- }
复制代码
所有资料51hei提供下载:
GAY03_V1.0.zip
(257.26 KB, 下载次数: 124)
|