STM32驱动MAX6675读取温度
VCC-GND接3~5.5V电压;
T+,T-分别接K型热电偶正负极;
CS为片选,低电平有效;
SCK为串行时钟,需要由STM32提供;
SO为数据串行输出;
接线方式:

MAX6675的输出方式是单片机输入时钟脉冲,MAX6675在时钟的下跳沿在SO管脚上输出数据。在数据手册第5页有时序说明,在6页有时序图,时序说明和时序图有差别。本人在读取数据过程中,发现按照时需说明操作,是正确的;而按时序图操作读取的数据有错误。MAX6675每次输出一共是16位数据,第一位也就是D15,是虚拟位;D14-D3,是12位的温度MSB-LSB,也就是高位在前地位在后;D2是一个标志,正常为0,一旦热电偶开路,则为1;D1是ID,通常为0,不懂啥意思,反正我不管怎样读都为0;D0是三态输出。
Force CS low to output the first bit on the SO pin. Acomplete serial interface read requires 16 clock cycles.Read the 16 output bits on the falling edge of the clock.The first bit, D15, is a dummy sign bit and is alwayszero. Bits D14–D3 contain the converted temperature inthe order of MSB to LSB. Bit D2 is normally low andgoes high when the thermocouple input is open. D1 islow to provide a device ID for the MAX6675 and bit D0 is three-state.
以上是时序说明,说的是在CS=0时,第一位就输出了,可以直接读取,不需要时钟,也就是读取16位数据只需要15个时钟;

#define Cs_HGPIOA->BSRR=GPIO_Pin_5
#define Clk_LGPIOA->BRR=GPIO_Pin_6
#define Clk_HGPIOA->BSRR=GPIO_Pin_6
#define So_HGPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)
/**************定义变量****************/
u16 Dat_Out=0;
u8 Cyc=0;
/****************程序******************/
Cs_L;
for(Cyc=0;Cyc<16;Cyc++)
{
/*第1位在CS被拉低之后产生,不需要时钟,
故在第1位将时钟屏蔽
*/
if(Cyc!=0)
{
Clk_H;
Clk_L;
}
{
Dat_Out++;
}
/*第15个时钟之后不再移位*/
if(Cyc!=15)
{
Dat_Out<<=1;
}
}
Cs_H;
return Dat_Out;
}
读取的数据处理:
u16 Tem_Handle(u16 TC_Num)
{
u16 Temp;
if(TC_Num&4)
{
LcdString(3,3,"TC Open");//液晶显示错误
while(Read_TC()&4)
{
Delay(1000);
}
LcdString(3,3," ");//如果热电偶恢复闭合,程序继续运行
}
else
{
Temp=((TC_Num&0x7fff)>>3)*25;//提取D14-D3,12位数据
}
return Temp;
}
评论