12F1822 HPWM Duty Not Changing


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default 12F1822 HPWM Duty Not Changing

    Can someone cast an eye over this code please.

    8mhz pic. Trying to generate 200hz HPWM.

    Duty seems stuck at about 60% whatever I try even when it's supposed to be off and idling high with

    Duty set at 255. Scope shows pwm is working at 200hz but I can't vary the duty.

    Does the Duty variable have to be a WORD?

    Confused.

    Code:
    #config
    
     __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOREN_OFF & _CLKOUTEN_OFF & 
    
    _FCMEN_OFF
     __config _CONFIG2, _PLLEN_OFF & _LVP_OFF
    
    #ENDCONFIG  
    
    DEFINE OSC 8			'Set PicBasic Pro processor speed to 8 Mhz (Must match oscillator 
    
    value)  
    OSCCON = %01110010      'Set osc 8mhz and stable
    
    TRISA  = %00101001		'PortA Output on Pin A1,A2,A4 & Input on A0,A3,A5
    ANSELA = %00000000      	'All Digital I/O 
    APFCON = %10000100      	'Set Tx/Rx function to RA4 & RA5
    ADCON0 = %00000000		'ADC Disabled
    
    OPTION_REG = %01111111		'Global Enable Weak Pull Ups
    WPUA   = %00011011		'Weak Pull Up enabled on Ports A0 A1 A3 A4 
    
    DEFINE HSER_BAUD 10400		'Set Baud rate to 10400bps
    DEFINE HSER_BITS 9		'Set to 9 bit mode
    DEFINE HSER_EVEN 1		'Set Even Parity
    DEFINE HSER_CLROERR 1 		'Clear overflow error automatically
    
    DEFINE CCP1_REG PORTA 		'HPWM channel 1 pin Port A Fan PWM Driver
    DEFINE CCP1_BIT 2 		'HPWM channel 1 pin Bit 2
    
    '*******************************************************************************
    
    'Variables Constants and I/O definitions
    
    '------------------------ Variables 16bit --------------------------------------
    
    
    
        FanSpeed var byte           'Ipu Fan Speed Pwm Setting
    
    
    Start:
    
        
        FanSpeed = 255
               
        HPWM 1, FanSpeed, 200   	'Change/Start 200hz HPWM 'Set Duty 0 = Off 255 = FullOn  
        
        end

  2. #2
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: 12F1822 HPWM Duty Not Changing

    I haven't worked through your code in detail, but the HWPWM is 10 bits wide so 50% dutycycle would be 512. I also don't see a lot of configuration code, but I'm not familiar with PBP3. PBP 2.6 doesn't have a HWPWM command.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,651


    Did you find this post helpful? Yes | No

    Default Re: 12F1822 HPWM Duty Not Changing

    Did you verify the minimum available PWM freq in your manual ???

    just an idea ...

    Alain
    Last edited by Acetronics2; - 11th April 2013 at 14:11.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: 12F1822 HPWM Duty Not Changing

    I agree I thought lowest frequency would have to be higher, but when set at 200hz with 8mhz pic scope shows output at 200hz but changing the duty variable does not change the duty :?

  5. #5
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: 12F1822 HPWM Duty Not Changing

    charlie, i'm using 2.6 and I can use HPWM, I dont see why you have to define ccp1, heres an example code for a 683 chip, reading a ADC and outputting pwm based on reading.

    Code:
    DEFINE ADC_BITS 10 ' A/D number of bits
    DEFINE ADC_CLOCK 1 ' Use A/D internal RC clock
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in us
    
    RES1 Var Word ' A/D converter result
      
    TRISIO.0 = 1 ' AN0 is input
    TRISIO.2 = 0 ' PWM1 is output
    
    AGAIN:
    ADCIN 0, Res1 ' Read Channel 0 data
    res1 = res1 / 256 ' 0-255
    
    if res1 < 10 then res1 = 0 ' Low Auto Shutoff
    
    HPWM 1, res1, 18000 ' PWM Pin, Setting, Frequency (20k Max)
    pause 10
    goto again
    
    end
    So with no defines it works fine, when HPWM command is used it will turn on the correct port and adjust the settings. If you dont use the hpwm it fuctions as a normal i/o port.

    you shouldnt have to define CCP1, its hardcoded into that chip to begin with, CCP1 will always be on PORTA.2 to begin with, unless you force a change if allowed to move to a different pin (which yours does, but only if you tell it too) NOTE that I set my PWM output pin to a 0 as output, you dont have to do this either I dont believe, unless you want to use it as a I/O before engaging the PWM. It works so I havent changed it. Unless you decide to change output pins, or enable the other 4 hpwm modules on this chip, you shouldnt have to use anything but base code to set pwm. Oh and about the duty cycle, I believe if you dont set it to 10 bit it defaults to 8bit, the documentation says it can handle up to 10 bit and theses a section on how to adjust registers to allow for it. but if you dont change them... they should work as default from 0-255.
    Last edited by wdmagic; - 11th April 2013 at 16:39.
    Chris


    Any man who has accomplished anything in electronics at one time or another has said... " STOP! WAIT! NOOO! Dangit.... Oh Well, Time to start over..."

  6. #6
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: 12F1822 HPWM Duty Not Changing

    You were right - for some reason I typed HWPWN instead on HPWM, so I couldn't find it in the 2.6 manual. I have always configured by hand, and have never used this command. I also always use 10 bits.
    I still recommend checking that all the configuration is correct. I have also seen issues with low frequencies on this device. Why not set it to 2K to test that's not the issue here, just to eliminate that possibility?

Similar Threads

  1. 12F1822 troubles
    By Charlie in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th June 2012, 12:53
  2. MIBAM and 12F1822...
    By alesniak in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 17th September 2011, 06:45
  3. Replies: 14
    Last Post: - 24th April 2011, 22:03
  4. Changing Freq of HPWM
    By Rayneobid in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 12th April 2005, 18:55
  5. HPWM on a 628 is not changing freq.
    By dtit in forum General
    Replies: 2
    Last Post: - 25th February 2005, 09:34

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts