+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Member level 4
    Points: 1,198, Level: 7

    Join Date
    Jun 2009
    Location
    japan
    Posts
    69
    Helped
    0 / 0
    Points
    1,198
    Level
    7

    Code for dimming a LED using ADC and PWM

    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?

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 8,424, Level: 21

    Join Date
    Jun 2009
    Location
    Japan
    Posts
    1,063
    Helped
    282 / 282
    Points
    8,424
    Level
    21

    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.

    Quote Originally Posted by jason1987
    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?



    •   AltAdvertisment

        
       

  3. #3
    Member level 4
    Points: 1,198, Level: 7

    Join Date
    Jun 2009
    Location
    japan
    Posts
    69
    Helped
    0 / 0
    Points
    1,198
    Level
    7

    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




    #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

    }



    •   AltAdvertisment

        
       

  4. #4
    Advanced Member level 3
    Points: 5,367, Level: 17

    Join Date
    Jul 2009
    Posts
    734
    Helped
    82 / 87
    Points
    5,367
    Level
    17

    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

    // 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<

    __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 if (i LED=1;
    else
    LED=0;
    // wait8; //wait to charge up Chold
    }
    }

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

    here is the schematic and output waveform.



  5. #5
    Member level 4
    Points: 1,198, Level: 7

    Join Date
    Jun 2009
    Location
    japan
    Posts
    69
    Helped
    0 / 0
    Points
    1,198
    Level
    7

    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



    •   AltAdvertisment

        
       

  6. #6
    Member level 4
    Points: 1,198, Level: 7

    Join Date
    Jun 2009
    Location
    japan
    Posts
    69
    Helped
    0 / 0
    Points
    1,198
    Level
    7

    define adc_bits 8

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



  7. #7
    Full Member level 4
    Points: 2,211, Level: 10

    Join Date
    Apr 2009
    Location
    Hyderabad, India
    Posts
    217
    Helped
    5 / 5
    Points
    2,211
    Level
    10

    htc.h adc

    Hello jason,

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

    http://www.microchip.com/stellent/id...param=en529514

    Please let me know how's going?

    Regards
    Chanchal


    1 members found this post helpful.

  8. #8
    Member level 4
    Points: 1,198, Level: 7

    Join Date
    Jun 2009
    Location
    japan
    Posts
    69
    Helped
    0 / 0
    Points
    1,198
    Level
    7

    microchip htc.h pwm

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



--[[ ]]--