Hello again everyone. I have not been on in a long time. I have new project I'm working on and can't seem to get the solution I'm looking for. Bascially it is suppose to set one of 3 DC for the PWM, and continuously re-locate the motor based on the position of some other pieces. I try to set up the PW registers at the start and only monitor the DC (Duty Cycle) and direction throughout the rest of the code. I'm using a PIC16F684 and would like to stick with registers and not hpwm. Mister E thank you for your simple yet great multi-calc program. I also double checked all of the register values before using them so I know they are correct. Here are the things I'm noticing with this software.
1. The frequency the PWM is running at is suppose to be 8kHz and is only at 4kHz.
2. The DC only works in the middle range, below shown as 717. Surrounding values do not work.
3. Sometimes it seems as if there are sections of 0% DC popping up.
Below I tried to give as much of the code as I could without getting to long. I've read through the data sheet and app notes many time now and can't seem to find where I'm messing this up. Is it possible I'm trying to update the DC to quickly? Any help from anyone is greatly appreciated, please ask me if you need any more information. I can post the entire code if anyone needs it.
My test is just on the chip with a scope, telling the motor to go to a point it can never reach. This should give me a constant set of pulses at the same DC forever. I have a pot conected to ADCIN 5 to change the DC but like I said before when I move out of the mid range the signal out drops to 0% DC.
Code:
'-------------------------------------------------------------------------'
' 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
'-------------------------------------------------------------------------'
' Initialise PIC Hardware '
'-------------------------------------------------------------------------'
CMCON0 = 7 'Turn comparators off
ANSEL = %00110111 'Turn on select analog registers
TRISA = %00001111 'Turn on select register inputs
TRISC = %00111111 'Turn on select register inputs
ADCON0.7 = 1 'Set A/D Values to be right justified
ADCON0.0 = 1 'Turn on A/D Converter
PR2 = 249
CCP1CON = %11001100
CCPR1L = 0
PIR1.1 = 0
T2CON.0 = 0
T2CON.1 = 0
T2CON.2 = 1
WHILE (PIR1.1 = 0)
WEND;
TRISC.5 = 0
TRISC.4 = 0
TRISC.3 = 0
TRISC.2 = 0
Main:
ADCIN 5, Speed
Speed.lowbyte = ADRESL
Speed.highbyte = ADRESH
IF (Speed > AnalogLow) & (Speed < AnalogHigh) THEN
DUTY = 717
ENDIF
IF (Speed < AnalogLow) THEN
DUTY = 512
ENDIF
IF (Speed > AnalogHigh) THEN
DUTY = 922
ENDIF
.
..
.... (To much to insert here)
.......
..........
TimeToMove:
ADCIN 2, Position
Position.LowByte = ADRESL
Position.HighByte = ADRESH
If (Position < Seeking + 10) OR (Position > Seeking - 10) THEN
DUTY = 0
CCP1CON.4 = DUTY.0
CCP1CON.5 = DUTY.1
CCPR1L = DUTY >> 2
Goto Main
ELSE
IF (Position > Seeking + 10) THEN
CCP1CON.7 = 1
CCP1CON.4 = DUTY.0
CCP1CON.5 = DUTY.1
CCPR1L = DUTY >> 2
GOTO MAIN
ENDIF
IF (Position < Seeking - 10) THEN
CCP1CON.7 = 0
CCP1CON.4 = DUTY.0
CCP1CON.5 = DUTY.1
CCPR1L = DUTY >> 2
Goto Main
ENDIF
ENDIF
GOTO Main
Bookmarks