go_away

Author Topic: Two PIC questions  (Read 4756 times)

0 Members and 1 Guest are viewing this topic.

Offline jsmokerTopic starter

  • Full Member
  • ***
  • Posts: 118
  • Helpful? 2
Two PIC questions
« on: March 23, 2007, 03:00:04 PM »
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: [Select]
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;
delay(100);
//ADCON1bits.SAMP = 1;
// while(ADCON1bits.DONE)continue;
while(!ADCON1bits.DONE)
{
Send_String("Stuck");
}
return(ADCBUF0); // return the result
};

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

Init_UART(95);


TRISB = 0x003;

AllOK = SDInit();
if(!AllOK)
{
_LATB3 = 1;
for(i = 0; i < 200; i++)
delay(30000);
AllOK = SDInit();

}
//Change_Baud();

writenow = 0;
  while(1)
    {
if(writenow)
{

_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




calculate(Temperature);
delay(30000);
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;
WriteiSD(Ti);

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

_LATB3 = 0;
writenow= 0;
}

}         
    return 0;
}


Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: Two PIC questions
« Reply #1 on: March 23, 2007, 03:12:15 PM »
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.

Offline snow

  • Full Member
  • ***
  • Posts: 73
  • Helpful? 0
Re: Two PIC questions
« Reply #2 on: March 24, 2007, 04:43:20 PM »
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"


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: http://ww1.microchip.com/downloads/en/DeviceDoc/70058D.pdf

Offline jsmokerTopic starter

  • Full Member
  • ***
  • Posts: 118
  • Helpful? 2
Re: Two PIC questions
« Reply #3 on: March 24, 2007, 05:28:01 PM »
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.

Offline snow

  • Full Member
  • ***
  • Posts: 73
  • Helpful? 0
Re: Two PIC questions
« Reply #4 on: March 25, 2007, 03:47:37 AM »
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: http://forum.microchip.com/tm.aspx?m=147383

Offline snow

  • Full Member
  • ***
  • Posts: 73
  • Helpful? 0
Re: Two PIC questions
« Reply #5 on: March 25, 2007, 09:32:55 AM »
This read-write-modify effect is the reason why they implemented LAT registers in PIC18, PIC24 and dsPIC series.

 


Get Your Ad Here

data_list