mikroC pic18f45k22 PWM

General discussion on mikroElektronika website & forums.
Post Reply
Author
Message
Karolina_1
Posts: 5
Joined: 10 Mar 2017 09:36

mikroC pic18f45k22 PWM

#1 Post by Karolina_1 » 10 Mar 2017 09:42

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.

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

}

}

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: mikroC pic18f45k22 PWM

#2 Post by hexreader » 10 Mar 2017 13:19

Welcome to the forum :)

Your code is OK, so maybe the problem is with configuration bits or hardware.

Project attached with working configuration assuming 8MHz crystal used. If this does not work, then maybe tell us more about your hardware setup.
Attachments
Forum.zip
(28.72 KiB) Downloaded 312 times
Start every day with a smile...... (get it over with) :)

User avatar
danilo.milovic
mikroElektronika team
Posts: 501
Joined: 05 Dec 2016 14:59

Re: mikroC pic18f45k22 PWM

#3 Post by danilo.milovic » 10 Mar 2017 15:10

Hi,

@hexreader thanks for helping our new users.

@Karolina_1 do you use EasyPIC7 board?

Regards,

Danilo

Karolina_1
Posts: 5
Joined: 10 Mar 2017 09:36

Re: mikroC pic18f45k22 PWM

#4 Post by Karolina_1 » 17 Mar 2017 00:02

thanks it working now

Post Reply

Return to “Website & Forums General Discussion”