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

So why bother with all these other input/output pins?

 
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

So why bother with all these other input/output pins?
PostPosted: Fri Oct 16, 2009 4:05 pm     Reply with quote

In my case I'm using rs232 for input/output. So why bother with all these other input/output pins?

Second question. The proper use of input_a or the strategy of port = input_a(). Why? This can be found in get_swi_status(), it's listed at the very bottom of this post.

I inherited this program, and I'm in the middle of a rewrite. I usually write at a higher level, not to say i'm a bad [spam] or anything, it's more like fourth gen languages like C++, C#, and Delphi. So this bit shifting thing is still a little new. I've received extremely helpful advice from this site.

Here we go again.

I am using PCWHD 4.079

The 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,  stream=rs_str)  // 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 <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <input.c>

//char* str1;
 //char str1[100];
 int1 tf_rs232=false,tf_data_ok=false,tf_send_can=false,tf_send_232=false,tf_lube_ovr=false;
 
 int8 swi_status=0,loop_cnt=0,conv_lok,req_gear;       //buffer counter
 int8 sel_gear,cur_gear,gear, fuel_psi,oil_psi,water,rm_volts,rm_hrs,alr_pump,fmi,spn_count=0,fmi_count=0,k_char,x=0;
 
 int buffer[8],rx_len,rxstat;
 
 int16 rpm,outspeed,inspeed,pressure_out,lube_psi,lube_temp,pressure_max,crpm,volts,injt_psi,spn,x_cnt;
 
 int32 rx_id,rx_ad,hrs;
 
 float bpm;
 
 //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;



PIC18F258 Pins used

PIN_C6 transmit via rs232/serial
Used by: Rs232()

PIN_C7 Receive via rs2232/serial
Used by: Rs232()

pin_b4
Set high & low
Digital I/O, interrupt-on-change pin
Used by: RS232()

pin_b5
Set high & low
Digital I/O, interrupt-on-change pin, Low-voltage ICSP programming enable
Used by: ck_rs232()

pin_c4
if (!input(pin_c4)) rx_id=300; //remote swi interface C4=0
if (input(pin_c4)) rx_id=301; //local switch interface C4=1

Digital I/O

Used by: send_swi_status()


The context/modules in which they're used

Code:
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");//this is how to dummy the info
    //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_rs232()
   {
   int1 val;
   //int8 x, k;
   val = kbhit();
   // printf(">>>> ENTERING ck_rs232() kbhit = %u >>>>>>",  val  );   line_feed();
    if (kbhit() /*|| getc()== '!'*/ )
      {
         
       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();}
      }
     
    if (tf_rs232)
        {
         //get_info_rs232_2();
         get_info_rs232_3();           
         tf_rs232=false;
         //tf_data_ok=true;
         output_high(pin_b5); // this maybe it!!!!!!!!!!!!! Nope leave alone         
        }
   }


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 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;
}

Thanks L.
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