Ive been designing a circuit to control a load, with current feedback and a dual 7 segment display.
I dont have a diagram of the circuit (that makes sense) at this point, so i cant really share, but the circuit appears to work properly.
I believe i have a code problem, and i'm brand new at C programming, and microcontrollers so I dont know what is wrong exactly.
I have a timer set to increment a couple variables by interrupt every 1ms.
First it reads the pot value, and sets the value of of ADC reading to a variable called dutycycle.
Then it reads the current value, and sets the value of the ADC reading to a variable called current.
I believe the error is in the code below.
This works perfectly as the display goes from 00 to 65, as i smoothly turn the pot, the display value goes up smoothly. When i get to 65, it jumps from there directly to 75, then counts backwards from 75-70, then jumps to 85, then backwards to 80, then jumps to 85.
The maximum current reading is 88, which is exactly as expected.
The odd display behaviour between 65 and 88 i dont understand, especially since it works at the lower values.
any thoughts?
I dont have a diagram of the circuit (that makes sense) at this point, so i cant really share, but the circuit appears to work properly.
I believe i have a code problem, and i'm brand new at C programming, and microcontrollers so I dont know what is wrong exactly.
I have a timer set to increment a couple variables by interrupt every 1ms.
First it reads the pot value, and sets the value of of ADC reading to a variable called dutycycle.
Then it reads the current value, and sets the value of the ADC reading to a variable called current.
I believe the error is in the code below.
Code:
while (1)
{
// Add your application code
if (ADCclk>120)
{
if (ADCsel==1) // sets ADC channel to read pot input
{
// PrevResult = ((ADRESH << 8) + ADRESL);
ADCON0bits.CHS = channel_AN7;
__delay_us(10);
ADCON0bits.GO = 1;
}
if (ADCsel==2) // sets ADC channel to read current feedback circuit
{
ADCON0bits.CHS = channel_AN3;
__delay_us(10);
ADCON0bits.GO = 1;
}
ADCclk=0;
}
PWM1DCL = DutyCycle;
PWM1DCH = (DutyCycle >> 8);
PWM1LDCON = 0x80;
// p = (current*10 / 1024); //Multiplying current by 10, so that integer math works, and gives an integer value between 0 and 9. Needs adjustment yet for proper rounding (add one half of a point)
//p is the value to display on 7 segment display
//seg7set[p](); // calling the 7 segment value specified by p
if (digitsel>1)
{
seg7set[10](); // clear 7 segment display to prevent ghosting
if (IO_RA5_PORT==1)
{
IO_RA5_SetLow();
p=(current*10 / 1024); //get tens value from adc reading
seg7set[p]();
}
else if (IO_RA5_PORT==0)
{
IO_RA5_SetHigh();
p=((current*100 / 1024)%10); //get ones value from adc reading
seg7set[p]();
}
digitsel=0;
}
}
}
The maximum current reading is 88, which is exactly as expected.
The odd display behaviour between 65 and 88 i dont understand, especially since it works at the lower values.
any thoughts?