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 found using the above posted code, changing the DUTY CYCLE of each color (RED_DUTY etc) I can alter the colors. Did some experimenting and eliminated WHILE WEND loops between the different CASE statements and the effect is better. A yellow tinged " flame" with highlights of red and a flicker of blue. I need to make some additional changes. The RED on basically all the time with the GREEN being under PWM and occasionally a BLUE added. Trying different code changes and still looking at other code options. I feel that code could be written that can be correlated to the color chart you posted. Thanks to you, that color chart got the imagination into a higher gear.
 
I am still experimenting. Am contemplating using 255 as full on, `100% duty Cycle. Using the chart that indicates in Hex what eacjh color is on in percent then us that to create desired color. Not sure of how to implement but thinking of a CONST array.
 
You're "contemplating using 255 as full on"? What a novel concept. Perhaps you might take a few moments to understand the numerical representation of RGB and what it means rather than making something up in your head? It's just a thought; a new way of approaching problems for you.

Wikipedia's article on THE RGB Color Model might be worthwhile reading for you, particularly the section on numerical representations. I've posted it below to save you the trouble of clicking the link.

The 255 value is not arbitrary. You should probably understand this before you get too creative in your wild fantasy world of programming.
image.jpg
 
The 255 value refers to the duty cycle (time on) and same as you refereed to in post #34 and #39
I'm thinking If I take one second (1000ms) to equal 255 then take the percentage from the color chart
Say the chart shows $FFAF00 Red = 255(full on) Green = 214 (55% on) and Blue = 00 (off)
not sure how I am going to do this but will tackle after I figure out how to get the 4 PWM ports to output. Using the CCP1CON register settings but have yet to achieve. I TRIS the C port as the data sheet states but no luck yet.
Yes all three candles will look identical but the candles are 2 feet apart on the table with each PWM output controlling just the GREEN color (3 pins) the RED is full on but adjusted with resistors (3 pins) and the Blue adjusted with resistors (3 pins). With this setup no pic pin to exceed 20ma current.
 
after I figure out how to get the 4 PWM ports to output. Using the CCP1CON register settings but have yet to achieve
What are "the" CCP1CON settings?

As LittleGhostman has pointed out numerous times the 4 PWM outputs aren't independant. They're used to drive half and full bridges so the signals will look different than a single output.
 
I realize I only have one PWM but 4 seperate output pins (C2,. C3, C4, C5)
These are all multiplexed together. I don't care if the outputs are different. Using three of the output pins so two will be the same and one will be 180 degrees different.
I tried changing the PWM module but toi no avail. Here is the first section of the PWM2 module that I made changes
Code:
Module pwm
Dim
  FMaxDuty As Word,
  FTMR2ON As T2CON.2
#if _device in (18F1220, 18F1320)
  Dim FPWM1Pin As PORTB.3  // RB3 connected to CCP1 module
#elseif _device in (18F13K22)
  Dim FPWM1Pin As PORTC.5  // RC5 connected to CCP1 module
  Dim FPWM2Pin As PORTC.4
  Dim FPWM3Pin As PORTC.3
#else
  Dim FPWM1Pin As PORTC.2  // RC2 connected to CCP1 module
  Dim FPWM2Pin As PORTC.1  // RC1 connected to CCP2 module
#endif
{
****************************************************************************
* Name    : Start1                                                        *
* Purpose : Start PWM Channel 1                                            *
****************************************************************************
} 
Public Sub Start1()
    CCP1CON = $0c    ' $0c  PWM mode P1A, P1C active high :P1B, P1D active high
    Output(FPWM1Pin)
    Output(FPWM2Pin)
    Output(FPWM3Pin)
    FTMR2ON = 1 
End Sub
{
****************************************************************************
* Name    : Start2                                                        *
* Purpose : Start PWM Channel 2                                            *
****************************************************************************
} 
'Public Sub Start2()
'  CCP2CON = $0C
  '  Output(FPWM2Pin)
  ' FTMR2ON = 1 
'End Sub
{
*************************************

Here are the ccp1con regesiter
upload_2013-12-10_9-51-30.png
 
So if "blue = 00 (off)" why does it need to be connected to a port pin or "adjusted by a resistor"?

And does "Yes all three candles will look identical but the candles are 2 feet apart on the table with each PWM output controlling just the GREEN color (3 pins)..." mean all three green LED elements will vary in lockstep with each other? Why would you use THREE PWM outputs (even if they were available) to vary 3 outputs identically?

Wouldn't it make more sense to use one PWM output to control a transistor or FET to drive all three LED elements?

Or wait...if you've come to accept the fact that you are limited to two PWM channels, wouldn't it make even more sense to use one of the outputs (plus transistor or FET) to drive the two outer candles and the other output (again with a transistor or FET) to drive the center candle with a different pattern?
 
If I have to I could phaps use a PNP transistor on portc.5 and connect the three Green LEDs to it but using the available PWM outputs, while all multiplexed, would be desired. Either setup would limit the outputs to 20ma max.each port. The Blue and Red LEDs are driven from their own ports to keep the 20ma max current under control. So only a total of 9 port pins are to be used plus Vdd, Vcc and the ICSP pins
 
So what resistor will you put on the blue LED to achieve "off"? Is 1 meg enough or do you want to use 100 meg just to be sure?

"I don't care if the outputs are different. Using three of the output pins so two will be the same and one will be 180 degrees different." So if the PWM did operate as you believe in fantasy land, and the "main" output is adjusted for 85% on, wouldn't the opposite output be 85% off? Won't that have some impact on the color? Hint: Yes.

How many people (who seem to know far more about this than you do) telling you it's no going to work will it take?

image.jpg
image.jpg
 
ok sit back mr DEB i am going to try and one last time to explain what 1 pwm mod with 4 outputs does then you decide if the tree your barking up is the correct one, it will take me a while to get this together so hang on for 30 mins while i do the post
 
I don't care if the outputs are different
Ok. If you want to use the Single Output PWM mode then you also need to set up the PSTRCON register.

In Start1() you already have the three pins set as outputs, so try adding PSTRCON = $07 to get the PWM signal out on P1A, P1B, and P1C
 
i know you obviously have googled H bridge circuit so you understand exactly what the pwm mod if for, but as a refresher lets take a look at a basic H bridge
BipolarHBridgeSchematic.gif

look at page 116 of the datasheet for the chip, you will see that first mode 1 pin modulates ((turns off ((pwm)))
so that mode is no good because only one colour will be under pwm control
next mode
now this is half bridge mode, almost looks promising, because you get two modulated out puts YAY problem solved you say! BUT NO, look at the diagram in the datasheet page 116 figure 13.4
in half bridge mode when one pin is high the other is low, this is for running a half bridge, look at the diagram above work out how it functions.so if you used half bridge mode only one colour would be on at a time then another colour on, they would never be on at the same time that is the point of this mode

next mode full bridge mode forward. now in this mode you would get one colour on as in no duty cycle it would just be on one other colour would be off completely off and the other would be doing want you want i.e it would be modulating

next mode is full bridge reversed wich the exactly the same as forward only reversed!

so as you see 4 outputs with just one pwm mod is as useful for simulating a candle as a circle is for drawing a straight line!

yes you have 4 outputs no they dont modulate at the same time no you cant make them into a candle.
study that drawing in the datasheet then look at the H bridge circuit and figure out what each mode does, a clue a pnp transistor needs to be logic 0 to conduct and a npn transistor needs a logic 1,
and that is exactly what that pwm unit is meant for, it is designed to run a motor forwards and backwards, it dosnt at any point have 4 or even 3 modulated outputs at the same time and the only time it has two outputs modulating they are not doing it at the same time they are designed to run a npn and pnp transistor at the same time,
it dosnt matter how good your code is you CAN NOT GET THOSE 4 OUT PUTS TO DO WHAT YOU WANT TO DO WITH THAT UNIT!
YOU WOULD NEED AT LEAST 3 PWM UNITS NOT PINS OR OUTPUTS.
go ahead write a simple code to use those 4 settings at best you will get 2 colours flash one after the other, if you choose red and blue you get a ace police car effect!
 
most simple way for you to do this, say you want a frequency of 100Hz (probably need a bit higher but just for arguments sake)
set a timer to go off every 10 milliseconds this will give you 100 events every second the number of events is your duty cycle, so set timer for 10 milliseconds and every interrupt increase a variable by 1, then have an array with 3 lots of elements of 3 arrays that bit dosnt matter,
so it works like this

timer interrupts every 10ms and variable duty is (if duty<100 then duty= duty +1 otherwise if duty =100 then duty =0) then in the interrupt check the value of the led variables, if they are equal to or Less than the variable DUTY turn them off,
this will give you basic pwm on as many pins as you want, after that you just have to do the randomize of which values are actually picked and the flicker which if you think about it can be done same way!


OR

even easier way, put the 18f13k22 chips in a draw, order some 18f1330 chips. these chips have 3 or 4 (cant remember but it's in one my other post's in this thread) pwm MOD's not out puts but modules, because this chip is designed to run 3 or 4 motors/H bridges, so it will do exactly what you think your chip will do, and it will do it easier, just set same frequency or different it dosnt matter for each module then set the duty cycle for each, really really simple code for you.
if you want this by xmas it's time to put these other chips away as you are further from a candle now than at the start, take the easy route and get a chip that will do the job with no fuss and alot less code.
 
YES I realize the BLUE is basically off BUT I found that adjusting the resistor value thus creating a dimmer BLUE then intermittently apply the BLUE. It adds a nice flicker. If you look at a candle (it does make a difference of candle material) it has some blue highlights.
Now back to the output pins. YES I thought about using a transistor or fet but utilizing three of the four PWM output pins since they are not being used at the moment. Going to try Tumbleweeds suggestion on the PSTRCON register and see what happens. If it doesn't work then bring on a transistor.See post #109
As far as needing this project, not until Jan. 25 2014. As far as a different chip, COST is a big factor. The K series pics seem to be cheaper and the 18F13K22 is the lowest in cost as I priced at 3 different online sources.
As far as code I have tried a timer interrupt code but not real realistic results. Jonsea suggested it looked like something in a different atmosphere or planet. Something along those lines.
 
Isn't your time worth something? How about the time of all of the people trying to help you with this project? Many hours have already been spent trying to help you. Take all the time wasted screwing around with something that's not going to work well and invest a bit more to use the right part.

You can spent a lot of time to accomplish a craptastic result or invest a little more money and far less time to achieve a better result.

*shrug* You can do what you like of course, but the more you ignore the advice of people proven to have a clue, the less help you will get.
 
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....
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top