1

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
            ;
    }
}
Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
Zerox
  • 15
  • 5
  • 1
    Your code is ... erm ... sub-optimal. The indentation is inconsistent. The outer `while(1)` in `rd_adc()` has no effect. Even if commented out, `delay_ms(150); //wait for one quarter of second` has a plain wrong comment. You say you use ports B and C for debug output, but the source uses B and D. The shown code is not complete due to missing include directives. Please post always well formatted, complete and minimal source code. – the busybee Jul 13 '23 at 06:52
  • The next debug step will be the output of `y` instead of the ADC result. Make sure the range is really mapped to 0 to 255. – the busybee Jul 13 '23 at 06:54
  • @thebusybee i have deleted while in rd_adc and delay lines from code and my ports are portb and portb but still problem exists.When i keep potentiometer above 3.15 i get flat wave on oscilloscope but leds remained on.And when i drag potentiometer down from 3.15v too it shows nothing and led remains on even at 0v...My CCPR1L values are changing continously in watch window of proteus from 0-255(biunary) with Potentiometer – Zerox Jul 13 '23 at 07:53
  • "Divide and conquer" and "model a reason and verify/falsify it" are the right approaches here. This is "bread and butter" in debugging. -- So the problem is in the generation of the PWM output signal, not in the acquisition of the analog value and its conversion, right? Your next debug step is a test program that reproduces the issue with a timed sequence of different values for `CCPR1L`. Find the specific value that triggers the problem. -- [Edit] your question and add new information and your findings to it. Comments are not for such and will not be read. – the busybee Jul 13 '23 at 08:16
  • 1
    OT: Oh, and I could not stand your source, it is formatted now. Please keep it formatted. – the busybee Jul 13 '23 at 08:24
  • This looks like a period issue. What is the level of output in the oscilloscope, low or high? Consider adding the period, frequency and duty cycle values that you calculated. – Kozmotronik Jul 13 '23 at 10:00

0 Answers0