|
以下是DS18b20的程序,我用的芯片是at91sam7s64,发现有些IO可以用,有些IO不行,比如我用PA0是可以正确读数的,但是用PA15读数一直是0,请问是怎么回事呢?
AT91S_PIO * pPIO = AT91C_BASE_PIOA;
void DS18b20_Init(void)
{
pPIO->PIO_PER = DS18B20;
pPIO->PIO_PPUDR = DS18B20;
}
//*-------------------------------------------------
//* Function Name: Reset_Ds18b20
//* Object: Reset Ds18b20
//* Input Parameters:none
//* Output Parameters:none
//*-------------------------------------------------
void Reset_Ds18b20(void)
{
SET_DIR_1WIRE;
SET_OP_1WIRE;
_delay_us(1);
CLR_OP_1WIRE;
_delay_us(600); //480usÒÔÉÏ
SET_OP_1WIRE;
CLR_DIR_1WIRE;
_delay_us(20); //15~60us
while(CHECK_IP_1WIRE); //* Ϊ±ÜÃâËÀÑ­Ðò£¬´ý´¦Àí
SET_DIR_1WIRE;
SET_OP_1WIRE;
_delay_us(140); //60~240us
}
//*-------------------------------------------------
//* Function Name: Write_Ds18b20
//* Object: Write a byte
//* Input Parameters:none
//* Output Parameters:none
//*-------------------------------------------------
void Write_Ds18b20(unsigned char x)
{
unsigned char m;
for(m=0; m<8; m++)
{
CLR_OP_1WIRE;
if(x&(1<<m))
SET_OP_1WIRE;
else
CLR_OP_1WIRE;
_delay_us(50); //15~60us
SET_OP_1WIRE;
_delay_us(1);
}
SET_OP_1WIRE;
}
//*-------------------------------------------------
//* Function Name: Read_Ds18b20
//* Object: Read a byte
//* Input Parameters:none
//* Output Parameters:none
//*-------------------------------------------------
unsigned char Read_Ds18b20(void)
{
unsigned char temp,k,n;
temp=0;
for(n=0; n<8; n++)
{
CLR_OP_1WIRE;
SET_OP_1WIRE;
CLR_DIR_1WIRE;
_delay_us(2);
k =(CHECK_IP_1WIRE);
if(k)
temp|=(1<<n);
else
temp&=~(1<<n);
_delay_us(70); //60~120us
//
SET_DIR_1WIRE;
_delay_us(1);
}
return (temp);
}
//*-------------------------------------------------
//* Function Name: Get_Temp
//* Object: Read tempture from ds18b20
//* Input Parameters:none
//* Output Parameters:none
//*-------------------------------------------------
float Get_Temp(void)
{
unsigned char temh,teml;
float temp;
unsigned int tmp;
Reset_Ds18b20();
Write_Ds18b20(skiprom);
Write_Ds18b20(convert);
_delay_ms(100);
_delay_ms(100);
Reset_Ds18b20();
Write_Ds18b20(skiprom);
Write_Ds18b20(readscrat);
teml = Read_Ds18b20();
temh = Read_Ds18b20();
tmp = temh;
tmp <<= 8;
tmp |= teml;
temp = tmp*0.0625+0.005;
return temp;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|