Saturday, December 16, 2017

7. Generate PWM

PartB
7.Using the Internal PWM module of ARM controller generate PWM and vary its duty cycle.
Theory:



















Explanation:

*********************************************************************************
//program to fade an LED or to demonstrate PWM on CRO

#include <lpc17xx.h>

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

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

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

 while(1)
    {
       for(i=0;i<=1000;i++); // delay

    }       
}                            //end of main

void pwm_init(void)
{
 LPC_SC->PCONP |= (1<<6); //PWM1 is powered
 LPC_PINCON->PINSEL7 |= 0x000c00000; //pwm1.2 is selected for the pin P3.25

 LPC_PWM1->PR  = 0x00000000;       //Count frequency : Fpclk
 LPC_PWM1->PCR = 0x00000400;        //select PWM5 single edge and PWM5 output is enabled
 LPC_PWM1->MCR = 0x00000003;       //Reset and interrupt on PWMMR0
 LPC_PWM1->MR0 = 0X00007530;      //setup match register 0[MR0] count to cycle time
 LPC_PWM1->MR2 = 0x00000064;       //setup match register 2[MR2] coount to duty cycle
 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)  // to increase the Ton period by 64 for every iteration till MR5 is less than 6978
    {

LPC_PWM1->MR2 += 0X00000064;//incrementation of MR2 count value by 64 for every iteration

LPC_PWM1->LER = 0x000000FF;


if(LPC_PWM1->MR2 >= 0X00004E20)
{
flag1 = 0xff;
flag = 0xff;
LPC_PWM1->LER = 0x000000FF;
}
for(i=0;i<8000;i++);
    }

else if(flag1 == 0xff) // to increase the Toff period or to decrease Ton period by 64 for every iteration till MR2 is greater than BB
    {

LPC_PWM1->MR2 -= 0X00000064; //decrementation of MR2 count value by 64 for every iteration
LPC_PWM1->LER = 0x000000FF;

  if(LPC_PWM1->MR2 <= 0x00000500)
{
flag  = 0x00;
flag1 = 0x00;
LPC_PWM1->LER = 0X000000FF;
}
  for(i=0;i<8000;i++);
    }
}


Download:PWM waves using internal PWM of ARM controller

No comments:

Post a Comment

lab record

TECHNICAL SPECIFICATIONS of LPC1768 (15-16 of ARMCTXM3) ******************************************************************************...