Posts Tagged “AVR”A project I’ve been hacking on for a while is a self-contained 1-wire to IPv6 bridge based on an Atmel AVR ATmega644 and the ENC28J60 Ethernet controller from Microchip. 1-wire: is a serial bus from Dallas Semiconductor/Maxim that only requires 1 data line, there are a number of cheap sensors and other devices for this bus. The strength of this bus is not its speed but that it supports large ranges (up to 300 meters). IPv6: Insanely large address space. It’s common to use a 64-bit netmask for site networks so that EUI-64 based addresses can be used for auto configuration. This leaves 64-bit for the node address – do you see where this is going now… :) Yes..I’ve built a device that assigned each 1-wire device it’s connected to its own IPv6 address. Why? you ask, mostly because I can. HardwareAs mentioned above, the device is based on an AVR ATmega644. It has 64KB of flash memory for program code and 4KB of RAM. It’s running on its built-in oscillator at 8MHz. The ENC28J60 Ethernet chip is connected to the AVR using SPI. The rest of the hardware is mostly for power distribution and management. The PCB was manufactured by BatchPCB, cheap service but a bit slow turn-around time. Unfortunately I screwed up the SPI connection but I managed to fix that with some green wires (or black wires in this case). You’ll note them in the picture above. As for the 1-wire devices I had implemented a bus master in software that generated the require waveforms. It worked great up to about 10-15 meters. Any cable length greater than that refused to work. This required an add-on board and because I didn’t want to wait for a new PCB I used a 2.54mm prototype board. With the DS2480 only available in SOIC8 packages it required some “creative” soldering :). The add-on board is extremely ugly. But hey, it works. Future improvements for the next revision
I’ll publish the PCB CAD files when the next revision is complete. SoftwareThe only small IPv6 stack I know of is the uIPv6 stack in the Contiki operating system created by Adam Dunkel et al. This is unfortunately only available together with Contiki and not as a stand alone package as the originally uIP (IPv4) stack. Contiki is a great operating system, but when you only have 4KB of RAM it becomes a bit heavy weight. So I broke out the uIPv6 stack from Contiki and made it run stand alone and ported in to AVR. I also ported the web server application from Contiki and made it run on AVR. As I wanted to use multiple IPv6 addresses I also had to add support for IP aliases to the uIPv6 stack. Since the uIPv6 was integrated with Contiki it used the Contiki process model which it self is based on “proto-threads” (another thing invented by Adam Dunkel). I felt that this didn’t fit so I turned all processes into a polling mode instead. So one has to call a set of polling functions from the main application loop or from timers. The other major parts of the code are drivers for ENC28J60, DS2480 and DS1820. Software
Currently, with 5 1-wire devices connected it uses about 3KB of RAM. In-actionI only have temperature sensors connected at the moment. If you happen to have an IPv6 capable connection you can access the sensors through a web browser. 2001:16d8:ffe5:002:2894:eaf6:100:0c7 (If you don’t have IPv6 you should get it, or you can view graphs based on the sensor values at lindberg.tl instead).
Handy system clock for AVR 8-bit microcontrollers suitable for measuring elapsed time or for use with timers. This provides The system clock is based around a 32kHz clock crystal and one of the 8-bit timers provided by the AVR, it is possible to use the CPU frequency as a timer base as long as it’s a nice, dividable, frequency. The AVR timer is a 8-bit register that simply counts at the rate of its clock source. The clock source can be either the CPU This example is creating a 1/32 second resolution timer (32 ticks per second) using an external 32768 Hz watch crystal. The following to equations can be used to calculate either comp or the prescale value.
Using 128 as the comp value and inserting the other values into equation 2 yields the following prescaler
So, a prescaler of 8 gives us two interrupts per 256 cycles, one at 128 and one at 256 (overflow). Using 256 as comp would yield a perscaler of 4 but the target device I used didn’t have a TS/4 prescaler. Complete source code for a 1/32 (or 31.25ms) second resolution timer for the ATmegaxx4 using Timer 2 and a 32kHz watch crystal connected to the pins TOSC1 and TOSC2. #include <avr/io.h> #include <avr/interrupt.h> typedef uint32_t clock_time_t; static clock_time_t global_system_ticks = 0; /* ISR for the timer overflow */ ISR(TIMER2_OVF_vect) { global_system_ticks++; } /* ISR for the comparator */ ISR(TIMER2_COMPA_vect) { global_system_ticks++; } /* Return number of elapsed ticks */ clock_time_t clock_time() { return (global_system_ticks); } /* Return number of elapsed seconds */ unsigned long clock_seconds(void) { uint32_t tmp; TIMSK2 &= ~(1 << OCIE2A) | (1 << TOIE2); tmp = global_system_ticks / 32; TIMSK2 |= (1 << OCIE2A) | (1 << TOIE2); return (tmp); } void clock_init() { /* Enable external oscillator (32 kHz crystal) connected to TOSC{1,2} */ ASSR |= (1 << AS2); /* Reset timer */ TCNT2 = 0; /* Set TS/8 prescaler, results in a 4096Hz clock */ TCCR2B |= (1 << CS21); /* Compare at half counter value */ OCR2A = 128; /* * Enable overflow and compare interrupt. * Triggers each 1/32 secs */ TIMSK2 |= (1 << OCIE2A) | (1 << TOIE2); } |




Entries (RSS)