Wednesday, November 18, 2009

Experiment No. 8: Use of PWM to control the brightness of a LED.

Introduction:
A PIC16F628A has an in-built Capture/Compare/PWM (CCP) module for which the I/O pin is served by RB.3 (Pin No. 9). In this experiment we are going to use the CCP as a PWM to control the power to a LED. PWM stands for the Pulse Width Modulation where the width of a digital waveform is varied to control the power delivered to a load. The underlying principle in the whole process is that the average power delivered is directly proportional to the modulation duty cycle. The term duty cycle describes the proportion of on time to the regular interval or period of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.
Image Source: http://www.micromouseinfo.com/introduction/images/intro_hardware/PWMod.gif
The mikroC has an in-built library functions for PWM hardware module. Click here for details.

Monday, November 2, 2009

Experiment No. 7: Timers and Interrupts

Background
Many microcontroller applications like generating periodic signals, measuring time interval, keeping date and time, use time as their variable. Therefore, microcontrollers need to have some internal resources to accurately measure time. The PIC16F628A has 3 timer modules which are known as Timer0, Timer1, and Timer2. The basic unit of a timer is a free-run 8-bit or 16-bit incrementing synchronous counter which can be programmed to count internal or external pulses. The count number stored by each counter can be read or modified by accessing the special function register associated with that timer. Some of the bits in these registers are also the indicators of timer overflow, which, therefore, can generate interrupt request to the microcontroller. The use of timer modules to keep record of time elapsed allows the microcontroller to carry on with its other operations like controlling outputs, reading inputs, etc.

Timers can also have another asynchronour counter, known as prescaler, which can be configured to divide the  number of pulses received by the timer register to be divided by a factor of 2, 4, 8, 16, 32, 64, 128 or 256.

1. Timer0 Module

The Timer0 module is an  8-bit timer/counter which can be configured to count machine cycles or external pulses. When counting machine cycles, it is known to operate as a timer, and when counting the external pulses, it is called to operate as a counter. The external pulses are given to RA4/T0CKI pin. The Timer0 also consists of an 8-bit prescaler which is a programmable division factor.

The operation of the timer is set up by moving a suitable control code into the OPTION register. Timer mode is selected by clearing the T0CS bit (OPTION<5>). In Timer mode, the TMR0 register value will increment every instruction cycle (without prescaler). Counter mode is selected by setting the T0CS bit. In this mode, the TMR0 register value will increment either on every rising or falling edge of pin RA4/T0CKI pin. The incrementing edge is determined by the source edge (T0SE) control bit (OPTION<4>). Clearing the
T0SE bit selects the rising edge.

Timer0 interrupt is generated when the TMR0 register timer/counter overflows from FFh to 00h. This overflow sets the T0IF bit of INTCON register. The interrupt can be masked by clearing the T0IE bit (INTCON<5>). The T0IF bit (INTCON<2>) must be cleared in software by the Timer0 module interrupt service routine before reenabling this interrupt.

The prescalar value can be set using the PSA and PS<2:0> bits of OPTION register.


Microcontroller-based Embedded Systems Design