|
|
View previous topic :: View next topic |
Author |
Message |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
Strange issue with if statement |
Posted: Mon Dec 26, 2011 8:19 am |
|
|
Hi
I'm working on a battery charger circuit based on 12F683. I have encountered a strange issue. My compiler version is 4.084.
Problem is in the function charge_end().
Program is written such that at 2290 interrupt of timer 1, it will call charge_end() function. Then it evaluvate some if statements to check whther charge terminations are met.
Problem is that first if condition is not get evaluated. Code simply assume first if condition is true and then executes the statements under it.
I have checked value of k with print f function, even though calculated k is negative (ex. k= -2095), if statement becomes true.
That is it says -2095 > 200 is true!
Code: |
int16 k=0;
k=(temp_mov_avg-prev_values[1]);
if(k>200)//dT/dt cutoff
{
printf("DT %Ld \r\n",k);
disable_interrupts (GLOBAL) ;
}
|
If statement become true no matter what is the k value is.
My project folder can be downloaded from following link
http://www.mediafire.com/?dqg3cbadu7ytr4z
Can someone point me the error?
Here is the code:
Code: |
//#include "E:\My Electronic projects\usb charger\code\pwm2analog\pwm2analog.h"
#include <12F683.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin used FOR I/O
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External SWITCH Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,bits=8)
void read_voltage();
void read_temperature();
void charge_end();
void init();
unsigned int16 loop_count=0;
int16 volt_mov_avg=0;
int16 temp_mov_avg=0;
int ac_count=0;
int16 prev_values[2]={0};
unsigned int16 volt_read[10]={0};
unsigned int16 temp_read[10]={0};
int16 initial_temp=0;
float temp=0;
#INT_TIMER1
void TIMER1_isr(VOID)
{
loop_count++;
if(loop_count%6==0)
{
ac_count++;
read_voltage();
read_temperature();
//printf("%.3f %.3f %Lu\r\n",volt_mov_avg,temp_mov_avg,loop_count);
//printf("%Ld %Ld %Ld %.3f \r\n",volt_mov_avg,temp_mov_avg,loop_count,temp);
printf(" %Ld %Ld\r\n",temp_mov_avg,prev_values[1]);
}
if(loop_count%2290==0)
{
if(loop_count>=230)
charge_end();
}
}
void charge_end()
{
int16 k=0;
k=(temp_mov_avg-prev_values[1]);
if(k>200)//dT/dt cutoff
{
printf("DT %Ld \r\n",k);
disable_interrupts (GLOBAL) ;
}
else if(((prev_values[0]-volt_mov_avg)>=15)&&(volt_mov_avg>3100)) // -dV cutoff
{
//printf("-dV \r\n");
disable_interrupts (GLOBAL) ;
}
else if(temp_mov_avg-initial_temp>15)//dT cutoff
{
}
else if(temp_mov_avg>4500)// peak T cutoff
{
}
else if(loop_count>34351)// timer cutoff - 2.5h
{
}
prev_values[0]=volt_mov_avg;
prev_values[1]=temp_mov_avg;
}
void read_voltage()
{
int i=0;
//float temp=0;
set_adc_channel (3) ;
delay_us (10) ;
if(loop_count>=73)
{
for(i=1;i<10;i++)
{
volt_read[i-1]=volt_read[i];
}
volt_read[9]=read_adc();
while(!adc_done())
{}
//printf("start\r\n");
//printf("%Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld %Ld\r\n\n",volt_read[0],volt_read[1],volt_read[2],volt_read[3],volt_read[4],volt_read[5],volt_read[6],volt_read[7],volt_read[8],volt_read[9],volt_read[10],volt_read[11],volt_read[12],volt_read[13],volt_read[14]);
}
else
{
volt_read[ac_count-1]=read_adc();
while(!adc_done())
{}
}
for(i=0;i<9;i++)
{
temp=temp+volt_read[i];
}
//volt_mov_avg=(3.275*temp)/14336;
temp=temp*0.31933;
volt_mov_avg=(int16)temp;
volt_mov_avg=volt_mov_avg+10;
if(volt_mov_avg>prev_values[0])
{
prev_values[0]=volt_mov_avg;
}
temp=0.0;
}
void read_temperature()
{
int i=0;
//float temp=0;
set_adc_channel (1) ;
delay_us (10) ;
if(loop_count>=73)
{
for(i=1;i<10;i++)
{
temp_read[i-1]=temp_read[i];
}
temp_read[9]=read_adc();
while(!adc_done())
{}
}
else
{
temp_read[(loop_count/3)-1]=read_adc();
while(!adc_done())
{}
}
for(i=0;i<10;i++)
{
temp=temp+temp_read[i];
}
//temp_mov_avg=(327.5*temp)/14336;
temp=temp*3.1933;
temp_mov_avg=(int16)temp;
temp_mov_avg=temp_mov_avg+30;
temp=0.0;
}
void init()
{
{
int i=0;
for(i=1;i<11;i++)
{
volt_read[i-1]=0;
temp_read[i-1]=0;
}
}
delay_ms (2000) ;
set_adc_channel (1) ;
delay_us (10) ;
initial_temp=read_adc();
temp=initial_temp*319.3359;
initial_temp=(int16)temp;
temp=0.0;
prev_values[1]=6000;
printf("start\r\n");
enable_interrupts (INT_TIMER1) ;
enable_interrupts (GLOBAL) ;
set_timer1(0);
}
void main()
{
setup_adc_ports (sAN0|VSS_VDD) ;
setup_adc (ADC_CLOCK_INTERNAL) ;
setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_1) ;
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8); //overflow 262ms
setup_timer_2(T2_DIV_BY_1,9, 1);// 200kHz
setup_ccp1 (CCP_PWM) ;
set_pwm1_duty(37L);
setup_comparator (NC_NC) ;
setup_vref (FALSE) ;
setup_adc_ports (sAN1|sAN3|VSS_VDD) ;
setup_adc (ADC_CLOCK_INTERNAL) ;
init();
WHILE (1)
{
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Dec 26, 2011 9:22 am |
|
|
k cannot be negative.
You have it declared as int16. Needs to be declared as _signed int16_.
You are outputting it with &ld, which is for a signed number, but the number itself is treated as unsigned. So what displays as -2095, is actually F7D1 (in hex), which is 63441 in an unsigned number, and 63441, _is_ greater than 200.....
You need to be careful in switching types. If you are using %d, which expects a signed number, then you need to be giving it a signed number.
Best Wishes |
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Mon Dec 26, 2011 9:31 am |
|
|
aha
That means I should declare all variables by signed or unsigned ? Right?
Thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Dec 26, 2011 9:41 am |
|
|
Values default to unsigned.
For everything you want to be treated as signed, (and that includes where you are doing arithmetic that can end up with a -ve result), then 'yes' they need to be declared as signed.
Best Wishes |
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Mon Dec 26, 2011 10:10 am |
|
|
Ttelmah
it worked
many thanks |
|
|
proweld
Joined: 15 Oct 2012 Posts: 1
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|