找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

帮忙看一下程序,定时器中断不能正常工作

[复制链接]
admin 发表于 2012-9-2 09:26:05 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#include <stdlib.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define COM XBYTE[0xffff]//定义显示数组
#define DAT XBYTE[0xfeff]//定义显示数组
/*-----------------------全局变量-------------------------*/
uchar buf[8];//数据缓冲区
uchar key;//键值存储
uchar ii0,si0,wi;
uchar ii1,si1;
uchar bh;
uchar i;
uchar temp1,temp2,temp3,temp4;//实时温度存储
uint sc0,sm0,st0;
uint sc1,sm1,st1;
uint t1,t2;
uint t3,t4;
uint j0,j1,l0,l1,t;
uint wd0,wd1,wd2,wd3,wd4; //设定温度存储
uint ds;
//uint flag_18b201,flag_18b202;
/*-----------------------I/O接口定义-------------------------*/
sbit SI=P1^0; //串行数据输入到看门狗
sbit SCK=P1^1; //看门狗串行时钟
sbit SO=P1^2; //看门狗串行输出
sbit CS=P1^3; //看门狗启动


sbit TSOR0=P1^4;//18b20-1数据传输
sbit KZ0=P1^5;//温控继电器1控制
sbit TSOR1=P1^6; //18b20-2数据传输
sbit KZ1=P1^7;//温控继电器2控制
sbit Beep=P3^2; //报警控制

/*-----------------------数字温度驱动---------------------*/
void Delay15();//延时15
void Delay60();//延时60
void Delay100ms();//延时100ms
/*--------------18b20-1操作--------------------------- */
void Write0TS0();
void Write1TS0();
bit ReadTS0();
void ResetTS0();
void WriteBTS0(uchar byte);
uchar ReadBTS0();
void InitTS0();
void GetTempTS0();
/*----------------18b20-1操作-------------------------*/
void Write0TS1();
void Write1TS1();
bit ReadTS1();
void ResetTS1();
void WriteBTS1(uchar byte);
uchar ReadBTS1();
void InitTS1();
void GetTempTS1();
/*---------------X5045操作--------------------------*/
void RESWDI(void);//存储初始化
void WREN(void);
void WRDI(void);
void WRSR(void);
uchar RSDR(void);
void WIPCHK(void);
void OUTByte(uchar Byte);
uchar INPUTByte(void);
uchar ReadByte(uchar ADD);
void WriteByte(uchar Byte ,uchar ADD);
uchar dushu(uchar ADD);
void xieshu(uchar ds,uchar ADD);

/*---------------------定义标志位--------------------*/
uchar bdata flag_use;
sbit flag_bit_cf=flag_use^0;
sbit flag_bit_end=flag_use^1;
sbit flag_bit_cb0=flag_use^2;
sbit flag_bit_cb1=flag_use^3;
sbit flag_bit_cf0=flag_use^4;
sbit flag_bit_cf1=flag_use^5;
sbit flag_bit_time0=flag_use^6;
sbit flag_bit_time1=flag_use^7;
sbit clflag=ACC^7;
/*---------------------定义定义缓存区--------------------*/
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x00};
uchar idata diss[8]={0,0,0,0,0,0,0,0};

uchar deky();
void disp(uchar idata *d);
/*--------------主程序-------------------*/
void main(void)
{

SP=0x60; //堆栈指针初始化
TMOD=0x21; //定时器工作于方式2,自动装载方式
TH0=0xe5;
TL0=0xf5; //初始化时钟基准
TR0=1; //启动定时器0
ET0=1; //开定时器0中断
EA=1; //开总中断
sm0=0;
sc0=0;
sm1=0;
sc1=0;
flag_bit_cb0=0;
flag_bit_cb1=0;
flag_bit_cf0=0;
flag_bit_cf1=0;
flag_bit_time0=0;
flag_bit_time1=0;
flag_bit_cf=0;
flag_bit_end=0;
RESWDI(); //存储初始化
WRSR();
j0=dushu(0x01); //读取时间1数据
wd0=dushu(0x04); //读取温度1数据
j1=dushu(0x08); //读取时间2数据
wd1=dushu(0x012); //读取温度2数据
InitTS0(); //温度1初始化
InitTS1(); //温度2初始化
COM=0xd1;
do{ACC=COM;}
while(clflag==1);
COM=0x00;
COM=0x2d;
while(1)
{
do{
GetTempTS0(); //实际温度1
GetTempTS1(); //实际温度2
if(key==21){ii0=0;sc0=0;sm0=0;j0=dushu(0x01);wd0=dushu(0x04); l0=j0;flag_bit_cf0=1;key=0;Beep=1;} //启动1
if(key==37){ii0=0;sc0=0;sm0=0;j0=dushu(0x01);wd0=dushu(0x04); l0=j0;flag_bit_cf0=0;key=0;Beep=1;KZ0=1;} //停止1
if(key==29){ii1=0;sc1=0;sm1=0;j1=dushu(0x08);wd1=dushu(0x012);l1=j1;flag_bit_cf1=1;key=0;Beep=1;} //启动2
if(key==45){ii1=0;sc1=0;sm1=0;j1=dushu(0x08);wd1=dushu(0x012);l1=j1;flag_bit_cf1=0;key=0;Beep=1;KZ1=1;} //停止2
//------------------停止1------------------
if(flag_bit_cf0==0)
{
//int i ;
i=1;
// if(i==1) {j0=key; xieshu(j0,0x01); key=0;} //时间1 +
if(key==12) {j0=j0+1; xieshu(j0,0x01); key=0;} //时间1 +
if(key==20) {j0=j0-1; xieshu(j0,0x01); key=0;} //时间1 -
if(key==28) {wd0=wd0+1;xieshu(wd0,0x04);key=0;} //温度1 +
if(key==36) {wd0=wd0-1;xieshu(wd0,0x04);key=0;} //温度1 -
if(j0>99) j0=99;
if(j0<=1) j0=1;
if(wd0>=99) wd0=99;
if(wd0<=1) wd0=1;
diss[0]=j0/10; //设置数据1
diss[1]=j0%10;
diss[4]=wd0/10;
diss[5]=wd0%10;
}
//------------------启动1------------------
if(flag_bit_cf0==1)
{
diss[0]=j0/10; //实时时间数据1
diss[1]=j0%10;
if(temp1%10<=9&&temp1/10<=9)
{
diss[4]=temp1/10;//实时温度数据1
diss[5]=temp1%10;
}
if(st0>=t2){flag_bit_time0=0;st0=0;}
if(temp1<wd0-2) {KZ0=0;}
if(wd0-2<=temp1<=wd0+2) {KZ0=0;t1=3;t2=3;}
if(temp1>wd0+2){KZ0=1;}
/*if(temp1<wd0-5) {KZ0=0;t1=2;t2=2;}
if(temp1<wd0-1) {KZ0=0;t1=2;t2=3;}
if(temp1>=wd0) {KZ0=1;t1=1;t2=4;}
if(temp1>=wd0+1){KZ0=1;t1=1;t2=5;}
if(temp1>=wd0+2){KZ0=1;t1=1;t2=6;}
if(temp1>=wd0+3){KZ0=1;t1=1;t2=7;}
if(temp1>=wd0+4){KZ0=1;t1=1;t2=8;}
if(temp1>=wd0+5){KZ0=1;t1=1;t2=9;}*/

if(j0==0){ii0=0;sc0=0;sm0=0;flag_bit_cf0=0;j0=0;Beep=0;} //结束1
}
//-------------------停止2------------------
if(flag_bit_cf1==0)
{
if(key==14) {j1=j1+1; xieshu(j1,0x08); key=0;} //时间2 +
if(key==22) {j1=j1-1; xieshu(j1,0x08); key=0;} //时间2 -
if(key==38) {wd1=wd1+1;xieshu(wd1,0x12);key=0;} //温度2 +
if(key==46) {wd1=wd1-1;xieshu(wd1,0x12);key=0;} //温度2 -
if(j1>99) j1=99;
if(j1<=1) j1=1;
if(wd1>=80) wd1=80;
if(wd1<=30) wd1=30;
diss[2]=j1/10; //设置数据2
diss[3]=j1%10;
diss[6]=wd1/10;
diss[7]=wd1%10;
}
//------------------启动2------------------
if(flag_bit_cf1==1)
{
diss[2]=j1/10; //实时时间数据2
diss[3]=j1%10;
if(temp3%10<=9&&temp3/10<=9)
{
diss[6]=temp3/10;//实时温度数据2
diss[7]=temp3%10;
}
/*if(temp3<wd1-5) {KZ1=0;t3=2;t4=2;flag_bit_end=0;}
if(temp3<wd1-1) {KZ1=0;t3=2;t4=3;flag_bit_end=0;}
if(temp3>=wd1) {KZ1=1;t3=1;t4=4;flag_bit_end=0;}
if(temp3>=wd1+1){KZ1=1;t3=1;t4=5;flag_bit_end=0;}
if(temp3>=wd1+2){KZ1=1;t3=1;t4=6;flag_bit_end=1;}
if(temp3>=wd1+3){KZ1=1;t3=1;t4=7;flag_bit_end=1;}
if(temp3>=wd1+4){KZ1=1;t3=1;t4=8;flag_bit_end=1;}
if(temp3>=wd1+5){KZ1=1;t3=1;t4=9;flag_bit_end=1;}*/
if(j1==0){ii1=0;sc1=0;sm1=0;flag_bit_cf1=0;j1=0;Beep=0;} //结束2
}
//------------------数码显示---------------
for(i=0;i<8;i++)
{
disp(diss);
}
}while(deky()==0);
COM=0x40;
key=DAT;
key=key&0x3f; //读按键值
}
}


/*=======================中断处理程序=====================*/

void time0() interrupt 1 using 1
{
TR0=0;
TH0=0xe5; //10ms 65536-(fosc*t*1000)/12
TL0=0xf5;
//--------------确认1---------------
if(flag_bit_cf0==1)
{
ii0=ii0+1;
if(ii0==100)
{
ii0=0;
st0=st0+1;
if(flag_bit_time0==0) //启振时间1
{
if(st0>=t1){KZ0=0;flag_bit_time0=1;st0=0;}
}
if(flag_bit_time0==1) //控温时间1
{
if(st0>=t2){KZ0=1;flag_bit_time0=0;st0=0;}
}
sc0=sc0+1;
if(sc0==60) //1分钟到
{
sc0=0;
sm0=sm0++;
j0=l0-sm0; //设置时间1递减
}
}
}
//--------------确认2---------------
if(flag_bit_cf1==1)
{
ii1=ii1+1;
if(ii1==400)
{
ii1=0;
st1=st1+1;
if(flag_bit_time1==0) //启振时间2
{
if(st1>=t3){KZ1=0;flag_bit_time1=1;st1=0;}
}
if(flag_bit_time1==1) //控温时间2
{
if(st1>=t4){KZ1=1;flag_bit_time1=0;st1=0;}
}
sc1=sc1+1;
if(sc1==60) //1分钟到
{
sc1=0;
sm1=sm1+1;
j1=l1-sm1; //设置时间1递减
}
}
}
TR0=1;
}


void disp(uchar idata *d)
{
uchar i;
COM=0x90;
for(i=0;i<8;i++)
{
COM=i+0x80;
DAT=table[*d];
d++;
}
}

uchar deky(void)
{
uchar k;
k=COM;
return(k&0x0f);
}

//---------------ds18b20--------------------

void Delay100ms()
{
uchar i,j,k;
for(i=0;i<8;i++)
for(j=0;j<25;j++)
for(k=0;k<250;k++);
}

void Delay15()
{
uchar i;
for(i=0;i<8;i++);
}

void Delay60()
{
uchar i;
for(i=0;i<30;i++);
}

void Write0TS0( )
{
TSOR0=1;
TSOR0=0;
Delay15();
Delay15();
Delay15();
Delay15();
TSOR0=1;
_nop_();
_nop_();

}

void Write1TS0()
{
TSOR0=1;
TSOR0=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TSOR0=1;
_nop_();
_nop_();
 楼主| admin 发表于 2012-9-2 09:26:10 | 显示全部楼层
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
Delay15();
Delay15();
Delay15();
}

bit ReadTS0()
{
bit b;
TSOR0=1;
TSOR0=0;
_nop_();
_nop_();
_nop_();
_nop_();
TSOR0=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
b=TSOR0;
Delay15();
Delay15();
Delay15();
_nop_();
_nop_();
return b;
}

void ResetTS0()
{
uchar i;
TSOR0=1;
TSOR0=0;
for(i=0;i<8;i++)
Delay60();
TSOR0=1;
//while(TSOR0)
for(i=0;i<8;i++)
Delay60();
}

void WriteBTS0(uchar byte)
{
uchar i;
for(i=0;i<8;i++)
{
if(byte&0x01)
Write1TS0();
else
Write0TS0();
byte=byte>>1;
}
}

uchar ReadBTS0()
{
uchar i,j;
bit b;
j=0;
for(i=0;i<8;i++)
{
b=ReadTS0();
if(b)
j+=1;
j=_cror_(j,1);
}
return j;
}

void InitTS0()
{
ResetTS0();
WriteBTS0(0xCC);
WriteBTS0(0x4E);
WriteBTS0(0x64);
WriteBTS0(0x8A);
WriteBTS0(0x1F);
}

void GetTempTS0()
{
temp1=0;
temp2=0;
ResetTS0();
WriteBTS0(0xCC);
WriteBTS0(0x44);
Delay100ms();
ResetTS0();
WriteBTS0(0xCC);
WriteBTS0(0xBE);
temp2=ReadBTS0();
temp1=ReadBTS0();
ReadBTS0();
ReadBTS0();
ReadBTS0();
ReadBTS0();
ReadBTS0();
ReadBTS0();
ReadBTS0();
temp1=temp1<<4;
temp1+=(temp2&0xF0)>>4;
temp2=(temp2&0x0F)?5:0;
}

//--------------------------------------
void Write0TS1( )
{
TSOR1=1;
TSOR1=0;
Delay15();
Delay15();
Delay15();
Delay15();
TSOR1=1;
_nop_();
_nop_();

}

void Write1TS1()
{
TSOR1=1;
TSOR1=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TSOR1=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
Delay15();
Delay15();
Delay15();
}

bit ReadTS1()
{
bit b;
TSOR1=1;
TSOR1=0;
_nop_();
_nop_();
_nop_();
_nop_();
TSOR1=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
b=TSOR1;
Delay15();
Delay15();
Delay15();
_nop_();
_nop_();
return b;
}

void ResetTS1()
{
uchar i;
TSOR1=1;
TSOR1=0;
for(i=0;i<8;i++)
Delay60();
TSOR1=1;
//while(TSOR0)
for(i=0;i<8;i++)
Delay60();
}

void WriteBTS1(uchar byte)
{
uchar i;
for(i=0;i<8;i++)
{
if(byte&0x01)
Write1TS1();
else
Write0TS1();
byte=byte>>1;
}
}

uchar ReadBTS1()
{
uchar i,j;
bit b;
j=0;
for(i=0;i<8;i++)
{
b=ReadTS1();
if(b)
j+=1;
j=_cror_(j,1);
}
return j;
}

void InitTS1()
{
ResetTS1();
WriteBTS1(0xCC);
WriteBTS1(0x4E);
WriteBTS1(0x64);
WriteBTS1(0x8A);
WriteBTS1(0x1F);
}

void GetTempTS1()
{
temp3=0;
temp4=0;
ResetTS1();
WriteBTS1(0xCC);
WriteBTS1(0x44);
Delay100ms();
ResetTS1();
WriteBTS1(0xCC);
WriteBTS1(0xBE);
temp4=ReadBTS1();
temp3=ReadBTS1();
ReadBTS1();
ReadBTS1();
ReadBTS1();
ReadBTS1();
ReadBTS1();
ReadBTS1();
ReadBTS1();
temp3=temp3<<4;
temp3+=(temp4&0xF0)>>4;
temp4=(temp4&0x0F)?5:0;
}

/*----------------------------X5045-----------------------------*/
uchar dushu(uchar ADD)
{
uchar ds;
WRDI();
ds=ReadByte(ADD);
return ds;
}

void xieshu(uchar ds,uchar ADD)
{
WREN();
WriteByte(ds,ADD);
}

void RESWDI(void)
{
CS=0;
CS=1;
}

void WREN(void)
{
SCK=0;
CS=0;
OUTByte(0x06);
SCK=0;
CS=1;
}
void WRDI(void)
{
SCK=0;
CS=0;
OUTByte(0x04);
SCK=0;
CS=1;
}
void WRSR(void)
{
SCK=0;
CS=0;
OUTByte(0x01);
OUTByte(0x20);
SCK=0;
CS=1;
WIPCHK();
}
uchar RSDR(void)
{
uchar Temp;
SCK=0;
CS=0;
OUTByte(0x05);
Temp=INPUTByte();
SCK=0;
CS=1;
return Temp;
}
void WIPCHK(void)
{
uchar Temp,TempCyc;
for(TempCyc=0;TempCyc<50;TempCyc++)
{
Temp=RSDR();
if(Temp&0x01==0)
TempCyc=50;
}
}
void OUTByte(uchar Byte)//看门狗写入数据
{
uchar TempCyc;
for(TempCyc=0;TempCyc<8;TempCyc++)
{
SCK=0;
SI=Byte&0x80;
Byte=Byte<<1;
SCK=1;
}
SI=0;
}
uchar INPUTByte(void)
{
uchar Temp=0,TempCyc;
for(TempCyc=0;TempCyc<8;TempCyc++)
{
Temp=Temp<<1;
SCK=0;
if(SO)
Temp=Temp|0x01;
SCK=1;
}
return Temp;
}
uchar ReadByte(uchar ADD)
{
uchar Temp;
SCK=0;
CS=0;
OUTByte(0x03);
OUTByte(ADD);
Temp=INPUTByte();
SCK=0;
CS=1;
return Temp;
}
void WriteByte(uchar Byte,uchar ADD)
{
SCK=0;
CS=0;
OUTByte(0x02);
OUTByte(ADD);
OUTByte(Byte);
SCK=0;
CS=1;
WIPCHK();

}
/*=======================END==============================*/
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-24 12:41 , Processed in 0.057724 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表