Using PWM and ADC at the same time

Home Forums Microcontrollers PIC Microcontroller Using PWM and ADC at the same time

Tagged: , , ,

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #11756
    mustunsultan
    Participant

    Hello,
    I am new to PIC Microcontrollers, but have gone through all the tutorials.  I can write codes on my own for PWM and ADC seperately. But when I try to use both at the same time I don’t get any output signal at CCP1 *checked with Proteus also*

    I am trying to use 2 buttons RC4 and RC5 to increase and decrease the PWM duty cycle. And use ADC for lighting up LEDs , but I can get any signal out of CCP1.

    Here is the code I tried:

    unsigned int ADC;
    
    void main()
    {
      short duty1 = 16;
    
      CMCON = 0x07;
      ADCON1 = 0x80;
    
      TRISA = 0xFF;    //input analog signal
      TRISC = 0x30;    //RC4 RC5 input button, RC2 output PWM
      TRISB = 0x00;    //output MSB's of ADC
      TRISD = 0x00;    //output LSB's of ADC
    
      //ADC Function
      do
      {
        ADC = ADC_Read(1);
        PORTB = ADC;
        PORTD = ADC>>2; 
      }
      while(1);
    
      ////////////////////////////////////////////////
    
      //PWM Function
      PWM1_Init(5000);
      PWM1_Start();
      PWM1_set_duty(duty1);
    
      while(1) 
      {
        if (PORTC.F4 == 0) 
        {
          Delay_ms(1);
          duty1++;
          PWM1_set_duty(duty1);
        }
        if (PORTC.F5 == 0)
        {
          delay_ms(1);
          duty1--;
          PWM1_set_duty(duty1);
        }
        delay_ms(100);
      }
    }
    
    #11770
    Ligo George
    Keymaster

    Dear Mustunsultan,

    There is no meaning in combining some codes without any logic.

    Try like this.

    unsigned int ADC;
    
    void main()
    {
      short duty1 = 16;
    
      CMCON = 0x07;
      ADCON1 = 0x80;
    
      TRISA = 0xFF;    //input analog signal
      
      //PWM Function 
      PWM1_Init(5000); 
      PWM1_Start();
      //ADC Function
      do
      {
        ADC = ADC_Read(1);
        PWM1_set_duty(ADC/4);
        delay_ms(400);
      }while(1);
    }
    #11782
    mustunsultan
    Participant

    Thank you Sir!
    It really helped to understand the code well.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
>