Tinkerer of Things
I posted all the code. And its mikroc version 7.2 and used Proteus 8 to simulate
Most of the members on this forum do not use the MikroElektronika IDE or compilers.
There are several reasons for this. Most common is that the MikroC IDE seems to hide the configuration setting that are frequently where new PIC developers encounter problems.
A lot of forum members will avoid even trying to answer questions about projects using MikroC because it can be too hard to provide a complete project that just works for the new kid.
Many times a new PIC developer will get hung up with some trivial settings issue in the IDE, compiler configuration or PIC configuration words.
The code you posted:
unsigned int ADCresult = 0; // declared variables
unsigned int PWM = 0;
PortA = 0; // clear port a
TRISA0_bit = 1; // set A0 as input
PortC = 0; // clear port c
TRISC2_bit=0; // set C2 as output
PortE = 0; // clear port e
TrisE1_bit= 1; // set E1 as input
ADCON0=0b00000001; // A/D converter module is powered up
ADCON1=0b11001110; // right justified, fosc/4 , and only AN0 is analog
CMCON=7; // disabled comparator mode
ADC_Init(); // initialize adc
PWM1_Init(5000); // initialize pwm1
PWM1_Start(); // start pwm1
while(1) // while 1 loop forever
if(RE1_bit==0) // if RE1 is brought low
Delay_ms(50); // software de-bounce
ADCresult = ADC_Get_Sample(0);
PWM1_Set_Duty(PWM); // update PWM DC obtained from ADC on AN0
Delay_ms(3000); // use updated PWM DC
PWM1_Set_Duty(250); // Revert back to near max DC
while(RE1_bit==1); // Pause here till RE1 is brought low again
Leaves too many things about the ADC implementation uncertain.
These are things you should be definite about:
1) The target PIC part number. By implication this is found in the Proteus schematic you posted. It is more helpful to put a comment line in the code like: "// Target: PIC16F877A"
2) The PIC you selected can use a system oscillator frequency up to 20MHz. For someone to be able to help you by checking that you have configured the ADC clock it is important to know your actual value.
3) Your code calls MikroC functions: ADC_Init(), ADC_Get_Sample(), PWM1_Init(), PWM1_Start(), PWM1_Set_Duty(). It is not obvious what these function do in detail or if you are using them correctly.
post edited by dan1138 - 2019/04/22 12:40:34