Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Using PWM on a 18F13K22

Status
Not open for further replies.
I have been reading through this thread.... I see where there is major confusion..

There is a pic18f23k22...
This chip has seven timers so it has CCP1, CCP2,CCP3 CCP4 and CCP5

So using pins RC2 ( CCP1) RC1 (CCP2) and RC6 (CCP3) You can use three completely different modules with different duty cycles or periods....
are we looking at the same Datasheet??? according to the one i have here it has 1 CCp pin on RC5??
the chip LG recommended had alot more. i cant screen shot on this computer but i am looking at the table on page 5 of the datasheet
 
ah sorry i got it we are talking different chips Ian, we are talking 18f13k22 you are talking 18f23k22 and mr deb is probably using neither :D
the 18f1330 LG mentioned i think has 6 modulated outputs and 3 duty cycle generators, so would do the job and is smaller. i dont see the confusion? Mr Deb has been told from the start that he is using the wrong pic for the job he wants to do, there are a number of others that would be better if he just wants to use CCP generated PWM with different duty cycles, but i think he is stuck with the 18f13k22 because that is what he went and got
 
Last edited:
Yeah! The chip he's using has the smallest peripherals.. However the confusion I am seeing is a tad enlightened now.

If MrDeb does indeed use the P1A and P1B outputs he will indeed have two of the LED's 180 degrees out of sync.
I'm watching this thread because I'm going to be changing a candle arch to LED operation..

I will be using two LED's in the same manner flicking between them in one stalk.. Anyway I'm only posting alternative pic's for MrDeb to use...
 
What your doing Ian is putting a very large cat amongst a very small group of pigeons! :D:D i must bookmark this thread and get some popcorn on :D
 
If one looks through this thread, a lot of stuff is rehashed over several times.
I stated on the first page the pic I want to use and why.
Layed out what my plan is with 3 candles. Rehashed about four times.
I asked if the schematics I posted were easy to read as I used the suggestions from several years ago. No reply. It was mentioned what compiler version I was using and I failed to reply that I am using the FREE version.
The one member that has offered concise information is Tumbleweed. His three or four post are to the point. No derogatory remarks etc.
I don't pretend to be an expert in electronics or associated field but I do like to experiment. Have learned a lot from Bebe and Jonsea or what ever name he is going by these days and I have thanked him numerous times as well as mention his development board which is an easy to use board. I used to use a Junebug and switched to a Pickit2 and eliminated several little bugs that were creeping into my projects. This alone (using the Junebug) caused some cyberdust or aggravation with certain members to the extent I have been cussed at with four letter words. YES I tend to ask lots of questions but a question is better than not asking and getting lost without information. I even defended Jonsea from being banned a second time. I hold no grudges. They tend to slow down or stop the exchange of information.
On this project I stated from the beginning what my goal was and how I hopefully going to achieve it.
 
Thanks for the post Ian. Hopefully this thread will be of some assistance in your project. THAT'S what this forum is for. The conveyance of information.
My main goal was COST. Yes a better pic with more PWM outputs would have been better but at what price?
Yes a transistor or Fet on the PWM output would be a easy fix but why not use the resources available. In this case 4 PWM outputs that are multiplexed together. Heck why not put a transistor or Fet on each output or use a uLN2308 instead(think I got the right numeration?). Then stuff a voltage regulator, a Mosfet for preventing the batteries from being inserted in backwards. The list could be endless but COST was my first item on the list.
Hopefully Ghostman 11 has a beer or brandy with that popcorn--lol
 
I looked into LED candles some time ago.
It seems for the best effect you need multiple fading LEDs.
Like one org and two yellow on separate channels.
You could do this with 3-555 timers, but random fading would be even better.
I think you could use three random number generators in a microprocessor and feed the output number as a PWM signal on a standard output pin?

I dont know why the OP wants to use RGB LEDs there are not that many colors in a candle flame.
 
Thanks for the post Ian. Hopefully this thread will be of some assistance in your project. THAT'S what this forum is for. The conveyance of information.
My main goal was COST. Yes a better pic with more PWM outputs would have been better but at what price?
Yes a transistor or Fet on the PWM output would be a easy fix but why not use the resources available. In this case 4 PWM outputs that are multiplexed together. Heck why not put a transistor or Fet on each output or use a uLN2308 instead(think I got the right numeration?). Then stuff a voltage regulator, a Mosfet for preventing the batteries from being inserted in backwards. The list could be endless but COST was my first item on the list.
Hopefully Ghostman 11 has a beer or brandy with that popcorn--lol
first off i think you have been disingenuous with LG, he is very young and put ALOT of effort into helping you.
1) Q. AT WHAT COST
A. it is hardly a cost saving when you buy a part that will NOT DO what you would like it to do, Despite Logan posting several diagrams and pointing you to the parts of the datasheet that show why the pic will not do what you want and why it will not do it,you still fail to see the error. ONCE you do see that the chip WILL NOT work with the pwm unit and once you discover that actually LOGAN was right, it will dawn on you that the only realistic way forward with this chip is bu using the method he has outlined above I.E by using a timer and array(s).
When that all hits home ask the question again, AT WHAT COST. Because once you switch to software PWM using a timer you will discover that there are many other cheaper chips that would have done the same job.

2) using the resource available, had you of had these chips in a box hanging around then i would of agreed with you, however you failed to plan the project properly, infact i was always taught a saying that goes along these lines.. Proper Planning Prevents P##s Poor Performance!
Before posting again go back and study the diagram of the H bridge that LG kindly posted,
Next you NEED to understand what the CCP1 peripheral does, in the case of pwm it is designed for such things as the H bridge circuit for driving motors, therefore it will not give you more than one usable modulated output at a time (actually it will but you cant use it for what you want), its not that it's multiplexed, its because it is designed to do a certain job.
Stubbornly staying on the path you are currently on is going to make you feel and look foolish in the end, because despite not only being told it will not work but because several attempts have been made to make you understand exactly why it wont work.
ERASE from your mind the belief that somehow you will be able to make the four outputs work to simulate a candle, because they wont.
Resign yourself to the fact that it is time to pay attention to what is being said to you, especially by a young lad that has given you the solution on a plate that will give you the end result you want and use the resource available!
YOU can get a candle effect with that chip, BUT NOT THE WAY YOU INSIST on doing it. actually LG's maths for the timer are off slightly but what he outlined is correct.

you have 3 options at this point

1) continue with trying to get the pwm peripheral to work, in which case the cost is more because you have wasted every dime/penny on a chip that aint gonna do it that way

2) PWM using a timer, software PWM with one timer will work the effect will be acceptable to pretty good (and way better than results so far) the cost for this is higher than it needed to be because any old pic with a timer could be used!

3) total software PWM using nothing but program loops and updating variables
i put this as an option because i cant think of a single chip you couldnt have used, but realistically without an oscilloscope and decent maths and reasonable to good coding skills its harder than method 2

4) choice 4 is your choice i have no idea what it is but i am sure its the one you are going to pick.
 
After making changes as per Tumbleweed the dang led blinks now. I am going to compare what Tumbleeweed posted and what I had for the module in post #86. I know I tried using CONFIG and kept getting compile errors. I want to try and find out WHY.

In an effort to provide a gem of useful information to anyone reading this epic tale, let's take some effort to help MrDEB "try and find out WHY" his fuse settings for the 18F13K22 resulted in errors. Perhaps someone will benefit from the information.

The first listing is MrDEB's which resulted in errors. Following this is Tumbleweed's code which solved the issue.

In MrDEB's listing, the first two lines, CONFIG (some parameter) are ok. But after that, MrDEB left off the CONFIG. Compare this to Tumbleweed's code, a portion of which is here:

Code:
Config
   FOSC = IRC,    ' internal RC osc
   PLLEN = OFF,    ' PLL is under software control

It looks similar at first glance, but there are some crucial differences. The CONFIG statement is on a line by itself, followed by different parameters on each line. But notice the comma on each line. MrDEB neglected to include the commas, which informs the compiler that the command is continued to the next line! Without the commas, the compiler doesn't know that the CONFIG goes to each line.

If Tumbleweed's code is rearranged, it's a little clearer how the compiler sees the code:
Config FOSC = IRC, PLLEN = OFF, PCLKEN = OFF, FCMEN = OFF, ...

Note that the final entery in the list does not include a comma; it's the end of the CONFIG line.


Here is my main code

Here is the module NOTE only the last 5 lines are enabled.
Code:
Module osc1322
{
Config FOSC = HS  'HS oscillator
Config PLLEN = OFF ' PLL is under software control
PCLKEN = On ' Primary clock enabled
FCMEN = OFF ' Fail-Safe Clock Monitor disabled
IESO = OFF 'Oscillator Switchover mode disabled
PWRTEN = OFF ' PWRT disabled
BOREN = OFF ' Brown-out Reset disabled in hardware and software
BORV = 22 ' VBOR set to 2.2 V nominal
WDTEN = OFF ' WDT is controlled by SWDTEN bit of the WDTCON register
WDTPS = 128 ' 1:128
MCLRE = OFF ' RE3 input pin enabled; MCLR disabled
HFOFST = OFF ' The system clock is held off until the HFINTOSC is stable.
STVREN = On 'Stack full/underflow will cause Reset
LVP = OFF ' Single-Supply ICSP disabled
BBSIZ = OFF ' 1KW boot block size
XINST = OFF ' Instruction set extension and Indexed Addressing mode disabled
}
mclr = 0
OSCCON  = %01100011        ' SCS<1:0> 00 = Primary clock
OSCCON2 = %00000111        ' PRI_SD<2> = Osc drive circuit on
ANSEL  = %00000000        ' all digital
ANSELH  = %00000000        ' all digital
'TRISC  = %00000000        ' portC cleared for output


Then that's a problem. You HAVE to set the CONFIG properly, and as been said many times you can't rely on the defaults to match your hardware.

Try this:
Code:
Module osc1322

Config
   FOSC = IRC,    ' internal RC osc
   PLLEN = OFF,    ' PLL is under software control
   PCLKEN = OFF,      ' Primary clock drive disabled (using internal)
   FCMEN = OFF,      ' Fail-Safe Clock Monitor disabled
   IESO = OFF,      ' Oscillator Switchover mode disabled
   PWRTEN = ON,    ' PWRT enabled
   BOREN = OFF,    ' Brown-out Reset disabled in hardware and software
   BORV = 22,      ' VBOR set to 2.2 V nominal
   WDTEN = OFF,    ' WDT is controlled by SWDTEN bit of the WDTCON register
   WDTPS = 128,   ' 1:128
   MCLRE = ON,    ' RA3 input pin disabled; MCLR enabled
   HFOFST = OFF,    ' The system clock is held off until the HFINTOSC is stable.
   STVREN = ON,    'Stack full/underflow will cause Reset
   LVP = OFF,    ' Single-Supply ICSP disabled
   BBSIZ = OFF, ' 1KW boot block size
   XINST = OFF, ' Instruction set extension and Indexed Addressing mode disabled
   DEBUG = OFF

  OSCCON = %01100010  // IRCF=8MHz, SCS1=1 (int osc)
  OSCCON2 = 0
  OSCTUNE = %10000000  // INTSRC=1, PLLEN=0, TUN=0

  // set all pins digital
  ANSEL = $00
  ANSELH = $00
  CM1CON0 = 0
  CM2CON0 = 0
  VREFCON1 = $00
 
If one looks through this thread, a lot of stuff is rehashed over several times.
I stated on the first page the pic I want to use and why.
Layed out what my plan is with 3 candles. Rehashed about four times.
I asked if the schematics I posted were easy to read as I used the suggestions from several years ago. No reply. It was mentioned what compiler version I was using and I failed to reply that I am using the FREE version.
The one member that has offered concise information is Tumbleweed. His three or four post are to the point. No derogatory remarks etc.
I don't pretend to be an expert in electronics or associated field but I do like to experiment. Have learned a lot from Bebe and Jonsea or what ever name he is going by these days and I have thanked him numerous times as well as mention his development board which is an easy to use board. I used to use a Junebug and switched to a Pickit2 and eliminated several little bugs that were creeping into my projects. This alone (using the Junebug) caused some cyberdust or aggravation with certain members to the extent I have been cussed at with four letter words. YES I tend to ask lots of questions but a question is better than not asking and getting lost without information. I even defended Jonsea from being banned a second time. I hold no grudges. They tend to slow down or stop the exchange of information.
On this project I stated from the beginning what my goal was and how I hopefully going to achieve it.

Oh really? Let's try to figure this out.

In post #44, you said:
ERRO in trying to program pic. I have an external voltage source of 45 v but using TROUBLESHOOT in PICKIt2 I keep getting a Vpp volta error. Tried w/ external voltage and W/O external voltage
Checked and double checked connections but ?


I replied in post #45 with detailed instructions to follow on how to reset the PICkit 2 if a short had caused it to shut down.

In post #49 you replied:

Have checked everything, found I had a 8K resistor instead of 22K on the MCLR (Vcc to MCLR) Replaced and still get 7.4 volts on Vpp Chip only requires 8v.

Did you follow the steps I suggested, which were based on actual problems I have had? There is no way of knowing from your reply.. I asked again.

In post #51 you replied:

I have followed ALL the suggestions except assembling another board. Thats next. One thing I noticed is when I first enable the Pickit2 I get a Vpp error .

Again, exactly what you mean when you say "enable" is not at all clear.

In post #53, I mentioned the need to calibrate the PICkit 2 from the tools menu.

In post #54 you replied:

Using the Pickit2 TOOLS/ TEST is when it says 7.6v under Vpp test. The voltage setting for this pic is 2.3v - 5.5v as per data sheet.
I already assembled a new board using same pic and same results. Changed the Pic and the Vpp says its good (8.2v if I recall using Vpp test)....


Did you calibrate before testing? Doesn't appear to be the case, and any "test" measurements will be invalid if the calibration step wasn't done. Then you said you changed "the PIC" and it worked, but what you didn't say is if "the PIC" was the same type or a different type, so it is not possible to know if the problem was resolved.

In post #62 you posted:

It's been awhile but I am using a pickit2. Eliminated several issues. I built a new board, used ohm meter as always before inserting pic. Measured voltage at the IC socket before inserting pic as well. Tested the pickit2 output as well using the brief instructions when using TOOLS / TROUBLESHOOTING and tested Vdd , mclr Vpp etc. Pickit2 works.
Inserted original pic and still low Vpp. Inserted new pic and all is well with Vpp etc.


Did you ever calibrate the PICkit? Still can't determine if you did or not. Swapped "pic"s again but we still don't know if the swap was with the same or a different type.

I explained the differences between calibrating and verifying the voltages on the PICkit in message 74. Still no answer so I asked direct questions in post #80 and again in post #90.

Finally in post #96, you answered the question with a simple "yes". Some details would have been helpful to know if the PICkit was operating with a reasonable supply voltage. So it took from post #44 to post #96 to come to a slight understanding that the PICkit problems were resolved, but without any details at all from you.

Yes, this is a lot of typing to clarify a simple issue. The questions didn't change along the way. Any "re-hashing" is the result of your inability to provide any clear information.

A number of others contributed to the PICkit 2 discussion, including LittleGhostman. All good comments; I didn't include them here for the sake of keeping this post down to several pages in length.

And I'm sorry if you didn't find this advice worthy of your consideration...it seemed to be essential as you were neglecting key steps.
 
After I studied Tumbleweeds code correction I saw the error in my code but I honestly didn't see the commas that you pointed out.
I have yet to try out Tumbleweeds suggestion on the PSTRCON register, been cutting out reindeer for my wife all morning and afternoon. Hopefully try it out in the morning.
I first want to study the data sheet and find this PSTRCON register.
As far as the H bridge, I understand what and why it works but in reality one PWM output would suffice but a transistor is needed. As far as the candle effect, I have a pretty realistic looking candle as is. Just need to add the hot melt glue and connections for two additional candles to run off one pic. Just using the PWM on the green led only and a small burst of blue occasionally.
This project I really wanted to use a cheaper pic , say a 12F or 10F but no experience in assy or other. Only compiler I have ever used is Swordfish so I am stuck using 18F chips.
 
Also MrDEB, regarding your comment that the "only person offering concise information is Tumbleweed" is RUBBISH.

LittleGhostman's approach to problem solving in post #15 is brilliant! Exactly the right way to approach a problem to achieve an excellent result without the scrap heap of different schematics and unworking boards you generate with every attempt at a project. You could learn a lot from this young lad. He is learning a lot from you that will help him in his future career.
 
Why does it seem this thread is getting no ware?
are thats because you are not used to the Mr Deb threads ;) this is perfectly normal for one of his projects and eventualy it will get done but there is probably still a couple of hundred posts to go, besides all of his threads end up with heaps of info that will help others, things like the colour chart only get posted because points have to be hammered in! but that enriches the thread with things that will also help others.
Sit back and watch it will eventually get there, it just normally goes on for a bit ;)
 
Lol it's kind of crazy but it's so much fun reading all this. Jon his chip didn't come on cause the ostune setting was wrong. And little ghost has done a great job posting. I wish I could write as good as he is his learning skills look really sharp for his age.
And his comprehension he's going places..
Now for the flame set two pin for color and kill the power to make it look real is all that's needed.
 
for many reasons this year has been a hard year for us as a family and finaly we see light at the end of our tunnel (this may infact turn out to be a train coming but never mind), i would say in the Last 8 months LG has had to deal with some hard situations and has been blessed with having some very good teachers at both the schools he has attended this year. Since we moved in July though, his thirst for knowledge has increased and he has found a genuine interest in learning for the sake of knowledge. Also in the last months i have been working from home far more and had much more time to spend with him. He is developing a flare for writing, and where as before he would approach a electronics project in the much the same way i do, he has been to my Lab at the uni and discovered that dad dosnt actually work that way in his academic and professional life, he has shown alot of interest in how i approach experiments in the biology Labs and has started to approach his electronic projects applying more scientific techniques of approach. This is something i never do at home! :oops:.
Each of his projects is now documented to an incredible level and i am extremely proud of the young man he is developing into to, he has a very sharp mind an abundance of energy a zest for learning and few distractions. So yes hopefully he will achieve the goals he sets himself in life because as his father i truly believe he deserves it for all the hard work he puts in!
and it's very satisfying for me as a parent to see so many good things said about the boy i am so proud of

Jason

as for the Mr deb threads yes they are addictive :D
 
My post #34 with the RGB color chart and #39 further explaining how to use the information shown in the chart seem pretty darned concise as well. I don't think you (MrDEB) had any idea how to use an RGB LED to get any desired color. If you did know this, you kept it pretty well hidden.

There's kind of a social contract when asking questions on a forum. You ask a question. Other people take time and effort to provide an answer – the quality of the answer depends on how clear and concise the question is. The social contract requires you acknowledge posts made by others. Maybe those posts help you move forward, maybe you've considered the points brought up, or maybe you don't understand the points being made. In all cases, you should acknowledge all of the posts people have written. When you fail to do so, people feel their time has been wasted or they get angry when they feel ignored. In MrDEB's case, he tends to ignore posts that don't agree with what he thinks should happen or wants to happen. This leads to more posts "rehashing" the points that have been ignored, usually with a less polite tone.

A further part of the social contract is to post results of how the particular problem was resolved. This makes the forum topic useful to others having similar problems and looking for answers. In this thread, the case in point is the Vpp error of the PICkit 2. There was an error and then there wasn't. Was there a wiring error on the board? Was there a defective PIC? Was their a calibration problem or low voltage from the USB port? The problem was apparently resolved, but by what means is unclear.

I'm sorry MrDEB is feeling picked on...but the reasons are pretty clear.
 
Lol it's kind of crazy but it's so much fun reading all this. Jon his chip didn't come on cause the ostune setting was wrong. And little ghost has done a great job posting. I wish I could write as good as he is his learning skills look really sharp for his age.
And his comprehension he's going places..
Now for the flame set two pin for color and kill the power to make it look real is all that's needed.

:rolleyes::rolleyes::rolleyes::rolleyes:Come on admit it you got that one straight out of the RED NECKS GUIDE TO ELECTRONIC THANGY's!
if we are going to go down that kind of route then how about just tape two pins together stick them to a resistor, pull them high and get people to blink now and then!!!:rolleyes::rolleyes::rolleyes::rolleyes::rolleyes: shame on you Mr Burt i have come to expect more elegance from a member of your class and standing:facepalm::facepalm::rolleyes:













ONLY JOKING :D
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top