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.

Code for dimming a LED using ADC and PWM

Status
Not open for further replies.

jason1987

Member level 4
Joined
Jun 22, 2009
Messages
69
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
japan
Activity points
1,745
hey guy i am jason.. i current doing a project which is dim down the LED using ADC N PWM to generate a wave form. to dim the led.. i hv connect through a variable resistor.. so if possible can help?
 

pwm 1bit adc

Hello!

If you have means to get a PWM wave, you don't need a variable resistor.
Choose the resistor that gives you the maximal illumination in DC, and then
use PWM to dim the LED. The good thing is that the average current crossing
the LED will be proportional to the PWM duty cycle.

Dora.

jason1987 said:
hey guy i am jason.. i current doing a project which is dim down the LED using ADC N PWM to generate a wave form. to dim the led.. i hv connect through a variable resistor.. so if possible can help?
 

void adc_init(void)

everyting i am okie just the software i don't know how to write.. give mi some hint.. my job is to develop firmware for ADC to acquire potentiometer setting and develop firmware for PWM generation and integrate ADC acquistion to PWM generation and lastly develop current driver circuit for LED.. the code i have no idea how to write.. give mi some guide.. and thank for replying..
the code i take from internet.. gt error.. why? i have attach the file too


#include <c8051f200.h>




#define SYSCLK 2000000
#define PULSE_WIDTH 35000d

sbit LED = P2^4;
sbit PIN = P3^7;


void PORT_Init ();
void Timer0_Init ();
void Timer0_ISR ();
void ADC_Init ();

void main () {

unsigned int adc_value=0;
unsigned char bright; // your brightness - range 0 (dark) to 255 (lght)

// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;

PORT_Init ();
Timer0_Init ();
ADC_Init();
EA = 1;
while (1) {




ADCINT = 0;
AMX0SL = 0x3f;
ADBUSY = 1;
while (ADCINT ==1)
{
}
ADCINT = 0;
return;

adc_value = ADC0H;
PIN = adc_value;
}
}

INT timer_int() ;
{
static unsigned char i = 0;

// do things like clear interrupt source, reload the timer

if(i++ < bright)
// turn on LED
else
// turn off LED

}


void PORT_Init ()
{
PRT2CF |= 0x10;
P3MODE = 0x80;
}
void Timer0_Init ()
{
TMOD = 0x01;
CKCON &= ~0x08;
TH0 = 00;
TL0 = 00;
TR0 = 0;
TF0 = 0;

ET0 = 1;
}




void ADC_Init ()
{
AMX0SL = 0x3f;
ADC0CF = 0x60;
ADC0CN = 0xC0;
REF0CN = 0x03;
while (PIN);
}



void Timer0_ISR (void) interrupt 5
{
TF0 = 0;
LED = ~LED;
}
void ADC_ISR (void)
{
ADCINT = 0; // clear ADC interrupt
LED = 1; // change state of LED

}
 

void timer0_isr (void) interrupt

here is what I wrote for an almost identical purpose. it takes an input signal, adc it and use that to control the intensity of an led.

it is done in picc/hi-tide for pic but the basic idea is the same.

===========code===========
#include <htc.h>

// hardware configuration

#define Vin_a GPIO0 //analyg in on GPio0;
#define LED GPIO5 //led out on gpio5;
// end hardware configuration

#define GETBIT(var,bit) (((var)>>(bit))&1) /* read bit from var */
#define SETBIT(var,bit) ((var)|=(1<<(bit))) /* set bit from var to 1 */
#define CLRBIT(var,bit) ((var)&=(~(1<<(bit)))) /* clear bit from var (set it to 0) */

#define Vin_d_th ((long) 2500*1023/5000) //input voltage threshold to turn on the LED. from 0-1023 (10bit adc)
#define wait2 NOP();NOP()
#define wait4 wait2;wait2
#define wait8 wait4;wait4
#define adc_bits 8 //how many bits in pwm stepping
#define Length (1<<adc_bits) //total length of pwm, should be consistent with sampling bits - 10 here


__CONFIG(MCLRDIS & INTIO & WDTDIS & BORDIS);

void
main(void)
{
int Vin_d; //adc result for Vin_a
int i=0; //where we are in length?

CMCON=7; //turn off comparators;
TRISIO=0b1; //input on GPIO0, all others digital output;
ADCON0=0b10000001; //right adjusted, fosc/2, Vdd reference, AN0 analog input, adc starts right away and will always be on;
ANSEL=0b1010001; //1/16th Fosc, analog input on GPIO0;
LED=0;
while (1){
//TODO Auto-generated main function
GODONE=1; //start the adc process
while (GODONE) ; //wait for adc to complete
Vin_d=((ADRESH<<8)+(ADRESL))>>(10-adc_bits); //read both the msb and lsb.
for (i=0; i<Length; i++)
if (i<Vin_d)
LED=1;
else
LED=0;
// wait8; //wait to charge up Chold
}
}

=======end code===========

here is the schematic and output waveform.
 

pic24 pwm not consistent

i dunno what your code is doing.. i have done the ADC part but the PWM i not very sure about it.. so i need some guide
 

define adc_bits 8

i really need help.. abt my project.. is urgent
 

htc.h adc

Hello jason,

Just go through the link you will get some information for Dimming & Lighting control.

**broken link removed**

Please let me know how's going?

Regards
Chanchal
 

    jason1987

    Points: 2
    Helpful Answer Positive Rating
microchip htc.h pwm

erm i have nt done anyting yet.. can help mi? i hv do research but i dun understand it
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top