这里附上代码,供大家参考;
max6675 驱动:
#include "max6675.h"
// CH1
sbit SCK4 = P1^7;
sbit CS4 = P1^6;
sbit SO4 = P1^5;
// CH2
sbit SCK3 = P3^4;
sbit CS3 = P3^3;
sbit SO3 = P3^2;
// CH3
sbit SCK2 = P3^7;
sbit CS2 = P3^6;
sbit SO2 = P3^5;
// CH4
sbit SCK1 = P2^0;
sbit CS1 = P2^1;
sbit SO1 = P2^2;
// 读取通道1 数据
unsigned int MAX6675_ReadReg1(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS1 = 0;
SCK1 = 0;
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK1 = 1;
dat = dat<<1;
if( SO1==1 )
dat = dat|0x01;
SCK1 = 0;
}
CS1 = 1;
return dat;
}
// 读取通道2 数据
unsigned int MAX6675_ReadReg2(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS2 = 0;
SCK2 = 0;
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK2 = 1;
dat = dat<<1;
if( SO2==1 )
dat = dat|0x01;
SCK2 = 0;
}
CS2 = 1;
return dat;
}
// 读取通道3 数据
unsigned int MAX6675_ReadReg3(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS3 = 0;
SCK3 = 0;
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK3 = 1;
dat = dat<<1;
if( SO3==1 )
dat = dat|0x01;
SCK3 = 0;
}
CS3 = 1;
return dat;
}
// 读取通道4 数据
unsigned int MAX6675_ReadReg4(void)
{
unsigned char i;
unsigned int dat;
i = 0;
dat = 0;
CS4 = 0;
SCK4 = 0;
for(i=0; i<16; i++) //get D15-D0 from 6675
{
SCK4 = 1;
dat = dat<<1;
if( SO4==1 )
dat = dat|0x01;
SCK4 = 0;
}
CS4 = 1;
return dat;
}
2. lcd1602驱动
#include "lcd.h"
/*******************************************************************************
* 函 数 名 : Lcd1602_Delay1ms
* 函数功能 : 延时函数,延时1ms
* 输 入 : c
* 输 出 : 无
* 说 名 : 该函数是在12MHZ晶振下,12分频单片机的延时。
*******************************************************************************/
void Lcd1602_Delay1ms(unsigned int c) //误差 0us
{
unsigned char a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
/*******************************************************************************
* 函 数 名 : LcdWriteCom
* 函数功能 : 向LCD写入一个字节的命令
* 输 入 : com
* 输 出 : 无
*******************************************************************************/
#ifndef LCD1602_4PINS //当没有定义这个LCD1602_4PINS时
void LcdWriteCom(unsigned char com) //写入命令
{
LCD1602_E = 0; //使能
LCD1602_RS = 0; //选择发送命令
LCD1602_RW = 0; //选择写入
LCD1602_DATAPINS = com; //放入命令
Lcd1602_Delay1ms(1); //等待数据稳定
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5); //保持时间
LCD1602_E = 0;
}
#else
void LcdWriteCom(unsigned char com) //写入命令
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 0; //选择写入命令
LCD1602_RW = 0; //选择写入
LCD1602_DATAPINS = com; //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
LCD1602_DATAPINS = com << 4; //发送低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 数 名 : LcdWriteData
* 函数功能 : 向LCD写入一个字节的数据
* 输 入 : dat
* 输 出 : 无
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdWriteData(unsigned char dat) //写入数据
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //选择输入数据
LCD1602_RW = 0; //选择写入
LCD1602_DATAPINS = dat; //写入数据
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5); //保持时间
LCD1602_E = 0;
}
#else
void LcdWriteData(unsigned char dat) //写入数据
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //选择写入数据
LCD1602_RW = 0; //选择写入
LCD1602_DATAPINS = dat; //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
LCD1602_DATAPINS = dat << 4; //写入低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 数 名 : LcdInit()
* 函数功能 : 初始化LCD屏
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdInit() //LCD初始化子程序
{
LcdWriteCom(0x38); //开显示
LcdWriteCom(0x0c); //开显示不显示光标
LcdWriteCom(0x06); //写一个指针加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //设置数据指针起点
}
#else
void LcdInit() //LCD初始化子程序
{
LcdWriteCom(0x32); //将8位总线转为4位总线
LcdWriteCom(0x28); //在四位线下的初始化
LcdWriteCom(0x0c); //开显示不显示光标
LcdWriteCom(0x06); //写一个指针加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //设置数据指针起点
}
#endif
3. 主函数
#include "reg52.h"
#include "lcd.h"
#include "max6675.h"
// 存储温度变量
unsigned int temp1,temp2,temp3,temp4;
// 存储发送温度数组
unsigned char temper1[a7];
unsigned char temper2[7];
unsigned char temper3[7];
unsigned char temper4[7];
// LED 指示灯
sbit led1=P1^3;
sbit led2=P1^2;
sbit led3=P1^1;
sbit led4=P1^0;
// 函数声明
void lcd_display_init(void);
void delay_ms(unsigned int z);
void timer_init(void);
void lcd_display(void);
void UartInit(void);
void PutString(unsigned char *TXStr);
// 延时函数 毫秒级
void delay_ms(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
// 定时器初始化
void timer_init()
{
TMOD |=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EA=1;
}
// 串口初始化
// 波特率:9600bps
// 晶振:11.0592MHz
void UartInit()
{
SCON=0x50;
TMOD|=0x20;
TMOD&=~0x10;
TH1=0xfa;
TL1=0xfa;
PCON|=0x80;
TR1=1;
TI=0;
RI=0;
ES=1;
}
// 串口发送数据
void PutString(unsigned char *TXStr)
{
ES=0;
while(*TXStr!='\0')
{
SBUF=*TXStr;
while(TI==0);
TI=0;
TXStr++;
}
ES=1;
}
// 主函数
void main()
{
/*********硬件初始化*********/
lcd_display_init();
timer_init();
UartInit(); // 串口初始化
PutString("GIMM实验室热电偶温度初始化中..........\r\n");
led1=led2=led3=led4=1;
while(1)
{
}
}
// 定时器0 中断函数
void timer0() interrupt 1
{
//unsigned char count;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
// count++;
//if(count==2)
//{
// count=0;
temp1=MAX6675_ReadReg1();
temp2=MAX6675_ReadReg2();
temp3=MAX6675_ReadReg3();
temp4=MAX6675_ReadReg4();
temp1= temp1<<1;
temp1= temp1>>4;
temp1= temp1/4;
if(temp1==1023)
{
temp1=0;led1=1;
}
else led1=~led1;
temp2= temp2<<1;
temp2= temp2>>4;
temp2= temp2/4;
if(temp2==1023)
{
temp2=0; led2=1;
}
else led2=~led2;
temp3= temp3<<1;
temp3= temp3>>4;
temp3= temp3/4;
if(temp3==1023)
{
temp3=0;led3=1;
}
else led3=~led3;
temp4= temp4<<1;
temp4= temp4>>4;
temp4= temp4/4;
if(temp4==1023)
{
temp4=0; led4=1;
}
else led4=~led4;
// temper1[0]='C';
// temper1[1]='H';
// temper1[2]='1';
// temper1[3]=':';
temper1[0]=temp1%1000/100+0x30;
temper1[1]=temp1%100/10+0x30;
temper1[2]=temp1%10+0x30;
temper1[3]=' ';
temper1[4]=' ';
// temper1[7]='\r';
// temper1[8]='\n';
PutString(temper1);
// temper2[0]='C';
// temper2[1]='H';
// temper2[2]='2';
// temper2[3]=':';
temper2[0]=temp2%1000/100+0x30;
temper2[1]=temp2%100/10+0x30;
temper2[2]=temp2%10+0x30;
temper2[3]=' ';
temper2[4]=' ';
// temper2[7]='\r';
// temper2[8]='\n';
PutString(temper2);
// temper3[0]='C';
// temper3[1]='H';
// temper3[2]='3';
// temper3[3]=':';
temper3[0]=temp3%1000/100+0x30;
temper3[1]=temp3%100/10+0x30;
temper3[2]=temp3%10+0x30;
temper3[3]=' ';
temper3[4]=' ';
// temper3[7]='\r';
// temper3[8]='\n';
PutString(temper3);
// temper4[0]='C';
// temper4[1]='H';
// temper4[2]='4';
// temper4[3]=':';
temper4[0]=temp4%1000/100+0x30;
temper4[1]=temp4%100/10+0x30;
temper4[2]=temp4%10+0x30;
temper4[3]=' ';
temper4[4]=' ';
// temper4[7]='\r';
// temper4[8]='\n';
PutString(temper4);
PutString("\r\n");
lcd_display();
//}
}
void lcd_display_init()
{
LcdInit();
// 初始化界面
LcdWriteCom(0x80);
LcdWriteData('G');
LcdWriteCom(0x81);
LcdWriteData('I');
LcdWriteCom(0x82);
LcdWriteData('M');
LcdWriteCom(0x83);
LcdWriteData('M');
LcdWriteCom(0x85);
LcdWriteData('i');
LcdWriteCom(0x86);
LcdWriteData('n');
LcdWriteCom(0x87);
LcdWriteData('i');
LcdWriteCom(0x88);
LcdWriteData('t');
delay_ms(500);
LcdWriteCom(0x80+0x40+9);
LcdWriteData('.');
delay_ms(500);
LcdWriteCom(0x80+0x40+10);
LcdWriteData('.');
delay_ms(500);
LcdWriteCom(0x80+0x40+11);
LcdWriteData('.');
delay_ms(500);
LcdWriteCom(0x80+0x40+12);
LcdWriteData('.');
delay_ms(500);
LcdWriteCom(0x80+0x40+13);
LcdWriteData('.');
delay_ms(500);
LcdWriteCom(0x80+0x40+14);
LcdWriteData('.');
delay_ms(500);
LcdWriteCom(0x80+0x40+15);
LcdWriteData('.');
delay_ms(1000);
// 显示界面
LcdWriteCom(0x80);
LcdWriteData('C');
LcdWriteCom(0x81);
LcdWriteData('H');
LcdWriteCom(0x82);
LcdWriteData('1');
LcdWriteCom(0x83);
LcdWriteData(':');
LcdWriteCom(0x84);
LcdWriteData(' ');
LcdWriteCom(0x85);
LcdWriteData(' ');
LcdWriteCom(0x86);
LcdWriteData(' ');
LcdWriteCom(0x87);
LcdWriteData(' ');
LcdWriteCom(0x88);
LcdWriteData('C');
LcdWriteCom(0x89);
LcdWriteData('H');
LcdWriteCom(0x80+10);
LcdWriteData('2');
LcdWriteCom(0x80+11);
LcdWriteData(':');
LcdWriteCom(0x80+12);
LcdWriteData(' ');
LcdWriteCom(0x80+13);
LcdWriteData(' ');
LcdWriteCom(0x80+14);
LcdWriteData(' ');
LcdWriteCom(0x80+15);
LcdWriteData(' ');
LcdWriteCom(0x80+0x40);
LcdWriteData('C');
LcdWriteCom(0x81+0x40);
LcdWriteData('H');
LcdWriteCom(0x82+0x40);
LcdWriteData('3');
LcdWriteCom(0x83+0x40);
LcdWriteData(':');
LcdWriteCom(0x84+0x40);
LcdWriteData(' ');
LcdWriteCom(0x85+0x40);
LcdWriteData(' ');
LcdWriteCom(0x86+0x40);
LcdWriteData(' ');
LcdWriteCom(0x87+0x40);
LcdWriteData(' ');
LcdWriteCom(0x88+0x40);
LcdWriteData('C');
LcdWriteCom(0x89+0x40);
LcdWriteData('H');
LcdWriteCom(0x80+10+0x40);
LcdWriteData('4');
LcdWriteCom(0x80+11+0x40);
LcdWriteData(':');
LcdWriteCom(0x80+12+0x40);
LcdWriteData(' ');
LcdWriteCom(0x80+13+0x40);
LcdWriteData(' ');
LcdWriteCom(0x80+14+0x40);
LcdWriteData(' ');
LcdWriteCom(0x80+15+0x40);
LcdWriteData(' ');
}
void lcd_display()
{
// T1:
LcdWriteCom(0x84);
LcdWriteData(temp1%1000/100+0x30);
LcdWriteCom(0x85);
LcdWriteData(temp1%100/10+0x30);
LcdWriteCom(0x86);
LcdWriteData(temp1%10+0x30);
// T2:
LcdWriteCom(0x80+13);
LcdWriteData(temp2%1000/100+0x30);
LcdWriteCom(0x80+14);
LcdWriteData(temp2%100/10+0x30);
LcdWriteCom(0x80+15);
LcdWriteData(temp2%10+0x30);
// T3:
LcdWriteCom(0x84+0x40);
LcdWriteData(temp3%1000/100+0x30);
LcdWriteCom(0x85+0x40);
LcdWriteData(temp3%100/10+0x30);
LcdWriteCom(0x86+0x40);
LcdWriteData(temp3%10+0x30);
// T4:
LcdWriteCom(0x80+13+0x40);
LcdWriteData(temp4%1000/100+0x30);
LcdWriteCom(0x80+14+0x40);
LcdWriteData(temp4%100/10+0x30);
LcdWriteCom(0x80+15+0x40);
LcdWriteData(temp4%10+0x30);
}
|
求大神分享细节