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

PWM 16F690, PIN_C5 issue, 5 wire stepping motor drive

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



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PWM 16F690, PIN_C5 issue, 5 wire stepping motor drive
PostPosted: Fri Mar 05, 2010 4:57 pm     Reply with quote

Compiler PCW 4.104 16F690

The code is supposed to generate drive signals for a 5 wire stepping motor and almost works.

The statements:
Code:

if(C==1)
{
   set_pwm1_duty(q);
   setup_ccp1(CCP_PWM_H_L | CCP_PULSE_STEERING_A | CCP_PULSE_STEERING_B);
     //A=C5 B=C4,  A rises as B falls
   output_bit( PIN_C3, 0);
   output_bit( PIN_C2, 1);
}

work as expected. However, the following code:
Code:

if(C==2)
{
   set_pwm1_duty(q);
   setup_ccp1(CCP_PWM_H_L | CCP_PULSE_STEERING_C | CCP_PULSE_STEERING_D);
      //C=C3 D=C2, C rises as D falls
   output_bit( PIN_C5, 1);
   output_bit( PIN_C4, 0);
}

(other two "if" statements)

works *almost* as expected, but the motor has a tick sound.
PIN_C5 is mostly high for this phase of the motor drive, but has a series of very short unexpected zero pulses.
The number of zero pulses is equal to the period count used to generate the PWM ramps.
The other 3 PWM pins respond correctly.
The PIN_C5 condition is not changed by disconnecting the motor drive circuit.

The statement:
Code:

output_bit( PIN_C5, 1);

was replaced with assembly code.
No change in PIN_C5 response(!)

Does anyone know a way around this?
Thanks,
Boyce
_________________
boyceg1@gmail.com
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 05, 2010 5:43 pm     Reply with quote

You didn't provide any theory. Why are you setting bits C2-C5 ?
What are the connections to the stepper motor ? Post a schematic
of your project. Post a link to the data sheet for the stepper motor.
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Fri Mar 05, 2010 7:34 pm     Reply with quote

Thanks for your response.

Think 5 or 6 lead UNIPOLAR motors...

4 PHASES:
A (A), NOT A (NA), B (B), and NOT B (NB)

PWM+ = RISING PULSE PWM

PWM- = FALLING PULSE PWM

1* TROUBLE, REASON FOR FORUM ENTRY...

PERIOD 1 2 3 4 1...

PHASE A PIN_C5 PWM+ 1* PWM- 0 PWM+

PHASE B PIN_C3 0 PWM+ 1 PWM- 0

PHASE NA PIN_C4 PWM- 0 PWM+ 1 PWM-

PHASE NB PIN_C2 1 PWM- 0 PWM+ 1

Here is a site showing UNIPOLAR stepping motors.
http://www.wimb.net/index.php?s=motion&page=52

Standard microstep drive for a 5 lead stepper motor.
5 lead motors have 4 windings with one end of each
winding connected to a common. Another way to look
at it is as a 6 lead motor with the winding center
taps connected. Very simple to drive. No H bridges
or high side drivers.

The common winding connection is typically connected
to V+. Each winding is connected to its own pull down
transistor and a flyback diode.

There is no data sheet. I used an oscilloscope to
determine what windings are A, NA, B, NB. A is
shifted 180 degrees from NA (no surprise). Same for
B, NB.

The system I have is really quiet,
except for the 1* pulse and delivers great torque.
That said, it is not a sin/cosin drive, so there is
some jitter in the rotation speed. This is triangle wave
drive. The duty cycle goes from 0 to the PWM period
in up to 255 steps during PWM+ and PWM- generation.
To control speed, a delay is inserted between the
increments to the PWM duty cycle. The motor hardly
heats at all.

Again, why does PIN_C5 oscillate during period 2? I
assume there is some stealth interaction between
PIN_C5 and the PWM control system.

This type control is the most exciting thing I
have done with motors in the 60 years I have worked
with them.
Thanks,
Boyce
_________________
boyceg1@gmail.com
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Sat Mar 06, 2010 11:35 pm     Reply with quote

PCM programmer wrote:
You didn't provide any theory. Why are you setting bits C2-C5 ?
What are the connections to the stepper motor ? Post a schematic
of your project. Post a link to the data sheet for the stepper motor.


Perhaps the whole main.c will help:

Code:

#include "G:\MICROCHIP-ALL\DEVELOPMENT PROGRAMS\STEP_MOTOR_PWM_5_LEAD_TYPE_2010-03-03\Prog_2010-03-06_main.h"

/*********************************************************************
COMPILER PCW 4.104  DEVICE 16F690
THIS PROGRAM MICROSTEPS A 5 LEAD STEPPING MOTOR.  THE NUMBER OF MICROSTEPS
BETWEEN STEPS AND THE DELAY PERIOD CHANGE THE MOTOR DRIVE SPEED.
#define period nnn  //1-255 to define number of microsteps
Delay=nnn; //to control time between each microstep
Delay can be changed from mS to uS
**********************************************************************/

#define period 20 //0-255

int C, q;
int16 Delay=10;//Delay currently in uS...

#byte PORTC = 0x07
#bit PORTC0 = PORTC.0 //OSCILLOSCOPE MARKER
#bit PORTC1 = PORTC.1 //NOT USED
#bit PORTC2 = PORTC.2
#bit PORTC3 = PORTC.3
#bit PORTC4 = PORTC.4
#bit PORTC5 = PORTC.5
#bit PORTC6 = PORTC.6 //NOT USED
#bit PORTC7 = PORTC.7 //NOT USED
 
void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_4, period, 16);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   //Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!

   OUTPUT_C(0x00);
   C=0;
   q=0; //q IS THE DUTY CYCLE COUNTER

   for(;;)
   {
      if(q>=(period))
      {
         q=0; //q IS THE DUTY CYCLE COUNTER
         C++; //C IS THE STEPPER MOTOR STATE COUNTER
      }
      if(C>=5) C=1;
     
      delay_us(Delay);
     
      //P1A=C5   P1B=C4    P1C=C3    P1D=C2 

      if(C==1)
      {  PORTC0 = 1; //oscilloscope marker
         set_pwm1_duty(q);
         setup_ccp1(CCP_PWM_H_L | CCP_PULSE_STEERING_A | CCP_PULSE_STEERING_B); //A=C5 B=C4
           //A rises as B falls
         PORTC3 = 0;
         PORTC2 = 1;
      }//end if(C=1)                                                         
 

      if(C==2)
      {  PORTC0 = 0; //oscilloscope marker
         set_pwm1_duty(q);
         setup_ccp1(CCP_PWM_H_L | CCP_PULSE_STEERING_C | CCP_PULSE_STEERING_D);
           //C=C3 D=C2, C rises as D falls
         PORTC4 = 0;
         PORTC5 = 1;  //<<<=====PROBLEM: VERY SHORT ZERO PULSES DURING THIS
                      //PIN_C5 = 1 PERIOD.  NUMBER OF ZERO PULSES EQUAL TO
                      //THE period COUNT GIVEN IN LINE 13:
                      //#define period 20 //0-255  .
                      //WITH A DELAY OF 10uS AND A PERIOD OF 20, THE "1"
                      //PULSES ARE HIGH FOR ABOUT 100uS AND LOW FOR
                      //ABOUT 15uS. 
                      //USING ASM LANGUAGE DID NOT HELP.
                      //ALL THE OTHER PULSES ARE CORRECT.
                      //HOW IS THIS PIN_C5 CONDITION CORRECTED?
                      //THANKS, BOYCE

// asm did not cure PIN_C5 = 1 problem       
//#asm
//  bsf 0x03,0x05      //status register; changing banks
//  bcf 0x87,5         //trisC register; setting pin as OP
//  bcf 0x03,0x05      //status register; changing banks
//  bsf PORTC, 5   
//#endasm       
       
      }//end if(C=1)


      if(C==3)
      {  PORTC0 = 0; //oscilloscope marker
         set_pwm1_duty(q);
         setup_ccp1(CCP_PWM_L_H | CCP_PULSE_STEERING_A | CCP_PULSE_STEERING_B); //A=C5 B=C4
           //A falls as B rises
         PORTC3 = 1;
         PORTC2 = 0;
      }//end if(C=3)

 
      if(C==4)
      {  PORTC0 = 0; //oscilloscope marker
         set_pwm1_duty(q);
         setup_ccp1(CCP_PWM_L_H | CCP_PULSE_STEERING_C | CCP_PULSE_STEERING_D);//C=C3 D=C2   

             //C falls as D rises
         PORTC5 = 0;
         PORTC4 = 1;
      }//end if(C=4)

      q++;

   }//end for
}//end main

_________________
boyceg1@gmail.com
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 06, 2010 11:45 pm     Reply with quote

Post the main.h file, so the program can be compiled.
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Sat Mar 06, 2010 11:59 pm     Reply with quote

PCM programmer wrote:
Post the main.h file, so the program can be compiled.


Code:
#include <16F690.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOCPD                    //No EE protection
#FUSES NOPUT                    //No Power Up Timer
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


_________________
boyceg1@gmail.com
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

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

PostPosted: Sun Mar 07, 2010 7:05 am     Reply with quote

Boyce wrote:
//WITH A DELAY OF 10uS AND A PERIOD OF 20, THE "1"
//PULSES ARE HIGH FOR ABOUT 100uS AND LOW FOR
//ABOUT 15uS.
Could this be a back EMF problem? Maybe a brownout reset problem? I don't know. Try disconnecting the stepper from the PIC. Run the PIC and see if you still have the problem with PORTC.5.

Rohit
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Sun Mar 07, 2010 10:02 am     Reply with quote

[quote="Rohit de Sa"]
Boyce wrote:
Try disconnecting the stepper from the PIC. Run the PIC and see if you still have the problem with PORTC.5.
Rohit


The PORTC.5 problem exists when the stepper driver is disconnected.

It also does not care if I have been drinking or not.

May be of interests that the driver is 4 2N3904 with 1K base resistors connected to the PIC outputs. Each transistor has a flyback diode. The diode positive ends are connected together and to a 200 ohm resistor to positive. Call it a $1 drive. Very quiet drive with lots of torque, low heating...and a tick sound for PORTC.5... The system has a research application here in Oak Ridge, TN. The boys with lots of expensive toys are not doing as well as I am because of lack of control. They will point at the PORTC.5 issue and try to use it to kill my idea or at least have fun at my expense just as I do to them.

I read about this problem some time ago, but forget where or the details.
Boyce
_________________
boyceg1@gmail.com
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 07, 2010 12:59 pm     Reply with quote

Can you clarify exactly what is wrong with the waveform ?
I installed vs. 4.104. I programmed your posted code into the 16F690
on my breadboard. I'm looking at pins C5 (pwm signal) and C0 (marker)
on the oscilloscope and on a logic analyzer.

The logic analyzer is set to a 1 MHz sample rate. I don't ever see what
you mention in your post. Here is what I see:

Four roughly equal time periods for the PWM signal:

1. constant low level

2. A "ramp up" interval, where the PWM has a short duty cycle, say 10%,
and slowly increases to about 90% (I didn't measure the exact amounts).

3. A constant 90% duty cycle interval.

4. A "ramp down" interval, in which the duty cycle slowly goes down
from 90% to 10%.

Then it starts over at state 1, and does these 4 states continuously.
Your C0 "Marker" pulse occurs during entire the 2nd part of the interval
(item 2, above).

I have triggered the logic analyzer on the positive edge of pin C0
(marker) with a 1 MHz sample rate. I set the L.A. for continuous
trigger mode. I don't see what you're seeing. Occasionally, I will
see a longer single pulse at the beginning of the PWM waveform,
just after the leading edge of pin C0. I don't see all these random
strange pulses that you're getting.

Here's what I see (in ASCII art). Occasionally there is a fat pulse
on the PWM signal, at the beginning of the marker pulse:
Code:

         _____     _    _    __    ___
C5       |   |     |    |    ||    | |
     ----     ----- ---- ----  ----   ----- 

        -----------------------------------
C0      |
     ---

I don't have any external circuits connected to the PWM output pins.
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Sun Mar 07, 2010 1:33 pm     Reply with quote

PCM programmer wrote:
Can you clarify exactly what is wrong with the waveform ?
I installed vs. 4.104. I programmed your posted code into the 16F690
on my breadboard. I'm looking at pins C5 (pwm signal) and C0 (marker)
on the oscilloscope and on a logic analyzer.

I don't have any external circuits connected to the PWM output pins.


Hey, thanks for all the work and detail!

I am running a PICKit 2 connected to a 20 year old oscilloscope...

I don't have anything connected to my PWM pins right now, either.

I have:
#define period 20
Delay=10 //uS

Ok, when pin C5 is supposed to be = 1:
The signal is high for 100uS and low for 15uS and is repeated 20 times.

---_--------------_---------------_--------...

There are 20 or these "---_---"

As you noticed, there are other pulses that are not exactly as they should be. The plan is to set the system up to run on interrupts to clear up those. Come to think of it, interrupts might change things, perhaps for the worse.

Again, thanks for your work.
Boyce[/code]
_________________
boyceg1@gmail.com
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 07, 2010 1:52 pm     Reply with quote

Here's the logic analyzer screenshot of what I see. There is an occasional
long pulse at the start of the "ramp up" section. I don't ever see the
pulses that you see.
http://img502.imageshack.us/img502/2374/ccptest.jpg
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Sun Mar 07, 2010 2:09 pm     Reply with quote

PCM programmer wrote:
Here's the logic analyzer screenshot of what I see. There is an occasional
long pulse at the start of the "ramp up" section. I don't ever see the
pulses that you see.
http://img502.imageshack.us/img502/2374/ccptest.jpg


I see the occasional long pulse, too. As for the C5 not being really =1, I am sitting here, cold sober, looking at my scope.

You have done more than enough work on this. Thanks. I will pass the problem around and see what comes up. I will also breadboard the bare chip and see what happens.

If I get any more information, I will let you know.

Thanks, again.
Boyce
_________________
boyceg1@gmail.com
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Mon Mar 08, 2010 10:11 pm     Reply with quote

PCM programmer wrote:
Here's the logic analyzer screenshot of what I see. There is an occasional
long pulse at the start of the "ramp up" section. I don't ever see the
pulses that you see.
http://img502.imageshack.us/img502/2374/ccptest.jpg


I got a response back from CCS support that got through my thick head. Turns out that CCP1 shares PIN_C5 and pulls it to zero during part of PWM operation. There may be smarter software fixes. I quit trying to make C5=1 and turned that over to C6 which was not being used. Then took a couple of diodes and wired an OR with C5 and C6. Works. C5 does the ramp up and ramp down and C6 holds when 1 is needed. Helps a lot to remember to turn C6 off when it is not needed to be 1... Drive is pretty quiet. Has forward and reverse, now. Could play with it forever.

Again, thanks for your help.
Boyce
_________________
boyceg1@gmail.com
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Tue Mar 09, 2010 9:47 am     Reply with quote

Hello

Maybe try to switch off the pwm module. It work for me in the same situation. The macro you get for free:-)

Code:

#define Pwm1(x) output_bit(PIN_C5,x)

setup_ccp1(CCP_OFF);
Pwm1(1);
Boyce



Joined: 07 Feb 2010
Posts: 39

View user's profile Send private message

PostPosted: Tue Mar 09, 2010 10:01 am     Reply with quote

hmmpic wrote:
Hello

Maybe try to switch off the pwm module. It work for me in the same situation. The macro you get for free:-)

Code:

#define Pwm1(x) output_bit(PIN_C5,x)

setup_ccp1(CCP_OFF);
Pwm1(1);


Hey, thanks for the info. There is a concern. The PWM is in use continuously, but in various ways with each of the four C states. If the PWM is switched off, I do not expect it to keep running...
True?
Thanks,
Boyce
_________________
boyceg1@gmail.com
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