- UID
- 813321
- 性别
- 男
|
程序一直调不出来,调的过程中结果不是全是F,就是全是0,哪位好心帮我看看吧~先谢过了!
#include <hidef.h> /* common defines and macros */
#include <mc9s12xf512.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xf512"
#include "Fr_UNIFIED_types.h"
uint16 TempeValue,TempeValdone;
boolean TempeReady = 0;
uint32 timech;
#define DQ 1
#define DQ_H PTP_PTP0 = 0 //设置P0.0口为输出
#define DQ_L PTP_PTP0 = 1 //设置P0.0口为输入
uint8 flag=0;
uint8 Temp=0;
uint8 tempH;
uint8 tempL;
uint8 f;
uint8 tmp;
uint8 tmp_t,COUNTER = 0;
void Delay(uint32 time) //3.75us
{
for(;time>0;time--);
}
//初始化作为输入口
void GPIO_Iint()
{
DDRP_DDRP0 = 0;
}
//初始化作为输出口
void GPIO_Oint()
{
DDRP_DDRP0 = 1;
}
// 函数功能:一线总线复位及从设备应答控制程序
void TxReset(void)
{
do{
int err = 5;
flag = 0;
GPIO_Oint();
DQ_H;
Delay(2); //延时5us
DQ_L; //置低,初始化开始
Delay(480); //延时722us
GPIO_Iint();
Delay(45); //延时60us以上
/*等待18B20发出存在脉冲--低电平*/
while((PTP_PTP0 == 1)||(err == 0)) {
err--;
Delay(12);
}
if(err == 0) flag = 0;
else flag = 1;
Delay(39); //60us
GPIO_Oint();
DQ_H;
Delay(79); //120us,总480us
Delay(79);
Delay(79);
Delay(79);
} while(flag == 0) ;
}
void WrByte(uint8 b)
{
uint8 i;
for(i=0;i<8;i++)
{
if(b&0x01)
{
GPIO_Oint();
/*先置高再置低,告诉18B20 写开始*/
DQ_H; //置高
Delay(2); //5us
DQ_L; //置低
Delay(5); //延时20us
DQ_H; //P0.0置高,写 1 开始
Delay(39); //延时10us
GPIO_Iint();
Delay(5); //保证写2位数据的间隙大于 1 us
}
else
{
GPIO_Oint();
DQ_H; //置高
Delay(2); //5us
DQ_L; //置低
Delay(45); //延时70us
DQ_H;
GPIO_Iint();
Delay(5); //保证写2位数据的间隙大于 1 us
}
b=b>>1;
}
}
//读数据的一位,满足读时隙要求读取
uint8 RdBit(void)
{
uint8 read_bit;
GPIO_Oint(); /*先置高再置低,告诉18B20 读开始*/
DQ_L;
Delay(2); //延时5us
GPIO_Iint();
Delay(5); //10us
if(PTP_PTP0)
read_bit=0x80;
else
read_bit=0;
GPIO_Oint(); //输出
DQ_H; //置高,释放总线
Delay(40);
return read_bit;
}
//读取数据的一个字节
uint8 RdByte(void)
{
uint8 i,Data=0;
for(i=0;i<8;i++)
{
Data=Data>>1;
Data=Data|RdBit();
}
return Data;
}
//启动一次DS18B20测温,并读取测温结果//得到完整的温度值(整数和小数)
uint8 get_temp_t(void)
{
uint8 i,j;
TxReset();
COUNTER++;
if(flag) //如果一线总线复位及从设备应答控制 成功 则向下执行
{
WrByte(0xCC); //跳过ROM
COUNTER++;
WrByte(0x44); //开始一次温度变换
COUNTER++;
Delay(480); //700us
COUNTER++;
TxReset(); //一线总线复位及从设备应答控制
COUNTER++;
WrByte(0xCC); //跳过ROM
COUNTER++;
WrByte(0xBE); //读暂存存储器
COUNTER++;
tempL=RdByte();
COUNTER++;
tempH=RdByte();
COUNTER++;
}
COUNTER = 0;
return(f);
}
void temperaturehandler(void) {
TempeReady = 1;
get_temp_t();
TempeValue = (uint16)tempH;
TempeValue = TempeValue << 8;
TempeValue = TempeValue + (uint16)tempL;
TempeValdone = TempeValue;
TempeReady = 0;
}
void main(void) {
/* put your own code here */
//EnableInterrupts;
PLLCTL_PLLON = 0; /* Disable the PLL */
SYNR_SYNDIV = 0x04; /* Set PLL synthesizer register to 4 */
REFDV_REFDIV = 0x00; /* Set PLL divider register to 0 */
/* Fvco = 2*OSCCLK*(SYNR+1)/(REFDV+1) = 8MHz* 5/1 = 40MHz */
/* -> VCOFRQ = 00b */
SYNR_VCOFRQ1 = 0;
SYNR_VCOFRQ0 = 0;
/* Fref = OSCCLK/(REFDV+1) = 4MHz/1 = 4MHz -> REFRQ= 01b */
REFDV_REFFRQ1 = 0;
REFDV_REFFRQ0 = 1;
PLLCTL_PLLON = 1; /* PLL enabled */
while(!CRGFLG_LOCK)
; /* Wait until the PLL is within the desired frequency */
CLKSEL_PLLSEL = 1; /* Select clock source from PLL */
GPIO_Oint();
for(;;) {
temperaturehandler();
} /* wait forever */
/* please make sure that you never leave this function */
} |
|