Wednesday, February 27, 2013

Simple software Pulse Width Modulation (PWM) code for PIC 12f675 microcontroller

// Software PWM code version 1.0
// Author: Vivek Sinha
// (c) Vivek Sinha 2013
//

#include <htc.h>
#include <pic12f675.h>

// Hardware config and 4MHz internal clock
__CONFIG( FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF );

#define _XTAL_FREQ 4000000 // Internal clock at 4 MHz
#define LED GPIO0 // LED on GP0 pin 7

// Variables for brightness level
int brightness=1;
int fadeamount=1;

// Initialize function. LED connected from GP0 (pin 7) with a 1K Ohm resistance to ground.
void initialize(void)
{
ANSEL = 0;
CMCON = 7;
TRISIO = 0;
GPIO = 0;
}

// Custom microsecond Delay function
delay_us(unsigned int microseconds)
{
while(microseconds > 0)
{
__delay_us(1);
microseconds--;
}
}

// Software PWM code
void pwm()
{
// Switch On or Off the LED with the appropriate delay
GPIO0 = 1;
delay_us(256-brightness);
GPIO0 = 0;
delay_us(brightness);
// Change brightness and when brightness becomes min or max reset.
brightness = brightness + fadeamount;
if(brightness ==0 || brightness == 256)
{
fadeamount = -fadeamount;
}
}

// Main function
void main()
{
initialize();
while(1) // Infinite loop
{
pwm(); // call PWM function
}
}

Monday, February 25, 2013

RGB LED software PWM fader using 12f675 - part 2

One of first program for learning microcontrollers is to blink a LED. This is considered as the "Hello World" version of hardware program. The basic concept is to make the pins as output and then make them high or low alternatively.

When you have learned the first program you would like move to more complex programming. A fader to fade-in and fade-out an LED would be a perfect next step. Here's the schematic to create a fader which uses a single RGB LED to create a rainbow effect for colors with a cheap PIC12F675 8 pin microcontroller.

Schematic

As you would have noticed that the circuit uses very few components. Now here is the flow chart for concept,
The program would start by setting up counters for all the LEDs and keep reducing the currently ON LED and increasing the counter for the next LED. While at the same time it switches on and off the LEDs based on the counter as delay. When the counter for current LED becomes zero it moves to the next LED.

Flowchart of the program,

Download the code in HITECH C from here.

This is how the it looks on the testboard.



Monday, February 18, 2013

DIY T10 LED light for Car (used on Hyundai Santro)

LED light is very popular for cars these days. There are different types of bulb sockets and wattage/lumens (light intensity) for different purpose. The common bulbs used are for dashboard/side turn indicators called T10 wedge bulbsFor front and rear turn indicators and stop light BA15S and BA 15D (Bayonet socket with single or double contact) bulbs are used. You can try eBay.in to check some deals available.

T10 wedge bulb being commonly used for motor cycles also, I decided to make one using a tri chip 5050 smd LED (3 LEDs in one package) of white light. You would also need a 100 ohm resistor to limit the current and voltage for the LED and a general purpose board or copper clad board piece. For the LED I have used smd 1206 resistances. I also used a IN4148 diode for ensuring right polarity. 



Finished product is a copper clad board with resistor and diode soldered and LED on the top side,
  

As visible on a Hyundai Santro car's dashboard when illuminated in dark.

Also used this t10 bulb for Hyundai Santro's pilot light.

Sunday, February 17, 2013

In Circuit Programming connection for PIC Microcontrollers

For the PIC Microcontrollers to function as per the program, the program needs to be loaded to its EEPROM. ICSP is the method which would enable the programming of the MC while in circuit, i.e., you do not have to remove the microcontroller from the circuit board to program it.

To program a microcontroller you would require a programmer (e.g., PICKIT2 or similar). It is a piece of hardware which can be connected to a computer with PICKIT2 programmer software or similar software through a serial or USB connection. The programmer has ICSP header pins which needs to be connected to PIC in the circuit which has to be programmed.


Following is the ICSP connection details to connect to different PIC microcontrollers.

Once the ICSP headers are connected to the PIC in circuit you can Write the program from the programmer software to the PIC.

More on programming the PIC in later posts...