Help with Voltage Regulation

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
tender95
Posts: 1
Joined: 01 Nov 2018 12:58

Help with Voltage Regulation

#1 Post by tender95 » 01 Nov 2018 13:07

Hello, I am new to embedded system.
I am using pic18f45k22 mikroc compiler and I want to implement a voltage regulation.
The regulations I need are P and PI. These are my values:
U1=1000vdc,fc=2000hz,U2=0-500V, I2=100A max.
tc=500micro s and a step of 2V as far as I calculated.

I implemented PWM and ADC and I see it through an osciloscope also. I need help or at least an inspiration on how to think and implement this regulator. Thank you !

P.S. Sorry for all these comments in my code.

This is my current status:

sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

unsigned short current_duty, old_duty, current_duty1, old_duty1;
int i=1;
//const int U1=1000, fc=2000, I2=100, Tc=1/fc, Up=0, Umas=0, eroare=0, Kp=0, Ki=0,Reg=0,Reg_old=0;
float res=0;
unsigned int voltage=0, potentiometer_value=0;
//int U1=1000, fc=2000, U2=0, I2=100,eroare=0,Up,Umas=0;
char txt1[15];

void InitMain() {
ANSELA = 0xFF; // Configure AN pins as digital
ANSELB = 0;
ANSELC = 0;

//PORTA = 255;
LATB = 0;
TRISA = 0xFF; // set PORTB to 0
TRISB = 0; // designate PORTB pins as output
LATC = 0; // set PORTC to 0
TRISC = 0; // designate PORTC pins as output
PWM1_Init(2000); // Initialize PWM1 module at 5KHz
PWM2_Init(2000); // Initialize PWM2 module at 5KHz

Lcd_Init(); // Initialize Lcd

Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
//Lcd_Out(1,3,"ADC"); // Write text in first row
//Lcd_Out(2,1, "Voltage:");
//Lcd_Out(2,8, "V");
//Lcd_Out(2,1, txt1);

}

void main() {
InitMain();
current_duty = 16; // initial value for current_duty
current_duty1 = 16; // initial value for current_duty1

PWM1_Start(); // start PWM1
PWM2_Start(); // start PWM2
PWM1_Set_Duty(current_duty); // Set current duty for PWM1
PWM2_Set_Duty(current_duty1); // Set current duty for PWM2

while (1) { // endless loop

while (current_duty1<1020)
{
current_duty1++;
PWM2_Set_Duty(current_duty1);
delay_ms(40);
}
if (current_duty1>1020) current_duty1=0;


if (RA0_bit) { // button on RA0 pressed
Delay_ms(40);
current_duty++; // increment current_duty
PWM1_Set_Duty(current_duty);
}

if (RA1_bit) { // button on RA1 pressed
Delay_ms(40);
current_duty--; // decrement current_duty
PWM1_Set_Duty(current_duty);
}

if (RA2_bit) { // button on RA2 pressed
Delay_ms(40);
current_duty1++; // increment current_duty1
PWM2_Set_Duty(current_duty1);
}

if (RA3_bit) { // button on RA3 pressed
Delay_ms(40);
current_duty1--; // decrement current_duty1
PWM2_Set_Duty(current_duty1);
}

Delay_ms(20);
//Lcd_Out(1,3,"ADC"); // Write text in first row
potentiometer_value = ADC_Read(0); //ADC_Get_Sample(2);
res = ( potentiometer_value * 5.0 ) / 1023.0; // Get 10-bit results of AD conversion
//voltage = ( potentiometer_value * 5000 ) / 1023; // Get 10-bit results of AD conversion
//res=res/1000;


// Get 10-bit results of AD conversion
//FloatToStr_FixLen(res, txt1, 4); // Convert Unsigned Float to Str
/*IntToStr(res, txt1);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,3,"ADC"); // Write text in first row
Lcd_Out(2,1, txt1);
Lcd_Out(2,8,"V");

Delay_ms(20);*/
PORTB = potentiometer_value ;
PORTC = potentiometer_value>>8;
if (ADC_Read(0) > 512)
current_duty++;

if (ADC_Read(0) < 512)
current_duty--;

/*Reg = Reg_old + Kp;
Reg_old = Reg;
eroare = Up - Umas;
P=Kp;
Pi = Kp(1+1/Ti*s);*/

}
}

Post Reply

Return to “PIC PRO Compilers”