金属循迹小车资料
单片机源程序如下:
- #include <reg51.h>
- #include <intrins.h>
- #include <math.h>
- #include "lcd1602.h"
- #include "ldc1000.h"
- #include "move.h"
- #include "nvic.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define BASIC_VALUE1 723 //720
- #define BASIC_VALUE2 644 ///640
- #define COIN 290
- #define BASIC_SPEED 10 //10 //15
- sbit BEEP = P3^6 ; //蜂鸣器
- sbit S1 = P3^5; //按键1
- uint j=0;
- uchar i=0;
- long time=0,num=0;
- uchar flag=0;
- uint dvalue1=0,dvalue2=0;
- extern unsigned char code ASCII[12] = {'0','1','2','3','4','5','6','7','8','9'};//字库
- unsigned short LDC_value1=0,LDC_value2=0;//返回值
- unsigned char disbuff[4]={0,0,0,0};//显示缓存
- void Lcd_Show();
- void Turn_Left2();
- void Turn_Right2();
- uint Key_Scan();
- void main(void)
- {
- InitLcd1602(); //1602液晶初始化
- FLOAT_LDC1_init(); //LDC1000模块1初始化
- FLOAT_LDC2_init(); //LDC1000模块2初始化
- Lcd_Show(); //lcd显示
- Tim0_Init(); //定时器0初始化
- Exti0_Init(); //外部中断0初始化
- while(1)
- {
- LDC_value1=filter1()/10;//采样1
- LDC_value2=filter2()/10;//采样2
- dvalue1=abs(LDC_value1-BASIC_VALUE1);
- dvalue2=abs(LDC_value2-BASIC_VALUE2);
- if((dvalue1>COIN)||(dvalue2>COIN)){i++;if(i>1){flag=1;i=2;}Go(BASIC_SPEED);} //硬币检测,第一次采样的值滤掉
- else
- if((dvalue1+dvalue2)<29)Go(BASIC_SPEED); //判断是否走直线(重要阈值)
- else
- if(dvalue1>dvalue2)
- {
- Turn_Left();
- }
- else
- {
- Turn_Right();
- }
- /********* LDC1000数值显示 *********/
- disbuff[0]=LDC_value1/1000;
- disbuff[1]=LDC_value1%1000/100;
- disbuff[2]=LDC_value1%1000%100/10;
- disbuff[3]=LDC_value1%1000%100%10;
- DisplayOneChar(2, 1, ASCII[disbuff[0]]);
- DisplayOneChar(3, 1, ASCII[disbuff[1]]);
- DisplayOneChar(4, 1, ASCII[disbuff[2]]);
- DisplayOneChar(5, 1, ASCII[disbuff[3]]);
- disbuff[0]=LDC_value2/1000;
- disbuff[1]=LDC_value2%1000/100;
- disbuff[2]=LDC_value2%1000%100/10;
- disbuff[3]=LDC_value2%1000%100%10;
- DisplayOneChar(11, 1, ASCII[disbuff[0]]);
- DisplayOneChar(12, 1, ASCII[disbuff[1]]);
- DisplayOneChar(13, 1, ASCII[disbuff[2]]);
- DisplayOneChar(14, 1, ASCII[disbuff[3]]);
- /********* 行驶距离显示 *********/
- disbuff[0]=(int)(num/4*3)/1000;
- disbuff[1]=(int)(num/4*3)%1000/100;
- disbuff[2]=(int)(num/4*3)%1000%100/10;
- disbuff[3]=(int)(num/4*3)%1000%100%10;
- DisplayOneChar(2, 0, ASCII[disbuff[0]]);
- DisplayOneChar(3, 0, ASCII[disbuff[1]]);
- DisplayOneChar(4, 0,'.');
- DisplayOneChar(5, 0, ASCII[disbuff[2]]);
- DisplayOneChar(6, 0, ASCII[disbuff[3]]);
- /********* 运行时间显示 *********/
- DisplayTime(time);
- if(Key_Scan()) //行驶结束检测
- {
- TR0=0;
- EX0=0;
- EA=0;
- Stop();
- while(1)
- {
- Stop();
- }
- }
- }
- }
- void Lcd_Show()
- {
- DisplayOneChar(0, 0, 'S');
- DisplayOneChar(1, 0, ':');
- DisplayOneChar(7, 0, 'm');
- DisplayOneChar(9, 0, 'T');
- DisplayOneChar(10, 0,':');
- DisplayOneChar(0, 1, 'L');
- DisplayOneChar(1, 1, ':');
- DisplayOneChar(9, 1, 'R');
- DisplayOneChar(10, 1, ':');
- }
- void Tim0() interrupt 1 //0.5ms定时器中断
- {
- TH0=0xfe;
- TL0=0x33; //0.5ms定时
- j++;
- if(flag)BEEP=~BEEP;
- if(j==2000)
- {
- time++;j=0;flag=0;
- }
- }
- void Exti0() interrupt 0 //外部中断0
- {
- if(!(dvalue2>dvalue1)) num++;
- }
- /********** 小车控制函数 **********/
- void Turn_Left2()
- {
- while(dvalue1>dvalue2)
- {
- LDC_value1=filter1()/10;//采样1
- LDC_value2=filter2()/10;//采样2
- dvalue1=abs(LDC_value1-BASIC_VALUE1);
- dvalue2=abs(LDC_value2-BASIC_VALUE2);
- Left(-1);
- Right(1);
- Delay_ms(9);
- Stop();
- Delay_ms(16);
- }
- Go(BASIC_SPEED);
- }
- void Turn_Right2()
- {
- while(!(dvalue1>dvalue2))
- {
- LDC_value1=filter1()/10;//采样1
- LDC_value2=filter2()/10;//采样2
- dvalue1=abs(LDC_value1-BASIC_VALUE1);
- dvalue2=abs(LDC_value2-BASIC_VALUE2);
- Left(1);
- Right(-1);
- Delay_ms(9); //12
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
%LDC1000寻迹小车.rar
(78.41 KB, 下载次数: 18)
|