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

16F690 pulse steering, 4 pin PWM
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 19, 2007 3:42 pm     Reply with quote

The closest I have to your version is PCM vs. 4.014. I was able to
make it work with that version. See the test program shown below.
The key point is that you have to put in code to make the additional PWM
pins into output pins. The compiler doesn't do it for you automatically.
This is shown in the code.
Code:

#include <16F690.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT, NOMCLR
#use delay(clock=8000000)

#define P1A PIN_C5
#define P1B PIN_C4
#define P1C PIN_C3
#define P1D PIN_C2

//===========================================
void main()
{
setup_timer_2(T2_DIV_BY_16, 124, 1);  // 1000 Hz

set_pwm1_duty(31);  // 25% duty cycle

// Enable PWM pins P1B, P1C, and P1D for output.
// Note:  P1A is done by the compiler.
output_low(P1B);
output_low(P1C);
output_low(P1D);

// Steer the PWM output to all four P1x pins.
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A
                       | CCP_PULSE_STEERING_B
                       | CCP_PULSE_STEERING_C
                       | CCP_PULSE_STEERING_D );


while(1);
}
ymoona



Joined: 20 Mar 2005
Posts: 32
Location: Heiloo, the Netherlands

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

PostPosted: Sun Jun 24, 2007 12:14 pm     Reply with quote

PCM programmer wrote:
The closest I have to your version is PCM vs. 4.014. I was able to
make it work with that version. See the test program shown below.
The key point is that you have to put in code to make the additional PWM
pins into output pins. The compiler doesn't do it for you automatically.
This is shown in the code.
Code:

#include <16F690.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT, NOMCLR
#use delay(clock=8000000)

#define P1A PIN_C5
#define P1B PIN_C4
#define P1C PIN_C3
#define P1D PIN_C2

//===========================================
void main()
{
setup_timer_2(T2_DIV_BY_16, 124, 1);  // 1000 Hz

set_pwm1_duty(31);  // 25% duty cycle

// Enable PWM pins P1B, P1C, and P1D for output.
// Note:  P1A is done by the compiler.
output_low(P1B);
output_low(P1C);
output_low(P1D);

// Steer the PWM output to all four P1x pins.
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A
                       | CCP_PULSE_STEERING_B
                       | CCP_PULSE_STEERING_C
                       | CCP_PULSE_STEERING_D );


while(1);
}


thanks!! that did the trick. But is seems to have a problem with the while loop Ive wrote.

Code:

  while(1){
      setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A );
      set_pwm1_duty(31);  // 25% duty cycle
      output_high( led );
      delay_ms(2000);

      // left wheels backward, right wheels not moving
      setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_B );
      set_pwm1_duty(31);  // 25% duty cycle
      output_low( led );
      delay_ms(2000);
   }


The problem is that the first time this loop is passed. Every thing is ok. but the second time the left wheels only steer backwards, changing from 100% to 25% duty.
_________________
checkout my site: www.ymoona.com/wiki
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 24, 2007 12:28 pm     Reply with quote

Post a test program similiar to the one that I did, except that it has your
loop code in it. Make sure the program is compilable. For example,
the LED pin must be defined.

Also, you have the P1A, P1B, etc. outputs attached to wheel motors.
Post a list of which output pin drives which motor. i.e., P1A is the left
motor, etc. In other words, post a full explanation.
ymoona



Joined: 20 Mar 2005
Posts: 32
Location: Heiloo, the Netherlands

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

PostPosted: Sun Jun 24, 2007 1:34 pm     Reply with quote

this is the code I currently use:
Code:

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

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#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

#use delay(clock=8000000)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=0x00110011)
/*
Pin defines
*/
#define led      PIN_A5
#define driver_a PIN_C0
#define driver_b PIN_C1

/*
Variables
*/

/*
Function prototypes
*/
void set_drivers( int1 value );

#int_SSP // i2c activty
SSP_isr()
{
   return;
}



void main(){
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   //setup_timer_2(T2_DIV_BY_1,255,1);
   setup_timer_2(T2_DIV_BY_16, 124, 1);  // 1000 Hz
   setup_comparator(NC_NC_NC_NC);
   //enable_interrupts(INT_SSP);
   //enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ);

   set_drivers( true );

   set_pwm1_duty(31);  // 25% duty cycle

   output_low(PIN_C5);
   output_low(PIN_C4);
   output_low(PIN_C3);
   output_low(PIN_C2);

    output_high( led );
    delay_ms(500);
    output_low( led );
    delay_ms(500);
    output_high( led );
    delay_ms(500);
    output_low( led );
    delay_ms(500);

   while(1){
      // left wheel foreward, right wheels not moving
      setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A );
      set_pwm1_duty(31);  // 25% duty cycle
      output_high( led );
      delay_ms(1000);

      // left wheels backward, right wheels not moving
      setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_B );
      set_pwm1_duty(31);  // 25% duty cycle
      output_low( led );
      delay_ms(1000);
   }
}

/*
Funtion boddies
*/

void set_drivers( values ){
   if( values ){
      output_high( driver_a );
      output_high( driver_b );
   }
   else{
      output_low( driver_a );
      output_low( driver_b );
   }
}


for hardware I use a l298(a dual H-bridge)
l298 input1 -> RC2 D
l298 input2 -> RC3 C
l298 input3 -> RC4 B
l298 input4 -> RC5 A

the driver enable pins are also connected.

note: when I drive de motors without PWM the dual H-Bridge setup works just fine. Tested with the following code:
Code:

 while(1){
      output_high( PIN_A5 );
      // drive motor A
      output_low( PIN_C3 );
      output_high( PIN_C2 );

      // drive motor B
      output_low( PIN_C4 );
      output_high( PIN_C5 );

      delay_ms(5000);
      output_low( PIN_A5 );
      // driver motor A
      output_low( PIN_C2 );
      output_high( PIN_C3 );

      // driver motor Ab      output_low( PIN_C5 );
      output_high( PIN_C4 );

      delay_ms(5000);


_________________
checkout my site: www.ymoona.com/wiki
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 24, 2007 1:43 pm     Reply with quote

Quote:

#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=0x00110011)


output_low(PIN_C5);
output_low(PIN_C4);
output_low(PIN_C3);
output_low(PIN_C2);

You're using software i2c on the same pins that you're using for your motor driver.
The 16F690 does have hardware i2c pins, but they're on pins B4 and B6,
not C4 and C3.
ymoona



Joined: 20 Mar 2005
Posts: 32
Location: Heiloo, the Netherlands

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

PostPosted: Sun Jun 24, 2007 1:55 pm     Reply with quote

PCM programmer wrote:
Quote:

#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=0x00110011)


output_low(PIN_C5);
output_low(PIN_C4);
output_low(PIN_C3);
output_low(PIN_C2);

You're using software i2c on the same pins that you're using for your motor driver.
The 16F690 does have hardware i2c pins, but they're on pins B4 and B6,
not C4 and C3.


thanks! stupid me! but it not the solution for my problem :(
any other ideas? this is the current software
Code:

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

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#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

#use delay(clock=8000000)
/*
Pin defines
*/
#define led      PIN_A5
#define driver_a PIN_C0
#define driver_b PIN_C1

/*
Variables
*/

/*
Function prototypes
*/
void set_drivers( int1 value );

#int_SSP // i2c activty
SSP_isr()
{
   return;
}



void main(){
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   //setup_timer_2(T2_DIV_BY_1,255,1);
   setup_timer_2(T2_DIV_BY_16, 124, 1);  // 1000 Hz
   setup_comparator(NC_NC_NC_NC);
   //enable_interrupts(INT_SSP);
   //enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ);

   set_drivers( true );

   set_pwm1_duty(31);  // 25% duty cycle

   output_low(PIN_C5);
   output_low(PIN_C4);
   output_low(PIN_C3);
   output_low(PIN_C2);

    output_high( led );
    delay_ms(500);
    output_low( led );
    delay_ms(500);
    output_high( led );
    delay_ms(500);
    output_low( led );
    delay_ms(500);

   while(1){
      // left wheel foreward, right wheels not moving
      setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A );
      set_pwm1_duty(31);  // 25% duty cycle
      output_high( led );
      delay_ms(1000);

      // left wheels backward, right wheels not moving
      setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_B );
      set_pwm1_duty(31);  // 25% duty cycle
      output_low( led );
      delay_ms(1000);
   }
}

/*
Funtion boddies
*/

void set_drivers( values ){
   if( values ){
      output_high( driver_a );
      output_high( driver_b );
   }
   else{
      output_low( driver_a );
      output_low( driver_b );
   }
}


_________________
checkout my site: www.ymoona.com/wiki
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 24, 2007 2:43 pm     Reply with quote

I was able to fix it by adding the line shown in bold below.

Also, it's best to define a symbolic name for the PWM pins.
If you use "PIN_C4", etc., as the pin name in your code, it doesn't
really tell you anything. You have to remember that it's the P1B
pin of the PWM module. You could also make a mistake and type
in the wrong pin. But if you see "P1B" as the pin, you know its the
"B" output of the PWM module. "PIN_C4" is basically a "magic number"
when you use it in your code. In C, the concept of "magic number"
means a number that has a special meaning, but it's known only to
the original programmer, and he might forget it someday. Then it
bites him (as well as some future maintenance programmer).


Add the line shown in bold below:
Quote:

#define P1A PIN_C5
#define P1B PIN_C4
#define P1C PIN_C3
#define P1D PIN_C2


while(1){
// left wheel forward, right wheels not moving
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A );
output_low(P1B);
set_pwm1_duty(31); // 25% duty cycle
output_high( led );
delay_ms(1000);

// left wheels backward, right wheels not moving
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_B );
set_pwm1_duty(31); // 25% duty cycle
output_low( led );
delay_ms(1000);
}
ymoona



Joined: 20 Mar 2005
Posts: 32
Location: Heiloo, the Netherlands

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

PostPosted: Mon Jun 25, 2007 9:50 am     Reply with quote

PCM programmer wrote:
I was able to fix it by adding the line shown in bold below.

Also, it's best to define a symbolic name for the PWM pins.
If you use "PIN_C4", etc., as the pin name in your code, it doesn't
really tell you anything. You have to remember that it's the P1B
pin of the PWM module. You could also make a mistake and type
in the wrong pin. But if you see "P1B" as the pin, you know its the
"B" output of the PWM module. "PIN_C4" is basically a "magic number"
when you use it in your code. In C, the concept of "magic number"
means a number that has a special meaning, but it's known only to
the original programmer, and he might forget it someday. Then it
bites him (as well as some future maintenance programmer).


Add the line shown in bold below:
Quote:

#define P1A PIN_C5
#define P1B PIN_C4
#define P1C PIN_C3
#define P1D PIN_C2


while(1){
// left wheel forward, right wheels not moving
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A );
output_low(P1B);
set_pwm1_duty(31); // 25% duty cycle
output_high( led );
delay_ms(1000);

// left wheels backward, right wheels not moving
setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_B );
set_pwm1_duty(31); // 25% duty cycle
output_low( led );
delay_ms(1000);
}


thanks now it work well!
_________________
checkout my site: www.ymoona.com/wiki
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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