Cannot Get PIC16F18456 PWM to Start..Please Help - Page 1
Forum section: electronics / general

author: SuzyC
author_rank: Frequent Contributor
timestamp: July 08, 2024, 04:02:43 am
content: 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/4TMR2IE = 0;PWM6OUT = 0b00010010;RC2PPS = 0b00010010;// FMT right aligned high bits// |PWM6CON=0b00001100; //Sets CCP module to PWM modeT2CON=0b1000000; //no pre/postscalar, TMR2ON=OnPR2=0xFF; // 0xFF for 10-bit resolution any PWM freq.T2HLTbits.MODE=0; //TMR2 free running SW restartTMR2ON = 1;DCyc = 50; //test Duty CycleTRISC2=0; //PWM 1 TRIS'd as outputPWM6CON= 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!

author: Andreas
author_rank: Super Contributor
timestamp: July 08, 2024, 06:17:15 am
content: 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 regardsAndreas

author: JPortici
author_rank: Super Contributor
timestamp: July 08, 2024, 07:18:09 am
content: PPS assignment is wrongPWM6OUT 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

author: SuzyC
author_rank: Frequent Contributor
timestamp: July 08, 2024, 09:59:59 am
content: //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 blahANSELC=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!

author: JPortici
author_rank: Super Contributor
timestamp: July 08, 2024, 10:35:53 am
content: Quote from: SuzyC on July 08, 2024, 09:59:59 amand the compiler lets you do 0b1101 = 0b00010010? i doubt thatis that your real code?Anyway, when doing a PPSinputyou 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

author: SuzyC
author_rank: Frequent Contributor
timestamp: July 08, 2024, 10:41:35 am
content: 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?

author: SuzyC
author_rank: Frequent Contributor
timestamp: July 08, 2024, 10:46:04 am
content: Yes, the compiler is fine withPWM6OUT = 0b00010010;RC2PPS = 0b00010010;OK, now I'm really confused! Page 240 of the attached says RC2 =01010This doesn't make sense to me since the 01 for xx means 0=port A 1=port B and10= PORTC( PORTC2 which is what I want!)See the attached from specsheet

author: MarkF
author_rank: Super Contributor
timestamp: July 08, 2024, 11:23:41 am
content: 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.

author: SuzyC
author_rank: Frequent Contributor
timestamp: July 08, 2024, 02:00:00 pm
content: 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 tochoose 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.

author: NorthGuy
author_rank: Super Contributor
timestamp: July 08, 2024, 05:01:14 pm
content: Quote from: SuzyC on July 08, 2024, 02:00:00 pmSame thing. Only the DAC is 10-bit (not 16-bit), so 6 lower bits are ignored.

author: JPortici
author_rank: Super Contributor
timestamp: July 08, 2024, 06:10:58 pm
content: Quote from: SuzyC on July 08, 2024, 02:00:00 pmif this is complicated to you i strongly recommend to never use an ARM chipRxyPPS to set the pin to useCCPTMRS1 select the timer associated to PWM6TMRx 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.

author: Andreas
author_rank: Super Contributor
timestamp: July 08, 2024, 06:26:54 pm
content: Quote from: SuzyC on July 08, 2024, 02:00:00 pmHello,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 regardsAndreas

author: SuzyC
author_rank: Frequent Contributor
timestamp: July 08, 2024, 08:12:42 pm
content: 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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: July 09, 2024, 02:20:08 pm
content: Dos?? Why? Do you like suffering? IDEs where invented for something!

author: SuzyC
author_rank: Frequent Contributor
timestamp: July 09, 2024, 05:13:37 pm
content: 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.

author: NorthGuy
author_rank: Super Contributor
timestamp: July 09, 2024, 07:23:56 pm
content: I have switched to a text editor as well - everything is fast, no bloat, no crashes, no spying, no unexpected updates ...

author: amwales
author_rank: Regular Contributor
timestamp: July 09, 2024, 09:02:17 pm
content: 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 has1 second blinking led on pin21khz pwm with 50% duty on pin 21

author: amwales
author_rank: Regular Contributor
timestamp: July 09, 2024, 09:04:28 pm
content: For PPS the following codeRB0PPS = 0x0D; //RB0->PWM6:PWM6OUT;Seems to sett PPS for PWM6 to output to RB0.

author: amwales
author_rank: Regular Contributor
timestamp: July 09, 2024, 09:10:37 pm
content: MCC suggests the following for PWM6 on RC2RC2PPS = 0x0D; //RC2->PWM6:PWM6OUT;