[an error occurred while processing this directive]
Берешь даташит и вперед все тайминги там есть, все ок. (+)
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено eXeC001er 31 июля 2006 г. 23:11
В ответ на: Люди, а DS1820 вообще работают или это фантастика ? Покажите код на С плиз отправлено ($)($) 31 июля 2006 г. 22:51

OneWire.c
#include
#include
#include "OneWire.h"
#include "delay.h"

#define DevicePort (PIND & (1 << PD2))
#define DevicePort2High (PORTD |= 1<#define DevicePort2Low (PORTD &= ~(1<#define DevicePort2Output (DDRD |= 1<#define DevicePort2Input (DDRD &= ~(1<#define DeviceLineToLow DevicePort2Output; \
DevicePort2Low;
#define DeviceLineToHigh DevicePort2Output; \
DevicePort2High;
#define DeviceLineFree DevicePort2Input; \
DevicePort2High;


unsigned char OneWireReset (void)
{
unsigned char Ready = 1;

DeviceLineToLow;
delay_us(480);
DeviceLineFree;
delay_us(70);
if(DevicePort != 0)
{
Ready = 0;
}
delay_us(410);
return Ready;
}

unsigned char OneWireReadBit (void)
{
unsigned char Bit = 0;

DeviceLineToLow;
delay_us(15);
DeviceLineFree;
delay_us(12);
if (DevicePort != 0)
{
Bit = 1;
}
delay_us(150);

return Bit;
}

void OneWireWriteBit_1(void)
{
DeviceLineToLow;
delay_us(6);
DeviceLineToHigh;
delay_us(64);
}

void OneWireWriteBit_0(void)
{
DeviceLineToLow;
delay_us(90);
DeviceLineToHigh;
delay_us(10);
}

unsigned char OneWireReadByte (void)
{
unsigned char i, ReadByte = 0;

for (i = 0; i < 8; i++)
{
if (OneWireReadBit() != 0)
{
ReadByte |= 1 << i;
}
}
return ReadByte;
}

void OneWireWriteByte (unsigned char WriteByte)
{
unsigned char i;

for (i = 0; i < 8; i++)
{
if (((WriteByte >> i) & 0x01) != 0)
{
OneWireWriteBit_1();
}
else
{
OneWireWriteBit_0();
}
}
}

unsigned char OneWireCRC(unsigned char *code, unsigned char count)
{
unsigned char j, i, Data, tmp, CRC = 0;
for (j = 0; j < count; j++)
{
Data = code[j];
for (i = 0; i < 8; i++)
{
tmp = 1 & (Data ^ CRC);
CRC >>= 1;
Data >>= 1;
if ( 0 != tmp ) CRC ^= 0x8c;
}
}

return CRC;
}

OneWire.h

#ifndef __1_WIRE_H
#define __1_WIRE_H

unsigned char OneWireReset (void);
unsigned char OneWireReadBit (void);
unsigned char OneWireReadByte (void);
void OneWireWriteBit_1(void);
void OneWireWriteBit_0(void);
void OneWireWriteByte (unsigned char WriteByte);
unsigned char OneWireCRC(unsigned char *code, unsigned char count);

#endif

delay.h
#ifndef DELAY_H
#define DELAY_H

#include

#define CPU_CLK_Hz 8000000
#define CPU_CLK_MHz 8
#define CPU_CLK_kHz (unsigned long)(CPU_CLK_Hz/1000)

#define delay_ns(x) __delay_cycles(x*CPU_CLK_kHz*0.000001)
#define delay_us(x) __delay_cycles(x*(CPU_CLK_Hz/1000000))
#define delay_ms(x) __delay_cycles(x*(CPU_CLK_Hz/1000))
#define delay_s(x) __delay_cycles(x*CPU_CLK_Hz)

#endif

Составить ответ  |||  Конференция  |||  Архив

Ответы


Отправка ответа

Имя (обязательно): 
Пароль: 
E-mail: 

Тема (обязательно):
Сообщение:

Ссылка на URL: 
Название ссылки: 

URL изображения: 


Rambler's Top100 Рейтинг@Mail.ru
Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание