您的位置 首页 软件

单片机驱动六向重力传感器

*********************************************************************************************程序名:单片机


/*********************************************************************************************
程序名:   单片机驱动六向重力传感器例程
编写人:    杜洋 
编写时刻:  2010年3月23日
硬件支撑:  STC12C4052AD 内部RC振动(4MHz)
接口阐明:  一位数码管(用于显现),传感器接口
修正日志:  
/*********************************************************************************************
阐明:
数码管显现数字即表明传感器方位状况。
0:无传感器衔接,1:上,2:下,3:左,4:右,5:前,6:后

# 6个方向的开关量联系如下:

朝上:全开
朝下:全关
朝前:K3、K4开
朝后:K1、K2开
朝左:K1、K3开
朝右:K2、K4开
/*********************************************************************************************/

#include //STC12C5A60S2系列单片机头文件

/**********************************************************************************************/
sbitDY_LED1_L1=P1 ^ 2; //a:数码管段码界说
sbitDY_LED1_L2=P1 ^ 3; //b
sbitDY_LED1_L3=P3 ^ 4; //c
sbitDY_LED1_L4=P3 ^ 3; //d
sbitDY_LED1_L5=P3 ^ 2; //e
sbitDY_LED1_L6=P1 ^ 1; //f
sbitDY_LED1_L7=P1 ^ 0; //g
sbitDY_LED1_L8=P3 ^ 5; //DP点

sbitD1=P1 ^ 7; //六向传感器接口
sbitD2=P1 ^ 6; //
sbitD3=P1 ^ 5; //
sbitD4=P1 ^ 4; //

unsigned char code disdata[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //无小数点数码管段码表(0~9)

/*********************************************************************************************/
void delay1ms (unsigned int a){ // 1ms延时程序
unsigned int i;
while( –a != 0){
for(i = 0; i < 900; i++);
}
}
/*********************************************************************************************/
void dis_off (void){ //关一切显现P1 = 0xff;
P3 = 0xff;
}
/*********************************************************************************************/
void display (unsigned char d){ //显现驱动程序
unsigned char i;
i = d & 0x01;
if(i == 0x01){
DY_LED1_L1 = 0;}
i = d & 0x02;
if(i == 0x02){
DY_LED1_L2 = 0;}
i = d & 0x04;
if(i == 0x04){
DY_LED1_L3 = 0;}
i = d & 0x08;
if(i == 0x08){
DY_LED1_L4 = 0;}
i = d & 0x10;
if(i == 0x10){
DY_LED1_L5 = 0;}
i = d & 0x20;
if(i == 0x20){
DY_LED1_L6 = 0;}
i = d & 0x40;
if(i == 0x40){
DY_LED1_L7 = 0;}
i = d & 0x80;
if(i == 0x80){
DY_LED1_L8 = 0;}dis_off();
}
/*********************************************************************************************
函数名:六向方位传感器驱动程序
调 用:S_6D();
参 数:无
返回值:0:无传感器衔接,1:上,2:下,3:左,4:右,5:前,6:后
结 果:输出传感器当时方位数值
备 注:
/**********************************************************************************************/
unsigned char S_6D (void){ //6向方位传感器(6D)驱动程序
unsigned char a;
a =0; //
if(D1 == 0 && D2 == 0 && D3 == 0 && D4 == 0){
a = 1;
}
if(D1 == 1 && D2 == 1 && D3 == 1 && D4 == 1){
a = 2;
}
if(D1 == 0 && D2 == 0 && D3 == 1 && D4 == 1){
a = 3;
}
if(D1 == 1 && D2 == 1 && D3 == 0 && D4 == 0){
a = 4;
}
if(D1 == 0 && D2 == 1 && D3 == 0 && D4 == 1){
a = 5;
}
if(D1 == 1 && D2 == 0 && D3 == 1 && D4 == 0){
a = 6;
}
return a; //返回值
}
/**********************************************************************************************/
/*********************************************************************************************/
void main (void){ //主程序(仅用与传感器测验)
unsigned char c;
dis_off();
c = 0;
while (1){
c = S_6D();//读取传感器状况
if(c != 0){//不为0时显现数值
display(disdata[c]);//调用数码管显现
}
}
}
/*************************************************************

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/ruanjian/255202.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部