Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Software pwm to drive RGB LED

Status
Not open for further replies.

aghielan

Member level 2
Joined
Jun 1, 2006
Messages
45
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,546
Hi i'm a newbie. I'm trying to develop software pwm to drive RGB LED using interrupt. but seems the code i developed doesn't works even the code was successfully compiled. Need experts help on my code. my code as below and i'm using HI TECH C v9.83. thanks in advance.. :)

Code:

#include <htc.h>
__CONFIG(FOSC_INTRC_NOCLKOUT & WDTE_OFF& PWRTE_OFF & BOREN_OFF & LVP_OFF & WRT_OFF & DEBUG_ON & CPD_OFF & CP_OFF);
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 8000000
#endif

#define RED_LED RD0 //red led
#define GRN_LED RD1 //green led
#define BLU_LED RD2 //blue led
#define ON 1
#define OFF 0
int red_dc, grn_dc, blu_dc, pulse = 0;
void init(void)
{
INTCON = 0b00100000;
TMR0 = 217;
ei();
}

void main(void) {
OSCCON = 0b1110001;
OPTION_REG = 0b00000000;
TRISD = 0b00000000; // 0=output
PORTD = 0b00000000;
TRISB = 0b11111111; // 1=input
ANSEL = ANSELH = 0;
red_dc = 256;
grn_dc = 1;
blu_dc = 256;

init();
while (1)
{
}
}

void interrupt isr(void){
di();
if(T0IF){

if(pulse==0 || pulse>256 )
{
pulse=0;
if(red_dc>0){ RED_LED=ON; }
if(grn_dc>0){ GRN_LED=ON; }
if(blu_dc>0){ BLU_LED=ON; }
}
else if(pulse>0)
{
if(pulse==red_dc){RED_LED=OFF;}
if(pulse==grn_dc){GRN_LED=OFF;}
if(pulse==blu_dc){BLU_LED=OFF;}
pulse++;
}
}
init();
}
 

Hi ,

I have set it under INTCON. Please help

INTCON = 0b00100000; //T0IE = 1
 

One more suggation just make about clearing timer flag.... try this code.... I don't uses Hi tech -c .....but i had tried pwm in PIC....

void interrupt isr(void){
di();
if(T0IF){

if(pulse==0 || pulse>256 )
{
pulse=0;
if(red_dc>0){ RED_LED=ON; }
if(grn_dc>0){ GRN_LED=ON; }
if(blu_dc>0){ BLU_LED=ON; }
}
else if(pulse>0)
{
if(pulse==red_dc){RED_LED=OFF;}
if(pulse==grn_dc){GRN_LED=OFF;}
if(pulse==blu_dc){BLU_LED=OFF;}
pulse++;
}

T0IF=0; // Clearing timer flag
}
init();
}


Good Luck
 

i would like to drive 3 RGB LEDs, thats why i'm using software pwm. please advice
 

I don't know HiTech compiler in detail, but one thing seems suspicious.

Your variable pulse is declared as int. I don't know if HiTech assumes signed integers unless explicitly stated 'unsigned'. Anyway you declare variable 'pulse' as int. Did you check if it doesn't overflow?

I also think you need to zero this variable at some point.
 

One more suggation just make about clearing timer flag.... try this code.... I don't uses Hi tech -c .....but i had tried pwm in PIC....

void interrupt isr(void){
di();
if(T0IF){

if(pulse==0 || pulse>256 )
{
pulse=0;
if(red_dc>0){ RED_LED=ON; }
if(grn_dc>0){ GRN_LED=ON; }
if(blu_dc>0){ BLU_LED=ON; }
}
else if(pulse>0)
{
if(pulse==red_dc){RED_LED=OFF;}
if(pulse==grn_dc){GRN_LED=OFF;}
if(pulse==blu_dc){BLU_LED=OFF;}
pulse++;
}

T0IF=0; // Clearing timer flag
}
init();
}


Good Luck

I want to make a LED PWM circuit with using PIC. Please can you explain shortly how to do it?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top