Read value calculation for good decision with pic16f877a

Status
Not open for further replies.

john120

Banned
Joined
Aug 13, 2011
Messages
257
Helped
11
Reputation
22
Reaction score
10
Trophy points
1,298
Visit site
Activity points
0
hello every body,I am having complications for reading frequency and take decisions using the value read;
I am using the T1CKI pin of PIC16F877A,i want to take decisions basing on the frequency I read for that I am using the if statement:
example:

void final()
{
if(1014500<=value1<=1014999)
{
t=25.5;
}
if(1014000<=value1<=1014499)
{
t=25.6;
}
if(1013500<=value1<=1013999)
{t=25.7;
}
if(1013000<=value1<=1013499)
{
t=25.8;
}
if(1012500<=value1<=1012999)
{
t=25.9;
}}

the problem which is major is that this frequency is not read with only 7 digits which me I am using,because of that the PIC takes the wrong decision because it is not seeing the good value;
so can any body help me to first convert the value which is interred on the T1CKI in only 7 digits and then the pic read only that one;I thing the ways is to use the arrays but I am not getting the real values:

see for example when entering frequency =1012300Hz the PIC is reading 1.01229999994216M

in proteus and then the decision become wrong.
So I want to extract
the first digit of the entered number 1
the second digit 0
the third digit 1
the fourth digit 2
the fifth digit 3
the sixth digit 0
the last digit 0

So that the PIC can only base the decision on that number of only 7 digits.

Thanks
 

Post full code.

See my full code here:
Code:
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
#define ka  PIN_C3
#use fast_io(c)
#use fast_io(b)
#use fast_io(a) 


void tempdisplay();

void final();

 

float voltsf,volts;
float period;
float jean;
float value;
int32 value1;
unsigned int adc_value;

void final()
{
 /*if(1234900<=value1<=1235900)
{
jean=7.2;
}
  if(1234100<value1<=1234800)
{
jean=7.3;
}
 if(1234000<=value1<=1231900)
{
jean=7.4;
}
 if(1231500<=value1<=1231800)
{
jean=7.5;
}
 if(1230090<=value1<=1230300)
{
jean=7.6;
}
 if(1230090<=value1<=1230300)
{
jean=7.6;
}
if(1228900<=value1<=1230100)
{
jean=7.7;
}
 if(122600<=value1<=1228000)

{
jean=7.8;
}
 if(1226200<=value1<=1226400)
{
jean=7.9;
}



 if(1224800<=value1<=1226120) //check Solution
{
jean=8.0;
}
 if(1223001<=value1<=1224500)
{
jean=8.1;
}
 if(1222999<=value1<=1223000)
{
jean=8.2;
}
 if(1220999<=value1<=1223000)
{
jean=8.3;
}
 if(1220001<=value1<=1221000)
{
jean=8.4;
}
 if(1219999<=value1<=1220000)
{
jean=8.5;
}
 if(1217995<=value1<=1219999)
{
jean=8.6;
}
 if(128000<=value1<=1219996)
{
jean=8.7;
}
 if(1217939<=value1<=1217999)
{
jean=8.9;
}
if(1217000<=value1<=1217938)
{
jean=9.0;
}
 if(1216000<=value1<=1216999)
{
jean=9.1;
}
 if(1215130<=value1<=1215999)
{
jean=9.2;
}
 if(1215000<=value1<=1215129)
{
jean=9.3;
}
if(1212000<=value1<=1214999)
{
jean=9.4;
}
 if(1210000<=value1<=1212000)
{
jean=9.5;
}
 if(129900<=value1<=1299999)
{
jean=9.6;
}
 if(1299500<=value1<=1299899)
{
jean=9.7;
}
 if(1209800<=value1<=1209499)
{
jean=9.8;
}
 if(1208500<=value1<=1209799)
{
jean=9.9;
}
 if(1207000<=value1<=1208499)
{
jean=10;
}
 if(1205000<=value1<=1206999)
{
jean=10.1;
}
 if(1202500<=value1<=124999)
{
jean=10.2;
}
 if(1201500<=value1<=1202499)
{
jean=10.3;
}
 if(1201000<=value1<=1201499)
{
jean=10.4;
}
if(1200000<=value1<=1200999)
{
jean=10.5;
}
 if(1199500<=value1<=1199999)
{
jean=10.6;
}
 if(1199000<=value1<=1199499)
{
jean=10.7;
}
 if(1198500<=value1<=1198999)
{
jean=10.8;
}
if(117000<=value1<=1198000)
{
jean=10.9;
}
 if(1196900<=value1<=116999)
{
jean=10.9;
}
 if(1196000<=value1<=1196899)
{
jean=11.0;
}
 if(1195500<=value1<=1195999)
{
jean=11.1;
}
 if(1195000<=value1<=1195499)
{
jean=11.2;
}
 if(1194500<=value1<=1194999)
{
jean=11.3;
}
 if(1194000<=value1<=1194499)
{
jean=11.4;
}
 if(1193500<=value1<=1193999)
{
jean=11.5;
}
 if(1193000<=value1<=1193499)
{
jean=11.6;
}
 if(1190000<=value1<=1192999)
{
jean=11.7;
}
 if(1188500<=value1<=1189999)
{
jean=11.8;
}
 if(1187000<=value1<=1188000)
{
jean=11.9;
}
 if(1184100<=value1<=1186999)
{
jean=12.0;
}
 if(1183240<=value1<=1184099)
{
jean=12.1;
}
 if(1182230<=value1<=1183239)
{
jean=12.2;
}
 if(1181200<=value1<=1182229)
{
jean=12.3;
}
 if(1181000<=value1<=1181199)
{
jean=12.4;
}
 if(118000<=value1<=118999)
{
jean=12.5;
}
 if(1174950<=value1<=117999)
{
jean=12.6;
}
 if(11738000<=value1<=1174949)
{
jean=12.7;
}
 if(11736500<=value1<=11737999)
{
jean=12.8;
}
 if(1173500<=value1<=11736499)
{
jean=12.9;
}
 if(1172500<=value1<=1173499)
{
jean=13.0;
}
 if(1171500<=value1<=1172499)
{
jean=13.1;
}
 if(1170000<=value1<=1171499)
{
jean=13.2;
}
 if(1168500<=value1<=1169999)
{
jean=13.3;
}
 if(1167500<=value1<=1168499)
{
jean=13.4;
}
 if(1166000<=value1<=1167499)
{
jean=13.5;
}
 if(1164500<=value1<=1165999)
{
jean=13.6;
}
 if(1163000<=value1<=1164499)
{
jean=13.7;
}
 if(1159999<=value1<=1162999)
{
jean=13.8;
}
 if(1158500<=value1<=1159997)
{
jean=13.9;
}
 if(1157500<=value1<=1158499)
{
jean=13.9;
}
 if(1156500<=value1<=1157499)
{
jean=14.0;
}
 if(1155000<=value1<=1156499)
{
jean=14.1;
}
 if(1153500<=value1<=1154999)
{
jean=14.2;
}
 if(1151500<=value1<=1152500)
{
jean=14.3;
}
 if(1150500<=value1<=1151499)
{
jean=14.4;
}
 if(1149880<=value1<=1150499)
{
jean=14.5;
}
 if(1144000<=value1<=1149870)
{
jean=14.6;
}
 if(1143000<=value1<=1143999)
{
jean=14.7;
}
 if(1141800<=value1<=1142999)
{
jean=14.8;
}
 if(114500<=value1<=1141799)
{
jean=14.9;
}
 if(1142600<=value1<=1143499)
{
jean=15.0;
}
 if(1141500<=value1<=1142599)
{
jean=15.1;
}
 if(1140400<=value1<=1141499)
{
jean=15.2;
}
 if(1139600<=value1<=1140399)
{
jean=15.3;
}
 if(1138560<=value1<=1139599)
{
jean=15.4;
}
 if(1137500<=value1<=1138559)
{
jean=15.5;
}
if(1136500<=value1<=1137499)
{
jean=15.6;
}
 if(1135500<=value1<=1136499)
{
jean=15.7;
}
 if(1134500<=value1<=1135499)
{
jean=15.8;
}

 if(1133500<=value1<=1134499)
{
jean=15.9;
}
 if(1129999<=value1<=1133499)
{
jean=16.0;
}
 if(1128500<=value1<=1129998)
{
jean=16.1;
}

 if(1127500<=value1<=1128499)
{
jean=16.2;
}
 if(1124999<=value1<=1127499)
{
jean=16.3;
}
 if(1123500<=value1<=1124998)
{
jean=16.4;
}
 if(1122500<=value1<=1123499)
{
jean=16.5;
}
 if(1121500<=value1<=1122499)
{
jean=16.6;
}
 if(1119950<=value1<=1121499)
{
jean=16.7;
}
 if(1118500<=value1<=1119949)
{
jean=16.8;
}
 if(1117500<=value1<=1118499)
{
jean=16.9;
}
 if(1116500<=value1<=1117499)
{
jean=17.0;
}
 if(1115000<=value1<=1116499)
{
jean=17.1;
}
 if(1113500<=value1<=1114999)
{
jean=17.2;
}
 if(1112000<=value1<=1113499)
{
jean=17.3;
}
 if(1110500<=value1<=1111999)
{
jean=17.4;
}
 if(1108000<=value1<=1109000)
{
jean=17.5;
}
 if(1106500<=value1<=1107999)
{
jean=17.6;
}
if(1105200<=value1<=1106499)
{
jean=17.7;
}

 if(1103500<=value1<=1105199)
{
jean=17.8;
}
 if(1102500<=value1<=1103499)
{
jean=17.9;
}
 if(1101000<=value1<=1102499)
{
jean=18.0;
}
 if(1099799<=value1<=1100999)
{
jean=18.1;
}
 if(1098500<=value1<=1099798)
{
jean=18.2;
}
 if(1097500<=value1<=1098499)
{
jean=18.3;
}
 if(1096400<=value1<=1097499)
{
jean=18.4;
}
 if(1095200<=value1<=1096399)
{
jean=18.5;
}

 if(1094000<=value1<=1095199)
{
jean=18.6;
}
 if(1092500<=value1<=1093999)
{
jean=18.7;
}
 if(1091500<=value1<=1092499)
{
jean=18.8;
}
 if(1090000<=value1<=1091499)
{
jean=18.9;
}
 if(1088500<=value1<=1089999)
{
jean=19.0;
}
 if(1087500<=value1<=1088499)
{
jean=19.1;
}
 if(1086200<=value1<=1087499)
{
jean=19.2;
}
 if(1085000<=value1<=1086199)
{
jean=19.2;
}
 if(1082000<=value1<=1084999)
{
jean=19.3;
}
 if(1080000<=value1<=1081999)
{
jean=19.4;
}
 if(1078500<=value1<=1079999)
{
jean=19.5;
}
if(1077500<=value1<=1078499)
{
jean=19.6;
}
 if(1076500<=value1<=1077499)
{
jean=19.7;
}
 if(1075900<=value1<=1076499)
{
jean=19.8;
}
if(1075860<=value1<=1075899)
{
jean=19.9;
}
if(1075830<=value1<=1075859)
{
jean=20.0;
}
 if(1074820<=value1<=1075829)
{
jean=20.1;
}
 if(1073500<=value1<=1074819)
{
jean=20.2;
}
 if(1072500<=value1<=1073499)
{
jean=20.3;
}
 if(1071200<=value1<=1072499)
{
jean=20.4;
}
 if(1070000<=value1<=1071199)
{
jean=20.5;
}
 if(1068500<=value1<=1069999)
{
jean=20.6;
}
 if(1067200<=value1<=1068499)
{
jean=20.7;
}
 if(1066100<=value1<=1067199)
{
jean=20.8;
}
 if(1065000<=value1<=1066099)
{
jean=20.9;
}
 if(1063200<=value1<=1064999)
{
jean=21.0;
}
if(1062400<=value1<=1063199)
{
jean=21.1;
}
 if(1061300<=value1<=1062399)
{
jean=21.2;
}
if(1061000<=value1<=1061299)
{
jean=21.3;
}
 if(1060000<=value1<=1060999)
{
jean=21.4;
}
 if(1058000<=value1<=1059999)
{
jean=21.5;
}
 if(1056000<=value1<=1057999)
{
jean=21.6;
}
 if(1055000<=value1<=1055999)
{
jean=21.7;
}
 if(1054000<=value1<=1054999)
{
jean=21.8;
}
 if(1053000<=value1<=1053999)
{
jean=21.9;
}
 if(1051950<=value1<=1052999)
{
jean=22.0;
}
if(1051500<=value1<=1051949)
{
jean=22.1;
}
 if(1050000<=value1<=1051499)
{
jean=22.2;
}
 if(1048500<=value1<=1049999)
{
jean=22.3;
}
 if(1047000<=value1<=1048499)
{
jean=22.4;
}
 if(1045500<=value1<=1046999)
{
jean=22.5;
}
 if(1044000<=value1<=1045499)
{
jean=22.6;
}
 if(1042500<=value1<=1043999)
{
jean=22.7;
}
 if(1041200<=value1<=1042499)
{
jean=22.8;
}
 if(1040000<=value1<=1041199)
{
jean=22.9;
}
 if(1038500<=value1<=1039999)
{
jean=23.0;
}
 if(1037000<=value1<=1038499)
{
jean=23.1;
}
if(1035500<=value1<=1036999)
{
jean=23.2;
}
if(1034000<=value1<=1035499)
{
jean=23.3;
}
 if(1032000<=value1<=1033000)
{
jean=23.4;
}
 if(1030000<=value1<=1031999)
{
jean=23.5;
}
 if(1028999<=value1<=1029999)
{
jean=23.6;
}
 if(1027500<=value1<=1028998)
{
jean=23.7;
}
 if(1026500<=value1<=1027499)
{
jean=23.8;
}
 if(1026000<=value1<=1026499)
{
jean=23.9;
}
 if(1025000<=value1<=1025999)
{
jean=24.0;
}
 if(1024000<=value1<=1024999)
{
jean=24.1;
}
 if(1022500<=value1<=1023999)
{
jean=24.2;
}
 if(1021500<=value1<=1022499)
{
jean=24.3;
}
 if(1021000<=value1<=1021499)
{
jean=24.4;
}
 if(1020000<=value1<=1020999)
{
jean=24.5;
}
 if(1018500<=value1<=1019999)
{
jean=24.6;
}
 if(1018200<=value1<=1018499)
{

jean=24.7;
}
 if(1018000<=value1<=1018199)
{
jean=24.8;
}
 if(1017500<=value1<=1017999)
{
jean=24.9;
}
 if(1017000<=value1<=1017499)
{
jean=25.0;
}
 if(1016500<=value1<=1016999)
{
jean=25.1;
}
 if(1016000<=value1<=1016499)
{
jean=25.2;
}
 if(1015500<=value1<=1015999)
{
jean=25.3;
}
 if(1015000<=value1<=1015499)
{
jean=25.4;
}
if(1014500<=value1<=1014999)
{
jean=25.5;
}
 if(1014000<=value1<=1014499)
{
jean=25.6;
}
 if(1013500<=value1<=1013999)
{
jean=25.7;
}
 if(1013000<=value1<=1013499)
{
jean=25.8;
}*/
 if(1012500<=value1<=1012999)
{
jean=25.9;
}
 else if(1012000<=value1<=1012499)
{
jean=26.0;
}
else
{
printf(lcd_putc,"Connect Probe");
delay_ms(1500);
}
}


void main(void)
{
   set_tris_c(0xff);
   set_tris_b(0x00);
   lcd_init();
   delay_us(200);
   
   setup_adc_ports(AN0);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);
   adc_value = read_adc();
    volts = ((float)(adc_value * 500)/1023.0);
     //volts = ((adc_value * 500)/1023.0);
    //voltsf=(volts*1.8)+32; 
      delay_ms(400);         
         
set_timer1(0);
setup_timer_1(T1_EXTERNAL| T1_DIV_BY_8);
delay_ms(250); 
setup_timer_1(T1_DISABLED);
value=get_timer1();
value1=value*8*4;  
   
/*printf(lcd_putc,"\fWELCOME");
delay_ms(1200);
      

         
while(true)
{ 
 if((input(PIN_C5)==0))
     {
     final();
    
    if(input(PIN_C5)==0)
    {  
      delay_ms(150);
       lcd_gotoxy(7,1);
       printf(lcd_putc,"\fWait");
        delay_ms(2500);
printf(lcd_putc,"\f%3.1f",volts);
  
      printf(lcd_putc,"\°C");
      delay_ms(2500);     
             
         lcd_gotoxy(7,1);

      //printf(lcd_putc,"\f%2.1f"jean);
            
     
     
      delay_ms(3500);   
      printf(lcd_putc,"\f%lu",value1);
      delay_ms(3500); 
      }       
     }
     
if(input(PIN_C4)==0)
 {
 if(input(PIN_C4)==0)
   {
   printf(lcd_putc,"\fSinusoidal");
    delay_ms(2000);   
    }
   }
   
 if(input(PIN_C1)==0)
{
 if(input(PIN_C1)==0)
{
printf(lcd_putc,"\fSquare");
delay_ms(2000);
}
}

}
}
 
Last edited by a moderator:

if(1224800<=value1<=1226120) //check Solution ????

I do not beleive this expression can be evaluated correctly.
it is normally written like this

if((value1 >= 1224800) && (value1 <= 1226120)){
Then....
}

There must be a way to condense this code a little.
For instance:

Code:
#define ARRAY_SIZE 8

const int hi_limit[] = {2,3,4,5,6,7,8,9};
const int lo_limit[] = {1,2,3,4,5,6,7,8};

unsigned char ix;
float jean = 8.0;

for(ix = 0; ix < ARRAY_SIZE; ix++)
  {
  if((value >= lo_limit[ix]) && (value <= hi_limit[ix]))
    {
    break;
    }

  jean += 0.1;
  }
 

hello btbass,the proposed codes here above are help full but not working, I am trying to run them with proteus but it is not going.it displays different values for one input frequency,the compiler is adding 0.1 many times to the first displayed value 8.0 and then when you push the button the value is increased by 0.1 and so on.

Check and let me know how I can proceed,in case the reduction if possible it is an important thing because with my if...else...if which I was using th ccs c compiler is now saying:A segment or the program is too large.

Help me to handle the issue:when I take few lines with if((value1 >= 1224800) && (value1 <= 1226120)){
Then....
} the program run very well.


PLZ Help .Thx
 
Last edited:

Your limits are very large numbers?
If you could scale them so they fit in an unsigned int, max value 65,535. It would save a lot of memory.
Also, if you had equal steps between the limits, you could save more memory by not needing an array of values.
You could try something like this:

Code:
#define HI_LIMIT  100
#define LOW_LIMIT 50

#define STEPSIZE   10
#define ARRAY_SIZE 180

unsigned char get_value(unsigned int value)
  {
  unsigned int hi_limit, lo_limit;
  unsigned char ix;

  hi_limit = HI_LIMIT;
  lo_limit = LOW_LIMIT;	

  for(ix = 0; ix < ARRAY_SIZE; ix++)
    {
    if((value >= lo_limit) && (value <= hi_limit))
      {
      break;
      }
    else{
      lo_limit += STEPSIZE;
      hi_limit += STEPSIZE;
      } 

  return ix;
  }

/*--- Call the function ---*/

jean += get_value(value);
 


May I use this those codes with my big values which I am hundling?
Can you plz help me to know what those values below are for?(100,50,10 and 180);
#define HI_LIMIT 100
#define LOW_LIMIT 50

#define STEPSIZE 10

the problem I have the step is not the same it keeps changing what to do?

#define ARRAY_SIZE 180
 

I was just trying to suggest an approach that reduces your code size.
You will have to figure out the fine details to suit the values you are using and the results you are trying to get.
You could scale all your limits by dividing them by 100 so they fit in an unsigned int. You have to decide whether that resolution would give the results you want.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…