• AVR Freaks

Hot!Function Declaration

Page: 12 > Showing page 1 of 2
Author
Ugochi
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2019/02/17 03:49:57
  • Location: 0
  • Status: offline
2019/04/25 08:43:24 (permalink)
0

Function Declaration

Pls, what's the functions of
 ADCON0&=0xc7
and
ADCON0|=(channel>>3)

in ADC conversion.
post edited by Ugochi - 2019/04/25 08:45:42
#1

21 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 2889
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Function Declaration 2019/04/25 08:46:58 (permalink)
    +1 (1)
    Pls, tell us at least your derivative !
     
    Even better: provide your code.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    Jim Nickerson
    User 452
    • Total Posts : 6123
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Jim Nickerson
    User 452
    • Total Posts : 6123
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: Function Declaration 2019/04/25 08:50:08 (permalink)
    +1 (1)
    And the question has changed and I forgot to quote it ….
    #4
    katela
    Super Member
    • Total Posts : 1326
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: online
    Re: Function Declaration 2019/04/25 15:32:01 (permalink)
    +1 (1)
    Which PIC are you using? What does the datasheet say...?

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 17610
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Function Declaration 2019/04/25 18:06:57 (permalink)
    0
    They "APPEAR" to be configuring the A/D by setting the Registers.  The details are in the Data sheet.
    #6
    Ugochi
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2019/02/17 03:49:57
    • Location: 0
    • Status: offline
    Re: Function Declaration 2019/04/25 22:09:18 (permalink)
    0
    Below is the code and the pic I'm using is pic16f684.

    #include <xc.h>
    float value;
    float Cval
    void Init_ADC(void)
    {
    ADCON0=0b10000001;
    ADCON1=0b01100000;
    TRISAbits.TRISA0=1;
    }
    unsigned int ADCValue(unsigned char channel)
    {
    ADCON0&=0xc7;
    ADCON0|=(channel>>3);
    __delay_ms(2);
    return((ADRESH<<8)+ADRESL);
    }
    void main (void)
    {
    Init_ADC();
    TRISCbits.TRISC0=1;
    while (1);
    {
    Value=ADCValue(0);
    GO_nDONE=1;
    while(GO_nDONE);
    ...
    }

    The code continues, and it's working. My problem is, I understand every other part of the code except this:

    ADCON0&=0xc7;
    ADCON0|=(channel>>3);
    post edited by Ugochi - 2019/04/25 22:11:15
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 17610
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Function Declaration 2019/04/25 22:28:09 (permalink)
    +1 (1)
    You reappear the exact same question with no elaboration.
    Do you not understand what it does ?
    Do you not understand the C language expression ?
    Can you not find ADCON0 in the data sheet?
    What don’t you understand ?
    #8
    du00000001
    Just Some Member
    • Total Posts : 2889
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Function Declaration 2019/04/26 00:06:06 (permalink)
    +1 (1)
    @ NKurzman
    At least he added the PIC...16F684. AND finally provided a bigger code snippet.
     
    As the ADC channels usually start at 0,
    ADCON0 |= (channel>>3);

    looks like nonsense.
    For the '684,
    ADCON0 &= 0xC7;

    is garbage as well.
     
    What might be appropriate:
    ADCON0 &= 0xE3;         // Clear channel settings
    ADCON0 |= ((channel << 2) + 2u);  // Set channel and start conversion


    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #9
    Ugochi
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2019/02/17 03:49:57
    • Location: 0
    • Status: offline
    Re: Function Declaration 2019/04/26 00:48:56 (permalink)
    -1 (1)
    @du00000001
    That's same as the binary:

    ADCON0 &= 0b11100011;
    ADCON0 |=((channel<<2) + 2u);

    The question is, how did u get this 0xE3 for clearing the bits, because from the datasheet, I didn't see where the bitwise operator "&=" was talked about relative to ADCON0 register.
    In the channel setting, how many times do one have to shift it and in what direction (left or right) when using more than one ADC channel?
    #10
    crosland
    Super Member
    • Total Posts : 1586
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: Function Declaration 2019/04/26 01:21:41 (permalink)
    +1 (1)
    Ugochi
    The question is, how did u get this 0xE3 for clearing the bits, because from the datasheet, I didn't see where the bitwise operator "&=" was talked about relative to ADCON0 register.


    Why do you expect it to be in the datasheet?
     
    Why would a PIC data sheet give explanations of basic C syntax?
     
    Get yourself a C textbook, or look at the link in post #3 and learn what those operators are doing.
     
    #11
    du00000001
    Just Some Member
    • Total Posts : 2889
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Function Declaration 2019/04/26 02:24:35 (permalink)
    +1 (1)
    @ Ugochi
    What are you? E/E student? I'd suggest to get some more lectures prior trying to program...
    Maybe starting with Numerical Mathematics 1o1.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #12
    1and0
    Access is Denied
    • Total Posts : 9495
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Function Declaration 2019/04/26 03:10:44 (permalink)
    +2 (2)
    Throw that silly piece of snippet away and just use
    ADCON0bits.CHS = channel;

    LoL: LoL
    #13
    du00000001
    Just Some Member
    • Total Posts : 2889
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Function Declaration 2019/04/26 03:37:59 (permalink)
    +1 (1)
    @ 1and0
    But then don't forget to start the conversion!
    ADCON0.whatever = 1u;      // Lookup 'whatever' in the header file. I currently have no access to a header file.

    In the end, this "silly piece of snipped" is expected to be faster and smaller.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #14
    1and0
    Access is Denied
    • Total Posts : 9495
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Function Declaration 2019/04/26 03:56:17 (permalink)
    +2 (2)
    du00000001
    But then don't forget to start the conversion!

    I realized that after my posting and seeing the 2u in your code. But I left that for the OP to do!
     
    du00000001
    In the end, this "silly piece of snipped" is expected to be faster and smaller.

    Do you think the OP is ready for that?  ... faster and smaller?  ;)
    post edited by 1and0 - 2019/04/26 04:00:39
    #15
    nice
    Super Member
    • Total Posts : 1080
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: Function Declaration 2019/04/26 10:31:45 (permalink)
    +2 (2)
    du00000001
    In the end, this "silly piece of snipped" is expected to be faster and smaller.

    It is faster and smaller, but won’t work as expected when converting more than one analog channel on a 16F684 (or any other PIC with a similar ADC module). Even if using only one channel, the first conversion result will be wrong, unless AN0 is converted.

    It’s not a good idea to select the channel and start the conversion in the same instruction cycle, as doing so yields to an acquisition time of about 100 ns. Even under nearly perfect conditions (VDD = 5V, low temperature, analog input driven by an ideal voltage source) an acquisition time smaller than about 1 us is not sufficient (see figure 9-2 and equation 9-1 in the device’s data sheet).
    uint8_t channel ;

    for (;;)
    {
        channel = 1;
        ADCON0 &= 0xE3;                    // AN0 gets connected to the holding capacitor for a few instruction cycles
        ADCON0 |= ((channel << 2) + 2u);   // AN1 gets connected to the holding capacitor for only 100 ns (typical value)
     
        // wait for the conversion result
        // after the conversion AN1  gets connected to the holding capacitor again

        delay_ms (10);
        
        channel = 2;                        // AN1 is still connected to the holding capacitor
        ADCON0 &= 0xE3;                     // AN0 gets connected to the holding capacitor for a few instruction cycles
        ADCON0 |= ((channel << 2) + 2u);    // AN2 is connected to the holding capacitor for only 100 ns (typical value)
        
         // wait for the conversion result
         // after the conversion AN2 gets connected to the holding capacitor again

         delay_ms (10);    

        //  AN2 is still connected to the holding capacitor…
    }


    If scanning through channels periodically, selecting the next channel right after the conversion has taken place is the most efficient way to go.





    #16
    1and0
    Access is Denied
    • Total Posts : 9495
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Function Declaration 2019/04/27 11:28:08 (permalink)
    +3 (3)
    du00000001
     
    In the end, this "silly piece of snipped" is expected to be faster and smaller.

    Actually it is NOT faster nor smaller.
     
       577                           ;test.c: 98: ADCON0 &= 0xE3;
       578  075C  30E3                movlw 227
       579  075D  00FC                movwf ??_main
       580  075E  087C                movf ??_main,w
       581  075F  1283                bcf 3,5 ;RP0=0, select bank0
       582  0760  059F                andwf 31,f ;volatile
       583                          
       584                           ;test.c: 99: ADCON0 |= (channel << 2);
       585  0761  087A                movf _channel,w
       586  0762  00FC                movwf ??_main
       587  0763  3001                movlw 1
       588  0764                     u135: 
       589  0764  1003                clrc
       590  0765  0DFC                rlf ??_main,f
       591  0766  3EFF                addlw -1
       592  0767  1D03                skipz
       593  0768  2F64                goto u135
       594  0769  1003                clrc
       595  076A  0D7C                rlf ??_main,w
       596  076B  00FD                movwf ??_main+1
       597  076C  087D                movf ??_main+1,w
       598  076D  049F                iorwf 31,f ;volatile
     
    In fact, bitfield is faster and smaller. ;)
     
       603                           ;test.c: 103: ADCON0bits.CHS = channel;
       604  076F  087A                movf _channel,w
       605  0770  00FC                movwf ??_main
       606  0771  0DFC                rlf ??_main,f
       607  0772  0DFC                rlf ??_main,f
       608  0773  1283                bcf 3,5 ;RP0=0, select bank0
       609  0774  081F                movf 31,w ;volatile
       610  0775  067C                xorwf ??_main,w
       611  0776  39E3                andlw -29
       612  0777  067C                xorwf ??_main,w
       613  0778  009F                movwf 31 ;volatile
    #17
    Ugochi
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2019/02/17 03:49:57
    • Location: 0
    • Status: offline
    Re: Function Declaration 2019/05/03 07:53:30 (permalink)
    0
    @Crossland
    Those are bitwise operators and from what I know, it's used for " ANDing" bits turning ON or OFF any bits required, so does the bitwise OR (|=) in combination with the shift operator.
    For instance, the code @Nice for pic16f684, in setting the ADC parameters, this suffices:

    ADCON0 = 0b10000001;
    ADCON1 = 0b01100000;

    When clearing and selecting the bits, in a situation where multiple channels are to be used, say, AN0, AN1 and AN2, the code below suffices:

    ADCON0 &= 0b11100011; //0xE3 in hexadecimal
    ADCON0 |= (channel<<2);


    My question is: how come about using 0b11100011 with bitwise AND for clearing the channel and how do you know the number of times to shift the bits to get to a specific channel.
    I'm seriously stuck on this, have been trying to figure it out for over a week now and nothing.
    post edited by Ugochi - 2019/05/03 07:56:50
    #18
    1and0
    Access is Denied
    • Total Posts : 9495
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Function Declaration 2019/05/03 08:28:53 (permalink)
    +1 (1)
    Ugochi
    My question is: how come about using 0b11100011 with bitwise AND for clearing the channel and how do you know the number of times to shift the bits to get to a specific channel.
    I'm seriously stuck on this, have been trying to figure it out for over a week now and nothing.

    If you'd take a look at your PIC datasheet, you will see the channel select bits are bits 4,3 and 2 of the ADCON0 register.
     
    You bitwise AND with 0 to clear the bits and bitwise OR with 1 to set the bits.  The number of shift is 2 because the lower bit of the channel select bits is bit 2. It's just simple digital logic arithmetic.
    post edited by 1and0 - 2019/05/03 08:33:46
    #19
    Ugochi
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2019/02/17 03:49:57
    • Location: 0
    • Status: offline
    Re: Function Declaration 2019/05/03 12:49:34 (permalink)
    0
    Got that
    I tried using channels AN0 and AN1in pic18f2550 but channels AN0 isn't readithe analog input. Don't know what's wrong with it.
    Here's the ADC definitions:

    #include <stdio.h>
    #include <xc.h>
    int NV, CNV;
    int AV, CAV;
    char N[16];
    char M[16];
    void Init_ADC(void)
    {
    ADCON0 = 0b00000001;
    ADCON1 = 0b00001100;
    ADCON2 = 0b10010101;
    }
    unsigned int GetValue (unsigned char channel)
    {
    ADCON0 &= 0b11000011;
    ADCON0 |= (channel<<3);
    __delay_ms(2);
    return((ADRESH<<8)+ADRESL);
    }
    void main(void)
    {
    Init_LCD();
    Init_ADC();
    TRISAbits.TRISA0=1;
    TRISAbits.TRISA1=1;
    while(1)
    {
    NV=GetValue(0);
    GO_nDONE=1;
    while (GO_nDONE=1);
    CNV=(NV*5)/1024;
    Lcd_Set_Cursor(1,1);
    Lcd_Write_String("CNV= ");
    sprintf(N, "%dV",CNV);
    Lcd_Write_String(N);

    AV=GetValue(1);
    GO_nDONE=1;
    while (GO_nDONE=1);
    CAV=(AV*5)/1024;
    Lcd_Set_Cursor(2,1);
    Lcd_Write_String("CAV= ");
    sprintf(M, "%dV",CAV);
    Lcd_Write_String(M);
    }
    }


    Channel AN1 is displaying normal but Channel AN0 is only showing "CAV=0V".
    Any ideas pls?
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5