stm32机械臂智能小车,已经很成熟,欢迎下载
制作出来的实物图如下:
单片机源程序如下:
- #include "stm32f10x.h"
- #include "usart1.h"
- #include "car_config.h"
- #include "pwm_output.h"
- #include "bsp_SysTick.h"
- #include "timers.h"
- #include "bizhang.h"
- #include "delay.h"
- //#include "LED.h"
- #include "ultrasonic.h"
- //#include "sys.h"
- #include "stm32f10x_gpio.h"
- /*
- #define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
- #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
- #define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
- #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
- */
- extern u8 receByte;
- //一次获取超声波测距数据,两次测距之间需要间隔一段时间,阻断回响信号
- //为了消除余震的影响,取五次数据的平均值进行加权滤波
- uint32_t Hcsr04GetLength(void )
- {
- int i = 0;
- u32 sum = 0;
- u32 lengthTemp = 0;
- while(i != 5)
- {
- delay_us(20);
- i = i+1;
- lengthTemp = get_distance();
- sum = lengthTemp + sum ;
- }
- lengthTemp = sum/5.0;
- return lengthTemp;
- }
- //#define LED PBout(10)
- #define LEDPORT GPIOB
- #define LED1 GPIO_Pin_10 //定义LED1
- void ESP8266_Init()
- {
- printf("AT\r\n");
- delay_ms(300);
- printf("AT+CWMODE=3\r\n");
- delay_ms(300);
-
- printf("AT+CIPMUX=1\r\n");
- delay_ms(300);
- printf("AT+CIPSERVER=1,8080\r\n");
- delay_ms(300);
- }
- //初始化LED
- void LED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;//构造结构体
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能其所在时钟
- GPIO_InitStructure.GPIO_Pin = LED1;//定义IO端口
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//定义IO端口输出模式
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//定义IO端口输出速度
- GPIO_Init(LEDPORT, &GPIO_InitStructure);//写入其中
- }
- int main(void)
- {
- u16 sudu = 0;
- u8 aflag = 0;
- u8 aflag_1 = 0;
- u32 a=5; //延时参数
- u32 distance;
- sudu = 250;//
- SysTick_Init(); /* 配置SysTick 为1us中断一次 */
- Car_GPIO_Config();
- USARTx_Config(); //USART1 config 9600 8-N-1
- NVIC_Configuration();
- Timer2Init();
- TIM3_PWM_Init();
- Speed_config(sudu);
- // ESP8266_Init();
- ultrasonic_init(); //初始化超声波测距模块
- delay_init(); //初始化系统延时
- for(a=5;a>0;a--)
- {
- delay_ms(500);
- }
- LED_Init();
- //distance = Hcsr04GetLength();
- //printf("i love you");
- ESP8266_Init();
-
- GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0));
- delay_ms(200);
- GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1));
- //GPIO_SetBits(LEDPORT,LED1);
- while(1){
-
-
- if(aflag==0){
-
-
-
- distance = Hcsr04GetLength();
-
-
- if(distance < 150)
- {
- hout();
- // baojing_1();
- //LED1=0;
- GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0));
- delay_ms(200);
- //LED1=1;
- //distance = Hcsr04GetLength(); //测量当前距离
- //printf("DISTANCE_av: %d mm\r\n", distance); // 蓝牙串口打印当前距离
- }else{
- GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1));
- }
-
-
- if(receByte==0x30){
- tingz();
- printf("i love you");
- receByte = 0x40;
- }else if(receByte==0x31){
- qianj();
- receByte = 0x40;
- }else if(receByte==0x32){
- hout();
- receByte = 0x40;
- }else if(receByte==0x33){
- zuoz();
- receByte = 0x40;
- }else if(receByte==0x34){
- youz();
- receByte = 0x40;
- }
- else if(receByte==0x35){
- sudu = sudu+100;
- if(sudu>=800){
- sudu = 0;
- }
- Speed_config(sudu);
- receByte = 0x40;
- }else if(receByte==0x36){
- if(sudu>100){
- sudu = sudu-100;
- } else if(sudu<=100){
- sudu = 0;
- }
- Speed_config(sudu);
- receByte = 0x40;
- }
-
- }else{
-
-
-
- distance = Hcsr04GetLength();
- if(aflag_1==0){
-
- if(distance > 200)
- {
- qianj();
- //aflag_1=1;
- //distance = Hcsr04GetLength(); //测量当前距离
- //printf("DISTANCE_av: %d mm\r\n", distance); // 蓝牙串口打印当前距离
- }else {
-
- aflag_1=1;
- youz_1();
- delay_ms(700);
- //delay_ms(400);
- //delay_ms(300);
- tingz();
- delay_ms(900);
- youz_1();
- delay_ms(1000);
- //delay_ms(150);
- delay_ms(100);
- // delay_ms(300);
- tingz();
- delay_ms(900);
- delay_ms(600);
- qianj();
-
- }
-
- } else{
-
- if(distance > 200)
- {
- qianj();
- //aflag_1=1;
- //distance = Hcsr04GetLength(); //测量当前距离
- //printf("DISTANCE_av: %d mm\r\n", distance); // 蓝牙串口打印当前距离
- }else {
-
- aflag_1=0;
- zuoz_1();
- delay_ms(1000);
- // delay_ms(300);
- delay_ms(200);
- //delay_ms(1000);
- tingz();
- delay_ms(1000);
- delay_ms(100);
- zuoz_1();
-
- //delay_ms(1000);
- delay_ms(800);
- delay_ms(200);
- //delay_ms(300);
- tingz();
- delay_ms(500);
- delay_ms(1000);
- qianj();
-
- }
- }
-
-
-
-
-
-
-
-
-
- /*
-
- sudu = 280;
- Speed_config(sudu);
- if((BiZhang_State(GPIOB,GPIO_Pin_0) == 0)&&(BiZhang_State(GPIOB,GPIO_Pin_1) == 0)){
- qianj();
- }else if((BiZhang_State(GPIOB,GPIO_Pin_0) == 1)&&(BiZhang_State(GPIOB,GPIO_Pin_1) == 1)){
- tingz();
- }else if((BiZhang_State(GPIOB,GPIO_Pin_0) == 1)&&(BiZhang_State(GPIOB,GPIO_Pin_1) == 0)){
- zuoz();
- }else if((BiZhang_State(GPIOB,GPIO_Pin_0) == 0)&&(BiZhang_State(GPIOB,GPIO_Pin_1) == 1)){
- youz();
- }
- */
- }
- if(receByte==0x37){
- aflag = 1;
- }
- if(receByte==0x38){
- aflag = 0;
- }
-
- }
- }
复制代码
所有资料51hei提供下载:
STM32程序_test.7z
(185.38 KB, 下载次数: 34)
|