共2条
1/1 1 跳转至页
AVR,DS18B20 哪位大侠有用AVR操纵DS18B20的C源程序啊?

问
我正在用mega16操纵DS18B20,但是现在用ICCAVR怎么也写不出可以正常工作的程序。不知哪位大侠有这方面的经验啊?
答 1:
这是某位网友的作品(可能就是在21ic下载的,我借助它编写了PPC8260上面读DS2520的程序,不敢掠美,特希望作者现一下身)
#include "io2313.h"
#include "macros.h"
#define BOOL unsigned char
#define TRUE 1
#define FALSE 0
#define SET_BIT(x, y) (x|=(1<<y))
#define CLR_BIT(x, y) (x&=~(1<<y))
#define GET_BIT(x, y) ((x&(1<<y))>>y)
#define PULLING_LOW() SET_BIT(DDRD, 0)
#define PULLING_UP() CLR_BIT(DDRD, 0)
#define READ_DQ() GET_BIT(PIND, 0)
#define SKIP_ROM 0xCC
#define READ_RAM 0xBE
#define CONVERT_T 0x44
void ShortDelay(unsigned char tt)
{
asm("_L2: subi R16,1"); // 1t
asm(" nop") // 1t
asm(" brne _L2"); // 2t/1t
asm(" nop"); // 1t
asm(" ret"); // 4t
}
unsigned char W1_Reset()
{
unsigned char c;
PULLING_LOW();
ShortDelay(198); // 500us
ShortDelay(198);
ShortDelay(198);
ShortDelay(198);
ShortDelay(198);
PULLING_UP();
ShortDelay(148); // 75us
c = READ_DQ();
if (c == 0x01)
{
return FALSE;
}
ShortDelay(198); // 300us
ShortDelay(198);
ShortDelay(198);
c = READ_DQ();
if (c == 0x00)
{
return FALSE;
}
return TRUE;
}
unsigned char W1_BitIO(unsigned char b)
{
if (b)
{
PULLING_LOW();
ShortDelay(2); //2us
PULLING_UP();
ShortDelay(18); //10us
b = READ_DQ();
ShortDelay(118); //60us
}
else
{
PULLING_LOW();
ShortDelay(2); //2us
PULLING_LOW();
ShortDelay(18); //10us
b = READ_DQ();
ShortDelay(118); //60us
PULLING_UP();
}
return b;
}
unsigned char W1_SendByte(unsigned char c)
{
unsigned char i = 8;
do
{
c = c>>1|(W1_BitIO(c&1)?0x80:0);
} while(--i);
return c;
}
unsigned char W1_ReadByte()
{
return W1_SendByte(0xFF);
}
unsigned char W1_Command(unsigned c)
{
if (W1_Reset())
{
W1_SendByte(SKIP_ROM);
W1_SendByte(c);
return 1;
}
else
{
return 0;
}
}
int ReadTemperature()
{
union
{
unsigned char c[2];
int i;
} temp;
if (W1_Command(READ_RAM))
{
temp.c[0] = W1_ReadByte();
temp.c[1] = W1_ReadByte();
if (W1_Command(CONVERT_T))
{
;
}
else
{
return -1000;
}
return (int)(temp.i*5/4+1)/2;
}
else
{
return -1000;
}
}
答 2: 谢谢hudaidai了,也感谢原作者。谢谢两位了,我来用用看吧! 答 3: thank very much 答 4: 有本介绍icc的书上有现成的原程序 答 5: 我的网上有
#include "io2313.h"
#include "macros.h"
#define BOOL unsigned char
#define TRUE 1
#define FALSE 0
#define SET_BIT(x, y) (x|=(1<<y))
#define CLR_BIT(x, y) (x&=~(1<<y))
#define GET_BIT(x, y) ((x&(1<<y))>>y)
#define PULLING_LOW() SET_BIT(DDRD, 0)
#define PULLING_UP() CLR_BIT(DDRD, 0)
#define READ_DQ() GET_BIT(PIND, 0)
#define SKIP_ROM 0xCC
#define READ_RAM 0xBE
#define CONVERT_T 0x44
void ShortDelay(unsigned char tt)
{
asm("_L2: subi R16,1"); // 1t
asm(" nop") // 1t
asm(" brne _L2"); // 2t/1t
asm(" nop"); // 1t
asm(" ret"); // 4t
}
unsigned char W1_Reset()
{
unsigned char c;
PULLING_LOW();
ShortDelay(198); // 500us
ShortDelay(198);
ShortDelay(198);
ShortDelay(198);
ShortDelay(198);
PULLING_UP();
ShortDelay(148); // 75us
c = READ_DQ();
if (c == 0x01)
{
return FALSE;
}
ShortDelay(198); // 300us
ShortDelay(198);
ShortDelay(198);
c = READ_DQ();
if (c == 0x00)
{
return FALSE;
}
return TRUE;
}
unsigned char W1_BitIO(unsigned char b)
{
if (b)
{
PULLING_LOW();
ShortDelay(2); //2us
PULLING_UP();
ShortDelay(18); //10us
b = READ_DQ();
ShortDelay(118); //60us
}
else
{
PULLING_LOW();
ShortDelay(2); //2us
PULLING_LOW();
ShortDelay(18); //10us
b = READ_DQ();
ShortDelay(118); //60us
PULLING_UP();
}
return b;
}
unsigned char W1_SendByte(unsigned char c)
{
unsigned char i = 8;
do
{
c = c>>1|(W1_BitIO(c&1)?0x80:0);
} while(--i);
return c;
}
unsigned char W1_ReadByte()
{
return W1_SendByte(0xFF);
}
unsigned char W1_Command(unsigned c)
{
if (W1_Reset())
{
W1_SendByte(SKIP_ROM);
W1_SendByte(c);
return 1;
}
else
{
return 0;
}
}
int ReadTemperature()
{
union
{
unsigned char c[2];
int i;
} temp;
if (W1_Command(READ_RAM))
{
temp.c[0] = W1_ReadByte();
temp.c[1] = W1_ReadByte();
if (W1_Command(CONVERT_T))
{
;
}
else
{
return -1000;
}
return (int)(temp.i*5/4+1)/2;
}
else
{
return -1000;
}
}
答 2: 谢谢hudaidai了,也感谢原作者。谢谢两位了,我来用用看吧! 答 3: thank very much 答 4: 有本介绍icc的书上有现成的原程序 答 5: 我的网上有
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
分享开发笔记,赚取电动螺丝刀(便携轻巧与无刷电机两款可选) | |
看《关键跃升》,聊聊工作心法、动力、能力、沟通与协作,从五个方面深入交流你的工作心得 | |
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
我要给自己挣一部逻辑分析仪 | |
用知识换取送给亲朋的礼物,做个浪漫的开发者 | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 |
打赏帖 | |
---|---|
Buck电路工作在CCM模式下电感电流的计算公式是什么?被打赏5分 | |
buck电路工作原理被打赏5分 | |
基于MSPM0L1306的MODBUS-RTU协议通讯实验被打赏100分 | |
我想要一部加热台+多合一调试工具被打赏18分 | |
每周了解几个硬件知识+485硬件知识分享被打赏10分 | |
【换取手持数字示波器】树莓派PICO调试器官方固件本地化部署实践被打赏24分 | |
【换取手持数字示波器】分享一个KEIL无法识别CMSIS-DAP调试器的解决办法被打赏20分 | |
【换取手持数字示波器】分享一个自制的ArduinoNano扩展板底板被打赏23分 | |
【换取手持示波器】树莓派PICOW网页烟花被打赏18分 | |
【换取手持示波器】树莓派PICO逻辑分析仪被打赏16分 |