main()
{
PORTC = 0;
CMCON0 = 7; // Turn off Comparators
ANSEL = 1 << 3; // RA4 (AN3) is the ADC Input
ADCON0 = 0b00001101; // Turn on the ADC
// Bit 7 - Left Justified Sample
// Bit 6 - Use VDD
// Bit 4:2 - RA4
// Bit 1 - Do not Start
// Bit 0 - Turn on ADC
ADCON1 = 0b00010000; // Select the Clock as Fosc/8
TMR2 = 0; // TMR2 Provides PWM Period
PR2 = 64; // 15 kHz PWM Frequency
T2CON = 0b00000100; // Enable TMR2
CCPR1L = 0; // 0 Duty Cycle to Start Off
while(1 == 1)
{
NOP();
for (Dlay = 0; Dlay < 6666; Dlay++); // 100 ms between Samples
NOP();
GODONE = 1; // Read Pot Value
while (GODONE);
ADCValue = ADRESH; // Read in ADC Value
if (ADCValue > 0x80) // go Forwards
{
CCPR1L = (ADCValue - 80) >> 1;
CCP1CON = 0b01001110;
TRISC = 0b011011; // RC5/RC2 Output, RC3/RC4 Input
}
else // Go in Reverse
{
CCPR1L = (ADCValue ^ 0x7F) >> 1;
CCP1CON = 0b11001110;
TRISC = 0b100111; // RC5/RC2 Output, RC3/RC4 Input
} // fi
} // elihw
} // End cMotor