Showing posts with label Programming Tips. Show all posts
Showing posts with label Programming Tips. Show all posts

Monday, April 29, 2013

Control Coding Step Respon In Mathlab

It is tutorial how to making listing step Respon in Software Matlab..
1. Open Mathlab

2. File .
        M file
Inside code   exsample


m=2;
b=5;
k=3;
A=[0 1;-k/m -b/m];
B=[0;1/m];
C=[1 0];
D=0;
state_space=ss(A,B,C,D)
press F5 
a =
        x1    x2
  x1     0     1
  x2  -1.5  -2.5
b =
       u1
  x1    0
  x2  0.5
c =
      x1  x2
  y1   1   0
d =
      u1
  y1   0
>> [A,B,C,D]=ssdata(state_space)
A =
        0    1.0000
  -1.5000   -2.5000

B =
        0
   0.5000
C =
    1     0
D =
    0
>> step(state_space)
you get it

And find transfer function you can make it.

>> tutorial_tf=tf(state_space)
Transfer function:
      0.5
-----------------
s^2 + 2.5 s + 1.5

>> 
read more "Control Coding Step Respon In Mathlab"

Friday, July 20, 2012

Control PWM Configuration for PIC16F877 microntroller

PWM can be used to control a motor driver or generate sound samples.

The register specifically used to configure PWM are the TRISC, PR2, CCP1CON, CCP2CON, CCPR1L, CCPR2L and T2CON registers. There are two PWM channels on the PIC16F877. These modules are coined CCP1 et CCP2. These modules are implemented on pins 16 and 17 of the PIC16F877. There are two ways to connect these pins to a L293D H-Bridge. I recommend to connect these to the L293D Chip Enable pins. PWM then controls the percentage of time the chip is enabled. 

There are two parameters that affect the behavior of the PWM. The first one is the frequency of the pulses, the second is the length of these pulses. The length of the pulse is also called the duty cycle, e.g. the percentage of time the motor is enabled. In an ideal environment, if a motor normally exhibit 100 RPM, then it should do 25 RPM with a 25% duty cycle

It is important to experiment with these parameters to find the configuration that is right for your application because no two motors are the same. Some motors are better when the frequency is low (between 2 000-5 000 Hz). Other motors are best when the frequency is high, say between 15 000 and 20 000 Hz. Note that a low frequency (between 1 000 and 8 000 Hz) will produce an audible sound that can be very annoying. I have tested three robots with varying weight (500 g to 15 kg) and with different motors and have found that a frequency of 15 000 to 20 000 Hz gives good results. I have never seen any robot move with a duty cycle of less than 50%. If you want to do this, you have to find ways to control PWM in software, i.e. for a duty cycle of 25%, use a 50% duty cycle and turn PWM 50% of the time. Ideally, you will find motors that are not too fast at 50% and fast enough at 100%. 

The TRISC register must be initialized so that RC1 et RC2 are identified as output ports (bits 1 and 2 of TRISC must be set to 0). CCP1CON and CCP2CON are used to configure CCP1 and CCP2 modules mode. A value of 12 configures these modules for PWM mode.

Timer2 is used by CCP1 and CCP2 modules for PWM purposes. Timer2 prescaler is configured using bits TCKPS0 et TCKPS1 of T2CON register (bits 0 and 1). A value of 0 for these two bits configures a prescaler of 1. Bit TMR2ON (bit 2) must be initialized to 1 to start Timer2.PR2 register controls the pulse frequency. The formula used to calculate the pulse period is:(PR2 + 1) * 4 * TOSC * (Timer2 prescaler value).Pulse frequency is 1 / period. If PR2 is equal to 255 and the PIC runs at 20 MHz, pulse frequency is 19.53 kHz with a Timer2 prescaler of 1, i.e. 1 / ((255 + 1) * 4 * 1 / 20 000 000 * 1).

The CCPR1L et CCPR2L registers configure the "duty cycle". The "duty cycle" is relative to PR2. If PR2 is 100 and CCPR1 is 50, then the "duty cycle" is effectively 50% (half of PR2).
CCS provides macros to configure these registers appropriately. Here is an example program that shows how to configure PWM with CCS:

demopwm.c
read more "Control PWM Configuration for PIC16F877 microntroller"

Control Timers on PIC16F877

The timer service is one of the basic services offered by a microcontroller. Although CCS compiler provides some simple macros that implement delay routines, in order to determine time elapsed and to maximize use of MCU (see Simple Scheduler section), understanding of the timer functionality is necessary.
To introduce delays in an application, the CCS macro delay_ms and delay_us can be used. These macros provide an ability to block the MCU until the specified delay has elapsed.
For tasks that require the ability to measure time (for instance, how much time has elapsed since a given pin has transitioned to low), it is possible to write code that uses the microcontroller timers.
There are three timers on the PIC16F877: Timer0, Timer1 and Timer2 (there is also a watchdog timer, but it is not typically used to drive application based behavior).
Timer0, Timer1 and Timer2 are essentially counters that increment based on the clock cycle and the timer prescaler. An application can monitor these counters to determine how much time has elapsed. On PIC16F877, Timer0 is an 8-bit counter whereas Timer1 and Timer2 are 16-bit counters. Individual timer counters can be set to an arbitrary value using CCS macro set_timer0, set_timer1 or set_timer2. When the counter reaches its limit (255 for 8-bit and 65535 for 16-bit counters), it wraps around to 0. Interrupts can be generated when wrap around occurs. Timer2 is normally used for PWM or capture and compare functions.
Each timer can be configured with a different source (internal or external) and prescaler. The prescaler determines the timer granularity. A timer with a prescaler of 1 increments its counter every 4 clock cycles (1,000,000 times a second if using a 4 MHz clock). A timer with a prescaler of 8 increments its counter every 32 clock cycles.
Given that Timer1 is set up with a prescaler of 8 on a MCU clocked at 20 MHz. If Timer1 is reset to 0 and the application performs a certain task and if the value of Timer1 read at the end of the task is 6 250, the application can assume that a delay of 10 ms has taken place. To determine the delay, use the following formula:
delay (in ms) = (# ticks) * 4 * prescaler * 1000 / (clock frequency)
or in our example:
delay (in ms) = (# ticks) * 4 * 8 * 1000 / 20 000 000
or
delay (in ms) = (# ticks) / 625
This translates in our example to:
delay (in ms) = 6 250 / 625 = 10 ms
Here is some sample code that shows how to initialize and use the timer to determine the length of a task:
long delay;
/* Set Timer1 prescaler to 8. */
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
/* Reset Timer1 to 0. */
set_timer1(0);

/* Perform some task. */
printf("Hello, world!");

/* Calculate delay in ms. */
delay = get_timer1() / 625;

In the above example, it is important that the task performed does not exceed the maximum delay a timer can sustain before wrapping around, otherwise, the calculations may be incorrect.
Here is another code sample that shows how to create a delay of 50 ms before resuming execution (alternative to delay_ms):
/* Set Timer1 prescaler to 8. */
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
/* Reset Timer1 to 0. */
set_timer1(0);

for ( ; get_timer1() < 3125; ) ;

/* 50 ms has elapsed. */
read more "Control Timers on PIC16F877"