标题: 求助呀 求大神帮忙看看程序错在哪儿? [打印本页]
作者: zlmai123 时间: 2016-4-15 14:09
标题: 求助呀 求大神帮忙看看程序错在哪儿?
求大神帮忙看看程序错在哪儿?
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <uart.h>
#define P_KEY P1
sbit DQ = P0^7; // 定义DQ引脚为P0.7
#include <max7219.h>
sbit P20 = P2^0;
sbit sound=P3^7;
unsigned char code ascii[15]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0F};
float t;
/******************************* 延时函数*************************
* 功能:在11.059MHz的晶振条件下调用本函数需要14μs 然后每次计数需13μs
********************************************************************/
void delay(int useconds)
{
for (; useconds>0;useconds--)
{
;
}
}
/******************************* 复位函数***************************
* 功能:完成单总线的复位操作。
* 复位时间为480μs,因此延时时间为(480-14)/13 = 36,
* 经过70μs之后检测存在脉冲,因此延时时间为(70-14)/13 = 5
********************************************************************/
unsigned char ow_reset(void)
{ unsigned char presence;
DQ = 0; // 将 DQ 线拉低
delay(36); // 保持 480μs
DQ = 1; // DQ返回高电平
delay(5); // 等待存在脉冲
presence = DQ; // 获得存在信号
delay(30); // 等待时间隙结束
return(presence); // 返回存在信号,0 = 器件存在, 1 = 无器件
}
void write_bit(char bitval)
{ DQ = 0; // 将DQ 拉低开始写时间隙
_nop_();
_nop_();
if(bitval==1) DQ =1; // 如果写1,DQ 返回高电平
delay(8); // 在时间隙内保持电平值,
DQ = 1; // Delay函数每次循环延时13μs,因此delay(7) = 105μs
}
void write_byte(char val)
{ unsigned char i;
unsigned char temp;
for (i=0; i<8; i++)
{ // 写入字节, 每次写入一位
temp = val>>i;
temp &= 0x01;
write_bit(temp);
} delay(5);
}
/**************************** 位读取函数 ***************************
* 功能:从单总线上读取一位信号,所需延时时间为15μs,因此无法调用前面定义 的delay()函数,而采用一个for()循环来实现延时。
********************************************************************/
unsigned char read_bit(void)
{ unsigned char i;
DQ = 0; //将DQ 拉低开始读时间隙
_nop_();
_nop_();
DQ = 1; // then return high
for (i=0; i<3; i++); // 延时15μs
return(DQ); // 返回 DQ 线上的电平值
}
unsigned char read_byte(void)
{ unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{ // 读取字节,每次读取一个字节
if(read_bit())
{
value |= 0x01<<i; // 然后将其左移
}
delay(8); // 120
}
return(value);
}
int temp_data[2] = {0};
void read_temp()
{ ow_reset();
write_byte(0xcc);
write_byte(0xbe);
temp_data[0]=read_byte();
temp_data[1]=read_byte();
ow_reset();
write_byte(0xcc);
write_byte(0x44);
}
char buf[20];
unsigned char RomCode[8];
unsigned char code Code1[8] = {0x28, 0x30, 0xC5, 0xB8,0x00, 0x00, 0x00, 0x8E};
unsigned char code Code2[8] = {0x28, 0x31, 0xC5, 0xB8,0x00, 0x00, 0x00, 0xB9};
unsigned char code Code3[8] = {0x28, 0x32, 0xC5, 0xB8,0x00, 0x00, 0x00, 0xE0};
unsigned char code Code4[8] = {0x28, 0x33, 0xC5, 0xB8,0x00, 0x00, 0x00, 0xD7};
void ReadCode(void)
{ char i;
ow_reset();
write_byte(0x33);
for(i=0; i<8; i++)
{
RomCode = read_byte();
}
for(i=0; i<8; i++)
{ sprintf(buf, " %x ", RomCode);
SendStr(buf);
}
}
unsigned char FindKey()
{ unsigned char key, temp;
P_KEY = 0x0f;
key = P_KEY;
if (key == 0x0f)
{
return 0;
}
delay(5000);
key = P_KEY;
if (key == 0x0f)
{
return 0;
}
temp = (key >> 4);
if (temp >= 4)
{
if (temp == 4)
{
temp = 3;
}
else
{
temp = 4;
}
}
P_KEY = 0xf0;
key = P_KEY;
key = key & 0x0f;
if (key >= 4)
{
if (key == 4)
{
key = 3;
}
else
{
key = 4;
}
}
key = (key - 1) * 4 + temp;
P_KEY = 0x0f;
while( P_KEY != 0x0f);
return key;
}
/**************报警输出******************/
warning() //报警输出;
{int i,j;
for(j=0;j<400;j++)
{
{sound=1;
for(i=0;i<100;i++)
sound=0;
}
}
}
main()
{ int key;
unsigned char temp = 1, i;
int iTemp,cTemp,bTemp,dTemp;
float ftTemp;
unsigned char temp_key;
unsigned char show = 1;
long int k;
unsigned char n1=0,n2=0,n3=0,n4=0;
DQ = 1;
uart_init();
InitialMax7219();
temp = ow_reset();
write_byte(0xcc);
write_byte(0x44);
for (i=0; i<8; i++)
{
WriteToMax7219(i + 1, ascii[0]);
}
while(1)
{
temp_key = FindKey();
if (temp_key != 0)
{
key = temp_key;
if (key==15)
show = 1;
if (key==16)
show = 0;
}
ow_reset(); //////////////////////////
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code1);
}
write_byte(0x44);
temp_data[0] = temp_data[1] = 0;
ow_reset();
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code1);
}
write_byte(0xbe);
temp_data[0]=read_byte();
temp_data[1]=read_byte();
delay(50000);
if(temp_data[1]>127) //如果最高位为1则将补码变为原码;
{temp_data[1]=(~temp_data[1]);
temp_data[0]=(~temp_data[0]+1);
n1=1;
}
k=((temp_data[1]&0x0f)*16+(temp_data[0]&0xf0)/16);
ftTemp = k + (temp_data[0] & 0x0f) * 0.0625;
if (n1 == 1)
{ if (ftTemp > 5.0)
{ show = 0;
key = 1;
warning();
}
}
else
{
if (ftTemp > 10.0)
{ show = 0;
key = 1;
warning();
}
}
if(key==1 || show)
{sprintf(buf, "one = %f ", ftTemp);
SendStr(buf);
bTemp = k/100;
cTemp=k%100/10;
iTemp = k%10;
dTemp=((temp_data[0]&0x0f)*0.0625*10);
WriteToMax7219(1, ascii[1] & 0x7f);
WriteToMax7219(2, ascii[11] & 0x7f);
if(n1==1)
{WriteToMax7219(2, ascii[10] & 0x7f);
n1=0;
}
WriteToMax7219(3, ascii[bTemp] & 0x7f);
WriteToMax7219(4, ascii[cTemp] & 0x7f);
WriteToMax7219(5, ascii[iTemp]+0x80 );
WriteToMax7219(6, ascii[dTemp]& 0x7f);
}
ow_reset(); //////////////////////////////
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code2);
}
write_byte(0x44);
temp_data[0] = temp_data[1] = 0;
ow_reset();
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code2);
}
write_byte(0xbe);
temp_data[0]=read_byte();
temp_data[1]=read_byte();
delay(50000);
if(temp_data[1]>127) //如果最高位为1则将补码变为原码;
{temp_data[1]=(~temp_data[1]);
temp_data[0]=(~temp_data[0]+1);
n2=1;
}
k=((temp_data[1]&0x0f)*16+(temp_data[0]&0xf0)/16);
ftTemp = k + (temp_data[0] & 0x0f) * 0.0625;
if (n2 == 1)
{
if (ftTemp > 5.0)
{ show = 0;
key = 2;
warning();
}
}
else
{
if (ftTemp > 10.0)
{ show = 0;
key = 2;
warning();
}
}
if(key==2 || show)
{sprintf(buf, "two = %f ", ftTemp);
SendStr(buf);
bTemp = k/100;
cTemp=k%100/10;
iTemp = k%10;
dTemp=((temp_data[0]&0x0f)*0.0625*10);
WriteToMax7219(1, ascii[2] & 0x7f);
WriteToMax7219(2, ascii[11] & 0x7f);
if(n2==1)
{WriteToMax7219(2, ascii[10] & 0x7f);
n2=0;
}
WriteToMax7219(3, ascii[bTemp] & 0x7f);
WriteToMax7219(4, ascii[cTemp] & 0x7f)
WriteToMax7219(5, ascii[iTemp]+0x80 );
WriteToMax7219(6, ascii[dTemp]& 0x7f);
}
ow_reset(); /////////////////////////
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code3);
}
write_byte(0x44);
temp_data[0] = temp_data[1] = 0;
ow_reset();
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code3);
}
write_byte(0xbe);
temp_data[0]=read_byte();
temp_data[1]=read_byte();
delay(50000);
if(temp_data[1]>127) //如果最高位为1则将补码变为原码;
{temp_data[1]=(~temp_data[1]);
temp_data[0]=(~temp_data[0]+1);
n3=1;
}
k=((temp_data[1]&0x0f)*16+(temp_data[0]&0xf0)/16);
ftTemp = k + (temp_data[0] & 0x0f) * 0.0625;
if (n3 == 1)
{
if (ftTemp > 5.0)
{ show = 0;
key = 3;
warning();
}
}
else
{
if (ftTemp > 10.0)
{ show = 0;
key = 3;
warning();
}
}
if(key==3 || show)
{sprintf(buf, "three = %f ", ftTemp);
SendStr(buf);
bTemp = k/100;
cTemp=k%100/10;
iTemp = k%10;
dTemp=((temp_data[0]&0x0f)*0.0625*10);
WriteToMax7219(1, ascii[3] & 0x7f);
WriteToMax7219(2, ascii[11] & 0x7f);
if(n3==1)
{WriteToMax7219(2, ascii[10] & 0x7f);
n3=0;
}
WriteToMax7219(3, ascii[bTemp] & 0x7f);
WriteToMax7219(4, ascii[cTemp] & 0x7f);
WriteToMax7219(5, ascii[iTemp]+0x80);
WriteToMax7219(6, ascii[dTemp]& 0x7f);
}
ow_reset(); ///////////////////
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code4);
}
write_byte(0x44);
temp_data[0] = temp_data[1] = 0;
ow_reset();
write_byte(0x55);
for (i=0; i<8; i++)
{
write_byte(Code4);
}
write_byte(0xbe);
temp_data[0]=read_byte();
temp_data[1]=read_byte();
delay(50000);
if(temp_data[1]>127) //如果最高位为1则将补码变为原码;
{temp_data[1]=(~temp_data[1]);
temp_data[0]=(~temp_data[0]+1);
n4=1;
}
k=((temp_data[1]&0x0f)*16+(temp_data[0]&0xf0)/16);
ftTemp = k + (temp_data[0] & 0x0f) * 0.0625;
if (n4 == 1)
{
if (ftTemp > 5.0)
{ show = 0;
key = 4;
warning();
}
}
else
{
if (ftTemp > 10.0)
{ show = 0;
key = 4;
warning();
}
}
if(key==4 || show)
{sprintf(buf, "four = %f ", ftTemp);
SendStr(buf);
bTemp = k/100;
cTemp=k%100/10;
iTemp = k%10;
dTemp=((temp_data[0]&0x0f)*0.0625*10);
WriteToMax7219(1, ascii[4] & 0x7f);
WriteToMax7219(2, ascii[11] & 0x7f);
if(n4==1)
{WriteToMax7219(2, ascii[10] & 0x7f);
n4=0;
}
WriteToMax7219(3, ascii[bTemp] & 0x7f);
WriteToMax7219(4, ascii[cTemp] & 0x7f);
WriteToMax7219(5, ascii[iTemp]+0x80 );
WriteToMax7219(6, ascii[dTemp]& 0x7f);
}
}
#include <uart.h>
void uart_init(void)
{
TMOD = 0x20; //COM2:自动重置初值的8位定时/计数器
TL1 = 0xfd;
TH1 = 0xfd;
TR1 = 1; //启动定时器
SCON = 0x50; //串口工作方式设置字节,1个起始位,8个数据位,1个停止位组成10位帧
PCON = 0x00;
ES = 1;
EA = 1;
}
void SendStr(char *str)
{
TI = 0;
while(*str != 0x00)
{
SBUF = *str++;
while(TI == 0);
TI = 0;
}
}
#include <max7219.h>
#include <INTRINS.H>
extern unsigned char code ascii[];
void WriteToMax7219 (unsigned char Cmd ,unsigned char Data)
{
unsigned char tLoop = 0;
unsigned int tMask = 0x0001;
unsigned int tWord = 0;
tWord = Cmd;
tWord = (tWord << 8) + Data;
CLK0 = 0;
CS0 = 0;
for(tLoop=0,tMask=0x8000; tLoop<16 ;tLoop++)
{
DIN0=tWord&tMask;
_nop_();
_nop_(); //rising edge
CLK0=1;
if(tLoop==15) //Take CS high (while CLK is still high after clocking in
CS0=1;
CLK0=0;
DIN0=0;
tMask=tMask>>1;
}
CS0=1;
}
void InitialMax7219(void)
{
unsigned char i;
CS0=1;
CLK0=0;
WriteToMax7219(0x0F,0x00);
WriteToMax7219(0x0C,0x01);
WriteToMax7219(0x0A ,0x06);
WriteToMax7219(0x09,0xFF) ;
WriteToMax7219(0x0B,0x07) ;
for(i=2;i<9;i++)
{
WriteToMax7219(i,ascii[8]);
}
WriteToMax7219(0x01,0x0E);
}
-
QQ图片20160415141517.png
(31.41 KB, 下载次数: 113)
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |