Entries from Spiff's Electronics Notebook tagged with 'Code'


Fading LED default test sketch (February 12, 2009)

For some time now, every Arduino bootloaded ATmega we've shipped has come with a default testing sketch that slowly pulses the LED on and off in a cyclic fashion.

A "fading" sketch using hardware PWM is included in the IDE distribution under ExamplesAnalogFading to fade an LED, but the Pin 13 status LED isn't connected to a hardware PWM pin.

The answer is to abuse the delayMicroseconds call to create crude software PWM. By setting the pin high; delaying; setting the pin off; and delaying for a different period, we can change the brightness simply by altering the delay.

One catch to this approach, is if you use purely the on/off delay to control the PWM speed, you're left with either a very fast fade or an unsettling flashing. Instead we keep the delay between 4 and 250 µs (about 3 to 4kHz) and add an additional loop around each on/off loop to set the fading speed. --You can't set the delayMicroseconds code to 0 or you'll get an unexpectedly long delay.

Finally, we use the serial port to output a rising count, just to ensure the serial port works.

Continue reading "Fading LED default test sketch"


Posted by spiffed at 11:11 AM

Arduino and Freescale MPX Pressure sensors (April 29, 2008)

Freescale makes a range of integrated pressure sensors with 0-5V analog outputs, in a vast array of configurations. Today, I've picked the MPXV5010DP, but you should be able to adapt this to a range of sensors.

Continue reading "Arduino and Freescale MPX Pressure sensors"


Posted by spiffed at 12:10 AM | Comments (0)

EEPROM read/write code for CC5X (January 30, 2008)

CC5X may already include internal-EEPROM handling code, but I couldn't find any. With this in mind, here are two short methods for writing and reading from the EEPROM in many PIC microcontrollers. (Based on Microchips datasheets and .)

Continue reading "EEPROM read/write code for CC5X"


Posted by spiffed at 12:00 AM | Comments (0)

Getting up to speed with the PIC16F737 (January 29, 2008)

Porting code between PIC micros from the same product range is normally a smooth process: redefine a few pins, change some clock-speed constants, and add support code for that new integrated peripheral widget.

Sometimes though, there's a large hurdle before you can even begin. The 16F737 posed such a hurdle, there are a few clever lines of code required to put the device into all digital I/O running under the internal oscillator (intosc).

Continue reading "Getting up to speed with the PIC16F737"


Posted by spiffed at 10:44 PM | Comments (0)

Serial LCD Controller (June 26, 2005)

makes a number of rather nice serial (and USB based) LCD and VFDs. What they do not make, are inexpensive LCDs. Thankfully, hacking serial onto a cheap parallel LCD is sometimes almost as good.

Continue reading "Serial LCD Controller"


Posted by spiffed at 1:11 PM

Digital Input to the PIC16F628 in C (March 26, 2005)

I recently mentioned that I could not convince C to read from PORTA of a PIC. I have seen the error of my ways. The only thing missing from my code was a single line to disable analog functions on PORTA. inserting CMCON = 0x07; into the top of main() nicely fixed the mysterious I/O failures.

Continue reading "Digital Input to the PIC16F628 in C"


Posted by spiffed at 12:46 PM

Blinkin' PIC (March 24, 2005)

This is a simple "welcome to the scary world of microcontrollers" circuit that counts up and down between 0 and 0xFF based on a PIC16F628, some LEDs and a 6V battery.

It could serve as a nice "welcome to the fun world of soldering", "welcome to the 2-bit world of binary", or (as I mentioned) "welcome to the scary world of microcontrollers" or, it might only be something with pretty flashing lights.

Continue reading "Blinkin' PIC"


Posted by spiffed at 9:23 PM