Hello,
I'm new to this forums and programming microcontrollers
I've done it for a while however I don't consider my self an expert in fact my programming is quite dirt and have some redundancies, anyway.
I have this problem with a PIC16F1718 (I almost know the datasheet from top to bottom), we've been requested to control duty cycle and frequency of a PWM signal (no homework already working but not as I wanted).
So I have assigned POT1 to control frequency and POT2 to control duty cycle and corresponds to AN0 and AN1, the problem is that POT1 is controlling duty cycle and POT2 is controlling frequency.
I have tried countless attempts to fix this like increasing acquisition time from 1us to 10ms, I have read tons of application notes but no one provides something helpful.
Before I post the code I will point out details:
I use MPLABX and XC8 compiler, I like programming register by register so everything is understandable.
Since I use the crappy set up stated above, the dumb compiler do not recognize functions I declare (I also like to do mine makes code look cleaner but with this compiler simply is not allowed or valid).
Here is the code, I will only post the ADC part because of personal reason I hope its enough and if it isn't I will answer any questions.
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| while(1)
{
__delay_ms(1);
ADIF=0; //clear this flag just in case
ADON=0; //turn off adc
ADCON0=0x04; //Switch channel AN1
ADON=1; //turn on adc
GO=1; //start conversion
while((ADCON0 & (1<<1))==1); // end conversion?
z=ADRES/4; //DECREASE RESOLUTION
PWM3DCH=z; //Duty
// PWM3DCL CAN MANTAIN CONSTANT VALUE like 0xa0 no other action is needed.
__delay_ms(1);
ADIF=0;
ADON=0;
ADCON0=0x00; //Switch channel AN0
ADON=1;
GO=1;
while((ADCON0 & (1<<1))==1);
f=(ADRES)/4;
PR2=f; //frequency
}
} |
I'LL APPRECIATE ANY SUGGESTION