guys,here are the program draft that try to simulate but it cant work.
so i not sure how to program it
the concept is like this,infront of car there will be a IR sensor which detect distance infront of car,if distance is decrease the speed of motor will decrease also,and vise verse.
here are part of the program.
unsigned int temp_res;
void main() {
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISB = 0x3F; // Pins RB7, RB6 are outputs
TRISD = 0; // PORTD is output
T2CON = 0b00011110; //set up PWM
CCP1CON = 0b00001100;
CCP2CON = 0b00001100;
CCPR1L = CCPR2L = 0; //initial PWM is zero
do {
temp_res = Adc_Read(2); // Get results of AD conversion
PORTD = temp_res; // Send lower 8 bits to PORTD
PORTB = temp_res >> 2; // Send 2 most significant bits to RB7, RB6
} while(1);
}
I looked at the compiler manual as did you. My question is why are you using the supplied ADC functions and not the supplied PWM functions. My guess is that they would work well together.
This is the way you can combine the two examples from the compiler manual. You still need to figure out what value to set the PWM duty to based on the ADC reading. I did not provide header files or fuse settings. These you should be able to figure out.
Code:
void main(void)
{
// setup ADC
ADCON1 = 0x80; // Configure analog inputs and Vref
TRISA = 0xFF; // PORTA is input
TRISB = 0x3F; // Pins RB7, RB6 are outputs
TRISD = 0; // PORTD is output
// setput PWM
TRISC=0x00;
PORTC=0xFF;
Pwm_Init(???);
Pwm_Start();
while(1)
{
// code to read ADC
.....
// code to set PWM duty cycle
....
Pwm_Change_Duty(???);
}
}
I do not use this compiler but it looks lke this should work.