Proportional Controller on a PIC16F9690

Thread Starter

NzoBoss

Joined Jun 2, 2010
3
Hello fellow EE Brothers,

I'm trying to code a very simple proportional controller for the first steps of my internship.

I'm acquiring an analog continuous signal let's say with voltage between 0 and 5V. I do it through AN0... and then I want to generate a PWM... my problem is that I don't know how to translate the output of the DAC into PWM multiplied by the proportional factor.

Here's my code, I'm using the CC5X free compiler:

#include "int16CXX.H"


// pic initialisation routine
void init(void)
{
// CONFIGURATION DES PORTS A/B/C
ANSEL =0b.0000.0001; // A0(AN0) en ANA (les autres en I/O)
ANSELH=0b.0000.0000; // PORTA/B/C en I/O
TRISA =0b.1111.1111; // configuration portA : XXEE EEEE
TRISB =0b.0111.1111; // configuration portB : SEEE XXXX
TRISC =0b.1111.1100; // configuration portB : EEEE EESS

// CONFIGURATION DU CAN
ADCON1=0; // TDA=100ns
ADCON0=1; // active le CAN (AN0, ref à 5V)

return;
}


// main routine
void main()
{
init(); // pic initialization

for(...)
{
// 1 - acquisition d'un échantillon
ADCON0.1=1; // lance une conversion sur AN0
while(ADCON0.1==1); // attend la fin de l'acquisition sur AN0

/*
* PWM registers configuration
* Fosc = 20000000 Hz
* Fpwm = 4006.41 Hz
* Duty Cycle = 50 %
* Resolution is 10 bits
* Prescaler is 16
*/
PR2 = 0b01001101 ;
T2CON = 0b00000111 ;
CCPR1L = 0b00100110 ;
CCP1CON = 0b00111100 ;

// This is were I'm stuck... ?? = ADRESH; // émission de l'acquisition sur AN0


// 2 - détection de la saturation
if(ADRESH>0xA0) PORTC.1=1; else PORTC.1=0; // > 50% : LED verte
if(ADRESH==0xFF) PORTC.0=1; else PORTC.0=0; // saturation : LED rouge
}
}
In the code, I've mentioned were I'm stuck... any help or input would be much appreciated !! :)

Nzo
 
Last edited:

ELECTRONERD

Joined May 26, 2009
1,147
I'm relatively new to C programming myself, but the trick is to think like a compiler or computer. Run through the code and see what the compiler would do. Also, does your free compiler have a simulator? I know that MPLAB does.
 

Thread Starter

NzoBoss

Joined Jun 2, 2010
3
Hello electronerd, yes I'm using MPLAB too... I did not know there were simulators... that's good to know. CC5X is just to compile from C code to the pic... and is called automatically from MPLAB.

My problem is how to go from ADRESH the output of the Analog-To-Digital converter... to the PWM... I want ADRESH * P(some gain) = PWM Output on P1A.. Does anyone know how to do this ?
 

John P

Joined Oct 14, 2008
2,026
Is it possible that you mean the PIC16F690? I don't think the PIC16F9690 exists.

Assuming it's the PIC16F690, what have you found in section 11.0 of the user manual?
 

Thread Starter

NzoBoss

Joined Jun 2, 2010
3
Hello John, yes you're right it's the PIC16F690.. I've looked at section 11 on the enhenced PWM but still I not able to figure out how to output the results of the DAC i.e. ADRESH through PWM... ? Anyone ?
 
Top