dredd9
Newbie level 3
Reading 2 Channel ADC overlaps. Help Please
Hello,
I'm new to this forum and programming microcontrollers.
I have programmed some however I'm not an expert, in fact I consider my programming quite dirt.
Anyway I have this problem with a PIC16F1718 (I almost know the datasheet from top to bottom) I'm producing a PWM signal for converters. We were asked to control frequency and duty cycle both with potentiometer this was already done however when I read for example POT1 corresponding to AN0 (frequency) and POT2 corresponding to AN1(duty cycle) they do the inverse POT1 modifies me the duty and POT2 the frequency.
I've done countless attempts like changing time from 1us to 10ms to meet ADC sampling time I have read application notes like hell and none seems to have something useful.
Before I post code I work with the next things:
I use MPLABX and XC8 compiler there is not much difference since I like programming register by register so everything is understandable.
Since I use the crappy set up stated above I cannot implement functions, compiler don't recognize them (I have nothing against functions they actually make code look more cleaner).
PWM specifications:
20Khz to 100Khz . (Yes this is achievable my overlapped PWM does it)
2 pots control duty and frequency.
complementary or full bridge outputs.
I WILL APPRECIATE ANY SUGGESTIONS
Hello,
I'm new to this forum and programming microcontrollers.
I have programmed some however I'm not an expert, in fact I consider my programming quite dirt.
Anyway I have this problem with a PIC16F1718 (I almost know the datasheet from top to bottom) I'm producing a PWM signal for converters. We were asked to control frequency and duty cycle both with potentiometer this was already done however when I read for example POT1 corresponding to AN0 (frequency) and POT2 corresponding to AN1(duty cycle) they do the inverse POT1 modifies me the duty and POT2 the frequency.
I've done countless attempts like changing time from 1us to 10ms to meet ADC sampling time I have read application notes like hell and none seems to have something useful.
Before I post code I work with the next things:
I use MPLABX and XC8 compiler there is not much difference since I like programming register by register so everything is understandable.
Since I use the crappy set up stated above I cannot implement functions, compiler don't recognize them (I have nothing against functions they actually make code look more cleaner).
PWM specifications:
20Khz to 100Khz . (Yes this is achievable my overlapped PWM does it)
2 pots control duty and frequency.
complementary or full bridge outputs.
I WILL APPRECIATE ANY SUGGESTIONS
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include <xc.h> #include "Config.h" #define _XTAL_FREQ 16000000 volatile unsigned char f; //f=199 volatile unsigned char z; int main(void) { OSCCON=0x78; //Intosc working at 16 MHZ ANSELB=0x00; TRISB=0x40; RB4PPS=0x0E; TRISA=0x07; ANSELA=0x07; TRISC=0x00; ANSELC=0x00; INLVLC=0x00; RC4PPS=0x08; RC5PPS=0x09; G1EN=0; COG1DBR=4; COG1DBF=4; //dead band delay on both sides with t= 200ns // Complementary output generator set up COGINPPS=0x0F; COG1CON0=0x54; COG1CON1=0x00; COG1RIS=0x40; COG1RSIM=0x00; COG1FIS=0x40; COG1FSIM=0x00; COG1ASD0=0x00; COG1ASD1=0x00; G1EN=1; PWM3CON=0xA0; PWM3DCL|=0xC0; ADCON1=0xA0; ADCON2=0; while(1) { __delay_ms(1); ADIF=0; ADON=0; ADCON0=0x04; //Switch channel AN1 ADON=1; GO=1; while((ADCON0 & (1<<1))==1); z=ADRES/4; //Duty (Divided to reduce resolution 10Bits to 8 bits) PWM3DCH=z; __delay_ms(1); ADIF=0; ADON=0; ADCON0=0x00; //Switch channel AN0 ADON=1; GO=1; while((ADCON0 & (1<<1))==1); f=(ADRES)/4; // Frequency () PR2=f; } }
Last edited by a moderator: