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

DC motor control with 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
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

DC motor control with PWM
PostPosted: Sun Nov 25, 2007 3:22 am     Reply with quote

Hi All,
I am newy registered to this forum and some questions and problems about the DC motor control with PWM.
I am supposed to implement a DC motor control with PWM by using speed up and slow down buttons.

So, this is my job.
As for my problem,
I could not understand why the following code lines does not affect each other.

Code:
setup_timer_2(T2_DIV_BY_16, 255, 1);   // Setup for 1465 Hz     
set_pwm1_duty(128);     



In addition, I also could not completely understand what the following code perform. I just know that they configure the analog to digital converter.
Code:
  setup_port_a(ALL_ANALOG);
  setup_adc(adc_clock_internal);
  set_adc_channel( 0 );

So, would you please help me on this by explaining what is the relationship between these two lines?

Thanks for your helps in advance.
Ttelmah
Guest







PostPosted: Sun Nov 25, 2007 4:02 am     Reply with quote

The timer setup, configures the actual frequency of the clock being fed to the PWM, and how large the count can be. The pwm setup, then says how many counts are to be used before the output changes. So:
Code:

setup_timer_2(T2_DIV_BY_16, 255, 1);   // Setup for 1465 Hz     
set_pwm1_duty(128);


Sets the timer to count in 'master clock/16' steps, and that it'll count from 0, to 255, then reset on the _next_ count.
The 'master clock', is the oscillator/4, so this will give 1465Hz, if the master oscillator is:

1465*256*16*4 = 24002560Hz (presumably actually 24MHz, giving 1464.84Hz).

Then the output will change state, after 128 of the master clock/16 steps, giving a 50:50 mark space ratio.

If the first line said:
Code:

setup_timer_2(T2_DIV_BY_4, 255, 1);


The output would still be 50:50 mark space ratio (unless the second line changed), but now at 5859.36Hz.

If the second line changed to:
Code:

set_pwm1_duty(64);     


The mark:space ratio, would become 25:75.

They set different things, but _both_ affect the resulting output.

The same is really true of the second set of lines.
The first says which pattern of input pins, are going to be setup to feed the analog multiplexer for the ADC. There are a number of pins that can be configured for use with this peripheral, and a number of allowed input patterns. The first line says to set _all_ the available pins for use with the ADC.

Then the second line says what clock the ADC is going to use. This selects the internal resistor/capacitor oscillator (not actually a good choice - this is recommended _against_, in most of the chip data sheets, on grounds of accuracy, unless the main chip is disabled during aquisition - this is because this oscillator is asynchronous to the processor, which can result in various amounts of noise being measured - OK, if the result is not terribly important, but not the way to go for accuracy....).

The third line, then says which of the available analog inputs, should actually be connected to the ADC, for reading.

Best Wishes
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 5:47 am     Reply with quote

Hi Sir,
Thanks for your reply and your clear clarifications. This really helped me.
Now, it seems that I understood some basic concepts about ADC and timers in PWM.
With regard to my understanding, I wrote a block of code which is supposed to take the user input(button-> speed up or slow down), and make the appropriate operation.
Would you please give some hints that I am going on the right way, or I have some logic problems in code ?

Code:
 // Taken back for a while
    }


Last edited by mcad on Sun Nov 25, 2007 10:30 am; edited 2 times in total
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 5:55 am     Reply with quote

In addition to my previous query,
I have no specified any port as an input or output. Do I have to do this ?
If yes, would you please tell me why ?


Thanks,
Regards
Ttelmah
Guest







PostPosted: Sun Nov 25, 2007 8:27 am     Reply with quote

No.
The compiler operates in three 'modes' regarding prgramming the I/O.
Fast_io, standard_io, and fixed_io mode.
In 'fast_io' mode, you have to set the port bit directions up, using a TRIS statement. In fixed_io mode, you instead use a list saying whether pins are inputs or outputs. In standard_io mode, the compiler will _automatically add the control instructions for you_. Standard_io, is the 'default', so you don't have to do anything. There is a slight cost in speed (typically one extra instruction, setting the TRIS, for every input/output instruction), but in terms of convenience, it wins!...

Best Wishes
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 8:41 am     Reply with quote

oh, thanks,sir. This made me being clarified again.
And, what would you suggest me about my previous question ?
As I mentioned before, I am just struggling to change the DC motor speed by means of the button pressed by the user. Do you think it is correct, sir ?

Thanks,

Regards
Ttelmah
Guest







PostPosted: Sun Nov 25, 2007 9:19 am     Reply with quote

There are some 'caveats' with it, which you will have to think about:
1) Remember that getc, simply sits and waits, if a character is not ready - kbhit, tests for a character being present. If you don't want the code to 'jam' at the getc, use kbhit to verify a character is waiting, before calling kbhit.
2) You should only get the character once. Once a character is fetched, it has gone from the input buffer. As it stands, you can only get to the second part of the routine, if the user presses 'a', then 'b'. You need to read the character, store it, and test on this value.
3) What happens if value is '1'?. You decrement by 2, so it'll 'wrap' to 255... You need to check that value>1, before decrementing it.
4) Why do you limit at 128?. 128, is the 'half power' output setting, The full power output, is 255.

Best Wishes
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 11:57 am     Reply with quote

Thank you sir,
I applied what you've suggested to me sofar.
Thanks again for your all help.

Regards
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 12:22 pm     Reply with quote

Sir,
I Just have a quick question to you.
I think that increasing and decrementing the value 1 times only is not enough when user press the speed up or slow down button.
But, I could not find any solution.
Would you suggest me something for this too ?
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 12:58 pm     Reply with quote

sorry sir,
A minute ago, I encountered one more difficulity too.
After writing the following code :
Code:
  setup_port_a(ALL_ANALOG);
  setup_adc(adc_clock_internal);
  set_adc_channel( 0 );



Then,
I am reading the digital value from the channel via the
Code:
read_adc()
method.
But, I wonder what the value variable can take from ADC channel at least and at most ?

Would you please explain it to me ?

thanks in advance,
regards
Ttelmah
Guest







PostPosted: Sun Nov 25, 2007 3:29 pm     Reply with quote

The default, is to return an 8bit ADC value.
However do a search in the manual, under #device. This allows you to specify that a 10bit value should be returned.
Remember though, that you will then need to use a long integer to hold this, and also, that if you use a long integer to drive the PWM, the maximum value the PWM can accept, will rise to 1023, from 255, and you would need to use 512, to get half power. A search here, will find details of how this works.

Best Wishes
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 3:56 pm     Reply with quote

First of all, thanks for your reply again.
But, I could not get what you mean about the long ttypes.
Would you be more clear for me, sir ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 4:06 pm     Reply with quote

Read the explanations in these two links:
http://www.ccsinfo.com/forum/viewtopic.php?t=18259&start=5
http://www.ccsinfo.com/forum/viewtopic.php?t=26751
mcad



Joined: 25 Nov 2007
Posts: 48

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 4:47 pm     Reply with quote

Sir, again thanks very much for your reply.
But, as for my previous question...
If I decrement or increment the duty cycle value 1 times when the user press speed up or slow down button, do you think that this create a reasonable affect on the DC motor speed ?
Or, should I use any other technique while changing (increasing / decreasing) the speed of the motor with regard to the user input ?

Thanks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 25, 2007 4:53 pm     Reply with quote

If the motor speed doesn't change enough, then increment it by 10
each time. Experiment and find the correct value. Also, do a limit
test, to make sure you don't increment the value beyond the maximum
allowed value. Also check that you don't go below 0.
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