CURRENT ISSUE

Contests

bottom corner

FEATURE ARTICLE



Issue #216 July 2008

Second Place Microchip 2007 Design Contest
’Net-Enabled Alarm Clock

by DJ Delorie

Start | System Overview | Network | Display | MP3 | ADC | Memory | Power | Software | Time | Alarms | GUI | Remote Protocols | Construction & Packaging | Smart Combinations | Sources & PDF

SOFTWARE

The PIC24FJ64 handles many tasks, and it uses a main polling loop to manage them. Each task has a poll function, which gets called repeatedly from main(). The main loop also calls the GUI each time a button is pressed.

A modified Microchip TCP/IP stack checks for incoming packets and timeouts, looking for requests to connect to the remote management ports and manage the MP3 streams. The real-time clock module manages the NTP requests and updates the time display and snooze and sleep timers.

The ADC is configured to run in Auto-Poll mode. This means all three channels are continuously sampled and one of two buffers is filled with the results. The ADC task checks to see if a new buffer is ready. If so, it reads the data and converts it into useful ranges, including debouncing the button channel and smoothing the photocell channel.

The MP3 task is a bit complicated, because it needs to keep track not only of what it’s doing, but also of what it’s supposed to be doing. First and foremost, if data can be moved from the PIC24FJ64’s RAM to the MP3 chip’s buffer, it is. The task also checks for new incoming connections on the MP3 port, processes incoming data for currently open ports (which is stored in the PIC24FJ64’s RAM buffer), and checks for time outs. If MP3 data stops showing up but the alarm is supposed to be on, the internal “beep” sound is used instead.

The EEPROM task manages the remote EEPROM socket. When a remote computer connects to the clock to update its settings, the EEPROM task checks for data on that socket and acts on it, reading and writing the EEPROM and sending the results back.

The alarm task is responsible for setting up connections to the remote web servers to obtain MP3 data. This includes looking up IP addresses and handling the HTTP protocol. The triggering of alarms happens as part of the RTC task. At the start of each minute, the alarm module is called to check for new alarms and update the snooze timer. If a new alarm is triggered, the alarm task starts setting up the TCP connection for it.

The volume task watches for significant changes in the volume control setting (from the ADC task). If it changes, the main display is changed to show a bar graph of the volume setting. If it stops changing, the main display reverts. Because the muting can be controlled via the GUI, this enables you to set the volume without needing to have music playing.

As you can see, it’s not about doing one complex task, but many simple ones.

One of the unique features of the PIC24FJ64 is its ability to map most of the pins to many of the internal peripherals. However, this also means that you have to configure all of those mappings, as well as all of the peripherals you’re using. This is done in config.c. To keep things straight, I configured the pins in numerical order. The first time a peripheral was connected to a pin, I configured it as well. This file also has the global configuration bits and sets the internal clock. While there are three crystals in this project, none of them run the PIC24FJ64 itself—it runs off its internal RC oscillator and has a 32-kHz crystal for keeping track of the time of day.

To help keep track of the pin mappings, the pins.h file has macros for each function that map to the pins it’s assigned to. When I built the prototype, I assigned pins as I needed them. Later, when I laid out the circuit board, I remapped all of the pins to keep the layout clean. I just had to update config.c and pins.h.

Once the PIC24FJ64 is configured, the next step is to reset and power up the remainder of the board. The GPIO assigned to reset is pulsed. After a delay, the OLED’s power is enabled. Next, each module is initialized: the OLED, timer, TCP/IP stack, Ethernet chip, MP3 decoder, and real-time clock.

Previous | Next                *| Subscription Deals |*

 


bottom corner