CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC16F1846 PWM GENERATION WITH SETUP_PWM1

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
halilemirhankara



Joined: 13 Dec 2024
Posts: 4

View user's profile Send private message

PIC16F1846 PWM GENERATION WITH SETUP_PWM1
PostPosted: Fri Dec 13, 2024 2:17 am     Reply with quote

Hi, this is my first position on the CCS forum. I tried to solve this problem on my own but reading datasheets or surfing the net didn't help me.

I am trying to generate PWM 50Hz through the PWM peripheral. However, there is no PWM output from the pins I selected. Below is my full code.

I always check the pins I use for PWM generation with an oscilloscope.

Things I tried:
- Setup timer 2
- Disable ccp, cwg

main.h
Code:
#ifndef MAIN_H
#define MAIN_H

#include <16F18146.h>
#include <stdint.h>

#fuses PUT_1MS, NOBROWNOUT, PROTECT
#use delay(internal=32MHz)

#define pwm_1           PIN_C6
#define pwm_2           PIN_A4

#pin_select PWM1S1P1 = pwm_1
#pin_select PWM1S1P2 = pwm_2

#endif


main.c
Code:
#include <main.h>

void main() {
   setup_pwm1(PWM_ENABLE | PWM_CLK_FOSC | PWM_CLK_DIV_BY_16);
   setup_pwm1_slice(0, PWM_PUSH_PULL);
   set_pwm1_period(10000);
   set_pwm1_duty(0, 5000,0);
   PWM1LD = 1;
   PWM1EN = 1;
       
   while (TRUE) {
   }
}


I want to use that setup because I want to generate low frequency PWM with high mcu frequency.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Dec 14, 2024 6:48 am     Reply with quote

I'm curious as to the application for 50Hz PWM.

If say for RC servo control, you may be able to use a slower PIC clock speed, if nothing else is 'time critical'.
Another option may be to just use delay(). While this dedicates the PIC to doing the inline delays. again it may work for you.
halilemirhankara



Joined: 13 Dec 2024
Posts: 4

View user's profile Send private message

PostPosted: Sat Dec 14, 2024 7:47 am     Reply with quote

It is for a full-bridge driver.

I already got it working with other methods but I am curious about this one because with this method I can use 32 MHz mcu clock and 50 Hz PWM at the same time as I thought.

I also have more coding for this project. I have not added it yet.

And also I am trying to run the code this way because there will be other projects where I will use this method.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Dec 14, 2024 8:13 am     Reply with quote

depending on what the bridge driver is driving, you may be able to speed up the PWM ??
halilemirhankara



Joined: 13 Dec 2024
Posts: 4

View user's profile Send private message

PostPosted: Sun Dec 15, 2024 1:35 am     Reply with quote

I tried increasing the PWM frequency, but that didn't work.
I also tried decreasing the frequency of the MCU, but that didn't work either.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sun Dec 15, 2024 4:57 am     Reply with quote

Key is one error in your setup.

You are telling the PWM, to clock of Fosc, so off 32MHz.

32000000/16/10001 =199.98Hz. just under 4* what you want.
More, the internal prescaler only supports /1, /2, /4, or /8, so the compiler
will try to synthesise larger prescalers by extending the division factor
This will run out, before you get to 50Hz.
The minimum frequency that can be developed by the PWM on this chip
fed off 32MHz, is:

32000000/65536/8 = 61Hz.

Why for just use the internal oscillator?

setup_pwm1(PWM_ENABLE | PWM_CLK_MFINTOSC_500KHZ | PWM_CLK_DIV_BY_1);

Then a period of 9999, will give 50Hz.

Remember the division is by one more than the period factor selected.
halilemirhankara



Joined: 13 Dec 2024
Posts: 4

View user's profile Send private message

PostPosted: Sun Dec 15, 2024 5:32 am     Reply with quote

Thank you for information. I will try it tomorrow and i will tell the results.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group