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 support@ccsinfo.com

PIC18F2520 and PWM problem

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



Joined: 07 Mar 2008
Posts: 28

View user's profile Send private message

PIC18F2520 and PWM problem
PostPosted: Sat May 17, 2008 3:16 pm     Reply with quote

This is my source code:
Code:
#include <18F2520.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES H4                       //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT               //Reset when brownout detected
#FUSES BORV27                   //Brownout reset at 2.7V
#FUSES PUT                     //Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                    //Internal External Switch Over mode enabled
#FUSES FCMEN                   //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOLPT1OSC                //Timer1 configured for higher power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(clock=40000000)
#define I2C_SCL   PIN_C3
#define I2C_SDA   PIN_C4
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0x10)

#define HR1 PIN_A0
#define HL1 PIN_A1
#define LR1 PIN_A2
#define LL1 PIN_A3

#define HR2 PIN_B2
#define HL2 PIN_B3
#define LR2 PIN_B4
#define LL2 PIN_B5

#define BCKWRD 0
#define STOP   1
#define FRWRD  2
#define LEFT   3
#define RIGHT  4

struct Motor
{
   signed int16 line;
   signed int16 turn;
   signed int16 error;     
   signed int16 pwm;
   int status;
   unsigned int32 pos;
};

void Init_Motors(void);
void Motor_Control(int direct, int16 speed, int m);

int value_X = 140, value_Y = 140;
struct Motor M1;
struct Motor M2;

#int_EXT1
void  EXT1_isr(void)
{
   M1.pos++;;
}

#int_EXT2
void  EXT2_isr(void)
{
   M2.pos++;
}

#int_SSP
void  SSP_isr(void)
{
   ;;
}

void Init_Motors(void)
{
   output_bit(HR1, 0);
   output_bit(HL1, 0);
   output_bit(LL1, 1);
   output_bit(LR1, 1);
   M1.line = 0;
   M1.turn = 0;
   M1.error = 0;
   M1.pwm = 0;
   M1.status = STOP;
   M1.pos = 0;
   output_bit(HR2, 0);
   output_bit(HL2, 0);
   output_bit(LL2, 1);
   output_bit(LR2, 1);
   M2.line = 0;
   M2.turn = 0;
   M2.error = 0;
   M2.pwm = 0;
   M2.status = STOP;
   M2.pos = 0;
}

void Motor_Control(int direct, int16 speed, int m)
{
   switch(m)
   {
      case 1:
         M1.line = speed;
         M1.pwm = M1.line + M1.turn + M1.error;
         if(M1.pwm == 0) direct = STOP;        
         set_pwm2_duty(M1.pwm);
         switch(direct)
         {
            case BCKWRD:
               output_bit(HR1, 0);
               output_bit(LL1, 0);
               output_bit(HL1, 1);
               output_bit(LR1, 1);
               M1.status = BCKWRD;
               break;
            case FRWRD:
               output_bit(HL1, 0);
               output_bit(LR1, 0);
               output_bit(HR1, 1);
               output_bit(LL1, 1);
               M1.status = FRWRD;
               break;
            case STOP:   
               output_bit(HR1, 0);
               output_bit(HL1, 0);
               output_bit(LL1, 1);
               output_bit(LR1, 1);
               M1.status = STOP;
               break;
         }
           break; 
      case 2:
         M2.line = speed;
         M2.pwm = M2.line + M2.turn + M2.error;
         if(M2.pwm == 0) direct = STOP;          
         set_pwm1_duty(M2.pwm);
         switch(direct)
         {
            case BCKWRD:
               output_bit(HR2, 0);
               output_bit(LL2, 0);
               output_bit(HL2, 1);
               output_bit(LR2, 1);
               M2.status = BCKWRD;
               break;
            case FRWRD:
               output_bit(HL2, 0);
               output_bit(LR2, 0);
               output_bit(HR2, 1);
               output_bit(LL2, 1);
               M2.status = FRWRD;
               break;
            case STOP:   
               output_bit(HR2, 0);
               output_bit(HL2, 0);
               output_bit(LL2, 1);
               output_bit(LR2, 1);
               M2.status = STOP;
               break;
         }   
         break;
   }
}

void Init()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,124,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   set_pwm1_duty(512);
   set_pwm2_duty(512);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//   enable_interrupts(INT_EXT1);
//   enable_interrupts(INT_EXT2);
//   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
}

void main()
{

   Init();
   Init_Motors();
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   set_pwm1_duty(200);
   set_pwm2_duty(200);
   for(;;)
      delay_ms(100);
   
}


I don't see any error anywhere but it just won't generate any freaquency on PIN_C1 and PIN_C2.... any ideas ?
Thanks in advance Smile
Ttelmah
Guest







PostPosted: Sat May 17, 2008 3:31 pm     Reply with quote

Your problem in the selected pulse width for the PWM.
A search here, will find some long posts explaining this (I have posted explanations _several_ times), or read the manual carefully about the effect of variable _type_ on the set_pwm_duty functions.

Change the lines to:

set_pwm1_duty(200L);
set_pwm2_duty(200L);

and they will start working. You can then work out 'why' from the sources mentioned.

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 17, 2008 5:47 pm     Reply with quote

Here are some of your previous posts that contain the explanation
of 8 or 10 bit PWM.
http://www.ccsinfo.com/forum/viewtopic.php?t=31674
http://www.ccsinfo.com/forum/viewtopic.php?t=28761
http://www.ccsinfo.com/forum/viewtopic.php?t=24403
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