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:
Nzo
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:
In the code, I've mentioned were I'm stuck... any help or input would be much appreciated !!#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
}
}
Nzo
Last edited: