View previous topic :: View next topic |
Author |
Message |
help please Guest
|
pic control stepper motor |
Posted: Mon Sep 22, 2008 4:28 am |
|
|
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
|
|
Posted: Mon Sep 22, 2008 11:31 am |
|
|
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:
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:
|
|
|
Guest
|
|
Posted: Wed Sep 24, 2008 8:51 am |
|
|
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
|
|
Posted: Wed Sep 24, 2008 9:35 am |
|
|
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. |
|
|
|