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?
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!
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....
....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?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
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.
Never mind...
Last edited by mtripoli; - 20th April 2011 at 22:11.
Well, for all my quick 'n dirty the workaround reverts my pin9 (PortC.1) ADC back to how it should be...
....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.Code:HPWM 2, duty, 32767 TRISC.1 =1 'Remake PIN 9 PortC.1 an input again following HPWM command
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.
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.
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.....
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.
HTH,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
Ioannis
Bookmarks