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


Results 1 to 11 of 11

Threaded View

  1. #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.

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