Thank you for the reply mackrackit. What I'm actually doing is taking a 3 position knob say (slow, medium, and fast speed settings) I bring 2 of the settings into a dual opto on a pcb and basically turn it into an analog signal using a set of four resistors on the output. For example slow speed would give me a close to 0V on the pin of the PIC. Medium means niether opto is on and gives 2.5V to the PIC. Fast speed turns the bottom opto on giving close to 5V signal on the PIC. Analog Low I have defined as 256 decimal or 1.25V on the pin. Analog high I have defined as 768 or 3.75V. It's just a way I tried to use 3 digital input as one analog to save I/O ports. I hope this is a good explaination. If you would like to see the circuit I can share that as well.
I have done this PWM before but with much less code so I went back to the basics. Here is the code that I have running right now and does work correctly.
There are some useless variables in there because I commented most of the code out. I'm going to slowly try to incorperate the rest in now since I know this basic works. One thing I just noticed is that when I pull the pot wiper wire out of AN5 the DC goes back to 50%, not sure why but I will figure it out. Thanks again and I hope to here from you soon because this is driving me nuts. I know it's something simple.Code:'-------------------------------------------------------------------------' ' PIC Defines Specific to PIC Basic Pro ' '-------------------------------------------------------------------------' ' 1. Use internal system clock @ DEVICE PIC16F684, INTRC_OSC_NOCLKOUT ' 2. Turn watchdog timer on @ DEVICE PIC16F684, WDT_OFF ' 3. Power on time on @ DEVICE PIC16F684, PWRT_ON ' 4. Turning off the master clear input @ DEVICE PIC16F684, MCLR_OFF ' 5. Brown out detect on @ DEVICE PIC16F684, BOD_ON ' 6. Data EE Read protect is off @ DEVICE PIC16F684, CPD_OFF ' 7. Code protect is off @ DEVICE PIC16F684, PROTECT_OFF '-------------------------------------------------------------------------' ' Hardware Defines Specific to PIC Basic Pro ' '-------------------------------------------------------------------------' define ADC_BITS 10 'Set A/D coverter to use 10 bits define ADC_SAMPLEUS 50 'Set A/D sample time as 50us DEFINE OSC 8 'Change clock speed to 8Mhz '-------------------------------------------------------------------------' ' Define Program Variables ' '-------------------------------------------------------------------------' R VAR BYTE ScaledX VAR BYTE ScaledY VAR BYTE ShortX VAR BYTE ShortY VAR BYTE Slope VAR WORD AnalogLow VAR WORD 'Digital value for an analog low signal AnalogHigh VAR WORD 'Digital value for an analog high signal CurrentSpeed VAR WORD DUTY VAR WORD ExtLimit VAR WORD 'Stored value of the extend limit setting JoyX VAR WORD JoyY VAR WORD RetLimit VAR WORD 'Stored value of the retract limit setting Position VAR WORD 'Analog value of the internal potentiometer Seeking VAR WORD Speed VAR WORD '-------------------------------------------------------------------------' ' Define I/O and EEPROM Definitions ' '-------------------------------------------------------------------------' ' The following 3 Data Commands write the Program Name and Revision ' number on the chips EEPROM in Location D0 - FF. This can be read ' by placing the chip in the programmer, reading it with MPLAB, ' and viewing the EEPROM. DATA @208, "BadBoy Mowers " DATA @224, "U3 Double " DATA @240, "Rev. 1 A.S. " data @0,word 768 'Set default extend limit data @2,word 256 'Set default retract limit AnalogHigh = 768 'Set analog high signal level AnalogLow = 256 'Set analog low signal level ' READ 1, RetLimit.Highbyte ' READ 0, RetLimit.Lowbyte ' READ 3, ExtLimit.Highbyte ' READ 2, ExtLimit.Lowbyte '-------------------------------------------------------------------------' ' Initialise PIC Hardware ' '-------------------------------------------------------------------------' CMCON0 = 7 'Turn comparators off ADCON0.7 = 1 'Set A/D Values to be right justified ADCON0.0 = 1 'Turn on A/D Converter ANSEL = %00110111 'Turn on select analog registers TRISA = %00001111 'Turn on select register inputs TRISC = %00111111 'Turn on select register inputs PR2 = 249 CCP1CON = %11001100 CCPR1L = 0 PIR1.1 = 0 T2CON.0 = 0 T2CON.1 = 0 T2CON.2 = 1 WHILE (PIR1.1 = 0) WEND; TRISC = %00000011 '-------------------------------------------------------------------------' ' Start of Main Program ' '-------------------------------------------------------------------------' Main: ADCIN 5, Speed Speed.lowbyte = ADRESL Speed.highbyte = ADRESH CCP1CON.4 = Speed.0 CCP1CON.5 = Speed.1 CCPR1L = Speed >> 2 GOTO Main




Bookmarks