Interface and Control a DC Motor

Interface and Control a DC Motor

THEORY:

The DC Motor can also be interfaced to the board by connecting it to the Reliamate RM4. The direction of the rotation can be changed through software.
RM4 – it's a 2 pin straight male reliamate. COM pins of relay RLY1 are terminated here. It is
used to run and direction control of DC motor.



Program:


* PWM 1.5 is used. Match register 0 is used for count purpose. PWM 1
 * match register 5 is increamented or decreamented at each interrupted.
 * Port line P1.24.
***********************************************************************/

#include <lpc17xx.h>

void pwm_init(void);
void PWM1_IRQHandler(void);

unsigned long int i,j;
unsigned char flag,flag1,flag2;

int main(void)
{  
            SystemInit();
            SystemCoreClockUpdate();  
            pwm_init();

            while(1)
            {
                        for(i=0;i<=1000;i++);                          // delay
           
    }//end of while
}//end of main

void pwm_init(void)
{
            LPC_SC->PCONP |= (1<<6);                                    //PWM1 is powered
            LPC_PINCON->PINSEL3 |= 0x00020000;  //pwm1.5 is selected for the pin P1.24
             
            LPC_PWM1->PR  = 0x00000000;                //Count frequency : Fpclk
            LPC_PWM1->PCR = 0x0002000;                 //select PWM5 single edge and PWM5 output is enabled
            LPC_PWM1->MCR = 0x00000003;             //Reset and interrupt on PWMMR0
            LPC_PWM1->MR0 = 30000;           //setup match register 0 count
            LPC_PWM1->MR5 = 0x00000100;              //setup match register MR5
            LPC_PWM1->LER = 0x000000FF;              //enable shadow copy register
            LPC_PWM1->TCR = 0x00000002;               //RESET COUNTER AND PRESCALER
            LPC_PWM1->TCR = 0x00000009;               //enable PWM and counter

            NVIC_EnableIRQ(PWM1_IRQn);
            return;
}

void PWM1_IRQHandler(void)
{
            LPC_PWM1->IR = 0xff;                                           //clear the interrupts
           
            if(flag == 0x00)
    {
                       
                        LPC_PWM1->MR5 += 100;
                        LPC_PWM1->LER = 0x000000FF;

           
                                                            if(LPC_PWM1->MR5 >= 37000)
                                                                        {
                                                            flag1 = 0xff;
                                                            flag = 0xff;
                                                            LPC_PWM1->LER = 0x000000fF;
                                                                        }         
              //  for(j=0;j<1;j++)
             for(i=0;i<8000;i++);
            }

    else if(flag1 == 0xff)
    {
           
                        LPC_PWM1->MR5 -= 100;
                        LPC_PWM1->LER = 0x000000fF;
   
                        if(LPC_PWM1->MR5 <= 0x500)
                        {
                                    flag  = 0x00;
                                    flag1 = 0x00;
                                    LPC_PWM1->LER = 0X000000fF;
                        }
                        // for(j=0;j<1;j++)
             for(i=0;i<8000;i++);
            }
}







TEST DC Motor:

Download the file “dcm.hex” to trainer using Flash Magic 6.01 version.
Connect the Female Reliamate of the DC motor to the male Reliamate RM4 present on the
board.
Short JP21/2&3

Result:
Observe the rotation of the dc motor (Controlled by On chip PWM) Varies from slow speed to higher speed and Vice Versa. This rotation of the motor will be continuously in loop.

Comments

Popular posts from this blog

7-segment LED interface

Interface a Stepper motor