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.
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;
}