Author Topic: Cannot Get PIC16F18456 PWM to Start..Please Help  (Read 1057 times)

0 Members and 1 Guest are viewing this topic.

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 801
Cannot Get PIC16F18456 PWM to Start..Please Help
« on: July 08, 2024, 04:02:43 am »
After carefully reading the datasheet and trying for hours and seemingly trying every possible settings, I am almost in tears! I cannot get PWM to start.
I have Fosc configured correctly at 32MHz. No WDT. I am trying to setup a 10-bit PWM as PWM6 using TMR2.

Here's my settings:
//-------------------------------------------------------
T2CLKCON=1;    //Fosc/4
TMR2IE = 0;
PWM6OUT = 0b00010010;
RC2PPS     = 0b00010010;
//                         FMT right aligned high bits
//                           |
PWM6CON=0b00001100; //Sets CCP module to PWM mode
T2CON=0b1000000;        //no pre/postscalar, TMR2ON=On
PR2=0xFF;                        // 0xFF for 10-bit resolution any PWM freq.
T2HLTbits.MODE=0;          //TMR2 free running SW restart

TMR2ON = 1;
DCyc = 50;              //test Duty Cycle
TRISC2=0;              //PWM 1 TRIS'd as output
PWM6CON= 0x80;  //Turns on PWM6 module

//------------------------------------------------------------------
// I set the PWM period in a TMR0 ISR that triggers every 100uSec and works perfectly, with the following:

       PWM6DCH = (DCyc>>2);
       PWM6DCL = (DCyc & 2)==2; //bit1   of PWM (LSB 1)
       PWM6DCL = (DCyc & 1);    //bit0   of PWM (LSB 0), 1024 duty cycle values

//-------------------------------------------------------------------------------------------------------------------------------
What I get is PWM6 on the output pin cbit2 totally nailed to low level!

What in the world am I missing?
 A thousand Thanks in Advance for any help!
« Last Edit: July 08, 2024, 10:31:13 am by SuzyC »
 

Offline Andreas

  • Super Contributor
  • ***
  • Posts: 3281
  • Country: de
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #1 on: July 08, 2024, 06:17:15 am »
Hmm,

normally you have to switch off any analog function before using a PIN as digital.
think twice: 50 is not 50% of 1024 (= 10 Bit)
ISR Works perfectly? no the last command overwrites the previous (Bit 1 is never set).
But then: The timer low values reside in Bit 7 + Bit 6.

So I would write: PWM6DC = DCyc; // (with DCyc being a correctly scaled 16 Bit variable e.g. uint16 DCyc = 50UL * 65536UL /100UL; ).

with best regards

Andreas
« Last Edit: July 08, 2024, 06:42:32 am by Andreas »
 
The following users thanked this post: SuzyC

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3495
  • Country: it
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #2 on: July 08, 2024, 07:18:09 am »
PPS assignment is wrong
PWM6OUT is not an alias to the PWM6 Output (i'm not sure what it is.. CTRL+Click, what does it expand to?)

RC2PPS = ??
check "Table 16-3. PPS Output Signal Routing Options" in the datasheet
 
The following users thanked this post: SuzyC

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 801
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #3 on: July 08, 2024, 09:59:59 am »
//Andreas: Oh my ports!, but I did do my chores! All my anseling and trising is in my startup code in initializing stuff in main().
I randomly chose DCyc = 50 just to see anything that looks like a PWM waveform on my scope..but I don't!

Even though I got the exact DCyc wrong..I should see something!

main()
{
//---blah blah blah
ANSELC=0;
TRISC=0;
PWM1Setup();   //This is the call to the code I posted.

Start:  goto Start;
}

Is it me?  Yes. I don't quite understand, having a problem with pin routing? This is my first experience with PPS! 

JPortici: The alias of PWM6  is 00 1101 (page 240 of attached)  I Don't understand that!
« Last Edit: July 08, 2024, 10:33:36 am by SuzyC »
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3495
  • Country: it
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #4 on: July 08, 2024, 10:35:53 am »
//Andreas: Oh my ports!, but I did do my chores! All my anseling and trising is in my startup code in initializing stuff in main().
I randomly chose DCyc = 50 just to see anything that looks like a PWM waveform on my scope..but I don't!

Even though I got the exact DCyc wrong..I should see something!

main()
{
//---blah blah blah
ANSELC=0;
TRISC=0;
PWM1Setup();   //This is the call to the code I posted.

Start:  goto Start;
}

Is it me?  Yes. I don't quite understand, having a problem with pin routing? This is my first experience with PPS! 

JPortici: The alias of PWM6  is 00 1101 (page 240 of attached)  I Don't understand that!

and the compiler lets you do 0b1101 = 0b00010010? i doubt that
is that your real code?

Anyway, when doing a PPS input you set the GPIO number to the peripheral input (for example U2RXPPS = 0b10010 to set RC2 as U2RX), for PPS output you assign the peripheral to the PPS output, so RC2PPS = 0b1101 sets the PWM6 Output to RC2
 
The following users thanked this post: SuzyC

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 801
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #5 on: July 08, 2024, 10:41:35 am »
Help me understand PPS!

RxyPPS Register Value of HMS6=00 1101.

Normally PWM1 on other older 28-pin and 40-pin parts use RC2 as PWM1 output, so if I wanna use that same pin then I:
1)assign PWM (PWM6) to RC2 ?? (by setting  HMS6 to output on RC2 then I PPS with:

RC2PPS=0b00011101; 

Is this right?
« Last Edit: July 08, 2024, 10:51:13 am by SuzyC »
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 801
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #6 on: July 08, 2024, 10:46:04 am »
Yes, the compiler is fine with

PWM6OUT = 0b00010010;
RC2PPS     = 0b00010010;

OK, now I'm really confused!   Page 240 of the attached says RC2 = 01010
This doesn't make sense to me since the 01  for xx means 0=port A  1=port B and 10= PORTC ( PORTC2 which is what I want!)

See the attached from specsheet
« Last Edit: July 08, 2024, 11:16:02 am by SuzyC »
 

Online MarkF

  • Super Contributor
  • ***
  • Posts: 2615
  • Country: us
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #7 on: July 08, 2024, 11:23:41 am »
On a pic18f2550, I had the capability to set the timing registers via a small OLED display to experiment with.
I believe the  pic18f2550 has 3 timing registers to control the PWM output.
I discovered that there were three basic ranges of the PWM timing registers that the PWM worked.
If you entered certain values (although valid), it resulted in PWM parameters the PIC was NOT able to generate.
No pulses.  Just a constant 'low' or 'high' output.

As I remember, it is possible to get the same PWM freq and duty cycle with different combinations of PIC register values.
 
The following users thanked this post: SuzyC

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 801
Re: Cannot Get PIC16F18456 PWM to Start(solved)
« Reply #8 on: July 08, 2024, 02:00:00 pm »
 JPortic   Thanks expecially for the right answer. Now, after some much needed sleep, reading the manual, especially with your help, I think I understand PPS!

When I assign the pin to the output function code the ID of HMS6 (0x0D) using RC2PPS=0b00001101; to RC2 everthing works.

Once thing I still don't understand is why does Microchip make it so  hard to configure their clock or a simple PWM for this chip and then to make things worse, make it so complicated to set the duty cycle.

Why should setting the PWM  Duty Cycle involve me having to choose between right and left alignment and concatenating bits of two registers?

For a much simpler chip 12F1572, an 8-legged chip that offers three 16-bit PWMs with all the trimmings, the task to setup PWM's is so simple.
« Last Edit: July 08, 2024, 02:09:31 pm by SuzyC »
 

Online NorthGuy

  • Super Contributor
  • ***
  • Posts: 3225
  • Country: ca
Re: Cannot Get PIC16F18456 PWM to Start(solved)
« Reply #9 on: July 08, 2024, 05:01:14 pm »
Why should setting the PWM  Duty Cycle involve me having to choose between right and left alignment and concatenating bits of two registers?

For a much simpler chip 12F1572, an 8-legged chip that offers three 16-bit PWMs with all the trimmings, the task to setup PWM's is so simple.

Same thing. Only the DAC is 10-bit (not 16-bit), so 6 lower bits are ignored.
 
The following users thanked this post: SuzyC

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3495
  • Country: it
Re: Cannot Get PIC16F18456 PWM to Start(solved)
« Reply #10 on: July 08, 2024, 06:10:58 pm »
JPortic   Thanks expecially for the right answer. Now, after some much needed sleep, reading the manual, especially with your help, I think I understand PPS!

When I assign the pin to the output function code the ID of HMS6 (0x0D) using RC2PPS=0b00001101; to RC2 everthing works.

Once thing I still don't understand is why does Microchip make it so  hard to configure their clock or a simple PWM for this chip and then to make things worse, make it so complicated to set the duty cycle.

Why should setting the PWM  Duty Cycle involve me having to choose between right and left alignment and concatenating bits of two registers?

For a much simpler chip 12F1572, an 8-legged chip that offers three 16-bit PWMs with all the trimmings, the task to setup PWM's is so simple.

if this is complicated to you i strongly recommend to never use an ARM chip

RxyPPS to set the pin to use
CCPTMRS1 select the timer associated to PWM6
TMRx sets the carrier frequency and resolution (PRx)
and write directly to PWM6DC to set the duty cycle (PWM6DC is an alias to the PWM6DCH/L pair, so you can write a 16bit variable to it. It is left aligned so you take a 16bit number and write it, simple as that.
 
The following users thanked this post: SuzyC

Offline Andreas

  • Super Contributor
  • ***
  • Posts: 3281
  • Country: de
Re: Cannot Get PIC16F18456 PWM to Start(solved)
« Reply #11 on: July 08, 2024, 06:26:54 pm »
Once thing I still don't understand is why does Microchip make it so  hard to configure their clock or a simple PWM for this chip and then to make things worse, make it so complicated to set the duty cycle.
Hello,

perhaps you are using the wrong tools.
In MPLAB X there is a PIN configurator (MCC) module.
Which generates all initialisation code needed. (At least in "C").
But of course you can take a peek for other languages.

with best regards

Andreas
 
The following users thanked this post: SuzyC

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 801
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #12 on: July 08, 2024, 08:12:42 pm »
JPortici,

I have made projects using Infineon ARM Cortex M0 PSC4200 and 4200M {a stamp with debugger) and found it easy to setup everything including setup PWM, but for PIC chips I do everything from a DOS prompt and write all my code in a DOS shell window using Edit and then even compile using XC8 in a DOS shell and rely on reading the specsheet to find how to configure everything.
« Last Edit: July 08, 2024, 08:33:35 pm by SuzyC »
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6142
  • Country: es
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #13 on: July 09, 2024, 02:20:08 pm »
Dos?? Why? Do you like suffering? IDEs where invented for something!
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: SuzyC

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 801
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #14 on: July 09, 2024, 05:13:37 pm »
DOS is fine for me, It allows me to edit and compose code easily. It also allows me to see the code easily in a maximized window. I can organize my code easily and very quickly open archived versions to recycle my code and so easy for me to browse versions and copy code from them. I know, of course, this is also a feature of most IDEs.

Being a very fast typist, I can quickly edit and write code without handling a mouse. I also ultimately more fully understand how a chip I am programming is organized and functions.

I also like to compile in a DOS shell to very quickly pipe the list of coding problems into a file that I can open almost instantly in a split window to see and edit my source file while also viewing the compiler's complaints.

I also write my own code for serial port debugging and to dump and directly view my code, and use my own custom hardware and SW for programming any PIC chip. I do this with my own Windows programs.

With a DOS EDIT editor working with PIC, I never have to worry about adapting and learning how use a new IDE version.

Between jobs, I write my own custom programmers and other software  to show the speed, accuracy, best functioning of my code.
I was to see how well my code does to accomplish the task it was written for.

It is a task to write my own SW to work with my own HW, bit this is a very special task I enjoy to best use the PIC chips I am new with.

I also do this extra task just to  keep my mind sharp between paid projects.
 
For chip projects requiring an IDE, like when working with Infineon ARM, I have no problem nor choice than using their latest IDE.
 

Online NorthGuy

  • Super Contributor
  • ***
  • Posts: 3225
  • Country: ca
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #15 on: July 09, 2024, 07:23:56 pm »
I have switched to a text editor as well - everything is fast, no bloat, no crashes, no spying, no unexpected updates ...
 
The following users thanked this post: SuzyC

Offline amwales

  • Regular Contributor
  • *
  • Posts: 99
  • Country: gb
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #16 on: July 09, 2024, 09:02:17 pm »
I know everyone hates it and will most likely shut me down for suggesting this.
When I play with a new PIC I often use MCC classic it allows you to setup the clock.
My first task is to setup a timer and blink a LED.
With that you get something running very quickly.
Then you can make small changes and see what changed in the code, it helped me understand PPS.
There is a learning curve to get going but after you've done it once it easy to apply to a new chip.
Have a look for a youtube video that demonstrates MCC classic.
Once you have something working you can pair it down to is most basic form but knowing you have something that works first moves you past a lot of barriers.



I attached a simple MCC generated project ( I have not tried ) for the PIC that has
1 second  blinking led on pin2
1khz pwm with 50% duty on pin 21


 
The following users thanked this post: SuzyC

Offline amwales

  • Regular Contributor
  • *
  • Posts: 99
  • Country: gb
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #17 on: July 09, 2024, 09:04:28 pm »
For PPS the following code

RB0PPS = 0x0D;   //RB0->PWM6:PWM6OUT;   

Seems to sett PPS for PWM6 to output to RB0.
 
The following users thanked this post: SuzyC

Offline amwales

  • Regular Contributor
  • *
  • Posts: 99
  • Country: gb
Re: Cannot Get PIC16F18456 PWM to Start..Please Help
« Reply #18 on: July 09, 2024, 09:10:37 pm »
MCC suggests the following for PWM6 on RC2

    RC2PPS = 0x0D;   //RC2->PWM6:PWM6OUT;   
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf