Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18 ADC measureing issue

Status
Not open for further replies.

Prince Vegeta

Member level 5
Member level 5
Joined
May 29, 2012
Messages
84
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
2,011
Hi

I need to continuously measure feedback voltage from a boost output. I wrote a code but I don't see that it does it correctly...

It should increase/decrease duty cycle in certain circumstances (assumed by me - I am still waiting for someone else to calculate the real ones)...

here's the code:

Code:
void main() {
     PORTE = 0;
     TRISE = 0;
     TRISA = 1;
     PORTA = 0;
     
     k_op = 204;
     k_min = 179;
     k_max = 230;

     v_op = 819;
     v_min = 717;
     v_max = 922;
     
     
     CCP4CON = 10;
     CCPR4H = 0x9C;
     CCPR4L = 0x40;

     CCP4IE_bit = 1;
     CCP4IF_bit = 0;
     GIE_bit = 1;
     PEIE_bit = 1;

     T3CON = 0x41; //Prescaler 1:1, start Timer 1


     ADCON1 = 0x0E;           // Configure AN0 pins as analog
     ADCON0 = 1;              // Configure ADC to work with channel 0 (AN0-RA0)
     delay_ms(1);             // Required for ADC to work properly
     ADCON2 = 0x06;
     CMCON  |= 7;             // Disable comparators
     GO_bit = 1;              // Start convertion
     
     CCP1CON = 0x0C;          // CCP1 configured as PWM (RC2 pin)
     k = 127;
     PWM1_init (1000);
     PWM1_set_duty (k);    // initialize PWM with 80% duty cycle
     PWM1_start();

  
  
     while(1){
     //Do whatever else is required

     v = ADC_Read (0);          // read boost's output voltage

     /////* duty cycle control */////
     
     if ( v >= 819 ) {         // if output voltage >= reference value:
     
        if ( v >= 922 ) {      // if output voltage exceeds max voltge, then:
        
          k = k_max;           // output max duty cycle then,
          k--;                 // decrease duty cycle
          }
     }
     
     if ( v <= 819 ) {         // if output voltage <= reference value:
     
        if ( v <= 717 ) {      // if output voltage is below min voltage, then:
        
          k = k_min;           // output min duty cycle then,
          k++;                 // increase duty cycle
          
          }
     }
     

     
     




     }    // end while loop
}         // end main loop

never mind any undefined variables cuz i defined them (I only pasted this part of code), also some parts here does other things, I only need to check the ADC stuff.

sim in proteus.

thanks
 

Ensure that the variable v is defined as a 16-bit variable.

For the duty cycle update part, instead of this:

Code:
     if ( v >= 819 ) {         // if output voltage >= reference value:
     
        if ( v >= 922 ) {      // if output voltage exceeds max voltge, then:
        
          k = k_max;           // output max duty cycle then,
          k--;                 // decrease duty cycle
          }
     }
     
     if ( v <= 819 ) {         // if output voltage <= reference value:
     
        if ( v <= 717 ) {      // if output voltage is below min voltage, then:
        
          k = k_min;           // output min duty cycle then,
          k++;                 // increase duty cycle
          
          }
     }

Use this:

Code:
     if ( v >= 819 ) {         // if output voltage >= reference value:
     
        if ( v >= 922 ) {      // if output voltage exceeds max voltge, then:
        
          k = k_max;           // output max duty cycle then,

          }

          k--;                 // decrease duty cycle

     }
     
     if ( v <= 819 ) {         // if output voltage <= reference value:
     
        if ( v <= 717 ) {      // if output voltage is below min voltage, then:
        
          k = k_min;           // output min duty cycle then,

          }

          k++;                 // increase duty cycle
     }

     PWM1_Set_Duty(k); //Assign k to duty cycle

Hope this helps.
Tahmid.
 
you are right about this code! I don't know how I forgot to decrease/increase duty... I did it when max/min overridden but I should use it always.

so, will this be enough to measure and update duty?


about variable v as 16-bit, u mean define it as unsigned long?

____

I see some codes use #define stuff and I wonder what it does... also when the say something like c |=0 rather than c =0... do you know a thread/doc that explain these stuff?

thank you for your help.
 

you are right about this code! I don't know how I forgot to decrease/increase duty... I did it when max/min overridden but I should use it always.

so, will this be enough to measure and update duty?

You will probably need to slow it down more. Currently it's probably too fast.

about variable v as 16-bit, u mean define it as unsigned long?

No. Unsigned long is a 32-bit data type, for mikroC at least. Unsigned int is a 16-bit data type.

I see some codes use #define stuff and I wonder what it does... also when the say something like c |=0 rather than c =0... do you know a thread/doc that explain these stuff?

Read up on logical operators:
https://en.wikipedia.org/wiki/Operators_in_C_and_C++#Logical_operators
https://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/logical_operators.htm
https://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/bitwise_operators.htm

Check the mikroC PRO for PIC manual:

Hope this helps.
Tahmid.
 
Last edited:

it's too fast? you mean in real life or for the sake of Proteus simulation?

for simulation, I slowed the PWM freq to 1Khz to be able to notice the difference, it did help but not that much.

I choose 25Khz cuz it's too fast and it will update fast which is very useful in real life. So I went with "faster is better"... Is it wrong?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top