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 CCS Technical Support

pic control stepper motor

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







pic control stepper motor
PostPosted: Mon Sep 22, 2008 4:28 am     Reply with quote

Hello, I need some help.
I am still writing on the source code to connect the stepper motor and ULN2803 with the pic 16f877.
I try to show a motor rotate front, back, left, right and stop with five switch.

The following is my code. Could you tell me where is the problem?

Code:

/********************************************
*File    :joe4.c
*Purpose :Led Blinker
*Author  :Tammanoon Luangoon
*********************************************/
#define _PIC16f877_

#ifdef _PIC16F877_
#include <16F877.h>
#define TxD    PIN_C6
#define RxD    PIN_C7
#define CLOCK_SP 20000000

#else
#include <16F628.h>
#define TxD    PIN_A2
#define TxD    PIN_A3
#define CLOCK_SP 4000000

#endif

#fuses   HS
#fuses   NOLVP, NOWDT
#fuses   NOPROTECT
#use delay (clock=CLOCK_SP)


//#define    delay_ms(x)


#define LED     PIN_D0
#define LED1    PIN_D1
#define LED2    PIN_D2
#define LED3    PIN_D3
#define LED4    PIN_D4
#define LED5    PIN_D5
#define LED6    PIN_D6
#define LED7    PIN_D7

#byte port_d=8

///////////////////////////////////////////
/*****************************************
Rotate Right at Motor1
*****************************************/
void Rotate_Front(void){
  set_tris_b(0xFF);
   while(TRUE){
  if(!input(PIN_B2)){
 port_d = 0x00 ;
 delay_ms(1000);
 break;

  }else{
 
   output_high(LED);
   delay_ms(60);
   output_high(LED1);
    output_high(LED);
   delay_ms(60);
    output_low(LED);
   output_high(LED1);
     delay_ms(60);
      output_high(LED2);
    output_high(LED1);
   delay_ms(60);
     output_high(LED2);
     output_low(LED1);
     delay_ms(60);
      output_high(LED2);
       output_high(LED3);
   delay_ms(60);
   output_low(LED2);
    output_high(LED3);
     delay_ms(60);
      output_high(LED3);
      output_high(LED);
   delay_ms(60);
   output_low(LED3);
 
  }
   }
}

/*****************************************
Rotate Lift at Motor1
*****************************************/
void Rotate_Back (void){
   
   while(TRUE){
   if(!input(PIN_B1)){
 port_d = 0x00 ;
 delay_ms(1000);
 break;

  }else{
 
   output_high(LED3);
   delay_ms(60);
   output_high(LED2);
    output_high(LED3);
   delay_ms(60);
    output_low(LED3);
   output_high(LED2);
     delay_ms(60);
      output_high(LED2);
    output_high(LED1);
   delay_ms(60);
     output_high(LED1);
     output_low(LED2);
     delay_ms(60);
      output_high(LED1);
       output_high(LED);
   delay_ms(60);
   output_low(LED1);
    output_high(LED);
     delay_ms(60);
      output_high(LED);
      output_high(LED3);
   delay_ms(60);
   output_low(LED);
   
  }
   }
}
/*****************************************
Rotate_Right at Motor2
*****************************************/
void Rotate_Righ(void){
   while(TRUE){
   if(!input(PIN_B5)){
 port_d = 0x00 ;
 delay_ms(1000);
 break;

  }else{
   output_high(LED4);
   delay_ms(60);
   output_high(LED5);
    output_high(LED4);
   delay_ms(60);
    output_low(LED4);
   output_high(LED5);
     delay_ms(60);
      output_high(LED6);
    output_high(LED5);
   delay_ms(60);
     output_high(LED6);
     output_low(LED5);
     delay_ms(60);
      output_high(LED6);
       output_high(LED7);
   delay_ms(60);
   output_low(LED6);
    output_high(LED7);
     delay_ms(60);
      output_high(LED7);
      output_high(LED4);
   delay_ms(60);
   output_low(LED7);
  }
   }
}
/*****************************************
Rotate Lift at Motor2
*****************************************/
void Rotate_Lift (void){
   while(TRUE){
   if(!input(PIN_B4)){
 port_d = 0x00 ;
 delay_ms(1000);
 break;

  }else{
   output_high(LED7);
   delay_ms(60);
   output_high(LED6);
    output_high(LED7);
   delay_ms(60);
    output_low(LED7);
   output_high(LED6);
     delay_ms(60);
      output_high(LED6);
    output_high(LED5);
   delay_ms(60);
     output_high(LED5);
     output_low(LED6);
     delay_ms(60);
      output_high(LED5);
       output_high(LED4);
   delay_ms(60);
   output_low(LED5);
    output_high(LED4);
     delay_ms(60);
      output_high(LED4);
      output_high(LED7);
   delay_ms(60);
   output_low(LED4);
  }
   }
}
/*****************************************
Stop at Motor
*****************************************/
void LedOff (void){
   int bit_chk=FALSE;
   set_tris_b(0x00);
   while(TRUE){
   if(!input(pin_B3)) bit_chk=FALSE;
       if(bit_chk) {
       port_d = 0x00 ;
       delay_ms(1000);
   break;
   
    }else{   
   output_low(PIN_D0);
   output_low(PIN_D1);
   output_low(PIN_D2);
   output_low(PIN_D3);
   output_low(PIN_D4);
   output_low(PIN_D5);
   output_low(PIN_D6);
   output_low(PIN_D7);
   
   delay_ms(60);
}
   }
}

/***************************************
Funtion : main
***************************************/
void main (void){
 
   set_tris_b(0x00);
 
  while(TRUE){
  if(!input(PIN_B1)){
  Rotate_Front();
  }
  if(!input(PIN_B2)){
  Rotate_Back();
  }
  if(!input(pin_B3)) {
  LedOff();
  }
  if(!input(PIN_B4)){
  Rotate_Righ();
  }
  if(!input(PIN_B5)){
   Rotate_Lift();
  }
  delay_ms(60);
 
  }
}


And my picture simulation
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 22, 2008 11:31 am     Reply with quote

Quote:
set_tris_b(0xFF);
set_tris_b(0x00);

1. Get rid of all the set_tris statements. Let the compiler set the TRIS
for you. If you use the CCS i/o functions, such as output_low(), etc.,
the compiler will set the correct TRIS before it sets the i/o pin to a low
level.


Quote:
#byte port_d=8
port_d = 0x00 ;

2. Get rid of direct writing to i/o Port registers. Use the CCS functions.
Then the compiler will also set the correct TRIS for you. Do it this way:
Code:
output_d(0x00);



Quote:
output_low(PIN_D0);
output_low(PIN_D1);
output_low(PIN_D2);
output_low(PIN_D3);
output_low(PIN_D4);
output_low(PIN_D5);
output_low(PIN_D6);
output_low(PIN_D7);

3. Don't use 8 statements to do this. Use one statement:
Code:
output_d(0x00);
Guest








PostPosted: Wed Sep 24, 2008 8:51 am     Reply with quote

Your Main function does not loop back.. the compiler adds a sleep() function at the end unless you tell the program to keep executing.
ckielstra



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

View user's profile Send private message

PostPosted: Wed Sep 24, 2008 9:35 am     Reply with quote

Anonymous wrote:
Your Main function does not loop back.. the compiler adds a sleep() function at the end unless you tell the program to keep executing.
The code layout is messed up but there is a while(TRUE), so his Main does never end.
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