//#include<reg51.h>
#include"STC12C5410AD.h"
#define unchar unsigned char
#define uint unsigned int
unchar code seg1[16] ={0x30,0xf9,0x64,0xe0,0xa9,0xa2,0x22,0xf8,0x20,0xa0,0x28,0x23,0x36,0x61,0x26,0x2e};//大模板上
//0,1,2,3,4,5,6,7,8,9,A,b,C,d,e,F
unchar code seg2[11] = {0x21,0xf3,0x19,0x51,0xc3,0x45,0x05,0xf1,0x01,0x41,0x84} ;//小模板上 (不带小数点)
unchar code seg3[11] = {0x20,0xf2,0x18,0x50,0xc3,0x45,0x04,0xf0,0x00,0x40,0x83} ; //小模板上 (带小数点)
unchar code act1[4] = { 0xf7,0xfb,0xfd,0xfe}; //单位选位
unchar code act2[9]={0xfe,0xfc,0xf8,0xf0,0xf1,0xf3,0xf7,0xff}; //用作渐出渐入显示选位
//uint sum=125;
uint key ; //按键按下标志(1到6)
uint xdata record1[128]; //用于角度传感器的转换
uint xdata record2[128]; //用于按键的转换识别
uint xdata record_v[128] ; //用于电压显示
uint flag ;
sbit led = P2^5;
sbit m10 = P3^5;
sbit m11 = P3^7;
sbit m20 = P3^2;
sbit m21 = P3^3;
sbit right = P1^4;
sbit left = P1^5;
sbit s0 = P1^0;
sbit s1 = P1^1;
sbit s2 = P1^2 ;
sbit s3 = P1^3;
/*延时程序*/
void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<121;j++)
{ ; }
}
/*启动AD转换并返回转换值*/
uint GET_AD_Result()
{
unchar temp;
uint data_temp;
data_temp=0;
ADC_DATA=0;
ADC_LOW2=0;
ADC_CONTR|=0x08;
re: temp=0x10;
temp&=ADC_CONTR; //判断转换是否完成
if(temp==0)
goto re;
ADC_CONTR&=0xe7; //软件清零
data_temp=ADC_DATA;
data_temp<<=2;
data_temp+=ADC_LOW2;
return data_temp;
}
/*显示角度值*/
show_r(int h)
{
int a , b ,i;
if(h > 0)
{
if(h < 10)
{
for(i = 0; i < 100; i++)
{
{ P1 = act1[2];P2 = seg2[h];delay(5); }
{ P1 = act1[3]; P2 = 0xc9; delay(5); }//显示“ °” (读数符号)
}
}
else
{
a = h%10;
b =h/10;
for(i = 0; i < 150; i++)
{
{ P1 = act1[2]; P2 = seg2[a];delay(5); }
{ P1 = act1[1]; P2 = seg2[b]; delay(5); }
{ P1 = act1[3]; P2 = 0xc9; delay(5); }
}
}
}
else
{
h = ~h;
if(h < 10)
{
for(i = 0; i < 150; i++)
{
{ P1 = act1[2];P2 = seg2[h];delay(5); }
{ P1 = act1[1]; P2 = 0xdf; delay(5); }
{ P1 = act1[3]; P2 = 0xc9; delay(5); }
}
}
else
{
a = h%10;
b =h/10;
for(i = 0; i < 150; i++)
{
{ P1 = act1[2]; P2 = seg2[a];delay(5); }
{ P1 = act1[1]; P2 = seg2[b]; delay(5); }
{ P1 = act1[0]; P2 = 0xdf; delay(5); } //显示负号
{ P1 = act1[3]; P2 = 0xc9; delay(5); }
}
}
}
}
/*将转换出来的数据转换成角度值*/
void moto(uint h)
{ int r ,i,k;
r=(h/5)-83; //转换成角度值
show_r(r); //显示角度值
if(flag)
{
if(r > 0)
{
{
led = 0;
m10 = 1;
m11 = 0;
delay(2000);
m10 = 1;
m11 = 1;
delay(100);
/*CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0xff;
CCAP0H = 0xff;
CCAPM0 = 0x42;
CCAP1L = 0x00;
CCAP1H = 0x00;
CCAPM1 = 0x00;
CR = 1; */
flag = 0;
}
}
else if(r < 0)
{
{
led = 1;
m10 = 0;
m11 = 1;
delay(2000);
m10 = 1;
m11 = 1;
delay(100);
flag = 0;
/*CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0x00;
CCAP0H = 0x00;
CCAPM0 = 0x00;
CCAP1L = 0xff;
CCAP1H = 0xff;
CCAPM1 = 0x42;
CR = 1;*/
}
}
else
{
led = 0;
m10 = 1;
m11 = 1;
flag = 0;
/*CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0x00;
CCAP0H = 0x00;
CCAPM0 = 0x00;
CCAP1L = 0x00;
CCAP1H = 0x00;
CCAPM1 = 0x00;
CR = 1;*/
}
}
else
{
if(r > 3)
{
{
led = 0;
m10 = 1;
m11 = 0;
delay(2000);
m10 = 1;
m11 = 1;
delay(100);
/*CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0xff;
CCAP0H = 0xff;
CCAPM0 = 0x42;
CCAP1L = 0x00;
CCAP1H = 0x00;
CCAPM1 = 0x00;
CR = 1; */
flag = 0;
}
}
else if(r < -3)
{
{
led = 1;
m10 = 0;
m11 = 1;
delay(2000);
m10 = 1;
m11 = 1;
delay(100);
flag = 0;
/*CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0x00;
CCAP0H = 0x00;
CCAPM0 = 0x00;
CCAP1L = 0xff;
CCAP1H = 0xff;
CCAPM1 = 0x42;
CR = 1;*/
}
}
else
{
led = 0;
m10 = 1;
m11 = 1;
flag = 0;
/*CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0x00;
CCAP0H = 0x00;
CCAPM0 = 0x00;
CCAP1L = 0x00;
CCAP1H = 0x00;
CCAPM1 = 0x00;
CR = 1;*/
}
}
}
/*震动传感器模数转换初始化*/
void init_AD()
{
uint a,i,j,b,result,sum;
uint aa[15];
unchar k;
record1[k]=0;
delay(10);
ADC_CONTR|=0x80; //开ADC电源
P1M0=0xb0; // 将P1.7口设为开漏模式 P1M0 = 1000 0000 (11 开漏 ; 10 高阻 ;01 推挽输出 ;00 普通)
// P1M1 = 1000 0000 上下共同表示
P1M1=0xb0;
//ADC_CONTR&=0xf8;
// ADC_CONTR|=0x07; //设置P1.7口为ADC转换口
ADC_CONTR=0xe7;
for(i = 0; i < 15; i++)
{
for(k=0;k<128;k++)
{
record1[k]=GET_AD_Result();//启动ADC并保存结果
}
aa[i++] = record1[k];
//shujvchuli(result);
//shujvchuli2(record1[k]);
}
for(i = 0; i < 15 ; i++)
for(j = 0; j < (15 - j); j++)
{
if(aa[j] > aa[j+1])
a = aa[j];
aa[j] = aa[j+1];
aa[j+1] = a;
}
for(i = 1; i < 13; i++)
sum = sum + aa[i];
result = sum/13;
moto(result);
}
/*转向*/
void turn()
{
if(right == 0)
{
led = 0;
CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0x00;
CCAP0H = 0x00;
CCAPM0 = 0x00;
CCAP1L = 0xf3;
CCAP1H = 0xf3;
CCAPM1 = 0x42;
CR = 1;
}
else if(left == 0)
{
led = 0;
CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0xf3;
CCAP0H = 0xf3;
CCAPM0 = 0x42;
CCAP1L = 0x00;
CCAP1H = 0x00;
CCAPM1 = 0x00;
CR = 1;
}
else
{
led = 1;
CMOD = 0x02;
CL = 0x00;
CH = 0x00;
CCAP0L = 0x00;
CCAP0H = 0x00;
CCAPM0 = 0x42;
CCAP1L = 0x00;
CCAP1H = 0x00;
CCAPM1 = 0x00;
CR = 1;
}
}
void main()
{
//delay(4000);
turn();
//init_AD();
}