PIC18f45k22 MikroC compiler PWM contorl

Status
Not open for further replies.

Karolina_1

Member level 3
Joined
Mar 10, 2017
Messages
67
Helped
0
Reputation
0
Reaction score
1
Trophy points
8
Activity points
639
hi, everyone. i am trying to control PWM modulation. But i am having some errors. I am using mikroC compiler and pic18f45k22. my code is given below can u possibly help. pleaseee. :|:|

Code:
void InitMain() {
PORTB = 0; // set PORTB to 0
TRISB = 0xF0; // designate PORTB pins as output
PORTC = 0; // set PORTC to 0
TRISC = 0xFF; // designate PORTC pins as output
Pwm1_Init(5000); // Initialize PWM1 module at 5KHz
PWM2_Init(5000); // Initialize PWM2 module at 5KHz

}

void main(){
InitMain();
PWM2_start();
   while (1) {                         // endless loop

  // 100% duty cycle:  duty ratio can be calculated as (Percent*255)/100.
  PWM2_Set_Duty(255); // Change the duty cycle
  delay_ms(3000); //3 seconds delay

  // 80% duty cycle
  PWM2_Set_Duty(204); // Change the duty cycle
  delay_ms(3000); //3 seconds delay

  // 50% duty cycle
  PWM2_Set_Duty(127); // Change the duty cycle
  delay_ms(3000); //3 seconds delay

  // 30% duty cycle
  PWM2_Set_Duty(76); // Change the duty cycle
  delay_ms(3000); //3 seconds delay

   // 10% duty cycle
   PWM2_Set_Duty(25); // Change the duty cycle
   delay_ms(3000); //3 seconds delay

  }

}
 
Last edited by a moderator:

Hello,

But i am having some errors.
This is no error description.

Please tell what you expect .. and what you see instead.
If there are error messages, then please post the exact and complete error message.

Klaus
 
PIC18f45k22, pin RC(CCP2) should give pwm signal and width could be controlled. i add 8MHz external oscillator.



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void main() {
  ANSELA = 0;    // Configure Analog pins as digital Input/Output pins
  ANSELC = 0;
 
  PORTC = 0;         // set PORTC to 0
  TRISC = 0;         // designate PORTC pins as output
  PWM1_Init(8000);   // Initialize PWM2 module at 8KHz
 
  PWM2_Start();              // start PWM2
  PWM2_Set_Duty(255);       // Set current duty cycle to 100%
 
  while (1) {                         // endless loop
 
  // 100% duty cycle:  duty ratio can be calculated as (Percent*255)/100.
  PWM2_Set_Duty(255); // Change the duty cycle
  delay_ms(3000); //3 seconds delay
 
  // 80% duty cycle
  PWM2_Set_Duty(204); // Change the duty cycle
  delay_ms(3000); //3 seconds delay
 
  // 50% duty cycle
  PWM2_Set_Duty(127); // Change the duty cycle
  delay_ms(3000); //3 seconds delay
 
  // 30% duty cycle
  PWM2_Set_Duty(76); // Change the duty cycle
  delay_ms(3000); //3 seconds delay
 
   // 10% duty cycle
   PWM2_Set_Duty(25); // Change the duty cycle
   delay_ms(3000); //3 seconds delay
 
  }
}



 
Last edited by a moderator:


Code C - [expand]
1
PWM1_Init(8000);   // Initialize PWM2 module at 8KHz


This might also cause a few issues! Is the comment (and all of the other code) correct or are you really wanting to use PWM #1?
Susan
 
Ok. In your circuit it shows that you want to use CCP2 and so you should use

Code:
PWM2_Init(8000);
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…