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.
Post the code your really using there no way to help you without it.
Swordfish doesn't like osccon setting in the main code I've had the osc not start up to full speed hours after the chip is powered on.

That is an excellent point Burt and I had forgotten it. Any initialization done in Swordfish modules will be done before any code executes in the main program. If the chip defaults to a slow clock speed, it can take a long time to reach oscillator settings in the main program code.

There's a reason I don't use the internal oscillators – it can be troublesome getting them setup correctly.
 
swordfish dosnt like OSCON in main code!!!:confused: learn another language ;) are you using free version of swordfish? if so i will down load later and try your code out. the chips are fine just fussy as i said before. i think most dont use because they have way more stuff on than most people need and people tend to stick with one chip or another! no idea why as i like to use them all :D
 
LG said:
people tend to stick with one chip or another! no idea why as i like to use them all

If you are making products, you need to do a hell of a lot of research.. pick one processor and mass produce your product.. Once you have settled on a design, you can then try and make your board do as much as humanly possible to suit other alternatives....

My main board has the 40 pin chip holder, so I can use ANY of the footprints from Microchip that fit... Quite a few.
That's why some tend to stick with a certain type...
 
but even hobbyist tend to stick with one or two! i use loads but i do most stuff on 18f4685 then when i am happy i have it doing all i want it to do i will try and use a smaller chip if needed. I use the 40 pin one because it gives me loads of options, i hate getting half way through and have to go jumping around trying to get a extra pin
 
I have been using a Tap28 development board but hve a development board w/ 40 pins as well.
This is my first foray using the K pic.
 
Here is my main code
Code:
Device = 18F13K22
Clock = 8
Include "osc1322.bas" 

// alias to port pin...
Dim LED1 As PORTB.5
Dim led2 As PORTB.6
Dim Led3 As PORTC.2
// main program...

Output(LED1)        ' portb.6 pin 11 BLUE
Output(led2)        ' portb.5 pin 12 RED
Output(Led3)        ' not connected yet port c.2 pin 14  GREEN

High(LED1)
High (led2)
High (Led3)
While 1 = 1
  Low (led2)
  DelayMS (1000)
  High (led2)
  DelayMS(1000)
Wend

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

schematic in next post
 
Here is the schematic. Hopefully you can read it.
testing schematic.jpg
 
Using jumper wires from a tap28 Dev Board to the Candle board, changed the DEVICE , disabled the INCLUDE osc1322.bas, added INCLUDE internaloscilliator.bas.. Used same ports as per 18F13K22. LED blinks as it should so this confirms board connections are correct and working as per code. AFTER testing per port I added the second LED
Code:
Device = 18F2420
Clock = 8
'Include "osc1322.bas" 
include "internaloscillator.bas"
// alias to port pin...
Dim LED1 As PORTB.5
Dim led2 As PORTB.6
Dim Led3 As PORTC.2
// main program...

Output(LED1)        ' portb.6 pin 11 BLUE
Output(led2)        ' portb.5 pin 12 RED
Output(Led3)        ' not connected yet port c.2 pin 14  GREEN

High(LED1)
High (led2)
High (Led3)
While 1 = 1
  Low (led1)
  DelayMS (1000)
  High (led1)
  DelayMS(1000)
    Low (led2)
  DelayMS (1000)
  High (led2)
  DelayMS(1000)
Wend

testing one step at a time.
 
Apparently, there was something unclear about my request for intelligible information?

By even writing this reply, I am doing more than I ought. "A clear, concise answer is required to each before I will do anything further to help you." Thank you for providing some information, but you have not addressed the key issues.

END OF DISCUSSION

Sigh. MrDEB, I truly wish you could post a message with pertinent details that can be comprehended by mere mortals such as myself.

In the paragraph above, you talk about PICkit calibration, internal oscillator settings and etc and say "it doesn't work." From that statement, nobody can know what "it" is.

Questions. A clear, concise answer is required to each before I will do anything further to help you.

1. Did you run the Vdd calibration procedure under the PICkit 2 GUI?

2. When you attempt to write to the 18F13k22 chip, does it complete satisfactorily or is there an error message displayed? If so, post screen captures of the PICkit 2 GUI and of the error message.

3. Did you verify the code was written correctly to the 18F13K22? It's an option under the PICkit 2 GUI.

4. If and only if the code was written into the 18F13K22, how do you know it's not working? Simplest possible program. Post code and schematic diagrams in a clear readable format. I'm not going to guess at what you're doing or assume you have correct connections. Sorry, past performance suggests to do so is foolish.

5. If the internal oscillator code isn't working (post your actual code), and the chip is actually being programmed (verified in the PICkit 2 GUI), try it with a crystal and 22 pF caps.

Sorry to be blunt, but if you want help, you need to post all of the details clearly and concisely with appropriate documentation posted. Many of us have been burned in the past by making assumptions about what you are asking, or assuming schematics actually show what you have connected.

I do not believe the 18F13K22 chip should be any more difficult than any other 18F chip to use, but as always, attention to detail and the data sheet will be required.
 
Here is the module NOTE only the last 5 lines are enabled.
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
I don't have a 13K22 to test it on, but I think that should get the internal osc running at 8MHz no matter what and set your pins for digital IO
 
i would try it on the chip but STILL waiting to find out if its the free compiler or not he is using, still i have only asked 2-3 times so maybe i havnt reached the magic number:facepalm: and maybe i am being rude but i am fed up being ignored when i ask questions so i can help! i even got dad to buy me the chip so i could try and help you but i get ignored when i ask a relevant question!
So for me thats its, just because i am kid dosnt give me any less rights than anyone else, good luck with the project but leave me out of it
 
Yes, the Swordfish Special Edition (free edition) should handle this. You may have to update with new include files from the Swordfish web page.
 
thanks jonsea but i have had enough,
 
Sorry you were dragged into the unrewarding effort of attempting to help MrDEB, LittleGhostman. Many have tried, all have failed I'm sad to report.
 
I thought I already answered Ghostman question about the compiler. YES it is the free download version
Questions. A clear, concise answer is required to each before I will do anything further to help you.

1. Did you run the Vdd calibration procedure under the PICkit 2 GUI? YES

2. When you attempt to write to the 18F13k22 chip, does it complete satisfactorily or is there an error message displayed? If so, post screen captures of the PICkit 2 GUI and of the error message. COMPLETES WITH OUT ERROR MESSAGE

3. Did you verify the code was written correctly to the 18F13K22? It's an option under the PICkit 2 GUI. YES I almost tried with the 18F2420 but realized my error.

4. If and only if the code was written into the 18F13K22, how do you know it's not working? Simplest possible program. Post code and schematic diagrams in a clear readable format. I'm not going to guess at what you're doing or assume you have correct connections. Sorry, past performance suggests to do so is foolish. I VERIFIED THAT THE CIRCUIT WORKS using a 18F2420

5. If the internal oscillator code isn't working (post your actual code), and the chip is actually being programmed (verified in the PICkit 2 GUI), try it with a crystal and 22 pF caps. HAVE YET TO CONNECT AN EXTERNAL CRYSTAL

Sorry to be blunt, but if you want help, you need to post all of the details clearly and concisely with appropriate documentation posted. Many of us have been burned in the past by making assumptions about what you are asking, or assuming schematics actually show what you have connected. The schematic I posted that I hope can be read is exactly what I have connected.
Going to try Tumbleweeds module suggestion next
 
Your answer to #3 leaves me skeptical to say the least.

Good luck. I think everything you need to know has been discussed here. If you can't make the 18F13K22 work, cut your losses and use a chip you have used before. Your time must be worth something; I know all the people who have attempted to help you have other things to do.
 
In response to question #3 I am assuming the option of VERIFY? Yes I checked that as well.
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.
THANKS to everyone who assisted in this endeavor.
 
Well making some progress.
Had to make some changes in the utilis module and now to attempt to correlate the time ON and OFF to the color chart Jonsea posted.
I found that after removing the componets from the breadboard to soldered on a board the colors need some adjustment with added resistance.
Using this code.
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                  *
*  Author  : [select VIEW...EDITOR OPTIONS]                                *
*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 11/18/2013                                                    *
*  Version : 1.0                                                            *
*  Notes  :  Module osc1322 for 18F13K22                                                              *
*          :                                                                *
*****************************************************************************

}
Device = 18F13K22
Clock = 8

Include "osc1322.bas"
Include "RandGen.bas"
Include "Util1322.bas"
Dim TMR2IE As PIE1.1,        // TMR2 interrupt enable
    TMR2IF As PIR1.1,        // TMR2 overflow flag
    TMR2ON As T2CON.2,        // Enables TMR2 to begin incrementing
    Signal_Pin As PORTB.0    // Signal output to frequency meter
Dim Red_Pin As PORTb.5,        //was b.0
    Green_Pin As PORTc.5,      //was b1
    Blue_Pin As PORTb.6,        //was b2
    Red_Duty As Byte,
    Green_Duty As Byte,
    Blue_Duty As Byte,
    Red_DutyVal As Byte,
    Green_DutyVal As Byte,
    Blue_DutyVal As Byte,
    RandomVal As Byte
Dim led As PORTB.3 
Dim uS As Word,
    mS As Word,         
    ranval(1)As Word
Interrupt TMR2_Interrupt()
  ' High(Signal_Pin)
    Save(0)                  // Back up system variables 
    If TMR2IF = 1 Then        // Check if the interrupt was from TMR2 
        TMR2IF = 0            // Clear the TMR2 interrupt flag
        uS = uS + 50
        If uS >= 1000 Then
            uS = uS - 1000
        Inc(mS)
        EndIf     
        Inc(Red_DutyVal)
        Inc(Green_DutyVal)
        Inc(Blue_DutyVal)
        If Red_DutyVal > Red_Duty Or Red_Duty = 250 Then
            Red_Pin = 0
        Else
            Red_Pin = 1
        EndIf
        If Green_DutyVal > Green_Duty Or Green_Duty = 0 Then
            Green_Pin = 0
        Else
            Green_Pin = 1
        EndIf
        If Blue_DutyVal > Blue_Duty Or Blue_Duty = 0 Then
            Blue_Pin = 0
        Else
            Blue_Pin = 1
        EndIf             
    EndIf
    {
      led = 0
      delayms(1000)
      led = 1
      delayms(1000)
      }                    //
    Restore                  // Restore system variables
  ' Low(Signal_Pin)
End Interrupt
Private Sub TMR2_Initialize()
    TMR2ON = 0                // Disable TMR2
    TMR2IE = 0                // Turn off TMR2 interrupts 
    PR2 = 149                // TMR2 Period register PR2
    T2CON = %00000000        // T2CON 0:1 = Prescale
                              //        00 = Prescale is 1:1
                              //        01 = Prescale is 1:4
                              //        1x = Prescale is 1:16                               
                              //      3:6 = Postscale             
                              //    0000 = 1:1 postscale
                              //    0001 = 1:2 postscale
                              //    0010 = 1:3 postscale...
                              //    1111 = 1:16 postscale
    TMR2 = 0                  // Reset TMR2 Value 
    TMR2IE = 1                // Enable TMR2 interrupts
    TMR2ON = 1                // Enable TMR2 to increment
    Enable(TMR2_Interrupt)
End Sub
// Start Of Program...
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 0            ' to decrease the amount of color (led on time) insert up to 255
Green_Duty = 5          ' to increase amount of on time < 255
Blue_Duty =250              ' contemplating adding PWM to change the DutyVal value
Red_DutyVal = 0
Green_DutyVal = 0
Blue_DutyVal =0
led = 1
uS = 0
mS = 0

RandGen.Initialize(255)      // Initialize the Random Number generator
TMR2_Initialize              // Setup and enable TMR2
While true                    // Create an infinite loop
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255
 
      Enable(TMR2_Interrupt)
   
   
    Select RandomVal
              // Find out what colour to increase/decrease
           
        Case 0 To 42
          If  Red_Duty =RandomVal-5
            Then  mS = 0
                Repeat
                Until mS = 10
                And Red_Duty>0
            End If
             
        Case 43 To 83
            If Red_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 25
                Inc(Red_Duty)
            EndIf
           
      Case 84 To 127
            If Green_Duty =RandomVal-20
              Then  mS = 0
                Repeat
                Until mS = 19
                Inc(Green_Duty)
            End If 
        Case 128 To 170
            If Green_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 10
                Inc(Green_Duty)
                DelayMS(5000)
            EndIf
        Case 171 To 212
          If  Blue_Duty = RandomVal-30
              Then  mS = 0
                Repeat
                Until mS = 19
                Inc(Blue_Duty)
            End If
           
        Case 213 To 255
            If Blue_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 20
                 
                inc(Blue_Duty)
            EndIf
     
        End Select
      Wend
     
        End
 
The code in the above post has absolutely nothing to do with the color chart I posted.

If you want to have a realistic color simulated candle, there is a relatively narrow range of RGB color combinations that result in reasonable colors. Red plus some blue plus some green. How do you get some amount of green and blue? PWM.

What the code you have posted does is randomly flash the red, green and blue LED elements. What colors can you get from doing this?

The obvious answers are red, green and blue if only one element is on. If all three are on at once, you get white depending on the color balance.

If both blue and red are on at the same time, you get purple. And if both blue and green are on, you get cyan. Call it light blue if you don't know what color cyan is. If red and green are on...wait for it...you get yellow.

So, of the 7 possible combinations, one of them gives you a desirable color. Six of the color combinations are crap. Or, in other words, all but one of the choices gives you something that doesn't look like a candle.

So, you can either figure out how to use PWM to get acceptable colors, or you can understand what the code you've posted does (wish is not what you wish it to be) and what a candle actually looks like, and replace the RGB LED with three yellow LEDs. This won't give you color variation but it will provide changing intensity and an illusion of motion.

Using yellow LEDs also allows expansion to three independant candles using separate port pins.[/B]
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top