// ad读取函数////////////////////////////////////////////////////
uchar ad0832read(uchar channel)
{
uchar i=0,value1=0,value2=0;
SCL=0;
delay_1ms(2);
DO=1;
delay_1ms(2);
CS=0; //开始
delay_1ms(2);
SCL=1; //第一个上升沿
delay_1ms(2);
SCL=0;
delay_1ms(2);
DO=1;
delay_1ms(2);
SCL=1; //第二个上升沿
delay_1ms(2);
SCL=0;
delay_1ms(2);
DO=channel;
delay_1ms(2);
SCL=1; //第三个上升沿
delay_1ms(2);
SCL=0;
delay_1ms(2);
DO=1;
delay_1ms(2);
for(i=0;i<8;i++) //从高位到低位
{
SCL=1;
delay_1ms(2);
SCL=0;
delay_1ms(2);
if(DO)
{
value1|=0x80>>i;
delay_1ms(2);
}
}
for(i=0;i<8;i++) //从低位到 高位
{
if(DO)
{
value2|=0x80<<i;
delay_1ms(2);
}
SCL=1;
delay_1ms(2);
SCL=0;
delay_1ms(2);
}
CS=1;
SCL=1;
delay_1ms(1);
DO=1;//数据读取完成,释放总线
delay_1ms(2);
return (value1);
}
/********************独立按键程序*****************/
uchar key_can; //按键值
void key() //独立按键程序
{
static uchar key_new;
key_can = 20; //按键值还原
P3 |= 0xf0;
if((P3 & 0xf0) != 0xf0) //按键按下
{
delay_1ms(1); //按键消抖动
if(((P3 & 0xf0) != 0xf0) && (key_new == 1))
{ //确认是按键按下
key_new = 0;
switch(P3 & 0xf0)
{
case 0xe0: key_can = 4; break; //得到k1键值
case 0xd0: key_can = 3; break; //得到k2键值
case 0xb0: key_can = 2; break; //得到k3键值
case 0x70: key_can = 1; break; //得到k4键值
}
}
}
else
key_new = 1;
}
void main()
{
beep = 0; //开机蜂鸣器叫一声
delay_1ms(1);
P0 = P1 = P2 = P3 = 0xff; //初始化IO口
init_1602(); //1602液晶初始化
delay_1ms(750);
while(1)
{
key(); //独立按键程序
if(key_can!= 0)
{
key_with(); //按键按下要执行的程序
}
flag_250ms ++;
if(flag_250ms >=250)
{
flag_250ms = 0;
clock_h_l(); //报警函数
temperature =read_temp(); //读取温度值
delay_1ms(2);
if(menu_1 == 0)
{
guangxian = ad0832read(1);
guangxian = guangxian * 99 / 255;
shidu = ad0832read(0); //读出湿度
shidu=shidu * 99 / 255;
write_sfm2(1,8,shidu); //显示湿度
write_sfm2(2,4,temperature); //显示温度
write_sfm2(2,11,guangxian); //显示光线
}
}
delay_1ms(1);
}
}
|