该段代码实际上模仿了spi时序(具体搞法,请看max6675的pdf)。也可直接用单片机的spi功能从6675获得数据(对于arduino用户来说,这意味着需要引用一些库)。
//max6675.c
#include <stmduino.h>
void read6675()
{
uint result=0;
pinlow(A1);//ask for converted data
pinlow(A2);
for(uchar i=0;i<16;i++)
{
pinhigh(A2);
result <<= 1; //right shift all bits
if(pinread(A3))//if this bit is 1
{
result |= 0x0001;//set LSB of result to 1
}
else
{
//do nothing
}
pinlow(A2);
}
pinhigh(A1);//end
result>>=3;//skip the useless bits
result*=25;//output unit: 1/100 degree
printf("%u",result);
endl();
}
uint cnt=0;
void t4()
{
cnt++;
if(cnt==200) //every 200ms
{
cnt=0;
read6675();
}
}
void main()
{
clock(4M);
pinmode(A1,OUTPUT); //CS
pinhigh(A1);
pinmode(A2,OUTPUT); //sck
pinlow(A2);
pinmode(A3,INPUTFL); //SO
attach(t4,timer4);
timer4clock(16,249);//1ms interrupt
serial(115200);
interrupts();
while(1)
{
sleep();
}
}