Zerox
Zerox

Reputation: 15

Continuously changing PWM with ADC on PIC18F452

I am writing code for generating continuously generated PWM with a potentiometer.The code is given below. The code is working fine, but in some scenarios, it stops working.

  1. I get a changing PWM when I give 0–3 volts from the potentiometer. But when input exceeds 3.15 volts, the output on the oscilloscope becomes flat, and the led on the ccp1 becomes flat.
  2. If I provide an input of 3.15 volts or above at start, then the ADC doesn't work, but I am getting proper output on PORTB and PORTD (used for checking 10 bit ADC output). And in this case when I move potentiometer it is not working.
  3. DEBUG window in Proteus is showing proper ADC value (CCPR1L from 0-255) for 0-5V but LED on CCP1 does not work.
int rd_adc()
{
    int x;
    int y;
    TRISB = 0;
    TRISD = 0;
    //trise.b1 = 1;
    ADCON0 = 0x81;   // Fosc/64, Channel 0, A/D is on
    ADCON1 = 0xCE;   // Right justified, Fosc/64, AN0 =Analog

    ADCON0.GO = 1;  //Start Converting
    while (ADCON0.GO == 1)  //wait for completion of conversion
        ;
    PORTB = ADRESH;  //Display low byte on PORT C
    PORTD = ADRESL;  //Display high byte on PORT D
    x = ADRESH;
    x = (ADRESH << 8) | (ADRESL);

    y = x * 0.2492; //for 0.00488x51(for making 10 bit x value within range of 0-255
    //x = x * 51;
    return y;
}

void main()
{
    TRISA.F0 = 1;
    TRISC.F2 = 0;
    PR2 = 156;
    CCP1CON = 0x0C;
    T2CON = 0x02;
    TMR2 = 0;            //clr timer reg
    T2CON.TMR2ON = 1;
    while(1)
    {
        CCPR1L = rd_adc();
        PIR1.TMR2IF = 0;         //clr timer flag
        while (PIR1.TMR2IF == 0)        // wait for end of period
            ;
    }
}

Upvotes: 1

Views: 89

Answers (0)

Related Questions