Software > Software

Two PIC questions

(1/2) > >>

I'm currently using a dsPIC30F3012 for a research project and I have a couple questions.

First, why is is that for digital outputs...using ports don't always work yet latches do work.  I thought for outputs they were interchangable. "PORTBbits.RB1 = 1; //or 0" doesn't always work yet using "LATCHBbits.LATCHB1 = 1;//or 0"

Second, I'm having a problem with my A/D converter.  I'm trying to take two readings and they seem to be influencing each other.  So when one is high and one is low the low one is higher than it should be and the high one is lower than it should be, and if one changes, it effects the other one.  Here's a few pertainant snippets of code. 

--- Code: ---void Init_ADC(void)
        //ADCON1 Register
        ADCON1bits.FORM = 0;//output form (integer)
        ADCON1bits.SSRC = 7;//sampling
        ADCON1bits.ASAM = 1;

        //ADCON3 Register
        //ADCON3bits.SAMC = 0;
        ADCON3bits.ADCS = 16;
        //ADCHS Register
        ADCHS = 0x0000;//channel 0 muxA
        //ADCSSL Register
        //Channel Scanning is disabled. All bits left to their default state
        ADCSSL = 0x0000;
        //ADPCFG Register
        ADPCFG = 0xFFFF;
        ADPCFGbits.PCFG0 = 0;//set AN0 as analog input
        ADPCFGbits.PCFG1 = 0;//set AN1 as analog input
        ADCON1bits.ADON = 1;

signed long read_a2d(int rchannel)
ADCON1bits.ADON = 1;
    ADCHS = rchannel;
//ADCON1bits.SAMP = 1;
// while(ADCON1bits.DONE)continue;
return(ADCBUF0); // return the result

int main (void)
for(i = 0; i<100; i++)


TRISB = 0x003;

AllOK = SDInit();
_LATB3 = 1;
for(i = 0; i < 200; i++)
AllOK = SDInit();


writenow = 0;

_LATB3 = 1;
A0_1 = read_a2d(0);
A0_1 = read_a2d(0);
A0_1 = read_a2d(0);
A0_2 = A0_1 +read_a2d(0);
A0_3 = A0_2 +read_a2d(0);
A1 = (A0_3/3)*100;
A1 = A1/853; // (4095/(4.8*(100)))conversion from digital x/1023 to degrees C
Temperature = (A1/6 + A2/6 + A3/6 + A4/6 + A5/6 + A6/6)*MTPL;
A6 = A5;
A5 = A4;
A4 = A3;
A3 = A2;
A2 = A1;
A1_1 = read_a2d(1);//something I tried on a hunch to jumpstart the read for channel 1
                       //this value is actually never used but written over down lower

WriteiSD(time);//write to UART command
WriteTab();//write to UART command
                        //and like 10 more write to UART command that I omitted to make this shorter

A1_1 = read_a2d(1);
A1_2 = A1_1 +read_a2d(1);
A1_3 = A1_2 +read_a2d(1);
B1 = (A1_3/3)*10000;
B1 = B1/853;
B1 = B1 * (MTPL/100);
Ti = (B1 + B2 + B3 + B4 + B5 + B6)/6;
B6 = B5;
B5 = B4;
B4 = B3;
B3 = B2;
B2 = B1;

A0_1 = read_a2d(0);same thing as the hunch above but for channel 0

_LATB3 = 0;
writenow= 0;

    return 0;

--- End code ---

I had the same problem when i was using my pic. try turning the a2d on and off after every conversion it should kill all the noise.


--- Quote ---First, why is is that for digital outputs...using ports don't always work yet latches do work.  I thought for outputs they were interchangable. "PORTBbits.RB1 = 1; //or 0" doesn't always work yet using "LATCHBbits.LATCHB1 = 1;//or 0"
--- End quote ---

You should always use LATCH when setting outputs and PORT when reading inputs.
Using LATCH register eliminates read-write-modify scenario which can lead to unpredicted behaviour.

As always, read manual first. In this case dsPIC I/O ports, chapter 11.2. Here you go:

I had read that section, but it says a "A write to the LATx register has the same effect as a write to the PORTx register."  Which doesn't seem to be the case, cause writing to the port doesn't always work, while writing to the latch does.

Somewhere on Microchip website it says: "When using I/O, be sure to write out to the LAT register, and read from the PORT register. Writing to PORT (instead of LAT) may cause read-modify-write issues and is not recommended."

When you modify output of single port using PORT register, processor first has to read the value of PORT register. And in some cases if you do that too fast, the read is somewhat different from what you expect it to be.

Good explanation of this can be read in PICmicro MID-RANGE MCU FAMILY, chapter 9.10 I/O Programming Considerations.

And a debate on Microchip forum is also very insightful:


[0] Message Index

[#] Next page

Go to full version