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 on CCP1 (RC1) on PIC18F4550
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
kidswizz



Joined: 21 Feb 2013
Posts: 16

View user's profile Send private message

PWM on CCP1 (RC1) on PIC18F4550
PostPosted: Fri Mar 01, 2013 4:52 pm     Reply with quote

Hi guys, I am trying to run 2 DC motors using a PIC18F4550 and an L298N H-Bridge driver. A few days ago, I was able to successfully run both motors at different speeds. However now none of the motors work. I checked CCP1 and CCP2 and it seems that CCP1 is not outputting anything but CCP2 outputs fine. I am wondering if this would be a problem with the PIC? Also, when I use PIN B3 instead for PWM, I get the outputs I want (12V on the + of both motors) however when I connect the motors they do not run. I have also posted my code below. Thanks.

Code:
#include <18F4550.h>
#use delay(clock=8000000)
#fuses NOWDT, INTRC_IO, NOPUT, NOPROTECT, NOLVP, NOMCLR, BROWNOUT


/*PWM Setup*/
void PWM_init()
{
   output_low(PIN_C1);   // Set CCP1 output low
   output_low(PIN_C2);   // Set CCP2 output low

   setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM
   setup_ccp2(CCP_PWM);  // Configure CCP2 as a PWM

   setup_timer_2(T2_DIV_BY_1,255,1);
}


/*Start of Main*/
void main(void)
{
   #use FAST_IO(A)
   #use FAST_IO(C)
   #use FAST_IO(D)

   SET_TRIS_A(0x01);
   SET_TRIS_C(0x00);
   SET_TRIS_D(0x00);

   PWM_init();

   while(1) //infinite loop
   {
               
         if (s0==1)
         {   
            set_pwm1_duty(255);                 
            set_pwm2_duty(128);
            
         }
         else
         {   
             set_pwm1_duty(128);         
            set_pwm2_duty(255);
         }
   }

}
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 01, 2013 5:24 pm     Reply with quote

couple of oddities..

1) usually #fuses is the 2nd line in the program ahead of #use delay...

maybe your version of compiler(??) is smarter than previous versions.

2) this line...

if (s0==1)

... hmm S0 is never referenced, again I have to wonder what the compiler makes of it.

3) depending on your wiring, you may have destroyed the CCP1 pin.
A simple 'toggle at 1Hz' program would confirm if it's OK

4) neither motor works ? Hmm..could be a blown driver chip, bad motor power supply, wiring.

5) I'd start by low level testing of the PIC and then reinstalling the known working program(from a few days ago), contiue from there. Hopefully you've got a log book of program vs. test results to get you back on track.

BTW you should always tell us the compiler version. Right now we don't know what it is or if you've 'upgraded' since the program last worked.

The more information you supply us, the faster the replies.

hth
jay
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Mar 01, 2013 5:30 pm     Reply with quote

adding to these comments, I think in your case the #USE FAST_IO lines should appear before main() - after the newly repositioned #use delay(...)
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
kidswizz



Joined: 21 Feb 2013
Posts: 16

View user's profile Send private message

PostPosted: Fri Mar 01, 2013 9:13 pm     Reply with quote

Thanks for the replies!

I am using compiler version 4.3.0.225.

I have changed s0 into PIN_A0 and also I have put #fuses after the device. Also the #use FAST_IO have been placed after #use delay.

I tried with CCP2 on RC2 but the motors would not run. I used #fuses CCP2B3 and used PORT B3 as CCP2 and this worked however only for one motor which was connected to CCP1.

I am extremely bad at keeping versions of code and a log book and I will take this experience as a learning point to keeping an updated logbook!

Also I was wondering how I could do a simple toggle 1kHz test on CCP2.

Again, thank you for your time.

Below is the updated code:

Code:


#include <18F4550.h>
#fuses NOWDT, INTRC_IO, NOPUT, NOPROTECT, NOLVP, NOMCLR, BROWNOUT
#use delay(clock=8000000)
#use FAST_IO(A)
#use FAST_IO(C)
#use FAST_IO(D)


/*PWM Setup*/
void PWM_init()
{
   output_low(PIN_C1);   // Set CCP1 output low
   output_low(PIN_C2);   // Set CCP2 output low

   setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM
   setup_ccp2(CCP_PWM);  // Configure CCP2 as a PWM

   setup_timer_2(T2_DIV_BY_1,255,1);
}


/*Start of Main*/
void main(void)
{
   SET_TRIS_A(0x01);
   SET_TRIS_C(0x00);
   SET_TRIS_D(0x00);

   PWM_init();

   while(1) //infinite loop
   {
               
         if (PIN_A0==1)
         {   
            set_pwm1_duty(255);                 
            set_pwm2_duty(128);
             
         }
         else
         {   
            set_pwm1_duty(128);         
            set_pwm2_duty(255);
         }
   }

}

Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 2:56 am     Reply with quote

Quote:
Also I was wondering how I could do a simple toggle 1kHz test on CCP2.

temtronic gave you the answer
Quote:
3) depending on your wiring, you may have destroyed the CCP1 pin.
A simple 'toggle at 1Hz' program would confirm if it's OK

For a crude 1kHz rather then 1Hz, simply change delay_ms(500) to delay_us(500).

Mike
kidswizz



Joined: 21 Feb 2013
Posts: 16

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 5:05 am     Reply with quote

Hello,

I have just created a simple program that switches 2 LEDs on and off..

Code:

output_high(PIN_C1);
output_high(PIN_C2);
delay_ms(1000);
output_high(PIN_C1);
output_low(PIN_C2);
delay_ms(1000);
output_low(PIN_C1);
output_high(PIN_C2);
delay_ms(1000);
output_low(PIN_C1);
output_low(PIN_C2);
delay_ms(1000);


C2 lights up however C1 does not. Am I right to assume that the RC1/CCP2 PIN is damaged?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 6:33 am     Reply with quote

Quote:
C1 does not. Am I right to assume that the RC1/CCP2 PIN is damaged?

Maybe not.

Check with the data sheet that there is not something else attached to RC1 which needs to be turned off.

(I don't have time to do it now. Due at the golf club in 20mins.)

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 6:49 am     Reply with quote

Yes.....
unless the LED is broken! Always confirm the LEDs are good, recheck the wiring as well just to be sure.
..but odds are the PIC is busted.

As for version keeping, I use a simple method.
1) create the first program(usually the 1Hz toggle).Get it running.Save project.
2) save a copy of the 1st source as 'program_name_v_2.c'
3) close the original program.
4) create a 2nd project 'program_name_v_2' and add 'project_name_v_2.c' as the main program.
5)edit this version(1st comment line is the program name ,compile ,test......save .
6)repeat steps 3...5, making small change to your code per program,v_3,v_4,etc.

yes, it means your harddrive may have 20,30,50 'versions' of your program but you'll have the ability to go back to previous version easily, seeing 'well it worked here....why not now'.
when you're happy with the 'final' program , you can delete all version except the 1st and last, IF you need storage space.

I also preface program names with the PIC type(ie:46k22_solar_v-8.c) as a reminder what PIC I'm using.Easy to locate on the harddrive.
As well, I have 'header' files for the 'fuses' and default defines for I/O pin usage.ie: '46k22_fuses.fuz', '46k22_solar_pins.pnz'. Benefits are 1) I have a KNOWN set of working fuses that can be imported into future projects. 2) source code is visually smaller,neater,easier to read. 3) Pin defines are neat,tidy, easy to read, simple to change.
Also, I have separate forlders for PIC types.Again, it's a 'free' housekeeping system.

Every programmer does thing a bit differently.For me, making things easy to read on the screen is important.colons and semicolons look very similar nowadays !

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 8:36 am     Reply with quote

Now, before you connect another PIC, consider if your circuit is likely to destroy the chip?.
What are you doing to handle motor generation, and flyback?.

You have two separate things that happen with electric motors. The first is that the motor has inertia. When you switch the drive off, the motor tends to keep spinning, and generate electricity. You have to worry where this is going to go. The second is that when you are driving an inductor (the coils of a motor are inductors), when you turn off the supply driving a coil, the magnetic energy has to go somewhere. If their isn't protection, The voltage will rise _until_ it finds a route to go. This is how tiny coils can be used to do things like charge flashgun capacitors up to several hundred volts. If their isn't protection, the odds are the voltage will rise till the driver transistor breaks down, normally between the drain and gate (if using FETs), and the current flows into the input driving the gate (the PIC....), and destroys the circuit driving this. This is why you need some form of circuit to trap this energy safely. Have you got such a circuit?.

Best Wishes
kidswizz



Joined: 21 Feb 2013
Posts: 16

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 10:57 am     Reply with quote

@Mike Walne

From what I have read from the datasheet nothing else needs to be turned off. Thanks for the advice.

@temtronic

I have checked the LED and it is indeed the PORT C1 that is faulty. I will just have to order a new one and see. Thank you very much for your advice, I will definitely take it on-board. It will make everything less complicated!

@Ttelmah

Hello, thank you for the explanation. I do have flyback diodes (1N4004) in my circuit. The circuit was working correctly before but something may have gradually damaged the chip. This is what I am not sure about. I have posted my circuit below (sorry you will have to follow the link, I couldn't get [img][/img] to work):

http://imageshack.us/photo/my-images/855/circuitc.jpg
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 12:12 pm     Reply with quote

Remember the diodes only work, if there is somewhere to dump the energy. Is the capacitor right of the diodes actually 1mF (1000uF), or smaller?. 1000uF ought to be OK, provided it has a good low ESR.

Best Wishes
kidswizz



Joined: 21 Feb 2013
Posts: 16

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 1:01 pm     Reply with quote

When I first built the circuit I did not use a 1mF capacitor and the motors were working correctly. Should it be able to work like this? Or will it work but damage components over time? Yesterday I added a 1mF capacitor and the motors were not working, probably because of the faulty uprocessor.

Many Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 1:34 pm     Reply with quote

It depends on what is 'off board'. Your 12v supply may have large reservoir capacitors, not as good as ones close to the clamp diodes, but possibly adequate. If there is not enough capacitance, the supply will rise _sharply_, and depending on how high this goes, damage is possible.

Best Wishes
kidswizz



Joined: 21 Feb 2013
Posts: 16

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 2:41 pm     Reply with quote

There is actually a 1mF capacitor already on the 12v power supply which possibly made the motors run without a 1mf capacitor on-board. When I receive a new ucontroller I will update everyone. Thanks for the help.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Mar 02, 2013 3:25 pm     Reply with quote

Whilst you're waiting for a new PIC, do some tests.

Build up a variable duty ratio oscillator using bog standard logic chips.
Feed the output to your motor drive circuit.
Have a VERY close look with a 'scope at all current and voltage waveforms.
Watch out for any spikes which could kill your your PIC.
Do things like changing the duty ratio fairly quickly.
In other words try to kill it.

Could save you lots of time and anguish.
It's all a lot simpler and more controllable than having the PIC in place.

Yes I know this is a CCS forum.
Once you've got the PIC installed, debugging becomes harder.
You need to KNOW the power part's OK first.

Mike
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