信工课程设计:电路焊接与时序级编程
- 主要芯片:AT89C52,DS18B20,24C16,HD7279
- 主要功能:实时测温,手动风扇变速,自动风扇变速,PID控温,参数配置,密码验证,键盘变速连按等
#include <REG52.H>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
#define U8 unsigned char
#define somenop(); _nop_();_nop_();_nop_();_nop_();_nop_();
#define somenop10(); somenop();somenop();
#define somenop25(); somenop();somenop();somenop();somenop();somenop();
#define somenop50(); somenop25();somenop25();
#define somenop100(); somenop50();somenop50();
#define kp 5
#define ki 5
#define kd 5
/*
#define CMD_RESET 0xA4
#define PIDA 12
#define PIDB 10
*/
unsigned char KeyNum;
unsigned char KeyValue;
int KeyCount1;
U8 temperature[2];
unsigned char MotorNow;
bit MotorFlag,returnFlag;
unsigned char firstmenu;
unsigned char MotorCount = 0;
unsigned int TempNow;
unsigned int TempCount =0;
bit TempInput = 0;
int duk,e1=0,e2=0,e3=0;
unsigned char Tset = 30;
unsigned char Tup[4];
unsigned char Tdown[4];
unsigned char Level[11] = {0,10,20,30,40,50,60,70,80,90,100};
unsigned char pass[4] = {1,2,3,4};
unsigned char code KeyTable[4] = {0x3B,0x3A,0x39,0x38};
unsigned char Buff[8] = {0,1,2,3,4,5,6,7};
//unsigned char Disp[8] = {0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97};
unsigned char code LEDValue[35] = {0xFC,0x44,0x79,0x5D,0xC5,0x9D,0xBD,0x54,0xFD,0xDD,
0xFE,0x46,0x7B,0x5F,0xC7,0x9F,0xBF,0x56,0xFF,0xDF,
0xF5,0xAD,0xB8,0x6D,0xB9,0xB1,0xF1,0x01,0xA9,0x00,//A B C D E F P - t 空
0x21,0x2C,0x25,0x2D// r u n o
};
sbit CS = P1^4;
sbit CLK = P1^5;
sbit DATA = P1^7;
sbit Motor = P1^2;
sbit DS1820_DQ= P1^3;
sbit ECLK = P1^1;
sbit EDTA = P1^0;
void Key(void);
void Init_7279(void);
void send_byte(unsigned char);
void write_7279(unsigned char,unsigned char);
void display(unsigned char buff[]);
unsigned char ReadKey(void);
unsigned char receive_byte(void);
bit count(int ,int ,unsigned char);
void clean(void);
//void func(int);
void temper(void);
//void motor(void);
void motorConfigure(void);
void configure(void);
//void run(void);
//void runMotor(unsigned char);
void showTemp();
void choiceLevel();
bit DS1820_Reset(); //DS1820 复位
void DS1820_WriteData(U8 wData); //写数据到DS1820
U8 DS1820_ReadData(); //读数据
void displayTemperature();
void estart();
void estop();
bit ack();
void init_24c16();
void ewrite_byte(unsigned char);
unsigned char eread_byte();
void ewrite_add(unsigned char,unsigned char);
unsigned char eread_add(unsigned char);
void Menu(void);
void button(void);
void parun();
void pacon();
void paconb();
void paconf();
bit mm();
void pid();
void main (void) {
int tmr;
for(tmr=0;tmr<0x2000;tmr++);
SP = 0xB0;
//Init_7279();
TMOD = 0x11;
EA = 1;
ET0 = 1;
ET1 = 1;
KeyCount1 = 0;
TR0 = 1;
TR1 = 1;
// init_24c16();
firstmenu = 1;
clean();
//ewrite_add(4,Tdown[0]);ewrite_add(5,Tdown[1]);
//ewrite_add(6,Tup[0]);ewrite_add(7,Tup[0]);
//ewrite_add(10,50);ewrite_add(11,50);ewrite_add(12,50);
Tdown[0] = eread_add(4);somenop25();
Tdown[1] = eread_add(5);somenop25();
Tup[0] = eread_add(6);somenop25();
Tup[1] = eread_add(7);somenop25();
Level[0] = eread_add(20);somenop25();
Level[1] = eread_add(21);somenop25();
Level[2] = eread_add(22);somenop25();
Level[3] = eread_add(23);somenop25();
Level[4] = eread_add(24);somenop25();
Level[5] = eread_add(25);somenop25();
Level[6] = eread_add(26);somenop25();
Level[7] = eread_add(27);somenop25();
Level[8] = eread_add(28);somenop25();
Level[9] = eread_add(29);somenop25();
Level[10] = eread_add(30);somenop25();
while(1){
display(Buff);
Menu();
button();
}
/* 电机
MotorCount = 60;
MotorNow = 0;
TR0 = 0;
while(1){
TR0 = MotorFlag;
// run();
} ;
*/
/* 存储
init_24c16();
somenop25();
ewrite_add(0,5);
somenop25();
i = eread_add(0);
somenop25();
write_7279(0x93,LEDValue[i]);
ewrite_add(0,4);somenop25();
ewrite_add(1,5); somenop25();
ewrite_add(2,1); somenop25();
ewrite_add(3,3); somenop25();
Buff[0] = eread_add(0);somenop25();
Buff[1] = eread_add(1);somenop25();
Buff[2] = eread_add(2);somenop25();
Buff[3] = eread_add(3);somenop25();
display(Buff);
*/
/* 温度显示
while (1)
{
DS1820_Reset(); //复位
DS1820_WriteData(0xcc); //跳过ROM 命令
DS1820_WriteData(0x44); //温度转换命令
DS1820_Reset(); //复位
DS1820_WriteData(0xcc); //跳过ROM 命令
DS1820_WriteData(0xbe); //读DS1820 温度暂存器命令
for (i=0;i<2;i++)
{
temperature[i]=DS1820_ReadData(); //采集温度
}
DS1820_Reset(); //复位,结束读数据
displayTemperature(); //显示温度值
somenop50();
}*/
/* 菜单目录
run();
while(1);
*/
/* 键盘测试
for(i=0;i<8;i++)
Buff[i] = 0x15;
while(1)
{
display(Buff);
temp = ReadKey(); //读键值
if(temp != 0xFF){
Buff[0] = (temp>>4) & 0x0f;
Buff[1] = temp & 0x0f;
}
} */
/* 显示字形表
while(1){
//write_7279(0x90,0x79);
//send_byte(0xBF);
// display(Buff);
}
*/
}
void Menu(void){
switch(firstmenu){
case 1:{Buff[4] = 28;Buff[5] = 26;Buff[6] = 27;Buff[7] = 29;};break; //tp-
case 2:{Buff[4] = 30;Buff[5] = 31;Buff[6] = 32;Buff[7] = 27;};break; //run-
case 3:{Buff[4] = 22;Buff[5] = 33;Buff[6] = 32;Buff[7] = 27;};break; //con-
case 4:{Buff[4] = 26;Buff[5] = 20;Buff[6] = 27;Buff[7] = 29;};break; //pa-
case 5:{Buff[4] = 26;Buff[5] = 1;Buff[6] = 23;Buff[7] = 27;};break; //pid-
default : break;
}
//display(DispBuff);
}
//按键控制
void button(void)
{
Key();
if(KeyNum==1){
if(firstmenu==5)firstmenu=1;
else firstmenu++;
}
if(KeyNum==0){
if(firstmenu==1)firstmenu=5;
else firstmenu--;
}
if(KeyNum==2){
returnFlag = 1;
}
if(KeyNum==3) {
switch(firstmenu){
case 1:{showTemp();}break;
case 2:{returnFlag = 0;choiceLevel();}break;
case 3:{motorConfigure();}break;
case 4:{if(mm()){configure();}}break;
case 5:{pid();}break;
default : break;
}
}
}
/*
void pid(){
unsigned char MotorCount1,MotorCount2,MotorCount3,mTnow;
float u;
char A,B,C;
A = 12;
B = -10;
C = 0;
// u = a*kp*(1+t/PIDA+PIDB/t)-b*kp*(1+2*PIDB/t)+c*kp*PIDB/t;
TempCount = 15;
MotorCount1 = eread_add(10);//now-1
MotorCount2 = eread_add(11);//now-2
MotorCount3 = eread_add(12);//now
//mTnow = Buff[4]*1000+(Buff[5]-10)*100+Buff[6]*10+Buff[7];
returnFlag = 0;
while(!returnFlag){
Key();
if(TempInput == 1 ){
temper();
Buff[4] = Buff[0];
Buff[5] = Buff[1];
Buff[6] = Buff[2];
Buff[7] = Buff[3];
TempInput = 0;
//sh = 1;
mTnow = Buff[4]*1000+(Buff[5]-10)*100+Buff[6]*10+Buff[7];
}
u = A*mTnow/100+MotorCount1*B+MotorCount2*C;
MotorCount = MotorCount3 + u;
MotorCount2 = MotorCount1;
MotorCount1 = MotorCount;
if(KeyNum == 2){
returnFlag = 1;
//MotorCount =0;
}
}
ewrite_add(10,MotorCount1);
ewrite_add(11,MotorCount2);
ewrite_add(12,MotorCount3);
}
*/
/*
void run(void){
clean();
if(count(0,3,0x93)==0){
clean();
KeyCount1 = 0;
}
func(KeyCount1);
}
void func(int ifunc){
switch(ifunc){
case 0: {
Buff[4] = 28;Buff[5] = 26;Buff[6] = 27; display(Buff);
temper();
break;
}; //tp-
case 1: {
Buff[4] = 30;Buff[5] = 31;Buff[6] = 32;Buff[7] = 27; display(Buff);
KeyCount1 = 1;
choiceLevel();
break;}; //run-
case 2: {
Buff[4] = 22;Buff[5] = 33;Buff[6] = 32;Buff[7] = 27; display(Buff);
motorConfigure();
break;}; //con-
case 3: {
Buff[4] = 26;Buff[5] = 20;Buff[6] = 27; display(Buff);
configure();
break;}; //pa-
default:
run();
}
}
*/
bit mm(){
unsigned char m1,m2,m3,m4;
returnFlag = 0;
Buff[4] = 27;Buff[5] = 27;Buff[6] = 27;Buff[7] = 27;
display(Buff);
if(!count(0,9,0x94)){
return returnFlag;
}else{
m1 = KeyCount1;
KeyCount1 = 0;
Buff[4] = m1;
display(Buff);
}
if(!count(0,9,0x95)){
return returnFlag;
}else{
m2 = KeyCount1;
KeyCount1 = 0;
Buff[5] = m2;
display(Buff);
}
if(!count(0,9,0x96)){
return returnFlag;
}else{
m3 = KeyCount1;
KeyCount1 = 0;
Buff[6] = m3;
display(Buff);
}
if(!count(0,9,0x97)){
return returnFlag;
}else{
m4 = KeyCount1;
KeyCount1 = 0;
Buff[7] = m4;
display(Buff);
}
if(pass[0]==m1 && pass[1]==m2 && pass[2]==m3 &&pass[3]==m4 ){
returnFlag = 1;
}
return returnFlag;
}
void temper(void){
unsigned char i;
DS1820_Reset(); //复位
DS1820_WriteData(0xcc); //跳过ROM 命令
DS1820_WriteData(0x44); //温度转换命令
DS1820_Reset(); //复位
DS1820_WriteData(0xcc); //跳过ROM 命令
DS1820_WriteData(0xbe); //读DS1820 温度暂存器命令
for (i=0;i<2;i++)
{
temperature[i]=DS1820_ReadData(); //采集温度
}
DS1820_Reset(); //复位,结束读数据
displayTemperature(); //显示温度值 存储温度
Buff[0] = eread_add(0);somenop25();
Buff[1] = eread_add(1)+10;somenop25();
Buff[2] = eread_add(2);somenop25();
Buff[3] = eread_add(3);somenop25();
// somenop50();
// display(Buff);
//Key();
//if(KeyNum==2){
//退出
//}
// Buff[0] = 29;Buff[1] = 29;Buff[2] = 29;Buff[3] = 29;
}
/*
void motor(void){
unsigned char i;
i = 60; //级别选择
runMotor(i); //启动电机并控制返回
while(1){
Key();
if(KeyNum==2){
run();
}
}
}
*/
/*
void runMotor(unsigned char i){
MotorCount = i*10;
MotorNow = 0;
TR0 = 1;
while(1);
}
*/
void choiceLevel(){
//KeyCount1 = 1;
while( !returnFlag ){
if(KeyCount1==10){
Buff[6] = 1;Buff[7] = 0;
}else{
Buff[6] = 29;Buff[7] = KeyCount1;
}
Buff[4] = 30;Buff[5] = 27;
display(Buff); //r- num
if(count(0,10,0x97)==0){
KeyCount1 = 0;
returnFlag = 1;
Buff[0] = 29;Buff[1] = 29;Buff[2] = 29;Buff[3] = 29;
}else{
MotorCount = Level[KeyCount1];
if( MotorCount/100){
Buff[1] = MotorCount/100;
}else{Buff[1] = 29;}
if( !((MotorCount%100)/10) && Buff[1] == 29){
Buff[2] = 29;
}else{Buff[2] = (MotorCount%100)/10;}
Buff[0] = 29;Buff[3] = MotorCount%10;
}
}
}
void motorConfigure(void){
bit sh;
unsigned int mTnow,mTup,mTdown;
float mi;
TempCount = 15;
returnFlag = 0;
mTup = Tup[0]*1000+Tup[1]*100+Tup[2]*10+Tup[3];
mTdown = Tdown[0]*1000+Tdown[1]*100+Tdown[2]*10+Tdown[3];
while (!returnFlag) {
Key();
if(KeyNum==2){ returnFlag = 1;TempInput = 0;}
if(TempInput == 1 ){
temper();
Buff[4] = Buff[0];
Buff[5] = Buff[1];
Buff[6] = Buff[2];
Buff[7] = Buff[3];
TempInput = 0;
sh = 1;
mTnow = Buff[4]*1000+(Buff[5]-10)*100+Buff[6]*10+Buff[7];
}
if(mTup < mTnow){
MotorCount = 100;
Buff[0] = 29;Buff[1] = 1;Buff[2] = 0;Buff[3] = 0;
}else if(mTdown > mTnow){
MotorCount = 0;
Buff[0] = 29;Buff[1] = 29;Buff[2] = 29;Buff[3] = 0;
} else if(sh){
mi = 50 * (mTnow-mTdown)/(mTup-mTdown);
MotorCount = 50 + mi;
Buff[0] = 29;Buff[1] = 29;Buff[2] = (MotorCount%100)/10;Buff[3] = MotorCount%10;
}
display(Buff);
}
Buff[0] = 29;Buff[1] = 29;Buff[2] = 29;Buff[3] = 29;
//TempCount = 0;
KeyNum = 0xff;
}
void configure(void){
bit secondmenu;
returnFlag = 0;
Buff[4] = 26;Buff[5] = 20;Buff[6] = 27;Buff[7] = 29;
while(!returnFlag){
Key();
if(KeyNum==1){
secondmenu = !secondmenu;
}
if(KeyNum==0){
secondmenu = !secondmenu;
}
if(KeyNum==2){
returnFlag = 1;
}
if(KeyNum==3) {
if(secondmenu){
parun();
}else{
pacon();
}
}
if(secondmenu){
Buff[1] = 30;Buff[2] = 31;Buff[3] = 32;Buff[0] = 29; // run
}else{
Buff[1] = 22;Buff[2] = 33;Buff[3] = 32;Buff[0] = 29; // con
}
display(Buff);
}
clean();
}
void parun(){
unsigned char mlevelvalue,mlevel ;
returnFlag = 0;
while(!returnFlag){
if(KeyCount1==10){
Buff[6] = 1;Buff[7] = 0;
}else{
Buff[6] = 29;Buff[7] = KeyCount1;
}
Buff[4] = 26;Buff[5] = 27;
display(Buff); //P- num
if(!count(0,10,0x97)){
KeyCount1 = 0;
returnFlag = 1;
}else{
mlevelvalue = Level[KeyCount1];
if( mlevelvalue/100){
Buff[1] = mlevelvalue/100;
}else{Buff[1] = 29;}
if( !((mlevelvalue%100)/10) && Buff[1] == 29){
Buff[2] = 29;
}else{Buff[2] = (mlevelvalue%100)/10;}
Buff[0] = 29;Buff[3] = mlevelvalue%10;
if(KeyCount1==10){
Buff[6] = 1;Buff[7] = 0;
}else{
Buff[6] = 29;Buff[7] = KeyCount1;
}
Buff[4] = 20;Buff[5] = 27;
display(Buff); //A- num
mlevel = KeyCount1;
KeyCount1 = mlevelvalue;
if(!count(0,100,0x93)){
KeyCount1 = 0;
Buff[0] = 29;Buff[1] = 29;Buff[2] = 29;Buff[3] = 29;
// returnFlag = 1;
}else{
Level[mlevel] = KeyCount1;
ewrite_add(mlevel+20,KeyCount1);
KeyCount1 = 0;
Buff[0] = 29;Buff[1] = 29;Buff[2] = 29;Buff[3] = 29;
}
}
}
Buff[4] = 26;Buff[5] = 20;Buff[6] = 27;Buff[7] = 29; //pa-
Buff[0] = 29;Buff[1] = 30;Buff[2] = 31;Buff[3] = 32; // run
returnFlag = 0;
}
void pacon(){
bit thirdmenu;
returnFlag = 0;
while(!returnFlag){
Key();
if(KeyNum==1){
thirdmenu = !thirdmenu;
}
if(KeyNum==0){
thirdmenu = !thirdmenu;
}
if(KeyNum==2){
returnFlag = 1;
}
if(KeyNum==3) {
if(thirdmenu){
paconb();
}else{
paconf();
}
}
if(thirdmenu){
Buff[4] = 26;Buff[5] = 20;Buff[6] = 27;Buff[7] = 21; //pa-b
Buff[0] = 29;
Buff[1] = 29;
Buff[2] = Tdown[0];
Buff[3] = Tdown[1]; //Tdown
}else{
Buff[4] = 26;Buff[5] = 20;Buff[6] = 27;Buff[7] = 25; //pa-f
Buff[0] = 29;
Buff[1] = 29;
Buff[2] = Tup[0];
Buff[3] = Tup[1]; //Tup
}
display(Buff);
}
Buff[4] = 26;Buff[5] = 20;Buff[6] = 27;Buff[7] = 29;
returnFlag = 0;
}
void paconb(){
Buff[4] = 20;Buff[5] = 27;Buff[6] = 29;Buff[7] = 21; //a- b
display(Buff);
returnFlag = 0;
KeyCount1 = Tdown[0]*10+Tdown[1];
if(!count(0,50,0x93)){
KeyCount1 = 0;
}else{
Tdown[0]=KeyCount1/10;
ewrite_add(4,Tdown[0]);somenop25();
Tdown[1]=KeyCount1%10;
ewrite_add(5,Tdown[1]);somenop25();
}
}
void paconf(){
Buff[4] = 20;Buff[5] = 27;Buff[6] = 29;Buff[7] = 25; //a- f
display(Buff);
returnFlag = 0;
KeyCount1 = Tup[0]*10+Tup[1];
if(!count(0,50,0x93)){
KeyCount1 = 0;
}else{
Tup[0]=KeyCount1/10;
ewrite_add(6,Tup[0]);somenop25();
Tup[1]=KeyCount1%10;
ewrite_add(7,Tup[1]);somenop25();
}
}
void clean(void){
unsigned char i;
for(i=0;i<8;i++)
Buff[i] = 0x1D;
display(Buff);
}
//返回1确认,0退出
bit count(int min,int max,unsigned char w2){
bit f,s;
unsigned char ten;
ten = 0;
f = max/100;
if(f==0){
s = max/10;
}else{
s = 1;
}
write_7279(0x90,LEDValue[29]);
while(1){
if( KeyCount1 > max){
KeyCount1 = min;
}else if(KeyCount1 < min){
KeyCount1 = max;
}
if(KeyCount1 > 9 && KeyCount1 < 100){
if(f){write_7279(w2-2,LEDValue[29]);}
write_7279(w2-1,LEDValue[KeyCount1 / 10]);
write_7279(w2,LEDValue[KeyCount1 % 10]);
}else if(KeyCount1 == 100){
if(f){write_7279(w2-2,LEDValue[1]);}
write_7279(w2-1,LEDValue[0]);
write_7279(w2,LEDValue[0]);
}else{
if(f){write_7279(w2-2,LEDValue[29]);}
if(s){write_7279(w2-1,LEDValue[29]);}
write_7279(w2,LEDValue[KeyCount1]);
}
Key();
if(KeyNum == 0){
KeyCount1--;
ten=0;
}else if(KeyNum == 1){
if(ten == 10){
KeyCount1+=10;
}else{
ten++;
KeyCount1++;
}
}else if(KeyNum == 3){
return 1;
}else if(KeyNum == 2){
return 0;
}
}
}
void write_7279(unsigned char cmd,unsigned char dta)
{
CS = 0;
somenop50();
somenop();
send_byte(cmd);
somenop25();
somenop();
send_byte(dta);
somenop();
somenop();
CS = 1;
}
void send_byte(unsigned char cmd)
{
unsigned char i;
somenop50();somenop50(); //时序初始延时
for(i = 0;i < 8;i++)
{ //写cmd的最高位,
if(cmd&0x80) // 最高位为1则写"1"
DATA = 1;
else // 最高位为0写"0"
DATA = 0;
// CLK形成下降沿,先1后0,注意延时时间要满足时序要求
CLK = 1; somenop10();somenop10();
CLK = 0; somenop10();somenop10();
//命令行左移,为写下一个位做准备
cmd = cmd << 1;
}
DATA = 0; //依照时序要求全部写完后Data清零
}
void Init_7279(void)
{
CS = 0;
somenop50();somenop50();
send_byte(0xA4);
somenop50();somenop50();
CS = 1;
}
void display(unsigned char buff[])
{
unsigned char i,j;
for( i=0x90,j=0 ; j<=7 ; i++,j++ )
write_7279(i, LEDValue[buff[j]]);
}
unsigned char ReadKey(void)
{
unsigned char readkey;
CS = 0;
somenop50();somenop50();
send_byte(0x15); //读键值命令15H
somenop25();somenop25();
readkey = receive_byte();
CS = 1;
return(readkey); //返回键值
}
unsigned char receive_byte(void)
{
unsigned char i, in_byte;
DATA=1;
somenop50();somenop50();
for (i=0;i<8;i++)
{
CLK=1;somenop10();somenop10();
//左移一位,空出最后一位存放新进来的位DATA
in_byte=in_byte*2;
//若新进来的位DATA为1,则in_byte的末位置1,否则不需要变
if (DATA)
in_byte=in_byte|0x01;
CLK=0; somenop10();somenop10();
}
DATA=0;
return (in_byte); //返回接收值
}
/*
void Key(void)
{
unsigned char temp,i;
temp = ReadKey(); //读键值
if (temp==0xff)
{
KeyNum = 0xff;
KeyValue = 0xff;
}
else
{
if(KeyValue!=0xff)
KeyNum=0xff;
else
{
KeyValue = temp;
for(i=0;i<=3;i++)
if (KeyValue == KeyTable[i])
{ KeyNum = i; break; }
}
}
}
*/
void Key(void)
{
unsigned char i,temp;
unsigned int Press_CNT;
temp = ReadKey();
if(temp==0xFF){
KeyNum = 0xFF;
KeyValue = 0xFF;
}else{
if(KeyValue!=0xff){
// KeyNum=0xff;
if(Press_CNT < 0xfff)
{
Press_CNT ++;
KeyNum = 0xFF;
}
if(Press_CNT >= 500)//调用时间间隔确定
{
Press_CNT -= 50;
for(i=0;i<=1;i++){
if(KeyValue == KeyTable[i]){
KeyNum = i;
break;
}
}
}
}else{
KeyValue = temp;
for(i=0;i<=3;i++){
if(KeyValue == KeyTable[i]){
KeyNum = i;
Press_CNT = 0;
break;
}
}
}
}
}
bit DS1820_Reset()
{
bit flag;
DS1820_DQ = 0; //拉低总线
somenop100();somenop100();
somenop100();somenop100();
somenop50();somenop10();
somenop10();somenop10();
DS1820_DQ = 1; //释放总线
somenop50();somenop10();
somenop10();somenop10();
flag = DS1820_DQ; //对数据脚采样
somenop100();somenop100();
somenop100();somenop100();
return (flag);
}
void DS1820_WriteData(U8 wData)
{
U8 i;
for (i=8;i>0;i--)
{
DS1820_DQ = 0; //拉低总线,产生写信号
somenop();
DS1820_DQ = wData&0x01; //发送1 位
somenop50();somenop10();
DS1820_DQ = 1; //释放总线,等待总线恢复
wData>>=1; //准备下一位数据的传送
}
}
U8 DS1820_ReadData()
{
U8 i,TmepData;
for (i=8;i>0;i--)
{
TmepData>>=1;
DS1820_DQ = 0; //拉低总线,产生读信号
somenop();
DS1820_DQ = 1; //释放总线,准备读数据
somenop();somenop();
if (DS1820_DQ == 1)
{ TmepData |= 0x80;}
somenop50(); somenop10();
DS1820_DQ = 1; //拉高总线,准备下一位数据的读取.
}
return (TmepData);//返回读到的数据
}
void displayTemperature(){
// int Tnow,Told;
U8 temp_data,temp_data_2;
U8 temp[7]; //存放分解的7 个ASCII 码温度数据
U8 TempDec; //用来存放4 位小数
temp_data = temperature[1];
temp_data &= 0xf0; //取高4 位
if (temp_data==0xf0) //判断是正温度还是负温度读数
{
//负温度读数求补,取反加1,判断低8 位是否有进位
if (temperature[0]==0)
{ //有进位,高8 位取反加1
temperature[0]=~temperature[0]+1;
temperature[1]=~temperature[1]+1;
}
else
{ //没进位,高8 位不加1
temperature[0]=~temperature[0]+1;
temperature[1]=~temperature[1];
}
}
temp_data = temperature[1]<<4; //取高字节低4 位(温度读数高4 位),注意此时是12 位精度
temp_data_2 = temperature[0]>>4; //取低字节高4 位(温度读数低4 位),注意此时是12 位精度
temp_data = temp_data|temp_data_2; //组合成完整数据
//temp[0] = temp_data/100; //取百位转换为ASCII 码
temp[1] = (temp_data%100)/10; //取十位转换为ASCII 码
temp[2] = (temp_data%100)%10; //取个位转换为ASCII 码
temperature[0]&=0x0f; //取小数位转换为ASCII 码
TempDec = temperature[0]*6.25; //625=0.0625*10000,表示小数部分,扩大1 万倍 ,方便显示
temp[3] = (TempDec %100)/10; //取小数十分位转换为ASCII 码
temp[4] = TempDec % 10; //取小数百分位转换为ASCII 码
/*Buff[0] = temp[1];Buff[1] = temp[2]+10;Buff[2] = temp[3];Buff[3] = temp[4];
display(Buff); */
//if(temp[1]<4 && temp[1] > 0){
//Tnow = temp[1]*1000+temp[2]*100+temp[3]*10+temp[4];
//Told = eread_add(0)*1000+eread_add(1)*100+eread_add(2)*10+eread_add(3);
//if(!(Tnow - Told >= 100 || Told - Tnow <= 100)){
if(temp[1]<4 && temp[1] > 0){
ewrite_add(0,temp[1]); somenop25();
ewrite_add(1,temp[2]); somenop25();
ewrite_add(2,temp[3]); somenop25();
ewrite_add(3,temp[4]); somenop25();
}
// }
//}
//display(Buff);
/*
// temp[5] = ((TempDec%1000)%100)/10+0x30;//取小数千分位转换为ASCII 码
// temp[6] = ((TempDec%1000)%100)%10+0x30;//取小数万分位转换为ASCII 码
if(temp[0]==0x30) DisplayChar(3,0,' ');//如果百位为0,显示空格
else DisplayChar(3,0,temp[0]); //否则正常显示百位
DisplayChar(4,0,temp[1]);//十位
DisplayChar(5,0,temp[2]);//个位
DisplayChar(6,0,0x2e); //小数点 .
DisplayChar(7,0,temp[3]);
DisplayChar(8,0,temp[4]);
DisplayChar(9,0,temp[5]);
DisplayChar(10,0,temp[6]);
DisplayChar(11,0,'\''); //显示'
DisplayChar(12,0,'C'); //显示C
*/
}
void showTemp(){
TempCount = 15;
returnFlag = 0;
while (!returnFlag) {
Key();
if(KeyNum==2){ returnFlag = 1;TempInput = 0;}
if(TempInput == 1 ){
temper();
TempInput = 0;
}
display(Buff);
}
Buff[0] = 29;Buff[1] = 29;Buff[2] = 29;Buff[3] = 29;
//TempCount = 0;
//TempInput = 0;
}
void estart() //起始信号 当时钟线为1,数据线有个下降沿
{
ECLK=1;
EDTA=1;
somenop();
EDTA=0;
ECLK=0;
somenop();
}
void estop()//终止信号 当时钟线为1,数据线有个上升沿
{
EDTA=0;
ECLK=1;
somenop();
EDTA=1;
ECLK=0;
somenop();
}
bit ack() //应答信号由从机发出信号为sda由1变为0
{
ECLK=1;
EDTA=1;
if(EDTA==1){
ECLK=0;
return 1;
}else{
ECLK=0;
return 0;
}
}
void init_24c16()//24c16初始化
{
EDTA=1;
somenop();
ECLK=1;
somenop();
}
void ewrite_byte(unsigned char dat) //字节写(写数据或地址)数据线sda不变,scl有个上升沿,写入数据
{
unsigned char i;
for(i=0;i<8;i++)
{
ECLK=0;
somenop();
EDTA=dat&0x80;
somenop();
ECLK=1;
somenop();
dat <<= 1;
}
ECLK=0;
somenop();
}
unsigned char eread_byte() //字节读 scl有下降沿读出
{
unsigned char i,k;
for(i=0;i<8;i++)
{
ECLK=1;
somenop();
k=(k<<1)|EDTA;
ECLK=0;
somenop();
}
return k;
}
void ewrite_add(unsigned char add,unsigned char dat)
{
do{
estart();
ewrite_byte(0xa0);
}
while(ack());
ewrite_byte(add);
ack();
do{
ewrite_byte(dat);
}
while(ack());
estop();
}
unsigned char eread_add(unsigned char add)
{
unsigned char dat;
do{
estart();
ewrite_byte(0xa0);
}
while(ack());
ewrite_byte(add);
ack();
do{
estart();
ewrite_byte(0xa1);
}
while(ack());
dat=eread_byte();
estop();
return dat;
}
void pid(){
unsigned char mTnow;
TempCount = 15;
returnFlag = 0;
while(!returnFlag){
if(TempInput == 1 ){
temper();
Buff[4] = Buff[0];
Buff[5] = Buff[1];
Buff[6] = Buff[2];
Buff[7] = Buff[3];
TempInput = 0;
mTnow = Buff[4]*10+(Buff[5]-10);
e1 = mTnow - Tset;
duk = (kp+ki+kd)*e1 - (kp + 2*kd)*e2 + kd * e3;
MotorCount = MotorCount + duk;
if(MotorCount > 100) MotorCount = 100;
else if (MotorCount < 50) MotorCount = 50;
e2 = e1;
e3 = e2;
if( MotorCount == 100){
Buff[4] = 29;
Buff[5] = 1;
Buff[6] = 0;
Buff[7] = 0;
}else{
Buff[4] = 29;
Buff[5] = 29;
Buff[6] = MotorCount/10;
Buff[7] = MotorCount%10;
}
}
display(Buff);
Key();
if(KeyNum == 2){
returnFlag = 1;
Buff[0] = 29;
Buff[1] = 29;
Buff[2] = 29;
Buff[3] = 29;
display(Buff);
MotorCount = 0;
TempInput = 0;
//TempCount = 0;
}
}
}
/*
void pid(){
unsigned char MotorCount1,MotorCount2,MotorCount3,mTnow;
// int e1,e2,e3;
char A,B,C;
bit sh;
// e1 = 0;
// e2 = 0;
// e3 = 0;
A = 12;
B = -10;
C = 0;
// u = a*kp*(1+t/PIDA+PIDB/t)-b*kp*(1+2*PIDB/t)+c*kp*PIDB/t;
TempCount = 15;
MotorCount1 = eread_add(10);//now-1
MotorCount2 = eread_add(11);//now-2
MotorCount3 = eread_add(12);//now
//mTnow = Buff[4]*1000+(Buff[5]-10)*100+Buff[6]*10+Buff[7];
//Tset = eread_add(13);
returnFlag = 0;
while(!returnFlag){
Key();
if(KeyNum == 2){
returnFlag = 1;
Buff[4] = 29;
Buff[5] = 29;
Buff[6] = 29;
Buff[7] = 29;
display(Buff);
//MotorCount =0;
}
if(TempInput == 1 ){
temper();
Buff[4] = Buff[0];
Buff[5] = Buff[1];
Buff[6] = Buff[2];
Buff[7] = Buff[3];
TempInput = 0;
mTnow = Buff[4]*10+(Buff[5]-10);
MotorCount = MotorCount3 + A*mTnow+MotorCount1*B+MotorCount2*C - Tset;
if( MotorCount/10 > 9){
Buff[4] = 29;
Buff[5] = 1;
Buff[6] = 0;
Buff[7] = 0;
}else if(MotorCount/10 < 3){
Buff[4] = 29;
Buff[5] = 29;
Buff[6] = 3;
Buff[7] = 0;
}else{
Buff[4] = 29;
Buff[5] = 29;
Buff[6] = MotorCount/10;
Buff[7] = MotorCount%10;
}
}
display(Buff);
MotorCount2 = MotorCount1;
MotorCount1 = MotorCount;
}
ewrite_add(10,MotorCount1);
ewrite_add(11,MotorCount2);
ewrite_add(12,MotorCount3);
}
*/
void timer0(void) interrupt 1 using 3
{
TH0 = 0xFC;
TL0 = 0x66;
if(MotorNow < MotorCount){
Motor = 1;
MotorNow++;
P2 = 0x00;
}else if(MotorNow >= MotorCount && MotorNow <= 100){
Motor = 0;
P2 = 0x02;
MotorNow++;
}else{
MotorNow=0;
}
}
void timer1(void) interrupt 3 using 2
{
TH0 = 0xDC;
TL0 = 0x00;
if(TempNow != TempCount){
TempNow++;
}else if(TempNow >= TempCount && TempNow != 0){
TempInput = 1;
TempNow = 0;
}
}
网友评论