After such a good response to Mr. Baybus, I decided to up the ante. I wanted temperature sensing and light control, as well as a more refined interface. I also wanted a chance to write much, much better code as Mr. Baybus was most definitely a kludge. This led to the design of Mr. Baybus 2.

Mr. Baybus had a few problems I wanted to take care of. One being price, it cost way too much compared to its utililty value, which is normal for projects like this, but still…

Mr. Baybus 2 uses a much more sophisticated microcontroller, a PIC 16F870. This is a 28-pin SDIP style chip, lots more I/O pins, an onboard UART, and even an ADC. This little guy also has twice the instruction memory, so I had more freedom to make the interface somewhat more sleek, and add more features. The benefit of more pins is CHEAPER LCD. The previous serial LCD ran about $42 shipped, which is semi-high for a serial LCD in general, but it was a CrystalFontz so at least it was high quality. Anyways, now I can move to a simple HD44780 based parallel LCD (by CrystalFontz, of course). These run around $20 shipped, and even less from other places selling generics.

Features

Three On/Off Fan controls

One 12V Light control (Neons, etc)

Two centrigrade temperature sensors

20×2 screen (any HD44780 compatible will work)

Simple, menu-driven style interface

Stores fan status in non-volatile memory

Display Images

Splash screen

Fan status

Fan status w/selection

Lighting status

Temperatures

Unit Images

High view of the front

High view of the back

Back view

Underneath

Version 2.1

A slight update to the original. I decided I wanted to re-write it in C as an exercise. While I was at it, I figured I’d add a feature or two.

Pretty basic, a complete re-write in C, Hi-Tech PICC to be exact. It’s a great compiler for the PICs and gave me opportunities to re-write the LCD, ADC and DELAY libraries in C.

Related posts

Previously, in PIC buttons (polling) we saw how to poll for the state of a line connect to a button, that is all fine and good but really that is not the best way to do them. The “real” way to interface with external components like that is through interrupts, a slick feature.

Interrupts provide you with lots of freedom in your code. They allow you to sit back, relax, and be told when an event occurs, and not be forced to sit and wait for it to happen.

For this program, the schematic and circuit are practically the same, the only thing that changed location is the button.

Instead of looping over and over again, we simply wait, using a goto $ we are essentially goto’ing the same address over and over, “goto here, goto here, goto here…” ad nauseum. A common technique is also to use a SLEEP command, which puts the PIC in a low power mode and halts the program counter. Same effect to the user though. You of course could do ‘real’ work too instead of just burn cycles.

Once the button is pressed and released, the PIC will generate an interrupt, forcing the program to goto memory location 4. This is labeled in the code as ISR (Interrupt Service Routine).

For this program we are using the RB0/INT Interrupt. This interrupt occurs when there is a low-high change in PORTB,0. It can also be configured for high-low as well.

To enable this, we set INTCON,INTE. This bit says we want to know if a change occurs. To enable interrupts in general, we must then set INTCON,GIE. This lets all enabled interrupts occur.

We then wait for the interrupt. Once it occurs, GIE is automatically cleared so we can’t have them inside each other, and blink the LED a couple times. We then clear the interrupt flag, saying we’ve handled the interrupt (INTCON,INTF). We then re-enable interrupts and return from the interrupt: retfie.

It should be noted that if we were using more than one interrupt type, we would have needed to check the flag bits to find out which one interrupted us. We then handle it, and clear its flag. The PIC is somewhat crippled in this manner. Any and all interrupts generated and thrown into address 4 and “we” have to figure out which one occured. Many higher end MCU’s will have a table of addresses to jump to for each particular interrupt type, we then code in each location the correct routine and the processor knows which to call based on what happens.

Downloads

Polling for button input, how useful! This is pretty brief and gives a good idea how to let buttons control your programs execution.

In this tutorial I’ve switched from using an oscillator to using a crystal. This changes the design a bit. Using the 2 OSC pins, OSC1/OSC2 (CLKOUT/CLKIN) they hook to the crystal in parallel. Then the two sides of the crystal are connected to ground via two capacitors, in this case 18pF. The speed and capacitance needed varies, and can be seen in most any PIC MCU datasheet as to how to lay it out. It will also be in my schematic.

Please note this is essentially NO DIFFERENT than the oscillator. It is simply a different means of providing a clock signal. You can swap in the oscillator to the normal CLKIN pin and it will work just fine.

For this program, we’ll start the processor, and wait for a button to be pressed. After it has been pressed we’ll essentially execute the LED blinker from before.

Polling is extremely simple. Polling is the act of checking something for a certain state to occur. In our case, we poll PORTB,4 waiting for it to go low. Once it does, we continue on and start blinking.

Circuit Image

Another topic that needs to be covered is that of Pull-Up resistors. In order for a button to be able to change from 0 to 1 (Ground to 5V) we need a way to protect everything from a short circuit. We do this with a Pull-Up (or Pull-Down in some cases) resistor. For our case, we connect PORTB,4 to +5V via a 10k resistor. So when we fire up the program, the PIC sees PORTB,4 as HIGH. We then connect our button to PORTB,4 on one side, and Ground on the other. Now, when we press the button, PORTB,4 is connected to ground and is now LOW. A short circuit between +5 and Ground would occur if not for the resistor, which being 10k limits the current to a measly 500uA, nothing to worry about. Once the button is released, Ground is disconnected and the pin returns to a HIGH state.

Reading the code, you may also notice the _BANK macro has changed a bit. I’ve simply modified it to encompass all possible BANK configs instead of using 4 different macros for each. Reading through it can give you a little insight as to how the conditional assembler works. It’s a bit like C/C++ and a bit like BASIC. Quite nice and handy.

A final new bit is the _MCLRE_OFF in the __CONFIG line at the beginning. This frees us from having to pull _MCLR high to keep from a RESET condition. Just keeps our parts count down.

Now for the new section of code.

btfsc PORTB,4
goto $-1

That’s polling, yup, that’s it. All it is doing is a bit test on PORTB,4, waiting for it to become clear (Ground). The goto line is telling it to go 1 instruction back ($ means the address of the current instruction, $-1 means one before, the btfsc). Once that pin becomes 0 it skips the loop forcing it to check, and lets it continue on down to the blinker!

Related posts

Beyond all doubt, the #1 beginning program in microcontrollers is the LED blinker. It’s super simple, and teaches the concept of pin voltages and busy-waits. Here is a busy-wait LED blinker program, and a walkthrough building it in MPLab.

First, the delay. This is a busy-wait delay program, busy-wait means you just burn instruction cycles for the delay, keeping the MCU “busy”. There’s a tiny bit of math behind them. First, the clock speed is 20MHz, the instruction frequency is (clock/4) so our instructions are executing at 5MHz. This gives us a period of 200ns per cycle.

I created two delay functions, for versatility, and for LCD stuff which will come later but it’s handy here. One is a “DELAY_US” which will delay a specified amount of microseconds. This is done by wasting 5 cycles (5*200ns = 1us) a specified amount of times (less than or equal to 255us, since I only made an 8-bit “delay” variable). We can learn the cycle times from the data sheet, and make it work from there.

Next, I created a “DELAY_MS” which delays a specified amount of milliseconds. Same 8-bit limitation of max 255ms, but that’s enough to have fun with… It simply calls DELAY_US a few times with specified amounts of delay, adding up to 1000us (=1ms) and repeats as many times as we tell it to.

These delays are used to make the LED blinking visible, otherwise it would blink faster than we could see (if at all, it takes a cycle or two for the pin to change state so it might not even change if we just toggle it every other cycle).

Here’s a little more PIC architecture information. The PIC’s data registers are broken into “banks” (bank 0, 1, 2, 3). Meaning you cannot get at them all at the same time, although some are mapped to all banks so you CAN get at them, important ones. We usually hang out in bank 0… This usually isn’t a problem, just something you need to remember. The data sheet illustrates it pretty well. When it comes up I’ll clarify things about it.

Also, for the I/O pins. Since they’re bi-directional, you need to choose which direction to set them to. Input or output. This is done by setting the tri-state register for the given port, for example PORTA’s tris register is called TRISA (not tough!). You set the direction of a specific bit, by setting the bit of the TRIS register to either 0 or 1, 0 meaning OUTPUT and 1 meaning INPUT. Not tough to remember: 0 = Out and 1 = In.

Source Code

Ok, first you need to make sure you have MPLab from Microchip.com

Once you have MPLab, download the LED Blinker (busy-wait) source code.

MPLab is a nice IDE, you’ll need to create a “project” and then pick your chip, and add the asm file to it (called a “node”). All code is going to be indented 2 spaces, labels will not be indented at all, assembler directives are either 1 or 2 spaces in…

First few lines are kinda simple, the title directive just sets a title for your project…

LIST R = DEC sets the default “radix” for the program, meaning the number base. So if I put 100 in a line somewhere, it means 100 DECIMAL. If I changed it to LIST R = HEX, then if I put 100 in somewhere, it means 0x100, TOTALLY different. I find DEC easier to work with, and you can still use 0x whatever and it means HEX so you get the best of both worlds.

Next we INCLUDE the “p16f628.inc” which will give us nice little names for our registers so we don’t need to remember their addresses, how nice of Microchip.

Then the __CONFIG line, arguably the ugliest line of code while still being fairly simple. You can see the list of __CONFIG’s in the “p16f628.inc” file, they’re simply setting the configuration of certain chip features, the ones I have listed do this: _CP_OFF copyprotect OFF, you can set the copyprotect bits to make your chip unreadable, this is only good for a production product, don’t use it. _WDT_OFF, watchdog timer off, watchdog timer is there for mission-critical applications, it’s constantly running and needs to be reset constantly, if it isn’t reset it will reset the chip (assuming your program has locked up), we don’t need it here. _HS_OSC specifies a high speed oscillator, it’s in the data sheet. _PWRTE_ON turns on the power up timer delay, it’ll make the MCU wait a bit before executing to make sure voltage is stable, oscillator is stable, etc… _LVP_OFF – low voltage programming, dun’need it… _MCLRE_ON makes us hold MCLR high, instead of letting the chip do it…

Now to the variable declarations, the CBLOCK directive lets us just list out our variable names, and the assembler will assign addresses for us, this is handy. The 0x20 is the starting address of general-purpose registers in BANK 0. We list em out, then end it with ENDC.

Next the Macro’s… Macro’s are one of the coolest features of MPLab, it’s kinda like an inline C function, and kinda like a #define. When called, the code is dumped into where it was called from, but you can use variables in it, and even arguments to customize it’s compiling, stuff I’ll show in later programs.

Anyways, you make one by saying: NAME macro in the first column, then code. End it w/a endm. The ones I have are fairly simple and re-usable. BANK0 sets the bank bits to get us into BANK 0, go figure. BANK1 sets them to get us into BANK1, crazy!

DELAY_MILLI takes the TIME argument and loads it into W, next it moves W to the register labeled DELAY. Then it calls our illustrious DELAY_MS function which will be explained in detail down below… DELAY_MICRO does the same damn thing with DELAY_US!

Next we have our subroutines, the delays, I have these before the main lines of code just out of habit, it’s not required.

DELAY_US… Pretty simple really, we start out by burning 2 cycles, so we’ve waited 400ns so far, next we decrement our counter, and test if it’s zero, if it isn’t, we goto DELAY_US, looping again, if not, we return. The test itself takes one cycle (600ns so far) and either the goto or return take 2 cycles (1000ns = 1us) so we have our microsecond delay!

MAIN is our label for the beginning of the code, jumped to by the first line up @ org 0. BANK1 gets us into BANK 1 so we can set our bit direction, we clear bit 2 of TRISA making bit 2 of PORTA our output pin, then we hop back to BANK 0…

Then our introductory programming teachers worst nightmare, a purposely created infinite loop. We label the beginning, then set our pin high, shutting off the LED (as you’ll see in the wiring diagram). We wait 250ms via our handy delay function, then clear the bit, turning the LED on, we wait again and loop ad nauseum.

‘end’ tells the assembler to give up…

Building

Alright, so we have our program, we run the assembler by clicking the weird funnel icon or by going to Project -> Build Node (or All). It’ll crunch and come up with no errors (of course). Then just toss it into the programmer and feed the chip your tasty code.

Schematic

The assembled circuit

Wire up the circuit as in the schematic at the top of the page. Hopefully it illustrates to you why the LED is on when the bit is off, and off when the bit is on… The LED is a typical ~2V yellow LED… Wired up it should look something like the image.

Running

Hook up +5V and Gnd, and fire it up! If everything is set up correctly you’ll get a steady blinking LED!

Something to try

Connect a momentary switch to the _MCLR line, wired to Ground on the other side. Pushing the button will reset the chip, releasing it will start it over from the beginning of the program, of course it will do the same stuff, but this demonstrates how the reset buttons work. I also highly recommend changing the code to add a more interesting blink pattern, longer/shorter delays, and other stuff to get used to modifying code…

Downloads

Mr. Baybus is a microcontroller-based fan control system. It is a completely stand-alone unit, with no computer-control whatsoever.

Control comes from momentary switches on the front panel. You have 4 switches to toggle your fans on/off, and a brightness/contrast button, which switches you into a screen to alter those settings. Another press gets you back to the fan status display.

All settings are saved in EEPROM memory on-chip. So when you shut your system down, then power back up, your fans will be running the same as they were before, and your brightness and contrast will remain unchanged as well.

The display is a CrystalFontz 16×2 Serial LCD. This unit is EXCELLENT. It supports SPI transfers which is what Mr. Baybus prefers!

The fans are switched by power MOSFETs. IRL3102’s to be exact. They are rated to handle up to around 7A for a 12V circuit like this. This is of course far beyond anything I would ever want to throw at it, but it’s nice to know you have the room to expand.

Connections to the system are made via a small 4-pin connector. This facilitates 2 fans per circuit, 4 circuits in all. The connector is the same as the CD-Audio connector on your CD-Rom’s so it’s quick and easy to remove the fans.

The brains of the system come in the form of an 18-pin microcontroller. A very basic PIC16F84a. At 4MHz this little guy is going way faster than this system needs but hey, if you got it, why not. All 912 lines of code were written in assembly over the course of a few nights.

Related posts

Your basic PIC16 microcontroller can’t hold down the fort by itself, it needs a little help from a few components.

Breadboard

I use a prototyping breadboard, the white kind you can just plug & unplug stuff to all day long. It makes life prototyping a whole lot easier.

Ok, now on to the PIC stuff. First and foremost, you need an oscillator, the oscillator is what keeps the PIC moving, it provides the clock signal for the chip. For our 16F628, I used a 20MHz oscillator in a “half-can” package. Many people like to use crystals and capacitors, etc, I use them on simpler designs, but they’re a bit harder to understand at first.

These oscillators are powered by +5V, and the output goes into the pin labeled OSC1/CLKIN on the 16F628. You can get these oscillators at Digi-Key (part#: CTX169-ND).

The other key part is the reset line. We need to hold this line HIGH (+5V) and drop to it 0V to reset the chip, which we won’t need for a while. To tie it high you use a “pull-up” resistor (10k is good for this) to the +5V line, connected to the _MCLR pin of the chip (reset pin). I like to use the outside rails for Ground, and inside rails for +5V as you’ll see in the pictures… This isn’t *required* as our chip is smart enough to pull up it’s own _MCLR pin, but for the first few projects we’ll explicitly do it so you get used to it.

Oscillator

Also connect Vss and Vdd to Gnd and +5 respectively to power the PIC.

After wiring these parts up, your breadboard should look something like this:

Basic PIC16 wired up

We also need a power supply. I use an old AT power supply, taking the red and black lines for +5 and Gnd respectively. You can use a 5V bench supply, batteries, wall-wart, anything 5V (but make sure it’s regulated).

Another smart part to use is a bypass capacitor, this lets the PIC draw power from the cap instead of the power supply during an increased power draw time, these are usually brief, but the power is needed quickly and a voltage drop would be bad, so you place a small, fast capacitor nearby the IC to provide the instant power and keep everyone happy. Typical values are a .1uF Tantalum capacitor.

Also handy are a pair of IC chip pullers, available in most little toolkits, they’ll help you avoid damaging the chip as you pull it out of the breadboard…

A PCB is much more desirable than just a mess of wires, or even the little breadboards you can get @ Radio Shack. They’re not always the best way to do it, might not be worth the effort, but when you have a fairly complex wiring project, a PCB is VERY HANDY.

So, what do we need to make a PCB? Well, it’s not TOO much stuff… This method is the Iron Transfer method of making a home made PCB or PC Board.

Supplies

El-cheapo glossy inkjet paper

Some way to draw the board. I use Eagle from CadSoft. It has a great free version available for download at their site.

Copper-clad board (we’re doing single-sided, 1oz.) these run around $5 @ Radio Shack or online for a single/double sided, 6″x6″ board

Process

Design and print the PCB Layout

An example layout

2-up sample layouts

First you draw your board, with whatever software you want. If you are using a PCB layout software package, then you already know what to do. You can also just use any old drawing program, but I’d stick to 1 layer for that. If simply drawing it, keep in mind you are drawing the BOTTOM of the PCB, so all pinouts, and arrangements are REVERSED (Unless you are making stuff for SMT parts, in which case, you probably don’t need this tutorial!). It’ll take practice and patience… Measure, draw, print on plain paper and see how your parts fit… Check, check, check, and check again to make sure all the lines you have drawn will mimic your wiring, you can’t change it without being sloppy once it’s etched…

Tip: Make sure your components have holes for each pad, once etched, it’ll make drilling much easier.

After we have that done, we’re ready to print it on our glossy paper. One thing you must remember to do, is to print this image flipped. You will be printing it to the page, then ironing it on, so to get it the right way, you must mirror it.

My laser printer doesn’t like the glossy paper, so I print it on a plain sheet of paper, then run it through a photocopier… Same result. You just need a flipped image of your board in toner on the glossy paper. The glossy paper lets the toner come off easier when you iron it.

Transfer the Image

Pre-etch copper clad

An iron!

Cut the paper to size to make it easier to handle and position on the copper-clad. You may also want to cut the copper-clad if don’t plan on using all of it for this board.

Really quick we need to prep the copper-clad to take the transfer. You simply scratch it up a bit, not gouge, just scratch. I usually wet-sand it with super-uber-fine grit sandpaper, or an abrasive sponge pad. It should be nice and shiny and dry when you are done.

Now we break out the Iron.

Turn it up to a medium temperature. I only put mine up about 1/3 of the way, too hot and it’ll smear the toner and burn the paper, no need for that.

Place the image on the copper-clad and line it up. Rub the iron across it holding firmly down til it starts to stick, once you have good contact you don’t need to worry about it moving around.

I usually put a piece of paper or two in between the iron and the transfer paper, so I can run the iron over easier. It helps diffuse both the heat and the pressure more evenly, but will slow the process down a bit. Check every so often to see if it is transferring. Some areas like the edges might need special attention with the tip of the iron. I usually push down real hard to ensure good contact, and leave it on, moving around for a few minutes. Whatever feels right. If you mess up you can just scrape it off (re-sand as per the prep instructions), so experiment and find what works for you.

Once you think you’re done, remove the iron. Take the board (using hot pads or something so you don’t singe your fingers) and put it in your sink. I use it to cover the drain hole, then turn on a light stream of water. This will make the sink fill, and let the board soak.

Stuck on

Soakin' it...

Transferred

After a while, the glossy material gets goopy and slimy and starts to come off, it can be a bit messy but it’s tolerable.

Once it’s pretty loose you can peel off a layer of paper… Some might stay on, it’s fine, just leave it in running water. Eventually it will lose it’s grip and just come right off, leaving (ideally) a perfect image of your PCB layout on the copper-clad.

Etch

Etchant and Tray

Etching the PCB

Now that the image is transferred on, we can start to etch. The etchant solution eats away all the copper it can react with, but the toner on there stops it from getting our pattern, the result? An etched PCB.

You want to use a plastic tray of some kind, something you won’t be putting food in or anything. Rinse it out and dry it, then set the PCB in there. Pour in some solution and watch it work.

Tip: The hotter the solution, the faster it works… Outside in the summer sun really makes it go fast.

Warning: Don’t do this in a closed area, breathing this is bad… Also try not to touch it, it stains quite effectively.

Agitate the solution as much as you can tolerate. It gets kinda boring so do it off and on. Just simply lift and set down one end to slosh the solution around so fresh solution can get at the board and continue the reaction.

Keep an eye on the board, I use plastic tongs to take it out every so often and check it, rubber gloves also work well. Eventually you’ll be able to see how it’s eating away the copper and your board is appearing.

Once you feel it’s done, flood the tray with water, be careful not to spill it, you’ll be quite unhappy if you do. I usually fill it up w/water, then pour it into a bucket. This stuff is hazardous waste, so you might want to find out where to dispose of it with whatever local agency deals w/that in your area. Once you rinse a few times to dilute the solution, take the pcb out, and run that under water for a minute or so to make absolutely sure the chemical reaction is neutralized.

Once you have that out of the way, you should have something that looks like this:

Etched PCB, bottom

Etched PCB, top

Drill

Itty Bitty Bits

Completed PCB

Ok lets drill. I have a little set of bits I found at Hobby Lobby. They’re for crafts or something, anyway they work really well and were cheap. You can also get a Dremel bit set from hardware stores.

Using these with a Dremel makes this process super easy, as the Dremel is easy to handle and nimble. But a normal hand drill should work fine too. Some people even go so far as to use a Dremel with a drill press, I’ve tried it, it’s neat, but no easier than by hand.

Just find the bit that fits your parts, and drill away, using the holes you left in your PCB layout to guide the bit. The material making up most of the board is super soft and cuts like butter w/the drill bits.

Warning: Hazardous Materials! Be careful not to breathe the dust, it’s fibreglass and some of its other nasty friends, not something you want in your lungs. Use appropriate masks/ventilation.

After you’re done drilling, I like to wet-sand again with super-fine sandpaper… The toner is VERY hard to get off with a simple scotch pad or other abrasive… But wet-sanding takes it right off and you have nice shiny copper underneath… Acetone (the nail polish remover kind, no need to go industrial here) will also work to remove the toner but some PCB materials may not particularly care for Acetone.

That’s all! An optional final step is tinning the PCB. Tinning puts a very thin layer of tin over the remaining copper, reducing oxidization problems you’ll get with exposed copper, and it also helps soldering a bit. You can find tinning solution from electronics shops online or in reality.