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

Motor Control

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







Motor Control
PostPosted: Mon Mar 27, 2006 7:46 pm     Reply with quote

I am using the PIC 16F874, I am trying to change the direction of a DC motor by simple interupt switches. I am outputing to an L272 chip which drives the motor and accepts two logic inputs. A C0 of one and C1 of zero puts a postivive voltage on the motor and a C0 of zero and C1 one should but a negative voltage on my motor. The L272 chip works fine independently of the PIC and the PIC works fine without connecting to the L272 chip. When I hook them together I can only get the motor to run in one direction.

Please can you take a look at my code and see if i am doing something wrong.

#include <16F874.H>
#use delay(clock=20000000)
char event;

void main()
{
enable_interrupts(INT_rb);
enable_interrupts(global);


// Initializing default values
event = 'r';


while(event == 'r')
{
//motor forward
output_low(PIN_C1);
output_high(PIN_C0);

}

while(event == 'g')
{
output_high(PIN_C1);
output_low(PIN_C0);
}
}


#int_rb
rb_isr ( )

{

// Initializing everything
byte changes;
int last_b;
int port_b;
changes = last_b ^ port_b;
last_b = port_b;

// if wheel has went one complete revolution
if (!input(PIN_b7)){event = 'r';}

// if bob hit grain
if (!input(PIN_b6)){event = 'g';}

// if bob is at housing
if (!input(PIN_b5)){event = 'h';}

//debounce
delay_ms(100);

}
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Mar 28, 2006 7:21 am     Reply with quote

What direction does the motor spin in? What are the logic lines doing when the motor is spinning the wrong way? Can you put a meter, or better yet a scope on them? Is the motor putting noise on the PIC power line?
_________________
The search for better is endless. Instead simply find very good and get the job done.
Ttelmah
Guest







PostPosted: Tue Mar 28, 2006 7:49 am     Reply with quote

The first few lines of the interrupt handler do nothing. You need to declare 'last_b', as a _static_ variable, or it's contents may be lost between interrupts. Also the code looks as if it was an attempt to see the port bit changes (in which case, 'port_b', wants to actually be the 'port', rather than a normal variable. As a second general comment _get rid of the delay in the interrupt handler_...
I'd suspect the code actuall wants to be:
Code:

#int_rb
void rb_isr(void) {
  // Initializing everything
  byte changes;
  static int last_b;
  int port_b;

  //Read the port - this resets the 'port changed' latch
  port_b=input_b();

  changes = last_b ^ port_b;
  last_b = port_b;

  // if wheel has went one complete revolution
  if (changes & 0x80) {
     //Here the revolution detector has _changed_. Check now it has fallen.
     if (!(port_b & 0x80)) event = 'r';
  }
  // if bob hit grain
  if (changes & 0x40) {
     if (!(port_b & 0x40)) event = 'g';
  }

  // if bob is at housing
  if (changes & 0x20) {
     if (!(port_b & 0x20)) event = 'h';
  }
}

As a seperate comment, if you need a huge delay like 100mSec, then program a hardware timer, and check the bits again when this has expired.
Also, look carefully at your 'event' codes. What happens if two edges trigger together?. You will 'lose' the first edge that you test for. Consider instead using an event 'mask'. So:

#define HOUSING (4)
#define GRAIN (2)
#define REV (1)

Then when you detect an event, _or_ the current event code, with the value for this event. In your main 'test', test for the event '&' the required code, and once handled, clear this code. This way the system can respond to more than one event.

Best Wishes
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