16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    I have a 16F1824 PortC.3 (ie pin 9) set as an analogue ADC input pin...it works fine, until my program code gets reaches this line...

    Code:
    HPWM 2, duty, 32767
    (where duty is a variable representing erhm...the duty cycle)

    once that point is reached, my PortC.3 apparently flips to a digital pin....ie when I vary my analogue input signal into PortC.3, I observe only a '0' or a '1' as the incoming Signal level.

    If I use the following as a workaround on each occurence...

    Code:
    HPWM 2, duty, 32767
    TRISC =  %11110111     ' PIN 7 RC3 HPWM  OUTPUT
    ...then all is well again (my ADC readings are as expected....ie between 0 & 255)

    Now I've never used the 2nd channel of PWM before (ie I've always just used one channel...."HPWM 1, duty, 32767"), so it's likely something simple I'm doing wrong, but I've looked at this so long now that the number of the beast starts appearing in my main loop. (which probably means it's time to get a fresh pair of eyes on the case)

    Any ideas?

    Here's the full test code...

    REDRUM, REDRUM,REDRUM,REDRUM,REDRUM, REDRUM,REDRUM,REDRUM,REDRUM, REDRUM,REDRUM,REDRUMREDRUM, REDRUM,REDRUM,REDRUM



    Code:
    @ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF & _LVP_OFF
    @ __CONFIG _CONFIG2, _LVP_OFF
    
    DEFINE  OSC 4   
    
    Osccon = %01101010   'sets the internal oscillator to 4Mhz
    
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 2
    DEFINE DEBUG_REG PORTC
    DEFINE DEBUG_BIT 4
    
    DEFINE ADC_BITS  8     ' ADCIN resolution  (Bits)
    DEFINE ADC_CLOCK 1     ' ADC clock source  (Fosc/8)
    DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)
    
    ADCON0 = %00010101    'AN5 (Pin 9) Enabled for BACKGROUND adc'ING VIA SPECIAL EVENT TRIGGER  + ADC On
    ADCON1 = %01000011     'set ADC +ve ref to be the internal Fixed VREF   
    FVRCON = %11000010     'turn fixed voltage reference on & set it to 2.048V (last two bits set the voltage ...01 =1.024V,10=2.048
    
    
    CM1CON0 = 0   ' COMPARATORS OFF
    CM1CON1 =0
    CM2CON0 = 0   ' COMPARATORS OFF
    CM2CON1 =0    
    CPSCON0 = 0   'CAPACTIVE SENSE MODULE OFF
    RCSTA.7 = 0   'SERIAL PORT OFF
    
    TRISA =  %11111111
    TRISC =  %11110111     ' PIN 7 RC3 HPWM  OUTPUT
    ANSELA = 0
    ANSELC = 0
    
    CCP2CON    = %00001100    'NOT ENTIRELY CONFIDENT ABOUT THIS ONE!
    
    Sig_In          VAR BYTE  ; a variable to store to DC level coming in on pin 19 (RA0)
    duty            var word
    latest_sample   var word
    
    
    'setup the AtoD & special event register to capture samples in the background
    T1CON = %00000001         ' timer1 on
    CCP4CON = %0001011     'set CCP4 to special event trigger.
    
    CCPR4H = 0         ' preset for timer1 match (MSB register)
    CCPR4L = 250       '  @16Mhz, 200 gives 20,000khz sample & 250 gives 16khz sample.
    
    duty = 255
    HPWM 2, duty, 32767
    TRISC =  %11110111     ' PIN 7 RC3 HPWM  OUTPUT
    
    TEST: 
    latest_sample = ADRESH
    PAUSE 500
    debug dec LATEST_SAMPLE,13,10
    GOTO TEST
    Last edited by HankMcSpank; - 20th April 2011 at 20:39.

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    Hank, I am fairly certain you can't use the pin for PWM output and Analog input at the same time. Your work around is not a work around at all. It is multiplexing the pin for both functions.

    There are both on C.3 - Yes?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    Quote Originally Posted by cncmachineguy View Post
    Hank, I am fairly certain you can't use the pin for PWM output and Analog input at the same time. Your work around is not a work around at all. It is multiplexing the pin for both functions.

    There are both on C.3 - Yes?
    Hi Bert.....you see how a fresh pair of eyes helps? :-) now as it goes, I'm not using the same pin for both functions...

    16f1824 Pin 7 (portC.3) is CCP2 (I'm assuming that the Picbasic command "HPWM 2" uses CCP2?)

    16f1824 Pin 9 (PortC.1) is my ADC input pin ...but I can see that that's been set wrong in my code cut/paste above (that pasted code was a quick rip/strip from my monster main program, and there's likely been an error as I've also been rejigging pins to make for a better PCB layout!)

    correction made to my code in bold below....

    Code:
    @ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF & _LVP_OFF
    @ __CONFIG _CONFIG2, _LVP_OFF
    
    DEFINE  OSC 4   
    
    Osccon = %01101010   'sets the internal oscillator to 4Mhz
    
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 2
    DEFINE DEBUG_REG PORTC
    DEFINE DEBUG_BIT 4
    
    DEFINE ADC_BITS  8     ' ADCIN resolution  (Bits)
    DEFINE ADC_CLOCK 1     ' ADC clock source  (Fosc/8)
    DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)
    
    ADCON0 = %00010101    'AN5 (Pin 9) Enabled for BACKGROUND ADC'ing VIA SPECIAL EVENT TRIGGER  + ADC On
    ADCON1 = %01000011     'set ADC +ve ref to be the internal Fixed VREF   
    FVRCON = %11000010     'turn fixed voltage reference on & set it to 2.048V (last two bits set the voltage ...01 =1.024V,10=2.048
    
    
    CM1CON0 = 0   ' COMPARATORS OFF
    CM1CON1 =0
    CM2CON0 = 0   ' COMPARATORS OFF
    CM2CON1 =0    
    CPSCON0 = 0   'CAPACTIVE SENSE MODULE OFF
    RCSTA.7 = 0   'SERIAL PORT OFF
    
    TRISA =  %11111111
    TRISC =  %11110101     ' PIN 7 RC3 HPWM  OUTPUT
    ANSELA = 0
    ANSELC = %00000010      ' AN5 Pin9 (PortC.1) ADC input ....BACKGROUND ADC'ing VIA CCP4 SPECIAL EVENT TRIGGER  
    CCP2CON    = %00001100    'NOT ENTIRELY CONFIDENT ABOUT THIS ONE!
    
    Sig_In          VAR BYTE  ; a variable to store to DC level coming in on pin 19 (RA0)
    duty            var word
    latest_sample   var word
    
    'setup the AtoD & special event register to capture samples in the background
    T1CON = %00000001         ' timer1 on
    CCP4CON = %0001011     'set CCP4 to special event trigger.
    
    CCPR4H = 0         ' preset for timer1 match (MSB register)
    CCPR4L = 250       '  @16Mhz, 200 gives 20,000khz sample & 250 gives 16khz sample.
    
    duty = 255
    HPWM 2, duty, 32767
    TRISC.1 =1   'Remake PIN 9 PortC.1 an input again following HPWM command
    
    TEST: 
    latest_sample = ADRESH
    PAUSE 500
    debug dec LATEST_SAMPLE,13,10
    GOTO TEST
    ....still the same though, when the "HPWM 2" line is reached, my ADC doesn't read properly. I have to reset PortC.1 back to an input, so it looks like the "HPWM 2" command is turning PortC.1 into an output....which for the life of me, I can't understand why that would happen?

    Now there'a lot of features in this new(ish) PIC and it's likely something I'm not grasping or seeing, but like I say...way too many hours trying to nail this one.......I now need all the help I can get!
    Last edited by HankMcSpank; - 20th April 2011 at 21:20.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    Never mind...
    Last edited by mtripoli; - 20th April 2011 at 22:11.

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    Well, for all my quick 'n dirty the workaround reverts my pin9 (PortC.1) ADC back to how it should be...

    Code:
    HPWM 2, duty, 32767
    TRISC.1 =1   'Remake PIN 9 PortC.1 an input again following HPWM command
    ....the actual HPWM command itself is still playing havoc with the signal....when I feed a steady sine wave into the ADC pin, it getting chopped up as viewed on a scope, as when the HPWM 2 command runs it interferes with PortC.1 - my now (naive) thinking is that it's forcing the pin to be an output, which then drags my sine wave analogue input signal down to zero, then my 'kludgey' follow on command makes the pin an input again...which restores the signal. But you can imagine the end result, it's as if a switch is being flicked rapidly thereby shunting the signal to ground .....fugly.

    i'm exhausted on this one & don't have an immediate good solution...I need to sleep on it (still a high possibility I'm just doing something wrong as my head is fuzzy now!). Tomorrow, I may sit down & work out how do do HPWM longhand using the PICs registers (one whole days lost on something as simple as this....eek!)
    Last edited by HankMcSpank; - 20th April 2011 at 23:31.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    DEFINE CCP2_REG PORTC
    DEFINE CCP2_BIT 3

    Should take care of it. PBP defaults to PORTC.1 for CCP2 if you don't tell it otherwise.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    Thanks Bruce ...that did it....you've just made an impossibly bad day...just bad now

    Like I say it's my first time using the 2nd HPWM channel ....but also my first time using this newish PIC in anger - thought i was going crazy.

    PWM, SHmeeWM.....

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,809


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    These new 16F18xx chips are small in the eye but really beasts!

    I had my difficult time in setting them too, but nowI really like them.

    Here is my setup for the 1827 which may help others to take into accoun the registers and not forget any of them.

    Of course this is my setup and other need to make their own. It just as an example.

    Code:
      TRISB = %11000101
      TRISA = %11111100
      
      ADCON0 = %00000001
      ADCON1 = %11110011
    
      ANSELA = %00011100           ' all digital. A/D disabled
      ANSELB = %01000000
      
      APFCON0 = %00100000
      APFCON1 = %00000001
        
      BAUDCON = %00011000
      
      CCP1CON = %00111100          'Control of CCPx modules
      CCP2CON = %00000000
      CCP3CON = %00000000
      CCP4CON = %00000000
    
      CM1CON0.7=0
      CM2CON0.7=0
      
      CPSCON0 = 0
      CPSCON1 = 0 
      
      DACCON0 = 0
      
      FVRCON = %10000011
      
      INTCON = %11000000   ' global & peripheral ints enabled
      
      OPTION_REG = %10000111    'Tmr0 from 256 presc and Int Clock
      
      PIR1 = 0             ' clear TMR1 int flag                     
      PIE1 = %00000001     ' TMR1 int enabled
      
      T1CON = %00110000    ' TMR1 1:8 prescale, timer1 on
      
      WPUB=%11000000
      
      'hpwm 1,0,2000
      DEFINE CCP1_REG PORTB
      DEFINE CCP1_BIT 3    
      ccptmrs=0
      pr2=249
      ccp1con=$0C
    HTH,

    Ioannis

  9. #9
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    I concur...the 16F182x are a great range of chips...absolutely chocka with features.

    I can't say I've had a lot of setup problems with them (certainly nothing that wasn't obvious when I complied)....other than this pesky HPWM 2 issue, the one other issue I had Darrel kindly helped sort....

    I needed to modify the 16F1824.INC file (mods in red)....

    Code:
    ;****************************************************************
    ;*  16F1824.INC                                                 *
    ;*                                                              *
    ;*  By        : Leonard Zerman, Jeff Schmoyer                   *
    ;*  Notice    : Copyright (c) 2010 microEngineering Labs, Inc.  *
    ;*              All Rights Reserved                             *
    ;*  Date      : 09/14/10                                        *
    ;*  Version   : 2.60b                                           *
    ;*  Notes     :                                                 *
    ;****************************************************************
            NOLIST
        ifdef PM_USED
            LIST
            "Error: PM does not support this device.  Use MPASM."
            NOLIST
        else
            LIST
            LIST p = 16F1824, r = dec, w = -302
            INCLUDE "P16F1824.INC"	; MPASM  Header
            ;__config _CONFIG1, _FOSC_HS
            ;__config _CONFIG2, _PLLEN_OFF & _LVP_OFF
            NOLIST
        endif
            LIST
    BLOCK_SIZE	EQU	32
    CCPTMRS0 = CCPTMRS
    The first two mods (semi colons) are standard fayre, but that last one would have taken me a good few years to get to the bottom of on my own.
    Last edited by HankMcSpank; - 21st April 2011 at 11:30.

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,809


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    What version of MPLAB do you have?

    On my 8.56 and in 1827.inc file of the MPLAB Suite there are two lines (BANK5 grroup):

    Code:
    CCPTMRS          EQU  H'029E'
    CCPTMRS0         EQU  H'029E'
    But on 1824.inc there is only one:

    Code:
    CCPTMRS          EQU  H'029E'
    So it may have to do with the update of MPLAB. Anyone with newer version?

    Ioannis

  11. #11
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: 16F1824 "HPWM 2" woes (command seemingly forces PortC.3 from analogue to digital)

    I'm running with V8.63.

Members who have read this thread : 1

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