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

Problems in CCS C for Servo Control on 16F627 - HELP!

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



Joined: 26 Jan 2006
Posts: 1

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

Problems in CCS C for Servo Control on 16F627 - HELP!
PostPosted: Thu Jan 26, 2006 2:12 pm     Reply with quote

I have been using the 16F627 having just migrated over to CCS C from other C compilers. Each is a little diferent. Can't seem to get it right in the CCS C world.

Code to follow - can anyone tell me what I am missing?

Thanks.

Dr D

---------------------
//SERVO TEST CONTROL 1.1, 20 JAN 06.
//for Biomass Energy Foundation, Golden, Colorado
//Programmed by David M Holmes-Smith of Holmes-Smith Enginering on
//20 Janauary 2006 with ALL RIGHTS RESERVED.
//HM: 303 344 2900, CELL: 720 840 7472


#include <16F627.h>
#use delay(clock=4000000)
#use fast_io(A)
#use fast_io(B)
#fuses INTRC, NOWDT,NOPROTECT,NOLVP, PUT

void main()
{
int input, loop;
SET_TRIS_B(0x03);
SET_TRIS_A(0xF0);

while(1)
{if (PIN_B0 == 1)
{input = input+1;
if (input > 15)
{input = 15;}
delay_ms(200);
}
else if (PIN_B1 == 1)
{input = input - 1;
if (input < 1)
{input = 1;}
delay_ms(200);
}


switch (input)
{case '0' :
for(loop=0; loop<10; loop++) //valve all the way closed
{output_high(PIN_B3);
delay_us (500);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;


case '1' :
for(loop=0; loop<10; loop++) //valve 1/15 open
{output_high(PIN_B3);
delay_us (563);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;


case '2' :
for(loop=0; loop<10; loop++) //valve 2/15 open
{output_high(PIN_B3);
delay_us (625);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_high(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;


case '3' :
for(loop=0; loop<10; loop++) //valve 3/15 open
{output_high(PIN_B3);
delay_us (687);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_high(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;


case '4' :
for(loop=0; loop<10; loop++) //valve 4/15 open
{output_high(PIN_B3);
delay_us (750);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_low(PIN_A1);
output_high(PIN_A2);
output_low(PIN_A3);
break;


case '5' :
for(loop=0; loop<10; loop++) //valve 5/15 open
{output_high(PIN_B3);
delay_us (812);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_low(PIN_A1);
output_high(PIN_A2);
output_low(PIN_A3);
break;


case '6' :
for(loop=0; loop<10; loop++) //valve 6/15 open
{output_high(PIN_B3);
delay_us (875);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_low(PIN_A3);
break;


case '7':
for(loop=0; loop<10; loop++) //valve 7/15 open
{output_high(PIN_B3);
delay_us (937);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_low(PIN_A3);
break;


case '8' :
for(loop=0; loop<10; loop++) //valve 8/15 open
{output_high(PIN_B3);
delay_us (1000);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_high(PIN_A3);
break;


case '9' :
for(loop=0; loop<10; loop++) //valve 8/15 open
{output_high(PIN_B3);
delay_us (1062);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_high(PIN_A3);
break;


case 10 :
for(loop=0; loop<10; loop++) //valve 9/15 open
{output_high(PIN_B3);
delay_us (1125);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_high(PIN_A1);
output_low(PIN_A2);
output_high(PIN_A3);
break;


case 11 :
for(loop=0; loop<10; loop++) //valve 10/15 open
{output_high(PIN_B3);
delay_us (1187);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_high(PIN_A1);
output_low(PIN_A2);
output_high(PIN_A3);
break;


case 12 :
for(loop=0; loop<10; loop++) //valve 11/15 open
{output_high(PIN_B3);
delay_us (1250);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_low(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
break;


case 13 :
for(loop=0; loop<10; loop++) //valve 12/15 open
{output_high(PIN_B3);
delay_us (1312);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_low(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
break;


case 14 :
for(loop=0; loop<10; loop++) //valve 13/15 open
{output_high(PIN_B3);
delay_us (1375);
output_low(PIN_B3);
delay_us (100);
}
output_low(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
break;


case 15 :
for(loop=0; loop<10; loop++) //valve 14/15 open
{output_high(PIN_B3);
delay_us (1437);
output_low(PIN_B3);
delay_us (100);
}
output_high(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
break;

}
}
}
Brian S



Joined: 06 Sep 2005
Posts: 13

View user's profile Send private message

PostPosted: Thu Jan 26, 2006 3:56 pm     Reply with quote

You're probably running into simple syntax issues....usually
the hardest things to spot with a new compiler.

For instance, to check Port pin B1, you need an explicit input call;
ie, "if ( input(PIN_B4) == 1 )" rather than your "if (PIN_B0 == 1)".

While the word "input" isn't specifically reserved, I avoid it as a
variable name; I find it confusing when looking for errors....

Haven't the time to check for other issues at the moment; sorry.

Good luck!
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Jan 26, 2006 5:01 pm     Reply with quote

This is what I have for a 16F877. A few changes to pin and the 0x05 and you should be running
Code:
#include <16F877.h>
#device *=16   //not needed for now.
#use delay(clock=16000000)
#fuses hs,nowdt,nolvp,protect,put,brownout
#use rs232(baud=19200,xmit=PIN_E0,INVERT,stream=DEBUG)
#case
#zero_ram
#use fast_io(A)
#byte port_a=0x05 //port A of a 16F877A (need to change this)
//======prototypes========//
void run_valve(int16 time,int8 port_a_value);
void long_delay_us(long count);//use an int16 in delay_us
//======main==============//
void main(){
  int8 input,last_value,loop;
//think of the int16 as the time delay
int16 time[17]={0,500,563,625,687,750,812,875,937,1000,1062,1125,1187,1250,1312,1375,1437};
  setup_adc_ports(NO_ANALOGS);
  fprintf(DEBUG,"Starting\n\r");
  set_tris_a(0xF0);
  while(TRUE){
    delay_ms(200);
    if (input(PIN_B0) == 0){ //button pressed,count up
      input = input+1;
      if (input > 16){input = 16;}//keep input < 16
    }
    else if (input(PIN_A4)==0){ //button pressed,count down
      input = input - 1;
      if (input < 1){input = 1;}//keep input >0
    }
    if(input!=last_value){
      fprintf(DEBUG,"input=%u ",input);
      run_valve(time[input],input);
      last_value=input;
    }
  }//end while(true)
}//end main
//===============run_valve==============//
void run_valve(int16 time_uS,int8 port_a_value){
  int8 loop;
  fprintf(DEBUG,"time=%lu value=%u\n\r",time_uS,port_a_value);
  for(loop=0; loop<10; loop++){ //valve 3/15 open
    output_high(PIN_B3);
    long_delay_us(time_uS);
    output_low(PIN_B3);
    delay_us (100);
  }
  port_a=port_a_value;
  //  output_high(PIN_A0);//  output_high(PIN_A1);//  output_low(PIN_A2);//  output_low(PIN_A3);
  break;
}
//===========long_delay_us===========//
void long_delay_us(long count)
{
  char i;
  i = (char)(count >> 8);
  while(i >= 1)
  {
    i--;
    delay_us(253);
    restart_wdt();
  }
  delay_us((char)count);
}
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