Erratic behavior on a HPWM on a 16F88


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default Erratic behavior on a HPWM on a 16F88

    HI,
    Wrote a short program to be able to input two ADCIN so I could have a variable duty cycle and variable frequency at my HPWM.
    Duty cycle works fine, but the frequency output is very erratic,
    example at 1v I get 500Hz
    1.2v 2.2Khz
    1.3v 300Hz
    1.4v 620Hz
    I mean the frequency is all over the place, , here is my code:
    Code:
     
    INCLUDE "modedefs.bas" 'Includes support for PicBasic language
    
    OSCCON = %01110000 '8 Mhz
    DEFINE OSC 8
      
    CMCON = 7 
    
    ' Define ADCIN parameters
    Define ADC_BITS 8 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 ' Set sampling time in uS
    
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 0 
    
    ANSEL = %00000011 ' set  AN0 & AN1  as analog, others to digital
    
    ADCON1 = %00000010 ' Set PORTA analog and RIGHT justify result
    
    TRISB = %01000010
    TRISA = %00000111
    
    DutyCycle var byte
    Frequency var byte
    
    Mainloop:
      'ADCON0 is binary '11000001' to read from AN0, and binary '11001001' to read from AN1.
    ADCON0 = %11000001			'Start Conversion
    pause 100
    ADCIN 0, DutyCycle  'Read channel PORTA.0 Duty Cycle
    pause 30
    
    ADCON0 = %11001001			'Start Conversion
    pause 100
    ADCIN 1, Frequency  'Read channel PORTA.1 Frequency
    pause 30	
    
            HPWM 1,DutyCycle,Frequency  'channel, dutycycle, frequency
            pause 20
            
    goto Mainloop
    END
    Any ideas ??

    thanks
    ken

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


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Hi, Ken

    1) just modify Duty and Frequency ... ONLY if pots inputs changes.
    no need any PAUSE then ...

    2) "Frequency" is a Byte ... and the minimum available frequency is ... 489 Hz !!!
    rings you a bell somewhere ???



    Alain
    ************************************************** ***********************
    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 " !!!
    *****************************************

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Do you need PAUSE 100 after setting ADCON0?

    Robert

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Alain,
    why should it change the data on 'only' if POT changes. It still should give me the same value and not going from 100hz then 2khz !!!
    Y I have 256 bits so I SHOULD BE GETTING MAX 256 Hz .......
    Like I said the duty cycle works fine, on both AN0 And AN1 !!
    I will try the Dont change thing, because i have no more idea
    ken

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    I am getting 1200 Hz with this HPWM command:
    HPWM 1,DutyCycle,350 'channel, dutycycle, frequency
    Do not make sense..... My OSCON is good at 8Mhz

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Try fixed values just to test setup.

    Robert

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


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    could it be a ADCON1 issue, maybe its not justifing? are you using a crystal or internal osc? throwing ideas here.
    Do you have to set CCP1CON

    CCP1CON = %00??1111
    see page 81

    oh and you need to modify TRISB = %01000010 to TRISB = %01000011
    see page 82, your pwm port must be configured as a input port.
    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..."

  8. #8
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    changed the TRIS but that didn,t do it,
    I only have this line of code:
    HPWM 1,127,150 'channel, dutycycle, frequency
    pause 20
    and its giving me 8.3kHz at 50% DC ......

  9. #9
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Could it be your defines for your ADC being in mixed case? I was reschooled in that recently.

    On your OSCCON statement, are the last 4 bits what you want? Assuming you're using an internal oscillator, Bit 3 should be 1 for internal clock, Bit 2 should be 1 for stable clock, Bits 0-1 as 00 uses Fosc to set oscillator.

    ADCON1 bits 0-3 are undefined. I'm assuming you want your Vref to be right justified with Vref and AVss so the ADCON1 should look like this 10110000. It says in the data sheet you need to set ANSEL AN2 and AN3 to inputs to use Vref. So make sure to change your ANSEL settings to 00001111 for AN0 to AN3 as inputs.

    I'm also wondering if your ADCON0 bit 2 should be a 1 to start the conversion process. If it's a 0 I wonder if the conversion process will even begin.

    I'm also assuming TRISB is for a part of the program not shown.

    All that said I'm not a true "programmer" and I must just be creating more fodder. Best wishes.

  10. #10
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Hi,
    thanks for answering, changed the OSCON but that did not help,
    this is my program:
    Mainloop:
    hPWM 1,127,150 'channel, dutycycle, frequency
    pause 20

    goto Mainloop
    END
    Therefore no ADCON to worry about, and I'm getting in the Kilohertz output...
    something wrong with the clocking output of the frequency

    K

  11. #11


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Your code is very similar to something I did a long time ago. I haven't tried it but try this:


    OSCCON = $70 '8mhz
    adcon1 = 7 ' set inputs to digital - the adcin command automatically converts them to analog
    @ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON

    DEFINE OSC 8

    CMCON = 7

    TRISB = %01000010
    TRISA = %00000111

    DutyCycle var byte
    Frequency var byte

    Mainloop:
    ADCIN 0, DutyCycle 'Read channel PORTA.0 Duty Cycle
    ADCIN 1, Frequency 'Read channel PORTA.1 Frequency
    HPWM 1,DutyCycle,Frequency 'channel, dutycycle, frequency
    goto Mainloop
    END

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Hi,
    Alain already wrote this in the very first reply but it seems to have gone unnoticed. When using the CCP-module in the PIC to generate a PWM output (which is what the HPWM command does) there's lower limit on the frequency depending on the frequency of the PICs oscillator. For a 14-bit PIC (which the 16F88 is) running at 8MHz that lower limit is 489Hz. All according to the manual. You're trying to set the frequency to 150Hz when it can't go lower than 489Hz, it's probably overflowing.

    And, which Alain also wrote, in the original code you had the Frequency variable declared as a BYTE. That won't work since the lowest possible value you can assign to that variable when the PIC is running at 8MHz is again 489 so you must declare Frequency as a WORD.

    Finally, I seem to remember something about HPWM not being suitable for continous update like this, probably because it sets up the CCP module for PWM each and every time but I'm not sure. I know that Darrel (of course...) wrote a routine better suited for that - and that was able to use the full 10bit resolution when available. Search for HPWM10 or something like that.

    /Henrik.

  13. #13
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: Erratic behavior on a HPWM on a 16F88

    Hi,
    thanks, I must of read it quickly, I thought that was the maximum frequency!!
    thank you
    .. working now with higher frequency

Similar Threads

  1. PIC16F887 erratic behavior with DT interrupts
    By GordonCook in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th October 2012, 02:58
  2. Erratic Behavior on a 18F452
    By markcadcam in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 18th October 2010, 17:20
  3. erratic behavior when using MPASM vs. PM
    By cool_justin in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th June 2006, 01:08
  4. Erratic LCD behavior
    By Travin77 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st April 2006, 19:48
  5. Erratic PIC12F675 behavior
    By russman613 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 27th February 2006, 14:46

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