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


Results 1 to 11 of 11

Threaded View

  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.

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