ninjathegreat
New Member
I've been trying to get current information from a DC motor that I'm running using the PWM (P1A single mode). I have the L298N connected, and a .5 ohm current sensing resistor connected to pin 15.
The problem I have is that the USART output looks like this:
24 24 25 23 0 0 0 23 26 0 0 25 26 26 0 0 0 .....
Why do I get the 0s? Am I making some error?
I've connected pin 15 of the L298N to AN0 (pin2). this is how my MPLABC code looks like (relevant portions only):
main(){
TRISA = 0xff;
.
.
.
.
while(1){
testADC();
}
}
testADC(){
float i;
char *c;
i = getCurrent();
atoi((int) i, c);
putsUSART(c);
Delay10kTCYx(3);
}
float getCurrent(){
float i;
unsigned int v;
ADCON1 = 0x0e; // reference: VDD and VSS, AN0 analog
ADCON0 = 0x00 // Channel 0 (AN0)
ADCON2 = 0x80 // right justified, 0 TAD, 2*TOSC conversion
ADCON0bits.ADON = 1; // turn on A/D
ADCON0bits.GO_DONE = 1; // start conversion
while (ADCON0bits.GO_DONE); // wait for completion
v = ADRESH * 256+ADRESL; //voltage in A/D levels
i = v*2*.0048*100 // current in mA, .5 ohm current sense resistor
return (i);
}
Appreciate suggestions,
thanks
N.
P.S. I am not doing the interrupt thingy now, that's the next step...
The problem I have is that the USART output looks like this:
24 24 25 23 0 0 0 23 26 0 0 25 26 26 0 0 0 .....
Why do I get the 0s? Am I making some error?
I've connected pin 15 of the L298N to AN0 (pin2). this is how my MPLABC code looks like (relevant portions only):
main(){
TRISA = 0xff;
.
.
.
.
while(1){
testADC();
}
}
testADC(){
float i;
char *c;
i = getCurrent();
atoi((int) i, c);
putsUSART(c);
Delay10kTCYx(3);
}
float getCurrent(){
float i;
unsigned int v;
ADCON1 = 0x0e; // reference: VDD and VSS, AN0 analog
ADCON0 = 0x00 // Channel 0 (AN0)
ADCON2 = 0x80 // right justified, 0 TAD, 2*TOSC conversion
ADCON0bits.ADON = 1; // turn on A/D
ADCON0bits.GO_DONE = 1; // start conversion
while (ADCON0bits.GO_DONE); // wait for completion
v = ADRESH * 256+ADRESL; //voltage in A/D levels
i = v*2*.0048*100 // current in mA, .5 ohm current sense resistor
return (i);
}
Appreciate suggestions,
thanks
N.
P.S. I am not doing the interrupt thingy now, that's the next step...