+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Newbie level 4
    Points: 866, Level: 6

    Join Date
    May 2014
    Posts
    5
    Helped
    0 / 0
    Points
    866
    Level
    6

    ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    I need to change duty cycle with adc module. I wrote a program and it changes duty cycle with a warning ("adc conversion started before wait time has expired following previous conversion or channel change") in proteus.

    how can I fix this ?
    how can I add a subprogram for interrup?

    thanks for your help.

    here is my code:

    Code:
     #include	
    #include	
    #include	"delay.h"
    
    
    unsigned int
    read_adc(void){
    
    	unsigned int val;
    
    	ADIF = 0;
    	GO_nDONE = 1;
    
    	while ( GO_nDONE || !ADIF )	continue;
    
    	val = ADRESL;
    	val += ((unsigned int)ADRESH * 256);
            DelayMs(10);
    	return val;
    } //
    
    void main(void)
    {
    TRISA=0x01;
    TRISC=0x00;
    PORTC=0x00;
    
    
    PCFG3=1;
    PCFG2=1;
    PCFG1=1;
    PCFG0=0;
    
    ADFM=1;
    ADON=1;
    
    CHS2=0;
    CHS1=0;
    CHS0=0;
    
    ADCS1=1;
    ADCS0=1;
    
    
    ADIF=0;
    ADIE=1;
    PEIE=1;
    GIE=1;
    
    
    
    CCP1X=1;
    CCP1Y=1;
    T2CKPS1=0;
    T2CKPS0=0;
    TOUTPS3=1;
    TOUTPS2=1;
    TOUTPS1=0;
    TOUTPS0=0;
    CCP1M0=0;
    CCP1M1=0;
    CCP1M2=1;
    CCP1M3=1;
    TMR2ON=1;
    
    
    
    
    
    while(1)
    {
    unsigned int	adc_val;
    		float	i;
    
    		adc_val = read_adc();
    
    		i = (249./1023)*adc_val;
    		CCPR1L=i;
    		PR2=249; 
    		DelayMs(50);
    	}
    }

    Click image for larger version. 

Name:	adc2.JPG 
Views:	6 
Size:	66.8 KB 
ID:	116673Click image for larger version. 

Name:	adc1.JPG 
Views:	11 
Size:	223.6 KB 
ID:	116674
    Click image for larger version. 

Name:	adc2.JPG 
Views:	6 
Size:	66.8 KB 
ID:	116673Click image for larger version. 

Name:	adc1.JPG 
Views:	11 
Size:	223.6 KB 
ID:	116674ADC ile PWM PROJE ödevi.X.rar

    •   AltAdvertisement

        
       

  2. #2
    Full Member level 6
    Points: 2,302, Level: 11

    Join Date
    Apr 2015
    Posts
    366
    Helped
    72 / 72
    Points
    2,302
    Level
    11

    Re: ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    Move ADON=1; to after ADCS0=1; so you select the channel before turning on the ADC.
    CCPR1L=i; may cause a problem because CCP reg needs integer, not a float.

    You can find all the information in the HITECH manual.

    void interrupt()
    {
    //add your code here
    }
    Last edited by Vbase; 21st April 2015 at 02:01.



  3. #3
    Newbie level 4
    Points: 866, Level: 6

    Join Date
    May 2014
    Posts
    5
    Helped
    0 / 0
    Points
    866
    Level
    6

    Re: ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    I've tried your suggestions but nothing has changed :/

    I know the tittle of subprogram for interrupt. I meant which part of code should be in that.
    I tried something but mplab x shows errors.
    "
    main.c:19: error: (204) void function can't return a value
    main.c:77: error: (184) calling an interrupt function is illegal
    ??? -> unsigned int
    "

    - - - Updated - - -

    I changed my code and there is no error. interrupt was the problem.
    thanks for your reply @Vbase

    here is my last code.
    Code:
    #include	
    #include	
    #include	"delay.h"
    
    void main(void)
    {
    TRISA=0x01;
    TRISC=0x00;
    PORTC=0x00;
    
    PCFG3=1;
    PCFG2=1;
    PCFG1=1;
    PCFG0=0;
    
    ADCS1=1;
    ADCS0=1;
    
    ADON=1;
    ADFM=1;
    
    ADIF=0;
    ADIE=1;
    PEIE=1;
    GIE=1;
    
    CCP1X=1;
    CCP1Y=1;
    T2CKPS1=0;
    T2CKPS0=0;
    TOUTPS3=1;
    TOUTPS2=1;
    TOUTPS1=0;
    TOUTPS0=0;
    CCP1M0=0;
    CCP1M1=0;
    CCP1M2=1;
    CCP1M3=1;
    TMR2ON=1;
    
    while(1)
    {
    CHS2=0; 
    CHS1=0;
    CHS0=0;
    DelayMs(25);
    ADIF = 0;
    GO_nDONE=1; 
    while(GO_nDONE);
                    unsigned int	val;
    		int	i;
    
    		;
            val = ADRESL;
    	val += ((unsigned int)ADRESH * 256);
            DelayMs(10);
    		i = (249./1023)*val;
    		CCPR1L=i;
    		PR2=249; 
    		DelayMs(50);
    	}
    }
    
    static void interrupt
    kesme(void)
    {
    if(ADIF) 
    {
    ADIF=0;
    }
    }
    Last edited by osman6464; 21st April 2015 at 09:24.



    •   AltAdvertisement

        
       

  4. #4
    Banned
    Points: 12,436, Level: 26

    Join Date
    Apr 2013
    Posts
    2,528
    Helped
    538 / 538
    Points
    12,436
    Level
    26

    Re: ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    I can help with mikroC code because it has PWM library.



  5. #5
    Newbie level 4
    Points: 866, Level: 6

    Join Date
    May 2014
    Posts
    5
    Helped
    0 / 0
    Points
    866
    Level
    6

    Re: ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    Problem has solved. thank you. @milan.rajik



    •   AltAdvertisement

        
       

  6. #6
    Newbie level 5
    Points: 58, Level: 1

    Join Date
    Apr 2015
    Posts
    8
    Helped
    0 / 0
    Points
    58
    Level
    1

    Re: ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    Osman please tell us how did you solve this problem I have the same .



  7. #7
    Newbie level 4
    Points: 866, Level: 6

    Join Date
    May 2014
    Posts
    5
    Helped
    0 / 0
    Points
    866
    Level
    6

    Re: ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    Quote Originally Posted by Cüneyt Ta View Post
    Osman please tell us how did you solve this problem I have the same .

    hi cüneyt . I had used interrupt codes but I had not created an interrupt subprogram. so I added it.

    Can you post your code and proteus file? I can think about it.

    I improved my code. this is the last program that I compiled.

    Code:
    #include	
    #include	
    #include	"delay.h"
    
    void main(void)
    {
    TRISA=0x01;
    TRISC=0x00;
    PORTC=0x00;
    
    PCFG3=1;
    PCFG2=1;
    PCFG1=1;
    PCFG0=0;
    
    ADCS1=1;
    ADCS0=1;
    
    ADON=1;
    ADFM=1;
    
    ADIF=0;
    ADIE=1;
    PEIE=1;
    GIE=1;
    
    CHS2=0;
    CHS1=0;
    CHS0=0;
    DelayMs(25);
    
    CCP1X=1;
    CCP1Y=1;
    T2CKPS1=0;
    T2CKPS0=0;
    TOUTPS3=1;
    TOUTPS2=1;
    TOUTPS1=0;
    TOUTPS0=0;
    CCP1M0=0;
    CCP1M1=0;
    CCP1M2=1;
    CCP1M3=1;
    TMR2ON=1;
    
    while(1)
    {
    
    ADIF = 0;
    GO_nDONE=1; 
                    
    	}
    }
    
    static void interrupt
    kesme(void)
    {
    if(ADIF) 
    {
                    unsigned int	val;
    		int	i;
    
                    val = ADRESL;
                    val += ((unsigned int)ADRESH * 256);
                    DelayMs(50);
    		i = (249./1023)*val;
    		CCPR1L=i;
    		PR2=249;
    		DelayMs(50);
                    ADIF=0;
    }
    }

    I need to improve this code. I need to add 1 second of timer for "GO_nDONE=1;" bit but I haven't made it :D



  8. #8
    Newbie level 5
    Points: 58, Level: 1

    Join Date
    Apr 2015
    Posts
    8
    Helped
    0 / 0
    Points
    58
    Level
    1

    Re: ADC controlling PWM Duty Cycle with 16f877a ( hitech C )

    Hi thank you for your codes I have given my files as proteus and MicroC in the thread " Result is not defined in function" "https://www.edaboard.com/thread336080.html" I think my problems are a little hard to solve .Now I read and learn about ADC PWM and PID .If I can not solve I think to ask Tahmid via this forum .But I dont know how can I contact to him.Bye



--[[ ]]--