define uchar unsigned char
#define uint unsigned int
uchar a;
//定时串口输出温度的百位十位个位
uchar ge;
uchar shi;
uchar bai;
//gipo定义
sbit d = P3^7;
//第1个max6675引脚定义
sbit SO = P1.0;
sbit SCK = P3^5;
sbit CS1 = P3^4;
//第2个max6675引脚定义
sbit SO_2 = P1^1;
sbit SCK_2 = P3^5;
sbit CS1_2 = P3^4;
//第3个max6675引脚定义
sbit SO_3 = P1^2;
sbit SCK_3 = P3^5;
sbit CS1_3 = P3^4;
//第4个max6675引脚定义
sbit SO4 = P1^3;
sbit SCK_4 = P3^5;
sbit CS1_4 = P3^4;
//第5个max6675引脚定义
sbit SO5 = P1^4;
sbit SCK_5 = P3^5;
sbit CS1_5 = P3^4;
//第6个max6675引脚定义
sbit SO6 = P1^5;
sbit SCK_6 = P2^2;
sbit CS1_6 = P3^4;
//第7个max6675引脚定义
sbit SO7 = P1^6;
sbit SCK_7 = P3^5;
sbit CS1_7 = P3^4;
//第8个个max6675引脚定义
sbit SO8 = P1^7;
sbit SCK_8 = P3^5;
sbit CS1_8 = P3^4;
//定义状态与温度变量
unsigned char Flag_connect;
unsigned int MAX6675_Temp;
unsigned int tt1,tt2,tt3,tt4,tt5,tt6,tt7,tt8;
//延时函数声明
void delay(unsigned int z);
//--------------------------------------
//串口输出换行
void say_CR_LF()
{
SBUF = 13; while (TI == 0); TI = 0;
SBUF = 10; while (TI == 0); TI = 0;
}
//--------------------------------------
//串口输出温度
void say_yeah()
{
SBUF = bai; while (TI == 0); TI = 0;
SBUF = shi; while (TI == 0); TI = 0;
SBUF = ge; while (TI == 0); TI = 0;
say_CR_LF();
}
//--------------------------------------
//串口输出hello字符串用于测试
void say_hello()
{
SBUF = 'H'; while (TI == 0); TI = 0;
SBUF = 'e'; while (TI == 0); TI = 0;
SBUF = 'l'; while (TI == 0); TI = 0;
SBUF = 'l'; while (TI == 0); TI = 0;
SBUF = 'o'; while (TI == 0); TI = 0;
SBUF = '!'; while (TI == 0); TI = 0;
say_CR_LF();
}
//--------------------------------------
//第一个max6675获取温度函数
unsigned int MAX6675_ReadReg(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1 = 0;
SCK = 0;
//从max6675循环获取16位数据
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK = 1;
dat = dat<<1;
if( SO==1 )
dat = dat|0x01;
SCK = 0;
}
CS1 = 1;
//返回获取的值
return dat;
}
//第2个max6675获取温度
//区别只是改变gpio
unsigned int MAX6675_ReadReg_2(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1_2 = 0;
SCK_2 = 0;
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK_2 = 1;
dat = dat<<1;
if( SO_2==1 )
dat = dat|0x01;
SCK_2 = 0;
}
CS1_2 = 1;
return dat;
}
//第3个max6675获取温度
//区别只是改变gpio
unsigned int MAX6675_ReadReg_3(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1_3 = 0;
SCK_3 = 0;
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK_3 = 1;
dat = dat<<1;
if( SO_3==1 )
dat = dat|0x01;
SCK_3 = 0;
}
CS1_3 = 1;
return dat;
//第4个max6675获取温度函数
unsigned int MAX6675_ReadReg_5(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1_4 = 0;
SCK _4= 0;
//从max6675循环获取16位数据
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK_4 = 1;
dat = dat<<1;
if( SO_4==1 )
dat = dat|0x01;
SCK_4 = 0;
}
CS1_4 = 1;
//返回获取的值
return dat;
//第5个max6675获取温度函数
unsigned int MAX6675_ReadReg_6(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1_5 = 0;
SCK_5 = 0;
//从max6675循环获取16位数据
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK_5 = 1;
dat = dat<<1;
if( SO_5==1 )
dat = dat|0x01;
SCK_5 = 0;
}
CS1_5 = 1;
//返回获取的值
return dat;
//第6个max6675获取温度函数
unsigned int MAX6675_ReadReg_7(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1_6 = 0;
SCK_6= 0;
//从max6675循环获取16位数据
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK_6= 1;
dat = dat<<1;
if( SO_6==1 )
dat = dat|0x01;
SCK_6 = 0;
}
CS1_6 = 1;
//返回获取的值
return dat;
//第7个max6675获取温度函数
unsigned int MAX6675_ReadReg(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1_7 = 0;
SCK_7 = 0;
//从max6675循环获取16位数据
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK_7 = 1;
dat = dat<<1;
if( SO_7==1 )
dat = dat|0x01;
SCK_7 = 0;
}
CS1_7 = 1;
//返回获取的值
return dat;
//第8个max6675获取温度函数
unsigned int MAX6675_ReadReg_8(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1_8 = 0;
SCK_8 = 0;
//从max6675循环获取16位数据
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK_8 = 1;
dat = dat<<1;
if( SO_8==1 )
dat = dat|0x01;
SCK_7 = 0;
}
CS1_8 = 1;
//返回获取的值
return dat;
}
//--------------------------------------
//串口初始化
void init()
{
SCON = 0x50;
PCON &= 0x7F;
TMOD = 0x20;
TH1 = 0xfd; //波特率 9600bps @ 11.0592MHz
TL1 = 0xfd;
TR1 = 1;
ES = 1;
EA = 1;
//串口输出测试字符串hello
//say_hello();
}
//--------------------------------------
//主函数
void main()
{
unsigned int t;
//串口初始化
init();
//开定时器
TI = 1;
a = '4';
//主循环
while(1)
{
//第1个max6675获取温度
t=MAX6675_ReadReg();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt1 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
// //串口输出
// say_yeah();
//第2个max6675获取温度
t=MAX6675_ReadReg_2();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt2 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
//串口输出
//say_yeah();
//第3个max6675获取温度
t=MAX6675_ReadReg_3();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt3 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
//串口输出
//say_yeah();
//延时
//第4个max6675获取温度
t=MAX6675_ReadReg_4();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt1 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
// //串口输出
// say_yeah();
//第5个max6675获取温度
t=MAX6675_ReadReg_5();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt1 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
// //串口输出
// say_yeah();
//第6个max6675获取温度
t=MAX6675_ReadReg_6();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt1 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
// //串口输出
// say_yeah();
//第7个max6675获取温度
t=MAX6675_ReadReg_7();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt1 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
// //串口输出
// say_yeah();
//第8个max6675获取温度
t=MAX6675_ReadReg_8();
Flag_connect=t&0x04;
Flag_connect=Flag_connect>>2;
t = t<<1;
t = t>>4;
tt1 = t/4;
//获取温度的个十百位用于输出
// bai = ((MAX6675_Temp%1000)/100)+'0';
// shi = ((MAX6675_Temp%100)/10)+'0';
// ge = ((MAX6675_Temp%10))+'0';
// //串口输出
// say_yeah();
MAX6675_Temp = (tt1+tt2+tt3+tt4+tt5+tt6+tt7+tt8)/8; //求温度均值
//获取温度的个十百位用于输出
bai = ((MAX6675_Temp%1000)/100)+'0';
shi = ((MAX6675_Temp%100)/10)+'0';
ge = ((MAX6675_Temp%10))+'0';
//串口输出
say_yeah();
delay(1000);
delay(1000);
}
}
//延时函数
void delay(unsigned int z)
{
unsigned int x , y;
for(x = z; z > 0; z--)
{
for(y = 120; y > 0; y--);
}
}
//--------------------------------------
//串口接收中断函数
void ser() interrupt 4
{
if (TI == 1)
TI = 0;
if (RI == 1)
{
RI = 0;
} }
//======================================
|