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
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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
     }
}
 

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
Hi Tahmid...
Thank you so much...it very simple and work!
 

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
Changing the pwm >> Pwm_Init (freq); // freq = __?_ Hz
Changing the suty >> Pwm_Change_Duty(duty_ratio); // duty_ratio
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

ZASto

Advanced Member level 3
Joined
Jan 9, 2008
Messages
822
Helped
158
Reputation
321
Reaction score
151
Trophy points
1,323
Location
Messy Corner, 44°47'31.56"N, 20°28'9.16"E
Activity points
5,764
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 :)
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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:

ZASto

Advanced Member level 3
Joined
Jan 9, 2008
Messages
822
Helped
158
Reputation
321
Reaction score
151
Trophy points
1,323
Location
Messy Corner, 44°47'31.56"N, 20°28'9.16"E
Activity points
5,764
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.
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,752
Helped
1,796
Reputation
3,584
Reaction score
1,653
Trophy points
1,413
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,537
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.
 

Korawy

Member level 3
Joined
Sep 8, 2011
Messages
61
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,776
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top