CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Input doesn't stay the same when it's output

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

Input doesn't stay the same when it's output
PostPosted: Thu Aug 13, 2009 8:54 am     Reply with quote

Why would an input of 5000, be written to and eeprom is later read as something different.
The ID number is what is important here.
What am I missing? Why the conversion? It needs to read in a number, keep the format, and display the ID in the same format


Let show the logging of the action:

08/12/2009 15:47:50.003 --> get_setup()>>>>>>>>>>>>>>>>>>>>>>>

08/12/2009 15:47:50.003 --> ID=5000
08/12/2009 15:47:53.097 --> Cat=01 Cummins=02 Detroit=03 Enter Engine Number=02
08/12/2009 15:47:55.003 --> Cat=01 Twin Disk=02 Allison=03 Enter Trans Number=.# #03
08/12/2009 15:48:00.159 --> inside write_data()
08/12/2009 15:48:00.159 --> i=0, offset=0, data =5000
08/12/2009 15:48:00.159 --> id_num=e_data[0]=136,id_num=e_data[1]=0, engine=e_data[2]=0, trans=e_data[3]=0

08/12/2009 15:48:00.159 --> This from - read_e_data() Id_Num= 0136 Engine=00 Trans=00
08/12/2009 15:48:00.175 --> i=1, offset=0, data =5000
08/12/2009 15:48:00.175 --> id_num=e_data[0]=136,id_num=e_data[1]=1, engine=e_data[2]=0, trans=e_data[3]=0

08/12/2009 15:48:00.175 --> This from - read_e_data() Id_Num= 0392 Engine=00 Trans=00
08/12/2009 15:48:00.175 --> write_eeprom_16bit - offset_id=0 ,id_num=392
08/12/2009 15:48:00.191 --> write_eeprom - offset_e=2 ,engine=0
08/12/2009 15:48:00.191 --> write_eeprom - offset_t=3 ,trans=0
08/12/2009 15:48:00.206 --> LEAVING write_data()
08/12/2009 15:48:00.206 --> This from - get_setup() Id_Num= 0392 Engine=00 Trans=00
08/12/2009 15:48:00.206 --> <<<<<<<<<<<<<<<<<<<<<<<<<<LEAVING get_setup()

And now the software.

Code:
#include <18F258.h>
#fuses HS,PROTECT,NOLVP,WDT32,nowdt
#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)  // Jumpers: 8 to 11, 7 to 12

#include <can-18xxx8.c>
#byte pir1=0x0F9E
#define TMR1iF  0X00
#rom int 0xf00000={0x00,0x00,0x00,0x00}

#define offset_id 0x00 // original
#define offset_e  0x02// original
#define offset_t  0x03// original

//below is an experiment in shifting to right the starting address
/*
#define offset_id 0x02
#define offset_e  0x04
#define offset_t  0x05
*/
#include <stdlib.h>
#include <input.c>

void read_e_data()   //reads stored values from eeprom
{
  int8 count=0,address=0;
  int8 e_data[4]; //original
  //int16 e_data[4];// don't use!!!!!!
  for(address=0; address<=4; ++address)
   {
   e_data[count++]=READ_EEPROM(address); } // LOADS E_DATA ARRAY, then feeds the parts
   printf(" id_num=e_data[0]=%u,id_num=e_data[1]=%u,  engine=e_data[2]=%u, trans=e_data[3]=%u ",e_data[0],e_data[1],e_data[2], e_data[3]);
  line_feed();
  id_num=make16(e_data[1],e_data[0]);
   engine=e_data[2];
   trans=e_data[3];
   line_feed();
   
   printf("This from - read_e_data() Id_Num= %04lu  Engine=%02u Trans=%02u",id_num,engine,trans);
   line_feed();
}


void write_eeprom_16bit(int offset, int16 data)
{int8 i;
for (i=0;i<2;i++)
   {
      write_eeprom(i+offset,*(&data+i));
      printf("i=%u, offset=%u, data =%lu",i, offset , data);
      line_feed();
      read_e_data();
   }
}

void write_data()  //store operating prarmeters in eeprom
{
  printf(" inside  write_data()");
  line_feed();
   write_eeprom_16bit(offset_id,id_num);
   printf(" write_eeprom_16bit - offset_id=%u ,id_num=%lu ", offset_id,id_num);
  line_feed();
    //write_eeprom(offset_id,id_num);
    write_eeprom(offset_e,engine);// orginal
   //write_eeprom_16bit(offset_e,engine);
    printf(" write_eeprom - offset_e=%u ,engine=%u ", offset_e,engine);
  line_feed();
    write_eeprom(offset_t,trans);//original
    //write_eeprom_16bit(offset_t,trans);
     printf(" write_eeprom - offset_t=%u ,trans=%u ", offset_t,trans);
  line_feed();
  printf(" LEAVING write_data()");
  line_feed();
}

void get_setup()
{
printf("get_setup()>>>>>>>>>>>>>>>>>>>>>>>");
  line_feed();
 setup_wdt(wdt_off);
  line_feed();
  printf("ID=");    //enter id number for pumper
 id_num=get_long(); //orignal statement
  //scanf(%4Lu, id_num); //my suggestion,
  //gets(id_num);//nope!
  line_feed();
  printf("Cat=01 Cummins=02 Detroit=03 Enter Engine Number=");
  engine=get_int();
  line_feed();
  printf("Cat=01 Twin Disk=02 Allison=03 Enter Trans Number=");
  trans=get_int();
  line_feed();
  write_data();
  printf("This from - get_setup() Id_Num= %04lu  Engine=%02u Trans=%02u",id_num,engine,trans);
  line_feed();
  //setup_wdt(wdt_on);
  printf(" <<<<<<<<<<<<<<<<<<<<<<<<<<LEAVING get_setup()");
  line_feed();
  setup_wdt(wdt_on);
}

The set up a PIC18F258, icd-u40, and PCWHD 4.079

Background, I'm not new to C, I've using on and off since college. Reccently, last six months, I've been working C#, love it!!!!!!!!!!!!!!!!!!!!!!!!

What I am new at is the PIC programming part. I usually don't program at this level.

Thanks



[/img]
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 13, 2009 9:00 am     Reply with quote

I don't know about the rest of your code but you better fix this:

Code:

#fuses HS,PROTECT,NOLVP,WDT32,nowdt



You want the WDT on or not?

In addition there is no main function unless I am missing something.

This code actually compiles?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 9:06 am     Reply with quote

mkuang wrote:
I don't know about the rest of your code but you better fix this:

Code:

#fuses HS,PROTECT,NOLVP,WDT32,nowdt



You want the WDT on or not?

In addition there is no main function unless I am missing something.

This code actually compiles?


There is actually no problem with this, I do a similar thing
#fuses NOWDT, WDT1024

I then start the WDT in software to wake me up from sleep mode :-

setup_wdt(WDT_ON); // If sleep is called then WDT will wakeup and NOT reset
sleep(); // Preserve Power (WDT1024 x 4)ms (4.096 seconds)
setup_wdt(WDT_OFF);
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

Yes it compiles
PostPosted: Thu Aug 13, 2009 9:25 am     Reply with quote

To Mkuang

This is obviously only part of the program. For the sake of brevity, I only posted the setup and the section that I'm have trouble with.

I'll post the entire program if need be.

L.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 13, 2009 9:38 am     Reply with quote

Why don't you take the time to create a simple test program showing how you are storing ID number to eeprom and then reading and assembling it? For starters just declare long ID = 5000, save it to eeprom, then read it back out and see what you get.
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

Already have
PostPosted: Thu Aug 13, 2009 9:45 am     Reply with quote

I've already done that. That's why I posted only the part I'm having problem with, everything else works fine.

If you notice the log in the very first of this thread I inputted the number 5000 and got out something else.

I write very sparse code with lots of printf's for this very reason: to see when it goes bad.

L
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 10:03 am     Reply with quote

This might be due to the line

write_eeprom(i+offset,*(&data+i));

&data returns a pointer to a 16 bit value. When you increment a pointer in C it should take into account the size of the type of data it is pointing to. So if

&data = 0 and data points to an int16 then &data + 1 should make
&data = 2

you can fix this by casting to an 8 bit pointer so
(char *)&data + 1

or in your case

write_eeprom(i+offset,*((char *)&data + i));

try it and let us know.

As stated a compilable program would help us to find and fix your problem, It may also help you find it yourself as the process of creating a compilable program using the parts of your program which seem to fail may lead you to discovering the problem yourself.

A lot of time the problem is a simple mistake but to trawl though someone elses code and not to be able to test the result ourselves due to it NOT being compilable usually results in very little help from our part. ME included.

There have been many times I have seen a post on here with a whole lot of un-compilable code and turned away because it is too much hassle on my part.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 13, 2009 10:06 am     Reply with quote

Looks like you are missing a nibble or two here and there.

5000=0b 0001 0011 1000 1000 //this is id_num
392 = 0b 0000 0001 1000 1000 //this is what you are getting out
136 = 0b 0000 0000 1000 1000 //this is data[0] which is correct

However, your data[1] is showing it is equal to 1 where it should be equal to 0b 0001 0011 which is 19 in decimal.

I would double-check this:

Code:

void write_eeprom_16bit(int offset, int16 data)
{int8 i;
for (i=0;i<2;i++)
   {
      write_eeprom(i+offset,*(&data+i));
      printf("i=%u, offset=%u, data =%lu",i, offset , data);
      line_feed();
      read_e_data();
   }
}


I can't say it is incorrect but it looks a bit funky. I wouldn't code like that just because it is very hard to read.

How about simplifiying it a bit by doing this:

Code:

void write_eeprom_16bit(int offset, int16 data)
{     
int8 data_lsb, data_msb;
data_msb = make8(data,1) // gets msb of data
data_lsb = make8(data,0) // gets lsb of data

write_eeprom(offset, data_lsb);
write_eeprom(offset+1, data_msb);

read_e_data();
   
}
Guest








The entire program
PostPosted: Thu Aug 13, 2009 11:23 am     Reply with quote

I knew there was something skewed on my end. Thanks to both of you guys, I have a another way to look at this. I'll give both a try.

For the record I inherited this project so some of it is hard to read. But I'll take care of that.

Once again thanks, I'll update later.

L


Here's the entire program.

Code:

//X361 RS232 THRL_SHFT
//
//PROGRAM TO SEND SWITCH STATUS TO LOCAL CONTROL
//
//WAITS FOR REQUEST FROM LOCAL CONTROL
//    PGN300 IF LOCAL SWITCHES
//        BUFFER[0] SWI STATUS
//        BUFFER[1] PLUNGER LUBE AUTO/MAN
//     IF BUFFER[2,3] LESS THAN 0XFF THEN VALUE IS REQUESTED RPM
//     IF BUFFER[4,5] LESS THAN 0XFF THEN VALUE IS REQESTED OVER PRESSURE
//     IF BUFFER[6] LESS THAN 0XFF THEN VALUE IS REQESTED GEAR
//        BUFFER[7] NOT USED
//    PGN301 IF REMOTE SWITCHES
//
//
// 0=NO ACTIVITY
// 1=THROTTLE UP
// 2=THROTTLE DOWN
// 3=SHIFT UP
// 4=SHIFT DOWN
// 5=SET PRESSURE UP
// 6=SET PRESSURE DONW
// 7=START ENGINE
// 8=STOP ENGINE
// 9=E-STOP ENGINE
//10=NEUTRAL & IDLE ENGINE
//11=SPARE#1
//12=SPARE#2
// 01/30/07 clean up problem with momentary rpm going to 0000
//          missing break after spn detection
//01/20/09 eliminate the retrun of rpm/gear/ovp/swi
//         add E and T for eng and trans equiped
//01/31/09 add id to incoming string
//7.23.09 - added id parse routine id_num at the begining to keep truks other than the one
// that's suppose to have info
//7.31.09 - checking all routines for protocol, form, and definitions
//8.03.09 - From this date on there will be modification no tof the orignal plan
//8.13.09 - Reached out to CCS community for help. Going to save this as reference
//  still having conversion problems - X361 RS232 THRL_SHFT_ID.N.0803.2
//8.13.09 -  using X361 RS232 THRL_SHFT_ID.N.0813.09.1


#include <18F258.h>
#fuses HS,PROTECT,NOLVP,WDT32,nowdt
#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)  // Jumpers: 8 to 11, 7 to 12

#include <can-18xxx8.c>
#byte pir1=0x0F9E
#define TMR1iF  0X00
#rom int 0xf00000={0x00,0x00,0x00,0x00}

//#define offset_id 0x00 // original

#define offset_id 0x00
#define offset_e  0x02// original
#define offset_t  0x03// original

//below is an experiment in shifting to right the starting address
/*
#define offset_id 0x02
#define offset_e  0x04
#define offset_t  0x05
*/
#include <stdlib.h>
#include <input.c>



 int8 swi_status=0,loop_cnt=0,/*tmp_port,*/conv_lok,req_gear;       //buffer counter
 int8 sel_gear,cur_gear,gear, fuel_psi,oil_psi,water;
 int buffer[8],rx_len,rxstat;
 int32 rx_id,rx_ad,hrs;
 int1 /*tf_send_status=false,*/tf_lube_ovr=false/*tf_eng_run=false*/;
 //int1 tf_psi_lo=false,tf_temp_hi=false;
 int16 rpm,outspeed,inspeed,pressure_out,lube_psi,lube_temp;
 int16 pressure_max,crpm,volts,injt_psi,spn/*rpm_pre,rpm_total=0,rpm_temp*/;
 float bpm;
 int8 rm_volts,rm_hrs,alr_pump,fmi, spn_count=0,fmi_count=0/*,rpm_cnt*/;
 //int32 spn_fmi;
 int8 k_char,x=0;
 int16 x_cnt;
 int1 tf_rs232=false,tf_data_ok=false,tf_send_can=false,tf_send_232=false;
 char s1[30];
 int16 rpm_rs=740,ov_press_rs=7500,pump_id=0,id_num;
 int8 swi_rs=0,gear_rs=125,cnt_send_can=0,engine=0,trans=0;

void line_feed()
{printf("\r\n");}

/*void read_e_data()   //reads stored values from eeprom
{
  int8 count=0,address=0;
  //int8 e_data[4]; //original
  int16 e_data[4];
  for(address=0; address<=4; ++address)
   {
   e_data[count++]=READ_EEPROM(address); }
   printf(" e_data[1]=%u  ,e_data[0]=%u ,id_num ",e_data[1],e_data[0]);
  line_feed();
  id_num=make16(e_data[1],e_data[0]);
   engine=e_data[2];
   trans=e_data[3];
   line_feed();// LOADS E_DATA ARRAY, then feeds the parts
   
   printf("This from - read_e_data() Id_Num= %04lu  Engine=%02u Trans=%02u",id_num,engine,trans);
   line_feed();
}
*/

void read_e_data()   //reads stored values from eeprom
{
  int8 count=0,address=0;
  int8 e_data[4]; //original
  //int16 e_data[4];// don't use!!!!!!
  for(address=0; address<=4; ++address)
   {
   e_data[count++]=READ_EEPROM(address); } // LOADS E_DATA ARRAY, then feeds the parts
   printf(" id_num=e_data[0]=%u,id_num=e_data[1]=%u,  engine=e_data[2]=%u, trans=e_data[3]=%u ",e_data[0],e_data[1],e_data[2], e_data[3]);
  line_feed();
  id_num=make16(e_data[1],e_data[0]);
   engine=e_data[2];
   trans=e_data[3];
   line_feed();
   
   printf("This from - read_e_data() Id_Num= %04lu  Engine=%02u Trans=%02u",id_num,engine,trans);
   line_feed();
}


void write_eeprom_16bit(int offset, int16 data)
{int8 i;
for (i=0;i<2;i++)
   {
      write_eeprom(i+offset,*(&data+i));
      printf("i=%u, offset=%u, data =%lu",i, offset , data);
      line_feed();
      read_e_data();
   }
}

void write_data()  //store operating prarmeters in eeprom
{
  printf(" inside  write_data()");
  line_feed();
   write_eeprom_16bit(offset_id,id_num);
   printf(" write_eeprom_16bit - offset_id=%u ,id_num=%lu ", offset_id,id_num);
  line_feed();
    //write_eeprom(offset_id,id_num);
    write_eeprom(offset_e,engine);// orginal
   //write_eeprom_16bit(offset_e,engine);
    printf(" write_eeprom - offset_e=%u ,engine=%u ", offset_e,engine);
  line_feed();
    write_eeprom(offset_t,trans);//original
    //write_eeprom_16bit(offset_t,trans);
     printf(" write_eeprom - offset_t=%u ,trans=%u ", offset_t,trans);
  line_feed();
  printf(" LEAVING write_data()");
  line_feed();
}

void get_setup()
{
printf("get_setup()>>>>>>>>>>>>>>>>>>>>>>>");
  line_feed();
 setup_wdt(wdt_off);
  line_feed();
  printf("ID=");    //enter id number for pumper
 id_num=get_long(); //orignal statement
  //scanf(%4Lu, id_num); //my suggestion,
  //gets(id_num);//nope!
  line_feed();
  printf("Cat=01 Cummins=02 Detroit=03 Enter Engine Number=");
  engine=get_int();
  line_feed();
  printf("Cat=01 Twin Disk=02 Allison=03 Enter Trans Number=");
  trans=get_int();
  line_feed();
  write_data();
  printf("This from - get_setup() Id_Num= %04lu  Engine=%02u Trans=%02u",id_num,engine,trans);
  line_feed();
  //setup_wdt(wdt_on);
  printf(" <<<<<<<<<<<<<<<<<<<<<<<<<<LEAVING get_setup()");
  line_feed();
  setup_wdt(wdt_on);
}



void get_input_232(char* s, int max)
 {
   int len;
   char c;
 printf("inside   get_input_232() >>>>>>>>>>>>>>>.........>>>>>>");
  line_feed();
   --max;
   len=0;
   do {restart_wdt();
     c=getc();
     if(c==8) {  // Backspace
        if(len>0) {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     } else if ((c>=' ')&&(c<='~'))
       if(len<max) {
         s[len++]=c;
         //putc(c);
       }
   } while(c!=38);
   s[len]=0;
 printf("LEAVING  get_input_232()<<<<<<<<<<<<<...........<<<<<<<<<<");
  line_feed();
}

 void ck_rs232()
   {
   //printf("inside   ck_rs232() >>>>>>");
  //line_feed();
    if (kbhit())
      {
       k_char=getc();
       if (k_char=='!')
        {
         tf_RS232=true;
         x=0;
         x_cnt=0;
         tf_data_ok=false;
         output_low(pin_b5);
        }
      if ((k_char=='i') | (k_char=='I')) get_setup(); // <<<<<<<<<<
      }
       if (tf_rs232)
        {
         get_input_232(s1, 30);
         tf_rs232=false;
         tf_data_ok=true;
         output_high(pin_B5);
         //x=0;while (x<24) {putc(s1[x]);x++;}
        }

 //printf("LEAVING   ck_rs232() <<<<<<<<<<");
  //line_feed();
   }
void process_rs232()
{
   char r[5],g[5],op[6],sw[5],id[5];// this also shows original order
   //char id[5],r[5],g[5],op[6],sw[5];
 int8 x1=0;
  tf_data_ok=false;
  x=0;x1=0; while (s1[x1]<0x3a)       
       { x1++;
         if (s1[x1]>0x2f)  {r[x]=s1[x1];x++;x1++;}   //get rpm string
       }
       r[x]=0; //add null to end of string
          x=0;x1++; while (s1[x1]<0x3a)
       {if (s1[x1]>0x2f) {g[x]=s1[x1];x++;x1++;} }//get gear string
       g[x]=0;   //add null to end of string
       x=0;x1++; while (s1[x1]<0x3a)
       {if (s1[x1]>0x2f) {op[x]=s1[x1];x++;x1++;}} //get over pressure string
       op[x]=0;    //add null to end of string
       x=0;x1++; while (s1[x1]<0x3a)
       {if (s1[x1]>0x2f) { sw[x]=s1[x1];x++;x1++;}} //get switch status string
       sw[x]=0;  //add null to end of string
        x=0;x1++; while (s1[x1]<0x3a)
      {if (s1[x1]>0x2f) {id[x]=s1[x1];x++;x1++;}}//get pump_id string
       id[x]=0;  //add null to end of string
    // putc(10);putc(13);
     //x=0;while (x<strlen(r)) {putc(r[x]);x++;} putc(10);putc(13);
     //x=0;while (x<strlen(g)) {putc(g[x]);x++;} putc(10);putc(13);
     //x=0;while (x<strlen(op)) {putc(op[x]);x++;} putc(10);putc(13);
     //x=0;while (x<strlen(sw)) {putc(sw[x]);x++;} putc(10);putc(13);
      // pump_id=atol(id);
       rpm_rs=atol(r);      //convert string to number
       gear_rs=atoi(g);
       ov_press_rs=atol(op);
       swi_rs=atoi(sw);
      pump_id=atol(id); // original place
     printf("RPM=%lu Gear=%u OP=%lu SWI=%u ID=%lu\r\n",rpm_rs,gear_rs,ov_press_rs,swi_rs,pump_id);
   if (pump_id==id_num)
       {tf_send_can=true;   //set true to send received command via j1939
       cnt_send_can=0;     //init number of times to send j1939 packet with 232 com.
       tf_send_232=true;}   //set true to return data string
       
   
}



void get_swi_status()
{  int8 port;
   swi_status=0;                       //assume all switches off
   tf_lube_ovr=0;                      //assume lube override=false
   port=input_a();
   delay_ms(1);
   if (!bit_test(port,0)) swi_status=1; //throttle up
   if (!bit_test(port,1)) swi_status=2; //throttle down
   if (!bit_test(port,2)) swi_status=3; //shift up
   if (!bit_test(port,3)) swi_status=4; //shift down
   if (!bit_test(port,4)) swi_status=5; //max up
   if (!bit_test(port,5)) swi_status=6; //max down
   port=input_c();
   delay_ms(1);
   if (!bit_test(port,0)) swi_status=7; //start
   if (!bit_test(port,1)) swi_status=8; //stop
   if (!bit_test(port,2)) swi_status=9; //e-stop
   if (!bit_test(port,3)) swi_status=10;//neutral/idle
   if (!bit_test(port,5)) tf_lube_ovr=true; //lube override=true;
}
void ck_j1939_input()
{int16 a;
//printf("inside  ck_j1939_input() >>>>>>");
  //line_feed();
 if (can_kbhit())
       {restart_wdt();
        can_getd(rx_id, &buffer[0],rx_len,rxstat);
        rx_ad=rx_id&0x000000Ff;       //get source address
        if (rx_ad==05) req_gear=buffer[2];   //if source address=05 buffer[2]=req_gear;
        if (rx_ad==44) {crpm=crpm=make16(buffer[2],buffer[1]); //store req spd buffer
                        crpm=crpm>>3;               //calculate speed received rpm/8
                       }
        rx_id=rx_id&0x00ffff00;  //mask out irrevllant data
        rx_id=rx_id>>8;          //shift data to right 8 times
        switch (rx_id)
         {case (61442) : {conv_lok=buffer[0] & 0x04;
                          outspeed=make16(buffer[2],buffer[1]);
                          outspeed=outspeed>>3;            //divide by 8
                          inspeed=make16(buffer[6],buffer[5]);
                          inspeed=inspeed>>3;              //divide by 8
                          }break;
         case (65226) : {fmi=buffer[4]&0x1f;
                         spn=buffer[2];
                         spn_count = 20;
                         fmi_count = 20;
                        }break;

         case (61444) : {*(&rpm)=buffer[3];
                         *(&rpm+1)=buffer[4];
                         }
                         break;                 //correct by dividing by 8
         case (61445) : {sel_gear=buffer[0];
                         cur_gear=buffer[3];}break;
         case (65271) : {volts=make16(buffer[5],buffer[4]);
                         rm_volts=(volts%20)>>1;volts=volts/20;
                         }break;

         case (65263) : {a=(long)buffer[3]*58;oil_psi=a/100;
                         a=(long)buffer[0]*58;fuel_psi=a/100;}break;
         case (65262) : {a=buffer[0]-40;
                         a=(long)a*9;
                         a=a/5;
                         water=a+32;}break;
         case (65253) : {hrs=make32(buffer[3],buffer[2],buffer[1],buffer[0]);
                         rm_hrs=(hrs%20)>>1;hrs=hrs/20;
                        }break;
         case(65143)  : {pressure_out=make16(buffer[1],buffer[0]);
                         lube_psi=make16(buffer[3],buffer[2]);
                         lube_temp=make16(buffer[5],buffer[4]);
                        }break;

         case(302)    : pressure_max=make16(buffer[1],buffer[0]);break;

         case(303)    :{alr_pump=buffer[0];
                        *(&bpm)=buffer[1];
                        *(&bpm+1)=buffer[2];
                        *(&bpm+2)=buffer[3];
                        *(&bpm+3)=buffer[4];
                        if (bpm>99) bpm=0;}break;
         case (65243) : {*(&injt_psi) =buffer[2];
                        *(&injt_psi+1)=buffer[3];
                        injt_psi = (injt_psi*58)/100;}
                        break;
         default :{//spn=0;
                 };
         }
       }
// printf("LEAVING   ck_j1939_input() <<<<<<<<<<");
//line_feed();
}

void RS232()
{
  printf("inside  RS232()>>>>>>");
  line_feed();
  output_low(pin_b4);
  printf("I%04lu",id_num);
  rpm=rpm>>3;                             //rpm
  printf("R%04lu",rpm);
  switch (cur_gear)                       //gear
  { case (124) : gear = 1; printf("G0%u", gear); break;
    case (125) : gear = 2; printf("G0%u", gear); break;
    case (126) : gear = 3; printf("G0%u", gear); break;
    case (127) : gear = 4; printf("G0%u", gear); break;
    case (128) : gear = 5; printf("G0%u", gear); break;
    case (129) : gear = 6; printf("G0%u", gear); break;
    case (130) : gear = 7; printf("G0%u", gear); break;
    case (131) : gear = 8; printf("G0%u", gear); break;
    case (132) : gear = 9; printf("G0%u", gear); break;
    case (133) : gear = 10; printf("G%u", gear); break;
    default    : {}; break;
  }
    //oil pressure
  printf("O%03u", oil_psi);
     //water temp
  printf("C%03u",water);
    //battery volts
  printf("V%02lu.%u", volts, rm_volts);
    //pump speed
  printf("W%04lu",outspeed);
    //fuel pressure
  printf("F%03u", fuel_psi);
    //convertor lock
  printf("L%u",conv_lok);
     //ov pressure set
  printf("S%05lu", pressure_max);
    //pump output pressure
  if (pressure_out>15000) pressure_out=15000;
  printf("P%05lu", pressure_out);
    //pump lube pressure
  printf("X%03lu", lube_psi);
    //pump lube temperature
  printf("Y%03lu", lube_temp);
     //barrels per min
  if (bpm<1) printf("B0%02.2f",bpm);
  if ((bpm>=1)&&(bpm<10)) printf("B0%2.2f",bpm);
  if (bpm>=10) printf("B%2.2f",bpm);
    //engine hrs
  printf("H%05lu", hrs);
  printf("E01T01");
    //diagnostics spn
  printf("D%04lu", spn);
    //diagnostics fmi
    printf("Z%04lu",pump_id);
  printf("_%02u", fmi);
  printf("!\r\n"); //delimiter
  tf_send_232=false;
  output_high(pin_b4);
  printf("LEAVING  RS232()<<<<<<<<<<");
  line_feed();
}

void send_swi_status()
{if (tf_send_can)       //if true send this data via
 {buffer[0]=swi_rs;     //swi status packet
 buffer[1]=0x00;
 buffer[2]=*(&rpm_rs);
 buffer[3]=*(&rpm_rs+1);
 buffer[4]=*(&ov_press_rs);
 buffer[5]=*(&ov_press_rs+1);
 buffer[6]=gear_rs;
 buffer[7]=0xff;
 }else                //else only send swi_status
 {buffer[0]=swi_status;
 buffer[1]=0x00;
 buffer[2]=0xff;
 buffer[3]=0xff;
 buffer[4]=0xff;
 buffer[5]=0xff;
 buffer[6]=0xff;
 buffer[7]=0xff;
 }
 if (tf_lube_ovr) buffer[1]=0x08; //bit4 buffer[1]=1 to override auto lube
 if (!input(pin_c4)) rx_id=300;  //remote swi interface C4=0
 if (input(pin_c4)) rx_id=301;   //local switch interface C4=1
 rx_id=rx_id<<8;
 can_putd(rx_id,&buffer[0],8,1,TRUE,FALSE);
 if (tf_send_can) cnt_send_can++;    //setup to end 232 data 3 times
 if (cnt_send_can==3) tf_send_can=false; //then revert to normal mode
}

void ck_spn_fmi()
{if (spn_count > 0) spn_count--; else spn = 0;
 if (fmi_count > 0) fmi_count--; else fmi = 99;
}

void main()
{
  int1 va;
  line_feed();line_feed();
  printf(".......inside main XXXXXX>>>>>>>>>>>>XXXX");
  line_feed();
  set_tris_a(0xff);
  set_tris_c(0xBF);
  setup_timer_1(t1_internal|t1_div_by_8);  //setup timer #1
  can_init();                    //init can module
  setup_wdt(wdt_on);
  set_timer1(0xBDC);             //init timer #1
  bit_clear(pir1,tmr1if);
 // printf("just b4 read_E_data");
  line_feed();
  read_e_data();  //reads stored values from eeprom
  //printf("just afta read_E_data");line_feed();
 while(TRUE)
  {
  printf("ENTERING  >>>>>>>>>>>>>> , inside while (true)");
  line_feed();
  va = bit_test(pir1,tmr1if);
  //printf("Bit_test = value = %x, pir1 = %x, tmr1if = %x ", va,pir1,tmr1if );
  /* printf("Bit_test = value = %2x ", bit_test(pir1,tmr1if));
  line_feed();
  printf(" pir1 = %x ", pir1);
  line_feed();
  printf("tmr1if = %x", tmr1if);
  */
  line_feed();
  while (!bit_test(pir1,tmr1if))    //sample j1939 rX for 100ms
     {
     //printf("inside !bit_test pir1,tmr1if, inside while (true)");//line_feed();
     ck_j1939_input();
     ck_rs232();
     }
     if (tf_data_ok) process_rs232(); //PROCESS RS232 IF DATA IS OK
     if (tf_send_232) rs232();        //SEND DATA STRING AFTER ANALYZING RS232 COMMANDS
     loop_cnt++;
     ck_spn_fmi();
     if (loop_cnt>=3)          //send swi status every 300ms
      { get_swi_status();
        send_swi_status();
        loop_cnt=0;
      }

      set_timer1(0xBDC);           //init timer #1
      bit_clear(pir1,tmr1if);
  printf("leaving <<<<<<< inside while (true)");
  line_feed();
     
  }
  printf(".........LEAVING main <<<XXXXXXXXXXXX");
  line_feed();
}
.

Code:
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 11:42 am     Reply with quote

CCS has routines to write 16 or 32-bit integers to eeprom.
They're in this file:
Quote:
c:\program files\picc\drivers\internal_eeprom.c
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 13, 2009 11:49 am     Reply with quote

PCM programmer wrote:
CCS has routines to write 16 or 32-bit integers to eeprom.
They're in this file:
Quote:
c:\program files\picc\drivers\internal_eeprom.c


Does CCS proof-read their drivers?

A couple of lines down I read this:

Code:

////   void write_int16_eeprom(address, int16 data)                         ////
////     Call to read a 16 bit integer                                     ////
////                                                                        ////
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 11:52 am     Reply with quote

Apparently they copy and paste. It's a quick way to duplicate code
and do a change to it, but at the risk of leaving errors like that.
Also, they could use a spell checker on their comments in some files.
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 12:40 pm     Reply with quote

Gentleman and ladies if present

All very valid comments and will be taken to heart. But good news is @ hand.
And MKuang is our winner.

Taking his advice first, the read portion of the output was right on.

The proof:
08/13/2009 12:57:40.675 --> .......inside main XXXXXX>>>>>>>>>>>>XXXX

08/13/2009 12:57:40.691 --> id_num=e_data[0]=0,id_num=e_data[1]=0, engine=e_data[2]=0, trans=e_data[3]=0

08/13/2009 12:57:40.691 --> This from - read_e_data() Id_Num= 0000 Engine=00 Trans=00
08/13/2009 12:57:40.894 --> get_setup()>>>>>>>>>>>>>>>>>>>>>>>

Here is where the data is first entered

08/13/2009 12:57:40.909 --> ID=5000
08/13/2009 12:57:44.456 --> Cat=01 Cummins=02 Detroit=03 Enter Engine Number=02
08/13/2009 12:57:46.519 --> Cat=01 Twin Disk=02 Allison=03 Enter Trans Number=03

The inside write is where the input to the eeprom happens and the eeprom is read to form the output.


08/13/2009 12:57:47.550 --> inside write_data()
08/13/2009 12:57:47.566 --> data_msb=19, data_lsb=136, offset=0, data =5000
08/13/2009 12:57:47.581 --> id_num=e_data[0]=136,id_num=e_data[1]=19, engine=e_data[2]=0, trans=e_data[3]=0

08/13/2009 12:57:47.581 --> This from - read_e_data() Id_Num= 5000 Engine=00 Trans=00
08/13/2009 12:57:47.581 --> write_eeprom_16bit - offset_id=0 ,id_num=5000
08/13/2009 12:57:47.597 --> write_eeprom - offset_e=2 ,engine=0
08/13/2009 12:57:47.597 --> write_eeprom - offset_t=3 ,trans=0
08/13/2009 12:57:47.597 --> LEAVING write_data()

The next statement is reverfication and leaves the writing section.

08/13/2009 12:57:47.613 --> This from - get_setup() Id_Num= 5000 Engine=00 Trans=00
08/13/2009 12:57:47.613 --> <<<<<<<<<<<<<<<<<<<<<<<<<<LEAVING get_setup()


08/13/2009 12:57:48.144 --> .......inside main XXXXXX>>>>>>>>>>>>XXXX

08/13/2009 12:57:48.144 --> id_num=e_data[0]=136,id_num=e_data[1]=19, engine=e_data[2]=0, trans=e_data[3]=0

08/13/2009 12:57:48.159 --> This from - read_e_data() Id_Num= 5000 Engine=00 Trans=00


Next is to get the engine and trans id's to keep. I think I can manage. If not I'll be back.

And now for my benefit: why the numbers 136 and 19. What are they?

Like I said in my disclaimer, this bit shifting is kinda new.

Thanks to everyone. And MKuang take the rest of the week off.

L.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 13, 2009 12:54 pm     Reply with quote

lecutus wrote:
Gentleman and ladies if present


And now for my benefit: why the numbers 136 and 19. What are they?

L.

You start out with a 16 bit number id_number = 5000.

In binary form:

5000=0b 0001 0011 1000 1000.

Now you break it up into two eight bit numbers. The lower byte you called data[0]. So in binary form:

data[0]=0b 1000 1000

which is equal to 136 in decimal.

The upper byte of id_num is 0b 0001 0011 which you call data[1]:

data[1] = 0b 0001 0011

which is equal to 19 in decimal.
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

I feel so edified
PostPosted: Thu Aug 13, 2009 1:15 pm     Reply with quote

Thanks M, it makes sense.

And now for Mr Wayne_. In my new found exuberance I tried your version.
Not to doubt you, of course. Now Wayne_, for your explanation as to why your version works. In the blur of the last days I vaguely remember reading something to the affect of you version.

Yes folks we have two winners!!!!!!!!!!!!!!!

I ran both versions, they have same numbers and behaviours. I shan't bother with any printouts. Just look at the rest of the thread

Wayne_ why don't you take the rest of the week off as well.

I like this place!!!!!!!!!!!!!!!!!!!!!!
L Cool
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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