Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC ADC read overlapping HEELP please.

Status
Not open for further replies.

dredd9

Newbie level 3
Joined
Apr 28, 2016
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
52
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


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:

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
 
Last edited by a moderator:

LOL silly me all the code is wrong I finally got it.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top