January 16, 2012

3 ways to toggle an LED with a push button, using a PIC16F628 microcontroller and a MikroC compiler

Filed under: General technical reference — VIP @ 12:07

Microcontrollers have a built-in processor, some memory and a number of different pin control and analysis hardware, all on a single chip.

Serial communication, pulse width modulation, frequency output, hardware USB, CAN bus are only few of the hard-logic modules that can be built into a microcontroller.

Then there are common options, such as an analog to digital converter input, Schmidt trigger input or a logical button input.

An internal clock source is a common option common on most modern microcontrollers.

Usually, the full list of hardware and software means to adapt the input and output pins of the microcontroller to your needs can be found in the datasheet. Also the compiler documentation is important so you don’t find yourself reinventing the wheel.

So here is the “hello world” project that I had started with about a year ago using the MikroC compiler. And if it doesn’t work, that isn’t a problem. It will give you some experience insolating the problem and correcting it.

If you want to control the delay interval with a variable, than you should use the Vdelay_ms((variable));.

This command also uses a lot more space in the microcontroller RAM.

To set the fuses, click Project>Edit Project, in the main menu.

Pay close attention on how you set the fuses for the microcontroller:

  • If you don’t want to pull up the MCLR pin, disable MCLR.
  • You don’t need to write-protect your code.
  • You don’t need a brownout, watchdog timer or a power up timer in this project
  • Use the internal oscillator at 8 mhz. Therefore use the INTOSCIO setting for the oscillator selection.

    “INTOSCIO” makes GB4, the PORTB pin 4, an ordinary I/O pin versus a clock output pin, used to synchronize the PIC with other devices.

I have a PicKit2 and a PicKit3 to program the microcontroller (Figure 1). There are many devices on the market to write your compiled code into the microcontroller.

Download an appropriate standalone utility for the PicKit2 or PicKit3, whatever is that you are using. Connect the device via USB to your computer. Find the ‘Check Communication’ button and see what your Microchip utility is going to say… Hopefully, your device was detected.

Now for the connection to the microcontroller and for the actual burn: I assume that you use a breadboard where things can be easily plugged into and pulled out off.

Look at the datasheet at I would recommend printing page 4 and page 137 of the datasheet.

You notice that there are pins:

  • MCLR
  • VDD – 5V
  • VSS – ground

PicKit2 or the PicKit3 has the same set of pin sockets, for programming. There is also an additional LVP pin socket, but that technology didn’t exist when PIC16F628 was developed. So five pins need to be connected to the five sockets on the device, by the use of headers and some copper jumpers, on your breadboard. At this point, I made a device that can simplify this procedure to a great extent. Power supply must be off. Also look at the Figure 2 to see what not to have connected when you program a PIC.

Go to Project>Build and build the code. Than find ‘import hex’ in the PicKit utility, find the .hex file in the root folder of the project you had just compiled. (I hope that you keep it away from the compiler’s examples folder) write it to the microcontroller. Connect and LED to the ground, via a 470 ohm resistor and connect a button between the ground and a 10kOhm resistor. (See Figure 4.)

One of the images here originated from This is a very useful website to get started with using PIC microcontrollers. The Mikroelektronika MikroC compiler has a great help file that will help you build projects. However it relies on the expensive development boards that the company is trying to promote for some more complicated projects.

Figure 1. Figure 2.
PicKit3 with a label that makes it easier for me to connect it to the target microcontroller. This information is directly copied from the poster that comes with thePicKit3. Be aware of those circuit connections when programming a PIC controller.
SFigure 3. Figure 4.
Image from

This is how your microcontroller should be connected to the programming device.

Image from

This is how a button should be connected to the PIC microcontroller.


Pick the pair of IF statements that you like and see how the PORTB

is toggled on and off by the button attached to RA0 pin of the PORTA

of PIC16F628 microcontroller.

The hardware circuitry for the button may cause instability and therefore is a

subject for research.

The value of the pullup resistor determines the stability.

The delay is necessary to supress the contact bounce effect or to ‘debounce’ the button.

The software part is here and it is tested to work in 3 combinations of IF statements.


Created by Vladimir Tolskiy*/

bit oldstate;

void main()


short oldstate=0;

CMCON = 0x07; // Disable comparators

TRISA.F0 = 1; //pin RA0 as input – it doesn’t matter if you are using F0 or B0

TRISB = 0x00; // PORTB as output

PORTB = 0x00; // PORTB pins are all “off”

do {

//Choose the IF statement that you like the most:

if (Button(&PORTA, 0, 1, 1)) // Detect logical one

//if(PORTA.F0 == 0)

//if ((PORTA & 0b00000001) == 0b00000000)


Delay_ms(10); // “debouncing” mechanical contacts

oldstate = 1; // Update flag


//Choose the IF statement that you like the most:

if (oldstate && Button(&PORTA, 0, 1, 0)) // Detect one to zero change

//if (oldstate && PORTA.F0 == 1)

//if (oldstate && (PORTA & 0b00000001) == 0b00000001)


Delay_ms(10); // “debouncing” mechanical contacts


oldstate = 0; // Update flag


} while(1); // Endless loop


Making an SMD circuit board without specialized equipment (This is a developing story.)

Filed under: To blow your mind — VIP @ 02:58

When you think of soldering an SMD – mounted component on to the circuit board, you are probably thinking of the reflow oven and some other expensive equipment. Not in my case.

But you have to remember that any DIY technology involves some risk taking and some estimation of the economic benefits/losses. I began searching the internet and looking for a cheap solution to make DIY circuit boards at home, and I didn’t find an instruction that would match my goal from the beginning to the end.

What I learned:

  • That this exponential growth but to but with exponential decay function of temperature over time can be obtained by making a control module for the toaster oven with an alarm that will warn you when to open the door. I am still looking for the website I read it at. (Remember the Newton’s Law of Cooling?)
  • There is a way to solder SMDs on the frying pen. I bought a frying pan with low edges for making a French toast.
  • After I tried to solder an SMD onto the breakout board and had the board bend and turn brown from uneven heating, I took some silicone grease that can hold 400˚F (204˚C) and used that to evenly transfer heat. I also tried using sand, but the grease works better.
  • Only if the board is pre-tinned, you can solder the component right on to the board.
  • I bought a laser thermometer. It is not very precise but it is fairly precise when it comes to change in temperatures. (How else would you measure the temperature of the molten solder?)
  • 60/40 solder contains sixty percent tin and forty percent tin. The melting temperature of the solder is 183–190 °C (361–374 °F). Although Tin and Lead have fairly high boiling points, the solder can boil below 300°C and that’s what you don’t want to happen by any means unless you are trying to get lead poisoning.
  • I bought a portable range so I could experiment with solder boiling outdoors.
  • Lead is a known toxic metal but more expensive ROHS-compliant solder has a number of other neurotoxic heavy metals to replace lead. Antimony is one of them.

Fluxes are the trickiest part: there are too many options on the market to try in a lifetime.

  • Plumbing paste is not recommended because it is acidic and will corrode the solder spot after the assembly is finished, because the moisture in the atmosphere will make the remains of the solder into electrolyte. (Can it be washed away with alkali? Not sure yet. Experiment is on the way.)
  • Old-school chunks of brown rosin can be dissolved in rubbing alcohol to make a very good flux for precision soldering. Store it in the bottle and use a linen brush to apply the flux onto the solder joint. The rosin can hold your SMD component.
  • Modern organic solvent rosins are more expensive, but superior to rosin flux. They have a limited shelf life. Some of them can glue the component on to the spot the way rosin solution can.
  • A rare and a more expensive industrial product – the solder paste has a mixture of both, flux and powdered metals. As the heat is applied, the paste melts, burns out the flux and forms a strong solder joint. Similar to brazing.
An example of a successfully soldered SMD negative voltage generator. This silicone grease makes an ideal medium to transfer heat from the frying pan to the circuit board.

Create a free website or blog at