Quantcast
Channel: Clock - Timer Projects - PIC Microcontroller
Viewing all articles
Browse latest Browse all 218

Toggle/Blink led on specific delay with pic microcontroller using timers: MPLABX and xc8 compiler

$
0
0
This is advance tutorial on blink/toggle led with pic microcontroller using pic microcontroller timers. I am going to teach you how to toggle led on specific delay time generated using timers of pic micrococntroller. This tutorial is not limited to only toggling led, it can be utilized at many other places. Like generating an event after specific delay, triggering a signal after specific time, reading data on known intervals, outputting data after desired time delay, pwm(pulse width modulation) signal generation, servo motor control etc. The algorithm discussed in the tutorial/post/project can also be used to output a desired frequency signal, by converting time domain signal in to frequency domain. 

In the previous tutorial we discussed about how to generate a specific delay using internal timers of pic microcontroller. We also derived and discussed the formula for calculating the output signal period. We also discussed the terms related with the time/frequency formula derivation, such as Tick_counter frequency, Timer Count etc. This tutorial is in chain with the previous one the code used below in inherited from the previous tutorial. I recommend to first go through the previous simple tutorial. You can easily understand the code below if you take the tutorial

Now you have taken the above tutorial its time to start with this tutorial. Suppose we want to toggle an led, generate and event or read data after 10 ms with 20 Mhz clock source. I selected the prescaller to be 1:4. For this we have to calculate the “Timer Count” value. Lets start deriving the “Timer Count”. The formula is given below.

Specific delay formula for Pic Microcontroller’s

Specific delay formula for Pic Microcontroller’s

10 ms = Timer Count * ( 4 / (20M hz/ 4) )
10 ms = Timer Count * ( 4 / 5 Mhz)
10 ms * 5 M hz = Timer Count * 4
50 k = Timer Count * 4
50000 / 4 = Timer Count
Timer Count = 12500 (Hex 0x30D4)

Timer register value comes out to be 12500(0x30D4) for the above given data. 0x30D4 is a 16-bit value which we have to load in the timer TMR register. TMR register is devided in to two 8-bit registers TMRxH and TMRxL register. TMRxH is timer high byte load the high byte in it, in our case its 30. TMRxL is timer low byte timer low byte is loaded in it, in our case its D4. x denotes the timer we are going to use. Pic base series microcontrollers generally have 3 timers Timer-1, Timer-2 and Timer-3. So if we are using Timer-1 in our project than the statements will look some thing like this
TMR1H = 0x30;
TMR1L = 0xD4;

Project circuit diagram

Pic16f887 microcontroller is used in the project. I am going to toggle the led’s connected to Port-B of pic16f887 microcontroller. Port-B is an 8-bit port. 8 led’s are connected with port-b. Whole port is going to be toggled after 10 ms delay. An external 20 Mhz crystal is used as input clock source to pic16f887 microcontroller.  Timer-1 of pic16f887 is used for generating specific delay. Timer-1 is used in 16-bit mode.

Note: ​Led’s anode is connected to pic microcontroller port-b pins and cathode is grounded with 330 ohm resistor placed in series. This configuration of led’s is a bad choice. I recommend you to connect the anode of led’s with external power source and connect the cathode with port-b pins of pic microcontroller. Resistor must be placed in series with the led to limit the current consumption by led.

My circuit configuration also worked but the led lights are too dim and its hard to see the effect of blinking/toggling in bright day light. I switched of room lights to see the clear pic microcontroller blinking led effect.   

Project circuit diagram

Pic microcontroller specific delay generation to toggle leds.
Project code is written in mplabx ide and xc8 compiler is used for compiling code. The function Timer0Delay in the code contains 10 ms delay generation logic.
 
I am initialing the timer settings bits each time the Timer0Delay function is called on in code. The timers settings bits can be globally initialized once and in Timer0Delay function we can only run timer for desired time delay. The timer-1 settings bits are below and are part of Timer0Delay function. 

 T1CON=0x01;             //Timer-1 16-bit mode Prescaler 1:4
 TMR1H=0x30;             //Count High Byte
 TMR1L=0xD4;             //Count Low Byte

Download the project code/files. Folder contains the mplabx ide project files. Please give us your feed back on the project. In case you have any queries please write them below in the comments section.
 

The post Toggle/Blink led on specific delay with pic microcontroller using timers: MPLABX and xc8 compiler appeared first on PIC Microcontroller.


Viewing all articles
Browse latest Browse all 218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>