TechnoSyndicate

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 http://ww1.microchip.com/downloads/en/devicedoc/40044d.pdf. I would recommend printing page 4 and page 137 of the datasheet.

You notice that there are pins:

  • CLKIN
  • CLKOUT
  • 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 http://talkingelectronics.com/projects/StartHereWithF628/StartHereF628-P1.html. 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 http://talkingelectronics.com

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

Image from www.e-shore.com

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.

enjoy

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

PORTB = ~PORTB; // Invert PORTB

oldstate = 0; // Update flag

}

} while(1); // Endless loop

}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: