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

Trying to understand how information is passed to and from

 
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

Trying to understand how information is passed to and from
PostPosted: Fri Sep 11, 2009 3:00 pm     Reply with quote

Trying to understand how information is passed to and from a PIC18F258.

I'm using rs-232 as the means of transmission. I've looked at the pin diagram, PIN 17/ C6/RC6/TX/CK and PIN18/C7/RC7/RX/DT are the trans and recv respectively. But the program I inherited this project so I'm trying to educate myself in this format as much as possible. The program uses couple of mechanisms that I need to clarify, for my benefit.

Pins b4 are used in rs232() and ck_rs232() in output_low mode and output_high.

I've read on the use of getc and how its tied to the RCV, which I think would be pin18, in this case. Is this correct? The getc is used with srt1, a char variable array: used in get_input_232().

Next question, the use of kbhit(), I've looked at the C Compiler book, pg.117 built in functions, and the explanation is very sparse. How is it tied to getc()?

The output seems to work, but to be through. I don't see at all in the program, where the output goes to, pin wise. I have printfs scattered through the program to check the behaviour of the program. The printf's work.



Now for the program snippets that are in question:
Preprocessor:
Code:

#include <18F258.h>
//#fuses HS,PROTECT,NOLVP,WDT32,nowdt// original
#fuses HS,PROTECT,NOLVP,nowdt,brownout,BORV42,noprotect
#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
#define offset_e  0x02// original
#define offset_t  0x03// original

#include <stdlib.h>
#include <stdio.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_lube_ovr=false/*tf_send_status=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 str1[30];
 //int16 rpm_rs=740,ov_press_rs=7500,pump_id=0,id_num; //original
 int16 rpm_rs=740,ov_press_rs=750,pump_id=0,id_num;
 int8 swi_rs=0,gear_rs=124,cnt_send_can=0,engine=0,trans=0;




The sections I'm having trouble with:
Code:

void get_input_232(char* s, int8 max)
 {
   int len;
   char c;
   --max;
   len=0;
   
   printf("inside   get_input_232() >>>>>>>>.........>>>>>>");  line_feed();
   /*do {
     restart_wdt();
     c = getc();
     printf("c =  %x in hex  %d in dec %c in char",c,c,c );line_feed();
     if(c==8)// Backspace= bs = 0x08
     { 
        if(len>0)
        {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     }
     else if ((c>=' ')&&(c<='~'))
       if(len<max)
       {
         s[len++]=c;
         //putc(c);
       }
   } while(c!=38);// & = ampersans = 0x26*/



//while(c!=38);// & = ampersans = 0x26 = 38 // orginal
while(c!= '&');
 {
     restart_wdt();
     c = getc();
     printf("c =  %x in hex  %d in dec %c in char",c,c,c );line_feed();
     if(c==8)// Backspace= bs = 0x08
     { 
        if(len>0)
        {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     }
     else if ((c>=' ')&&(c<='~'))// '  ' = space = 0x20, '~' = tilde = 0x7e
       if(len<max)
       {
         s[len++]=c;
         //putc(c);
       }
   }

   s[len]=0;
   printf("LEAVING  get_input_232()<<<    S=%s  <<<<<<<<<<", s);  line_feed();
}

 void ck_rs232()
   {
   int1 val;
    val = kbhit();
    printf(">>>> ENTERING ck_rs232() kbhit = %u >>>>>>",  val  );   line_feed();
    if (kbhit())
      {
       k_char=getc();
       printf("k_char=%c" , k_char); line_feed();
       if (/*k_char =='!' ||*/ k_char == 0x21) // ! = exclamation = 0x21
         {
            tf_rs232=true;
            printf("tf_RS232 = %u", tf_RS232,);line_feed();
            x=0;
            x_cnt=0;
            tf_data_ok=false;
            output_low(pin_b5);
         }
       if ((k_char=='i') | (k_char=='I'))
         {get_setup();}
      }
     
     //tf_rs232 = true;
    printf("XXX tf_rs232=%U xxx",tf_rs232);   line_feed();
    if (tf_rs232)
        {
            //printf("B4 -- str1=%S --",str1);   line_feed();
            get_input_232(str1, 30);
            printf("afta -- str1 = %S",str1);   line_feed();
            tf_rs232=false;
            tf_data_ok=true;
            output_high(pin_b5);
            //x=0;while (x<24) {putc(str1[x]);x++;}
        }

    printf("<<< LEAVING ck_rs232()^^^ tf_rs232= %u  tf_data_ok= %u <<<<<", tf_rs232, tf_data_ok );   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;
   //printf("ENTERING   process_rs232() ^^^^^^^");   line_feed();
   x=0;x1=0;
   printf("str1 = %S",str1);   line_feed();
   // 0x2f  = '/' = forward slash, < 0-9 are between these symbols >, 0x3a = ':' = colon  0x3b = ';' = semicolon;
   while (str1[x1]<0x3a)//get pump_id string
    {
      x1++;
      if (str1[x1]>0x2f) {id[x]=str1[x1];x++;x1++;}
    }
   id[x]=0;  //add null to end of string
   pump_id=atol(id);
   printf(" ID=%lu, RPM=%lu Gear=%u OP=%lu SWI=%u \r\n",pump_id, rpm_rs,gear_rs,ov_press_rs,swi_rs);
   
   //if (pump_id == id_num)
   //{
       x=0;x1++;
       while (str1[x1]<0x3a)
       {
         if (str1[x1]>0x2f)  {g[x]=str1[x1];x++;x1++;}
       }//get gear string
        g[x]=0;   //add null to end of string
   
       x=0;x1++;
      while (str1[x1]<0x3a)
       {
         if (str1[x1]>0x2f) {op[x]=str1[x1];x++;x1++;}
       } //get over pressure string
       op[x]=0;    //add null to end of string
     
      x=0;x1++;
      while (str1[x1]<0x3a)
        {
          if (str1[x1]>0x2f) { sw[x]=str1[x1];x++;x1++;}
        } //get switch status string
      sw[x]=0;  //add null to end of string
     
       x=0;x1++;
       while (str1[x1]<0x3a)
       {
         if (str1[x1]>0x2f)  {r[x]=str1[x1];x++;x1++;}   //get rpm string
       }
     r[x]=0; //add null to end of string
     
     
         // 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(" ID=%lu, RPM=%lu Gear=%u OP=%lu SWI=%u \r\n",pump_id, rpm_rs,gear_rs,ov_press_rs,swi_rs);
     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 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
        printf("rx_id = %4lu", rx_id);
        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);//for blinking light?
  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 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;
     int8 while_cnt=0;
     //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
     printf("b4 pir1 = %x, tmr1if = %x ",pir1,tmr1if );  line_feed();
     bit_clear(pir1,tmr1if);
     printf("afta  pir1 = %x, tmr1if = %x ",pir1,tmr1if );  line_feed();
     //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();
  line_feed();
  va = !bit_test(pir1,tmr1if);
  printf("!Bit_test = value = %x, pir1 = %x, tmr1if = %x ", va,pir1,tmr1if );  line_feed();
  printf("ENTERING   small while");line_feed();
  while (!bit_test(pir1,tmr1if))    //sample j1939 rX for 100ms, original
     {
        printf("while_cnt = %u",++while_cnt);   line_feed();
        printf("!Bit_test = value = %x, pir1 = %x, tmr1if = %x ", !bit_test(pir1,tmr1if),pir1,tmr1if );  line_feed();
        ck_j1939_input();
        ck_rs232();
        printf("AFTA CHECKING !Bit_test = value = %x, pir1 = %x, tmr1if = %x ", !bit_test(pir1,tmr1if),pir1,tmr1if );  line_feed();
       
     }
     printf("LEAVING  small while");line_feed();
     printf("vvvvvv afta checking tf_data_ok=%u , tf_send_232=%u",tf_data_ok,tf_send_232);line_feed();
     while_cnt = 0;
   tf_data_ok = true;
     if (tf_data_ok) //PROCESS RS232 IF DATA IS OK
        {
        process_rs232();
        }
    //tf_send_232 = true;
    if (tf_send_232)//SEND DATA STRING AFTER ANALYZING RS232 COMMANDS
         {
         //process_rs232();//
         rs232();
         }       
    // printf("^^^^^^^^^^^^^^^^^^^^^^  tf_data_ok=%u , tf_send_232=%u",tf_data_ok,tf_send_232);line_feed();
     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
      printf("at the end b4 clear----- pir1 = %u ,tmr1if  = %u",pir1,tmr1if); line_feed();
      bit_clear(pir1,tmr1if);
      printf("at the end afta clear----- pir1 = %u ,tmr1if  = %u",pir1,tmr1if); line_feed();
  //printf("leaving <<<<<<< inside while (true)");
  //line_feed();
     
  }
  printf(".........LEAVING main <<<XXXXXXXXXXXX");  line_feed();
}


Map of the trouble:

When in main(), the “while (!bit_test(pir1,tmr1if)”, does go into the ck-j1939 and the ck_rs232, but nothing happens. If nothing happens here, the rest is not turned on to do any processing.

It all seems pretty straight forward, send an info packet, to the Pic18F258, it send along the CANBUS route to the engine, engine sends its form of info packet back to the PIC18F258. This information is then sent, via rs232, to a computer for further processing.

Form of the packets:

sent from from computer to PIC to CANBUS/J1939 is:
Packet format looks like this:
!!!5000 ;2000;125;7400;0&&&
!!!.TRUCK_ID:rpm;gear;over_pressure_setpt;switch_status&&&

sent from CANBUS/J1939 to PIC to computer is:

I5000R0740G02O099C060V23.3W2442F099L4S00500P00000X126Y057B27.10H65253E01T01D0000_99!

The above form/packet is defined in rs232().


I'm using PIC18F258 and PCHWD 4.079

There other parts to this program, but I left them out as I didn't feel they were germain to the problem. For completeness I will post the the entire thing seperately for completeness.

Thanks.

L.
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

The entire progam
PostPosted: Fri Sep 11, 2009 3:03 pm     Reply with quote

Here it is folks in all its gore.


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
//8.14.09 - Fixed the ID, engine, and trans id's they stick. Now working on Id matching truck/pump id
//   to process the rest of the input


#include <18F258.h>
//#fuses HS,PROTECT,NOLVP,WDT32,nowdt// original
#fuses HS,PROTECT,NOLVP,nowdt,brownout,BORV42,noprotect
#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
#define offset_e  0x02// original
#define offset_t  0x03// original

#include <stdlib.h>
#include <stdio.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_lube_ovr=false/*tf_send_status=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 str1[30];
 //int16 rpm_rs=740,ov_press_rs=7500,pump_id=0,id_num; //original
 int16 rpm_rs=740,ov_press_rs=750,pump_id=0,id_num;
 int8 swi_rs=0,gear_rs=124,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
 
  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, count=%u",e_data[0],e_data[1],e_data[2], e_data[3], count);
  //line_feed();
  id_num=make16(e_data[1],e_data[0]);
  engine=e_data[2];
  trans=e_data[3];
  printf(" 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,*((char *)&data + i));
      printf("i=%u, offset=%u, data =%lu",i, offset , data);
      line_feed();
      read_e_data();
   }
}

void write_eeprom_16bit_1(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);
   
   printf("data_msb=%u, data_lsb=%u, offset=%u, data =%lu",data_msb, data_lsb, offset , data);
   line_feed();

//read_e_data();
   
}

void write_data()  //store operating prarmeters in eeprom
{
   printf(" inside  write_data()");
   line_feed();
   write_eeprom_16bit_1(offset_id,id_num);// this works
   //write_eeprom_16bit(offset_id,id_num); //this works too!!!!
   printf(" write_eeprom_16bit - offset_id=%u ,id_num=%lu ", offset_id,id_num);
   line_feed();
   
   write_eeprom(offset_e,engine);// orginal
   printf(" write_eeprom - offset_e=%u ,engine=%02u ", offset_e,engine);
   line_feed();
   write_eeprom(offset_t,trans);//original
   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
     
     line_feed();
     printf("Cat=01 Cummins=02 Detroit=03 Enter Engine Number=");
     //printf("crap");
     
     engine = get_int();
     line_feed();
      printf("engine=%02u", engine);
     line_feed();
   
     printf("Cat=01 Twin Disk=02 Allison=03 Enter Trans Number=");
     trans = get_int();
     line_feed();
     printf("trans=%02u",trans);
     
     line_feed();
     write_data();//<<<<<<<<<<<<<<<<<<<<<<<<<<
     printf("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);
}

//From this point up, all worx don't touch.

void get_input_232(char* s, int8 max)
 {
   int len;
   char c;
   --max;
   len=0;
   
   printf("inside   get_input_232() >>>>>>>>.........>>>>>>");  line_feed();
   /*do {
     restart_wdt();
     c = getc();
     printf("c =  %x in hex  %d in dec %c in char",c,c,c );line_feed();
     if(c==8)// Backspace= bs = 0x08
     { 
        if(len>0)
        {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     }
     else if ((c>=' ')&&(c<='~'))
       if(len<max)
       {
         s[len++]=c;
         //putc(c);
       }
   } while(c!=38);// & = ampersans = 0x26*/



//while(c!=38);// & = ampersans = 0x26 = 38 // orginal
while(c!= '&');
 {
     restart_wdt();
     c = getc();
     printf("c =  %x in hex  %d in dec %c in char",c,c,c );line_feed();
     if(c==8)// Backspace= bs = 0x08
     { 
        if(len>0)
        {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     }
     else if ((c>=' ')&&(c<='~'))// '  ' = space = 0x20, '~' = tilde = 0x7e
       if(len<max)
       {
         s[len++]=c;
         //putc(c);
       }
   }

   s[len]=0;
   printf("LEAVING  get_input_232()<<<    S=%s  <<<<<<<<<<", s);  line_feed();
}

 void ck_rs232()
   {
   int1 val;
    val = kbhit();
    printf(">>>> ENTERING ck_rs232() kbhit = %u >>>>>>",  val  );   line_feed();
    if (kbhit())
      {
       k_char=getc();
       printf("k_char=%c" , k_char); line_feed();
       if (/*k_char =='!' ||*/ k_char == 0x21) // ! = exclamation = 0x21
         {
            tf_rs232=true;
            printf("tf_RS232 = %u", tf_RS232,);line_feed();
            x=0;
            x_cnt=0;
            tf_data_ok=false;
            output_low(pin_b5);
         }
       if ((k_char=='i') | (k_char=='I'))
         {get_setup();}
      }
     
     //tf_rs232 = true;
    printf("XXX tf_rs232=%U xxx",tf_rs232);   line_feed();
    if (tf_rs232)
        {
            //printf("B4 -- str1=%S --",str1);   line_feed();
            get_input_232(str1, 30);
            printf("afta -- str1 = %S",str1);   line_feed();
            tf_rs232=false;
            tf_data_ok=true;
            output_high(pin_b5);
            //x=0;while (x<24) {putc(str1[x]);x++;}
        }

    printf("<<< LEAVING ck_rs232()^^^ tf_rs232= %u  tf_data_ok= %u <<<<<", tf_rs232, tf_data_ok );   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;
   //printf("ENTERING   process_rs232() ^^^^^^^");   line_feed();
   x=0;x1=0;
   printf("str1 = %S",str1);   line_feed();
   // 0x2f  = '/' = forward slash, < 0-9 are between these symbols >, 0x3a = ':' = colon  0x3b = ';' = semicolon;
   while (str1[x1]<0x3a)//get pump_id string
    {
      x1++;
      if (str1[x1]>0x2f) {id[x]=str1[x1];x++;x1++;}
    }
   id[x]=0;  //add null to end of string
   pump_id=atol(id);
   printf(" ID=%lu, RPM=%lu Gear=%u OP=%lu SWI=%u \r\n",pump_id, rpm_rs,gear_rs,ov_press_rs,swi_rs);
   
   //if (pump_id == id_num)
   //{
       x=0;x1++;
       while (str1[x1]<0x3a)
       {
         if (str1[x1]>0x2f)  {g[x]=str1[x1];x++;x1++;}
       }//get gear string
        g[x]=0;   //add null to end of string
   
       x=0;x1++;
      while (str1[x1]<0x3a)
       {
         if (str1[x1]>0x2f) {op[x]=str1[x1];x++;x1++;}
       } //get over pressure string
       op[x]=0;    //add null to end of string
     
      x=0;x1++;
      while (str1[x1]<0x3a)
        {
          if (str1[x1]>0x2f) { sw[x]=str1[x1];x++;x1++;}
        } //get switch status string
      sw[x]=0;  //add null to end of string
     
       x=0;x1++;
       while (str1[x1]<0x3a)
       {
         if (str1[x1]>0x2f)  {r[x]=str1[x1];x++;x1++;}   //get rpm string
       }
     r[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(" ID=%lu, RPM=%lu Gear=%u OP=%lu SWI=%u \r\n",pump_id, rpm_rs,gear_rs,ov_press_rs,swi_rs);
     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
        printf("rx_id = %4lu", rx_id);
        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);//for blinking light?
  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 send 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;
     int8 while_cnt=0;
     //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
     printf("b4 pir1 = %x, tmr1if = %x ",pir1,tmr1if );  line_feed();
     bit_clear(pir1,tmr1if);
     printf("afta  pir1 = %x, tmr1if = %x ",pir1,tmr1if );  line_feed();
     //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();
  line_feed();
  va = !bit_test(pir1,tmr1if);
  printf("!Bit_test = value = %x, pir1 = %x, tmr1if = %x ", va,pir1,tmr1if );  line_feed();
  printf("ENTERING   small while");line_feed();
  while (!bit_test(pir1,tmr1if))    //sample j1939 rX for 100ms, original
     {
        printf("while_cnt = %u",++while_cnt);   line_feed();
        printf("!Bit_test = value = %x, pir1 = %x, tmr1if = %x ", !bit_test(pir1,tmr1if),pir1,tmr1if );  line_feed();
        ck_j1939_input();
        ck_rs232();
        printf("AFTA CHECKING !Bit_test = value = %x, pir1 = %x, tmr1if = %x ", !bit_test(pir1,tmr1if),pir1,tmr1if );  line_feed();
       
     }
     printf("LEAVING  small while");line_feed();
     printf("vvvvvv afta checking tf_data_ok=%u , tf_send_232=%u",tf_data_ok,tf_send_232);line_feed();
     while_cnt = 0;
   //tf_data_ok = true;
     if (tf_data_ok) //PROCESS RS232 IF DATA IS OK
        {
        process_rs232();
        }
    tf_send_232 = true;
    if (tf_send_232)//SEND DATA STRING AFTER ANALYZING RS232 COMMANDS
         {
         //process_rs232();//
         rs232();
         }       
    // printf("^^^^^^^^^^^^^^^^^^^^^^  tf_data_ok=%u , tf_send_232=%u",tf_data_ok,tf_send_232);line_feed();
     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
      printf("at the end b4 clear----- pir1 = %u ,tmr1if  = %u",pir1,tmr1if); line_feed();
      bit_clear(pir1,tmr1if);
      printf("at the end afta clear----- pir1 = %u ,tmr1if  = %u",pir1,tmr1if); line_feed();
  //printf("leaving <<<<<<< inside while (true)");
  //line_feed();
     
  }
  printf(".........LEAVING main <<<XXXXXXXXXXXX");  line_feed();
}




Thanks

L.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Sep 11, 2009 5:18 pm     Reply with quote

you have a fuse set for NO_WDT

then you set up the WDT in main

so these two lines of code are not something i think i would ever use together in your "trouble code"

restart_wdt();
c = getc();

what happens if NO RS-232 chars come in during the wdt interval ?

seems like an awfully strong cure for not using KBHIT(); function

Very Happy
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 6:20 am     Reply with quote

I wouldn't use the given code as a basis for new developments. It will take you lots of time trying to understand the original coder's intentions, especially because the code is poorly written, has a terrible layout, meaningless variable names and bugs.

Here is an example of a bug I spotted:
Code:
       buffer[3]=*(&rpm_rs+1);
This worked correctly in the v3 compiler but version4 handles this ANSI compliant and will calculate with a 16bit pointer, not with the 8bit pointer as intended. Correct code would now be:
Code:
       buffer[3]=*(((int8*)&rpm_rs)+1);
But as this is difficult to read code I would set up the whole function different and use a structure instead.

In my experience you will get faster and better results by rewriting the code. Take the known requirements and write a new program from scratch.
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

Where to go to see the latest technique of Compiler 4?
PostPosted: Mon Sep 14, 2009 7:46 am     Reply with quote

Where do I go to see the latest technique of Compiler 4?

As ckielstra pointed out rewriting to the code is best. But the question remains, how is information in general passed to and from this particular chip?

Now I am going to rewrite using structures, this is what I did on the computer side, and had pretty good luck.

Thanks

L.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Using RS2323 & Compiler 4
PostPosted: Mon Sep 14, 2009 1:15 pm     Reply with quote

Maybe I don't understand what your real problem is.

CCS provides a sample code "EX_STR.C" with their compiler.

This shows your how to go about getting RS232 data in and out.

I agree with the other replies. It's easier to write your own code than try to decipher someone elses.

Mike W
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

I'm doing at that right now
PostPosted: Mon Sep 14, 2009 2:21 pm     Reply with quote

As to the example thanks

L.
lecutus



Joined: 12 Aug 2009
Posts: 40

View user's profile Send private message

Let me see if I got this right.
PostPosted: Tue Sep 15, 2009 1:26 pm     Reply with quote

I've studied the EX_STR.C and the get_string seems to be my best bet. As to the mechanics, is the get_string is tied logically to what ever pin I have designated as receiver pin for rs232. In my case, rcv=pinc7. One other related question, what are these and their purpose, jumpers 8 to 11 and 7 to 12



I've also looked at http://www.ccsinfo.com/forum/viewtopic.php?t=39007, this is a lot like what I'm going through.

The big picture is I want to read what's coming in, rcv pin, see if it's the right length and process.

pcwhd 4.079

Thanks
L.
Code:

#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)  // Jumpers: 8 to 11, 7 to 12
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Jumpers & pin settings
PostPosted: Mon Sep 21, 2009 3:49 pm     Reply with quote

The Jumpers refer to CCS prototyping boards. If you are using your own hardware the jumper settings are irrelevant.

The pin selection settings are needed for PICs without a hardware UART.

PIC16c56 for example needs a software UART to achieve RS232 comms.

It doesn't make sense to use a software UART on a PIC with a hardware one built in.

I haven't tried the effect of setting xmit & rcv to anything other than the dedicated pin_C6 & pin_C7, but I suppose it may be interesting.

The microchip data sheet tells you which pins are dedicated to the UART function.

The EX_STR.C example shows you how to use standard 'C' functions for character counting etc.

Mike W
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