PIC16F877A-SPI-TC77(温度传感器)
TC77:分辨率0.0625℃
#include
#define CS RC2
void initPORT();
void initMSSP();
void delay(unsigned char time);
void display(float data);
float ReadTC77();
const unsigned char disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void main()
{
float temp;
initPORT();
initMSSP();
while(1)
{
temp=ReadTC77();
display(temp);
}
}
/*初始化PIC单片机端口*/
void initPORT()
{
TRISB=0x00; //控制段选
PORTB=0x00;
TRISC=0xf0; //与温度传感器连接
PORTC=0x00;
TRISA=0x00; //控制位选
PORTA=0xff;
}
/*初始化MSSP模块的SPI模式*/
void initMSSP()
{
SSPSTAT=0x00;
SSPCON=0x30;
SSPIF=0;
}
/*读取温度传感器的13位数值并且转换成浮点类型*/
float ReadTC77()
{
float result,PowerTwo=1;
unsigned char count,HighByte,LowByte;
CS=0;
SSPBUF=0;
while(!SSPIF){}
HighByte=SSPBUF; //获取温度寄存器的高字节
SSPIF=0;
SSPBUF=0;
while(!SSPIF){}
LowByte=SSPBUF; //获取温度寄存器的低字节
SSPIF=0;
CS=1;
for(count=0;count<8;count++) //转换整数部分
{
PowerTwo*=2;
if(HighByte&(0x01 } PowerTwo=1; for(count=0;count<5;count++) //转换个位及小数部分 { if(LowByte&(0x80>>count)) result+=PowerTwo; PowerTwo=PowerTwo/2.; //注意,不能换成.2,否则会出错 } return result; //返回转换浮点结果 } /*液晶显示*/ void display(float data) { unsigned int conver,First,Second,Third,Fourth; conver=data*100; First=conver/1000; Second=conver/100%10; Third=conver/10%10; Fourth=conver%10; PORTA=0xfe; //显示十位 PORTB=disp[First]; delay(2); PORTB=0x00; PORTA=0xfd; //显示个位 PORTB=0x80+disp[Second]; //加上0x80即可带点 delay(2); PORTB=0x00; PORTA=0xfb; //显示十分位 PORTB=disp[Third]; delay(2); PORTB=0x00; PORTA=0xf7; //显示百分位 PORTB=disp[Fourth]; delay(2); PORTB=0x00; } void delay(unsigned char time) { unsigned char i,j; for(i=time;i>0;i--) for(j=255;j>0;j--); }
PIC16F877ASPITC77温度传感 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)