RadioLocman.com Electronics ru
Advanced Search +
  
Go Back   RadioLocman Forum > Electronics and ... > Microcontrollers (MCU)

Microcontrollers (MCU) Application and programming of MCUs, Evaluation Kits and Demonstration Boards, Programmers.

Reply
 
Thread Tools Search this Thread
Old 02-13-2016, 08:09 AM   #1
usama14
Junior Member
 
Join Date: Feb 2016
Posts: 1
usama14 is on a distinguished road
Default PWM code help for full bridge PIC18F4550 xC8

Hello everyone. Ive written this code for the PWM control of the full bridge dc-dc converter. My switching freq is 100kHz(10us period) and I'm using PIC18F4550 and "xC8" compiler. I have made various cases for the selection depending upon the A/D conversion from the feedback resistive network, as I want to maintain 330Vdc at the output. I am also using the PLL functionality to make use of the 48MHz from the 4MHz crystal. I hope you all will understand the code as it is clearly written in a very easy possible way.
The problem is that during the simulation and debugging, I found that the program is taking about 39 cycles i.e., about 17us for the A/D conversion and I am reading this after every cycle. By the methodology I used, this much delay is causing my switching frequency to change a lot. I want a constant switching frequency of 100kHz, i.e, the time period must remain 10us that I've ensured in each case statement.
Any effective ideas to solve this problem?
Thankyou!
Code:
#include<xc.h>
#define _XTAL_FREQ   4000000
unsigned int answer=0;
unsigned int x=0;

void main()
{
	TRISC = 0;
	PORTC = 0;
	TRISB=0;
	PORTB=0;
	RB0=0;
	RC1=0;
	RC2=0;
	ADCON0=0b00000001;
	ADCON1=0b00000000;
	ADCON2=0b10000100;
	
while (1)
	{
	unsigned int temp=0;
	GODONE=1;
	while(GODONE==1);
	temp=ADRESH;
	temp=ADRESH<<2;
	temp=temp|ADRESL;
	answer= 4.883*temp;
	RC1=0;
	if(answer>3771)
	 {
		 x=x+1;
	} 
	else x=x-1;
	
 switch (x)
{
    case 1 : 
 	{
	 	RC1 = 1;
       __delay_us(18);
	RC1 = 0;
	__delay_us(42);
	RC2=1;
	__delay_us(18);
	RC2=0;
	__delay_us(42);
       break;
  }     
           case 2 : 
 	{
	 	RC1 = 1;
       __delay_us(19.5);
	RC1 = 0;
	__delay_us(41.5);
	RC2=1;
	__delay_us(19.5);
	RC2=0;
	__delay_us(41.5);
       break;
  }     
           case 3 : 
 	{
 	RC1 = 1;
       __delay_us(21);
	RC1 = 0;
	__delay_us(39);
	RC2=1;
	__delay_us(21);
	RC2=0;
	__delay_us(39);
       break;
  }     
           case 4 : 
 	{
	 RC1 = 1;
       __delay_us(22.5);
	RC1 = 0;
	__delay_us(38.5);
	RC2=1;
	__delay_us(22.5);
	RC2=0;
	__delay_us(38.5);
       break;
  }     
           case 5 : 
 	{
	 	RC1 = 1;
       __delay_us(24);
	RC1 = 0;
	__delay_us(36);
	RC2=1;
	__delay_us(24);
	RC2=0;
	__delay_us(36);
       break;
  }     
           case 6 : 
 	{
	 	RC1 = 1;
       __delay_us(25.5);
	RC1 = 0;
	__delay_us(35.5);
	RC2=1;
	__delay_us(25.5);
	RC2=0;
	__delay_us(35.5);
       break;
  }     
           case 7 : 
 	{
	 	RC1 = 1;
       __delay_us(27);
	RC1 = 0;
	__delay_us(33);
	RC2=1;
	__delay_us(27);
	RC2=0;
	__delay_us(33);
       break;
  }     
           case 8 : 
 	{
	 	RC1 = 1;
       __delay_us(28.5);
	RC1 = 0;
	__delay_us(32.5);
	RC2=1;
	__delay_us(28.5);
	RC2=0;
	__delay_us(32.5);
       break;
  }     
           case 9 : 
 	{
	 	RC1 = 1;
       __delay_us(30);
	RC1 = 0;
	__delay_us(30);
	RC2=1;
	__delay_us(30);
	RC2=0;
	__delay_us(30);
       break;
  }     
           case 10 : 
 	{
	 	RC1 = 1;
       __delay_us(31.5);
	RC1 = 0;
	__delay_us(29.5);
	RC2=1;
	__delay_us(31.5);
	RC2=0;
	__delay_us(29.5);
       break;
  }     
           case 11 : 
 	{
	 	RC1 = 1;
       __delay_us(33);
	RC1 = 0;
	__delay_us(27);
	RC2=1;
	__delay_us(33);
	RC2=0;
	__delay_us(27);
       break;
  }     
           case 12 : 
 	{
	 	RC1 = 1;
       __delay_us(34.5);
	RC1 = 0;
	__delay_us(26.5);
	RC2=1;
	__delay_us(34.5);
	RC2=0;
	__delay_us(26.5);
       break;
  }     
           case 13 : 
 	{
	 	RC1 = 1;
       __delay_us(36);
	RC1 = 0;
	__delay_us(24);
	RC2=1;
	__delay_us(36);
	RC2=0;
	__delay_us(24);
       break;
  }     
           case 14 : 
 	{
	 	RC1 = 1;
       __delay_us(37.5);
	RC1 = 0;
	__delay_us(23.5);
	RC2=1;
	__delay_us(37.5);
	RC2=0;
	__delay_us(23.5);
       break;
  }     
           case 15 : 
 	{
	 	RC1 = 1;
       __delay_us(39);
	RC1 = 0;
	__delay_us(21);
	RC2=1;
	__delay_us(39);
	RC2=0;
	__delay_us(21);
       break;
  }     
           case 16 : 
 	{
	 	RC1 = 1;
       __delay_us(40.5);
	RC1 = 0;
	__delay_us(20.5);
	RC2=1;
	__delay_us(40.5);
	RC2=0;
	__delay_us(20.5);
       break;
  }     
           case 17 : 
 	{
	 	RC1 = 1;
       __delay_us(42);
	RC1 = 0;
	__delay_us(18);
	RC2=1;
	__delay_us(42);
	RC2=0;
	__delay_us(18);
       break;
  }     
           case 18 : 
 	{
	 	RC1 = 1;
       __delay_us(43.5);
	RC1 = 0;
	__delay_us(17.5);
	RC2=1;
	__delay_us(43.5);
	RC2=0;
	__delay_us(17.5);
       break;
  }     
           case 19 : 
 	{
	 	RC1 = 1;
       __delay_us(45);
	RC1 = 0;
	__delay_us(15);
	RC2=1;
	__delay_us(45);
	RC2=0;
	__delay_us(15);
       break;
  }     
           case 20 : 
 	{
	 	RC1 = 1;
       __delay_us(46.5);
	RC1 = 0;
	__delay_us(14.5);
	RC2=1;
	__delay_us(46.5);
	RC2=0;
	__delay_us(14.5);
       break;
  }     
           case 21 : 
 	{
	 	RC1 = 1;
       __delay_us(48);
	RC1 = 0;
	__delay_us(12);
	RC2=1;
	__delay_us(48);
	RC2=0;
	__delay_us(12);
       break;
  }     
       default :
       {
	       RC1=0;
	       RC2=0;
	      } 
}	
	
}
}
usama14 is offline   Reply With Quote
Old 02-17-2016, 06:01 PM   #2
symbol
Senior Member
 
Join Date: Oct 2013
Posts: 128
symbol is on a distinguished road
Default

Use the built-in PWM module of the uC, instead of manually assembling with delays.
symbol is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2.5 kW Full bridge boost converter Error_Amplifier Projects and Advices 1 02-10-2016 04:43 PM
Use of IR2110 for three phase Full bridge IGBT driving jean12 Projects and Advices 3 01-10-2016 07:09 PM
Discussion: Current Source For LED Microscope Illuminator Provides Full-Spectrum Light Radiolocman Robot Projects and Advices 1 01-13-2015 03:28 PM
Discussion: Simple solutions for a single-device PWM waveform generator Radiolocman Robot Microcontrollers (MCU) 1 06-17-2014 06:27 PM
Discussion: H-bridge paves new ways for LED lighting Radiolocman Robot Projects and Advices 1 04-01-2014 06:07 PM

All times are GMT. The time now is 10:04 AM.




Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Slices ↓
Radiolocman facebook Radiolocman twitter Radiolocman google plus