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.

Hi Sir..I have a good PMW code for share.. it works!! 100%. [mikroC complier]

Status
Not open for further replies.

boyguitar1

Member level 1
Joined
Nov 8, 2010
Messages
33
Helped
12
Reputation
24
Reaction score
12
Trophy points
1,288
Activity points
1,486
first of all...
:-DI just registered this website from Thailand it very good website for me....
I got many knowledge from here..So i give u back..
You can Sim on Proteus.


Code:
// Microcontroller : P16F877
// PWM module is set on RC2 (CCP1)  Pin No 17.
// Freq : 50kHz
// X-tal : 20MHz

 short i,j;
 int k;
 void check_display();
 void main()
 {
  PORTC = 0; // Set PORTC to $FF
  TRISC = 0; // PORTC is output
  TRISB = 0xFF;
  
  Pwm1_Init(50000); // Freq 50kHz (You can change u want)
  Pwm1_Start(); // Start PWM
  
  i = 0;
  j = 12.75;        // Start 5% duty
  k=0;
while (1)
{
  if(PORTB.F0 == 0)       // SW1 for increase Duty  5%
   {
   i=i+j;
   delay_ms(20);
   Pwm1_change_duty(i);
   T2CON.TMR2ON=1;
   Delay_us(10);
   k=k++;
   delay_ms(50);
   }

   if(PORTB.F1 == 0)       // SW1 for decrease Duty  5%
   {
   i=i-j;
   delay_ms(20);
   Pwm1_change_duty(i);
   T2CON.TMR2ON=1;
   Delay_us(10);
   k=k--;
   delay_ms(50);
   }
   
}   // Endless Loop
}  //main

Hope it good for u...
Thank you
boyguitar1:-D
 

Hi,
Very similar, but simple:
Code:
//Program to generate 40kHz output at RC2(CCP1) pin
//Microcontroller: Microchip PIC18452
//Language: C
//Compiler: mikroC v8.20
//Programmer: Tahmid

void main (void){
     TRISC = 0;
     PORTC = 0;
     ADCON1 = 7;
     T2CON = 0;
     TMR2 = 0;
     PWM1_Init(40000); //40kHz
//The value in brackets in PMW1_Init represents the frequency in Hz
     PWM1_Change_Duty(128); //50% duty cycle
// Choose Duty cycle as such:
// PWM_Change_Duty(x);
// x = ( (Duty Cycle in %) / 100) * 255
     PWM1_Start(); //Start PWM
     while (1){ //Loop forever
// Whatever else might be needed to be done while PWM is running
     }
}
 
Hi Tahmid...
Thank you so much...it very simple and work!
 

Changing the pwm >> Pwm_Init (freq); // freq = __?_ Hz
Changing the suty >> Pwm_Change_Duty(duty_ratio); // duty_ratio
 

Yes zasto, that's the method.
I've commented it in the sample code I provided:
Code:
     PWM1_Init(40000); //40kHz
//The value in brackets in PWM1_Init represents the frequency in Hz
     PWM1_Change_Duty(128); //50% duty cycle
// Choose Duty cycle as such:
// PWM_Change_Duty(x);
// x = ( (Duty Cycle in %) / 100) * 255

Hope this helps.
Tahmid.
 

Hi Tahmidm what I ment was changing frequency "on the fly", i.e. during program execution :)

In Proton+ it goes like this:

Code:
HPWM Channel , Dutycycle , Frequency

In none of Mikroelektronika's compilers there is no possibility to change the pwm frequency during program execution :)
 

Hi,
Why can't I do it?
Code:
program something
'Initialization
PWM1_Init(40000)
while true
   if PORTA.B0 then
      PWM1_Set_Duty(64) '25%
   end if
   if PORTA.B1 then
      PWM1_Set_Duty(192) '75%
   end if
wend
end.
That's in mikroBASIC.

I can have a register called duty and adjust the value of that as required in the code and then just assign that value to PWM1_Set_Duty(duty).

Since you brought up Proton, I posted the code in mikroBASIC, not mikroC.

Hope this helps.
Tahmid.
 

O, ok.
Yea, that's right. If I did need to change frequency, I could manually do it and make my own subroutine/function. But I get your point.
However, I never needed to change the frequency, so I had no problem.
 
Last edited:

Let's assume that you need to simulate an injector for engine, you need to change the RPM (Frequency) and quantity of fuel (Duty Cycle). No way with ME compilers. I've discussed this matter with people in ME in person (we are quite near each other), but that will not be 'fixed' soon, at least not until they perform an major overhaul of their libraries.
 

Yea, I got the point. :smile: Of course, since I'm using mikroBASIC or mikroC, I'll just have to make my custom routines for changing frequency when I need it.
 

Hi,
Yes, it's doable, but through custom routines as I mentioned:
I'll just have to make my custom routines for changing frequency when I need it.
But, it's not possible using the library functions in mikroC/mikroBASIC without making your custom routines as mentioned in the link you posted:
The standard Mikroelektronika PWM library only allows constant values for setting the PWM frequency. I needed to set them through variables in software so I created the functions shown in the code below.
 

Changing frequency in mikroC is not so impossible...Although we cant put a variable in this code PWM_INIT(constant),,But we can call this function more than once..i did it in my program,and i made it....Using a variable 1k resistor on RA0 (ADC input) i did change the frequncy every 100 ohm of the resistor....The Only disadvantage that this change is not stepped,,I mean that i cant make a cahnge in frequency for every ohm of the resistor,as that will make a long program especially when using a large frequency.....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top