//owdevice - A small 1-Wire emulator for AVR Microcontroller
//
//Copyright (C) 2012 Tobias Mueller mail (at) tobynet.de
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
// any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program. If not, see .
//
//
//VERSION 1.1 DS2423 for ATTINY2313 and ATTINY25
//FOR MAKE by hand
/*
avr-gcc -mmcu=[attiny25|attiny2313] -O2 -c ow_slave_DS2423.c
avr-gcc.exe -mmcu=[attiny25|attiny2313] ow_slave_DS2423.o -o ow_slave_DS2423.elf
avr-objcopy -O ihex ow_slave_DS2423.elf ow_slave_DS2423.hex
*/
#include
#include
#include
#include "ledCommunication.h"
//does not work here because less memory by ATtiny13
#if defined(__AVR_ATtiny13A__) || defined(__AVR_ATtiny13__)
// OW_PORT Pin 6 - PB1
//Analog input PB2
//OW Pin
#define OW_PORT PORTB //1 Wire Port
#define OW_PIN PINB //1 Wire Pin as number
#define OW_PORTN (1<= 8) {
lmode = OWM_SLEEP; //all bits processed
break;
}
}
lsrcount = 0;
lactbit = (owid[lbytep] & lbitp) == lbitp;
lwmode = lactbit;
}
break;
}
break;
case OWM_MATCH_ROM:
if (p == ((owid[lbytep] & lbitp) == lbitp)) { //Compare with ID Buffer
lbitp = (lbitp << 1);
if (!lbitp) {
lbytep++;
lbitp = 1;
if (lbytep >= 8) {
lmode = OWM_READ_COMMAND; //same? get next command
cbuf = 0;
break;
}
}
} else {
lmode = OWM_SLEEP;
}
break;
case OWM_GET_ADRESS:
if (p) { //Get the Address for reading
counterpack.bytes[lbytep] |= lbitp;
}
//address is part of crc
if ((lscrc & 1) != p)
lscrc = (lscrc >> 1) ^ 0xA001;
else
lscrc >>= 1;
lbitp = (lbitp << 1);
if (!lbitp) {
lbytep++;
lbitp = 1;
if (lbytep == 2) {
lmode = OWM_READ_MEMORY_COUNTER;
lactbit = (lbitp & counterpack.bytes[lbytep]) == lbitp;
lwmode = lactbit;
lsrcount = (counterpack.addr & 0xfe0) + 0x20
- counterpack.addr;
//bytes between start and Counter Values, Iam never understanding why so much???
break;
} else
counterpack.bytes[lbytep] = 0;
}
break;
case OWM_READ_MEMORY_COUNTER:
RESET_LOW
;
//CRC16 Calculation
if ((lscrc & 1) != lactbit)
lscrc = (lscrc >> 1) ^ 0xA001;
else
lscrc >>= 1;
p = lactbit;
lbitp = (lbitp << 1);
if (!lbitp) {
lbytep++;
lbitp = 1;
if (lbytep == 3) {
lsrcount--;
if (lsrcount)
lbytep--;
else { //now copy counter in send buffer
switch (counterpack.addr & 0xFe0) {
case 0x1E0:
counterpack.counter = Counter0;
break;
case 0x1C0:
counterpack.counter = Counter1;
break;
default:
counterpack.counter = 0;
}
}
}
if (lbytep >= 13) { //done sending
lmode = OWM_SLEEP;
break;
}
if ((lbytep == 11) && (lbitp == 1)) { //Send CRC
counterpack.crc = ~lscrc;
}
}
lactbit = (lbitp & counterpack.bytes[lbytep]) == lbitp;
lwmode = lactbit;
break;
#ifdef _ONE_DEVICE_CMDS_
case OWM_READ_ROM:
RESET_LOW;
lbitp=(lbitp<<1);
if (!lbitp) {
lbytep++;
lbitp=1;
if (lbytep>=8) {
lmode=OWM_SLEEP;
break;
}
}
lactbit=(lbitp&owid[lbytep])==lbitp;
lwmode=lactbit;
break;
#endif
}
if (lmode == OWM_SLEEP) {
DIS_TIMER;
}
if (lmode != OWM_PRESENCE) {
TCNT_REG = ~(OWT_MIN_RESET - OWT_READLINE); //OWT_READLINE around OWT_LOWTIME
EN_OWINT;
}
mode = lmode;
wmode = lwmode;
bytep = lbytep;
bitp = lbitp;
srcount = lsrcount;
actbit = lactbit;
scrc = lscrc;
}
int main(void) {
mode = OWM_SLEEP;
wmode = OWW_NO_WRITE;
OW_DDR &= ~OW_PINN;
uint8_t i;
for (i = 0; i < sizeof(counterpack); i++)
counterpack.bytes[i] = 0;
Counter0 = 0;
Counter1 = 0;
SET_FALLING;
INIT_AVR
DIS_TIMER;
#define LASER_BIT PB0
#define LED_KATHODE_BIT PB4
#define LED_ANODE_BIT PB3
#define DATA_BIT PB1
#define ONEWIRE_BIT PB2
PORTB |= PORTB
| (1 << LASER_BIT)
// | (1 << LED_ANODE_BIT)
| (1 << LED_KATHODE_BIT)
;
DDRB |= DDRB
| (1 << LASER_BIT)
| (1 << LED_ANODE_BIT)
// | (1 << LED_KATHODE_BIT)
;
wdt_enable(WDTO_500MS);
sei();
uint32_t brightness = ((uint32_t) getLedBrightness()) << 2;
for (;;) {
wdt_reset();
brightness += -(brightness >> 2) + (uint32_t) getLedBrightness();
if (ledTestForChange(brightness >> 2)) {
Counter0++;
}
Counter1 = brightness >> 2;
}
}