Cantafford
Member
Hello,
Quick question. I want to learn how to control a duty cycle with an analogue value that I convert to a digital one via ADC module.
In my case I'm trying to set the duty cycle of a PWM signal for a DC motor with the value converted by the ADC. I will be using a potentiometer to modify the value.
The duty cycle of my motor is a 10 bit value: CCPR1L and CCP1CON(5:4).
The value of the ADC is a 10 bit value: ADRESH and ADRESL(I will be using left-justified mode so I can get a 10 bit resolution)
I have searched some examples and to my understanding is I have to read the digital value converted by the ADC(which is CCPR1L and CCP1CON registers) and feed it to the duty cycle(ADRESH+ADRESL).
Now I'm guessing that I have to read the value from (ADRESH+ADRESL) convert it to binary then feed it to (CCPR1L and CCP1CON(5:4)). Since for the duty cycle I need only bits 5:4 of CCP1CON, how can I do to not change the others bits when I pass my desired value?
I can get the result of the ADC like this with the function: ReadADC(); provided by the compiler
But then how can I store this result in the duty cycle registers?: CCPR1L and CCP1CON(5:4)
Quick question. I want to learn how to control a duty cycle with an analogue value that I convert to a digital one via ADC module.
In my case I'm trying to set the duty cycle of a PWM signal for a DC motor with the value converted by the ADC. I will be using a potentiometer to modify the value.
The duty cycle of my motor is a 10 bit value: CCPR1L and CCP1CON(5:4).
The value of the ADC is a 10 bit value: ADRESH and ADRESL(I will be using left-justified mode so I can get a 10 bit resolution)
I have searched some examples and to my understanding is I have to read the digital value converted by the ADC(which is CCPR1L and CCP1CON registers) and feed it to the duty cycle(ADRESH+ADRESL).
Now I'm guessing that I have to read the value from (ADRESH+ADRESL) convert it to binary then feed it to (CCPR1L and CCP1CON(5:4)). Since for the duty cycle I need only bits 5:4 of CCP1CON, how can I do to not change the others bits when I pass my desired value?
I can get the result of the ADC like this with the function: ReadADC(); provided by the compiler
Code:
ADCResult = ReadADC(); // Read the converted value
result = (ADCResult*5.0)/1024;
Last edited: