高级会员
- 积分
- 992
- 威望
- 538
- 贡献
- 230
- 兑换币
- 4
- 注册时间
- 2011-7-13
- 在线时间
- 112 小时
|
初学飞思卡尔,用飞思卡尔和DS18B20实现测温,写的程序实现不了,求高手帮助 ,谢谢啊
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define DS PORTA_PA7
uchar buffer[4]={0x00,0x01,0x02,0x03};
uchar codetable1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar codetable2[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uchar codetable3[]={0xf7,0xfb,0xfd,0xfe};
uchar data=0;
void Delay_DS18B20(int num)
{
while(num--) ;
}
void delayms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<920;jj++); //busclk:8MHz--1ms
}
void DS_Init(void)
{
unsigned char i;
DS=1;
Delay_DS18B20(2);
DS=0;
Delay_DS18B20(20);
DS=1;
Delay_DS18B20(10);
i=DS;
Delay_DS18B20(10);
}
uchar Readonedata(void) //读一个字节数据
{
uint i;
unsigned char data;
for(i=0;i<8;i++)
{
DS=0;
data>>=1;
Delay_DS18B20(2);
DS=1;
if(DS)
data|=0x80;
Delay_DS18B20(2);
}
return(data);
}
void Writeonedata(uchar data) //写一个字节数据
{
unsigned char i=0;
for (i=0; i<8; i++)
{
DS = 0;
DS = data&0x01;
Delay_DS18B20(10);
DS = 1;
data>>=1;
}
}
void datachange(void) // 温度转换
{
DS_Init();
delayms(3);
Writeonedata(0xcc);
Writeonedata(0x44);
Writeonedata(0xbe); //读取温度寄存器
}
uchar get_data()
{
uchar a,b;
uchar f_data;
datachange();
a=Readonedata();
b=Readonedata();
data=b;
data<<=8;
data=data|a;
f_data=data*0.0625;
data=f_data*100;
return(data);
}
/*void SCI_Init(void)
{
SCI0CR1=0x00;
SCI0CR2=0x0c;
SCI0BDH=0x00;
SCI0BDL=0x34;
}
void SciTx(uchar text)
{
unsigned char temp;
temp=SCI0SR1;
SCI0DRH=0;
SCI0DRL=text;
while(!(SCI0SR1&0x80));
}
char SciRx(void)
{
char result,temp;
temp=SCI0SR1;
while(!(SCI0SR1&0x20));
result=SCI0DRL;
return result;
} */
void scan(uchar data)
{
delayms(3);
PORTA=0xf7;
PORTB=codetable1[data/1000];
delayms(3);
PORTA=0xfb;
PORTB=codetable1[data%1000/100];
delayms(3);
PORTA=0xfd;
PORTB=codetable1[data%100/10];
delayms(3);
PORTA=0xfe;
PORTB=codetable1[data%10];
}
void main(void)
{
//SCI_Init();
DDRA=0xff;
DDRB=0xff;
PORTA=0xff;
PORTB=0xff;
DS_Init();
for(;;)
{
datachange();
scan(data);
delayms(20);
}
} |
|