 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		|         
#include "lpc17xx_gpio.h"
#include "system_LPC17xx.h"
#include "core_cm3.h"
#include "lpc17xx_pwm.h"
#define FORWARD   0x24
#define BACKWARD  0x12
#define LEFT      0x1
#define RIGHT     0x8
uint32_t F = 22;//Global variables
uint32_t B = 22;//Initialize number of pulses
uint32_t L = 22;
uint32_t R = 22;
//This is used for PWM practicing
void PULSE37500Hz()
{
      LPC_SC->PCONP |= (1 << 6);               //PWM Power on
      LPC_PINCON->PINSEL7 |=(0<<19)|(0<<18);   //Reset output pins P3.25 (19,18bit)
      LPC_PINCON->PINSEL7 |=(1<<19)|(1<<18);   //set pin function to PWM1.2 at P3.25
      LPC_PWM1->TCR = 2;                       //Reset the counter, TCR used for disable/reset TC
      LPC_PWM1->PR = 3;                        //System f=120,000,000 /4=30,000,000Hz, use PR to set PWM to 10MHz
      LPC_PWM1->MCR = (1<<1);                  //Match control register, TC reset if MR0 matches it
      LPC_PWM1->MR0 = 266;                     //set PWM cycle 37.5kHz (26.6)
      LPC_PWM1->MR2 = 133;                     //set duty cycle using channel 2 match register, = 50% (13.3)
      LPC_PWM1->LER = (1<<0)|(1<<2);           //latch MR0 & MR2
      LPC_PWM1->PCR = (1<<10);                 //Enable PWM1 Channel 2 output
      LPC_PWM1->TCR = (1<<0)|(1<<3);           //Enable counter & PWM
}
//This is used for Timer Study
void Init_PULSE10Hz()
{
      LPC_SC->PCONP |= (1 << 1);               //Timer0 Power on
      LPC_PINCON->PINSEL7 |=(0<<21)|(0<<20);   //Reset output pins P3.26 (21,20bit)
      LPC_PINCON->PINSEL7 |=(1<<21)|(0<<20);   //set pin function to MAT0.1 at P3.25
      LPC_TIM0->TCR = 2;                       //Reset the counter (TCR is used for disable/reset TC)
      LPC_TIM0->PR  = 30;                      //System f=120,000,000/4=30,000,000Hz, use PR to set Timer to 1MHz
      LPC_TIM0->MCR |=(1<<4);                   //Match control register, TC reset if MR1 matches it
      LPC_TIM0->EMR = 0;                       //Reset EMR do nothing when TC=MR1
      LPC_TIM0->EMR |= (1<<7)|(1<<6);           //when TC=MR1, toggle on MATx.1(EM1)
      LPC_TIM0->MR1 = 250000;                 //half of the pulse T, toggle at this point
      LPC_TIM0->TCR = (1<<0);                  //Enable counting
}
uint8_t NavigationSM_ModifyPulseLength_ReturnState()
{
    uint8_t STATE = 0;
    uint8_t TEST  = ((~GPIO_ReadValue(2)) & 0x3F);//Test Port2 bit5,4,3,2,1,0
    switch (TEST)
    {
        case (FORWARD):        STATE =(1<<3); break;
        case (BACKWARD):       STATE =(1<<2); break;
        case (LEFT):           STATE =(1<<1); break;
        case (RIGHT):          STATE =(1<<0); break;
        case (FORWARD+LEFT):   STATE =(1<<3)|(1<<1); break;
        case (FORWARD+RIGHT):  STATE =(1<<3)|(1<<0); break;
        case (BACKWARD+LEFT):  STATE =(1<<2)|(1<<1); break;
        case (BACKWARD+RIGHT): STATE =(1<<2)|(1<<0); break;
        default:               STATE = 0;
        }
    return STATE;
}
uint32_t count = 0;  //set up a counter to count the pulse
uint32_t EN    = 1;  //Enable signal use for External Match MAT0.0(P3.25) output
void TIMER0_IRQHandler(void)
{
 uint32_t flag;
 flag = LPC_TIM0->IR;
 uint32_t S = 88; //start - Pulse numbers
 uint32_t C = 44; //car select
 uint32_t G = 40; //Gap
 if (flag & (1 << 1)) //Interrupt flag for match channel 1.
 {
     LPC_TIM0->IR = (1<<1); //reset
     LPC_TIM0->EMR = (1<<1);
     uint8_t TEST  = ((~GPIO_ReadValue(2)) & 0x3F);//Always when interrupt, test Port2 bit5,4,3,2,1,0
     switch (TEST)
     {
         case (FORWARD):  F = 44;break;
         case (BACKWARD): B = 44;break;
         case (LEFT):     L = 44;break;
         case (RIGHT):    R = 44;break;
         case (FORWARD+LEFT):   F = 44; L = 44;break;
         case (FORWARD+RIGHT):  F = 44; R = 44; break;
         case (BACKWARD+LEFT):  B = 44; L = 44;break;
         case (BACKWARD+RIGHT): B = 44; R = 44;break;
         default:F = B = L = R = 22;
             }
     count++; //The MR1 interrupt is entered after every 37.5kHz pulse, "count" used for pulse counting
     if (count == S + G + C + G + R + G + L + G + B + G + F + 3750-1 ) count=0;//One package, approximately 10 Hz
     if (  ((count>S )&&(count<S+G -1)) //Test the counter, during the GAP and the 10Hz delay, the EN is reset to 0.
         ||((count>S + G + C )&&(count<S+G+C+G -1))
         ||((count>S + G + C + G + R )&&(count<S + G + C + G + R + G -1))
         ||((count>S + G + C + G + R + G + L )&&(count<S + G + C + G + R + G + L + G -1))
         ||((count>S + G + C + G + R + G + L + G + B )&&(count<S + G + C + G + R + G + L + G + B + G -1))
         ||((count>S + G + C + G + R + G + L + G + B + G + F )&&(count<S + G + C + G + R + G + L + G + B + G + F + 3750 -1))
        )
          EN = 0;
     else EN = 1;
 }
 if (flag & (1 << 0)) //Interrupt flag for MR0(channel 0)
 {
     LPC_TIM0->IR = (1<<0); //reset
     if (EN)
     LPC_TIM0->EMR = (1<<0);
 }
}
#define TIM0PWR_ON 1
uint32_t OneCycle = 266;                //cycle time * prescaler * clock
uint32_t On_Time    = 133;                //on time
void timer0_init(void)
{
 LPC_PINCON->PINSEL7 |= (2<<18);            //function MAT0.0 P3.25 11->19,18bits
 LPC_SC->PCONP       |= (1 << TIM0PWR_ON);  //turn power for TIMER0 on
 LPC_TIM0->PR  = 3;                      //prescaler set timer to 1MHz
 LPC_TIM0->MR1 = OneCycle-1;            //use MR1 to set cycle
 LPC_TIM0->MR0 = LPC_TIM0->MR1 - On_Time; //match register 0 = off time
 LPC_TIM0->MCR = (1<<0)|(3<<3);            //1<<0 Interrupt on MR0, 3<<3 reset + Interrupt on MR1
 LPC_TIM0->EMR = (1<<4)|(2<<6);            //1<<4 Clears EM0, MAT0.0 cleared. 2<<6 Sets EM1, MAT0.1 Set
 NVIC_EnableIRQ(TIMER0_IRQn);              //Enable timer0 interrupt
 LPC_TIM0->TCR = 1;                        //Enable Timer0
}
 
 | 
 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		| 
//counter values
#define MAX_SEQUENCE 4                    //set number of sequence
volatile uint8_t counter = 0;            //count pulses
volatile uint8_t sequence=0;            //switch sequence
volatile uint8_t pulse[4] = {10,2,4,7}; //values for on/off/on/off...
volatile uint8_t pulse_enable=1;        //flag to switch output
void TIMER0_IRQHandler(void)
{
 uint32_t regval;
 regval = LPC_TIM0->IR;
 if (regval & (1 << 1)) //Interrupt flag for match channel 1.
 {
  LPC_TIM0->IR = (1<<1); //reset
  LPC_TIM0->EMR = (1<<1);
  counter++;                            //count pulse
  if(counter >= pulse[sequence])        //reached counter
  {
   counter =0;                            //reset counter
   sequence++;                            //next sequence
   if(sequence >= MAX_SEQUENCE) sequence = 0;//restart sequence
   pulse_enable = !(sequence % 2);        //set on for sequence = 0,2,4...
  }                                      //end reached counter
 }
 if (regval & (1 << 0)) //Interrupt flag for match channel 0.
 {
  LPC_TIM0->IR = (1<<0); //reset
  if(pulse_enable)                        //if pulse enabled
  {
   LPC_TIM0->EMR = (1<<0);                //switch on
  }
 }
}
 | 
 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		| 
//counter values
#define MAX_SEQUENCE 4                    //set number of sequence
volatile uint8_t counter = 0;            //count pulses
volatile uint8_t sequence=0;            //switch sequence
volatile uint8_t pulse[4] = {10,2,4,7}; //values for on/off/on/off...
volatile uint8_t pulse_enable=1;        //flag to switch output
void TIMER0_IRQHandler(void)
{
 uint32_t regval;
 regval = LPC_TIM0->IR;
 if (regval & (1 << 1)) //Interrupt flag for match channel 1.
 {
  LPC_TIM0->IR = (1<<1); //reset
  LPC_TIM0->EMR = (1<<1);
  counter++;                            //count pulse
  if(counter >= pulse[sequence])        //reached counter
  {
   counter =0;                            //reset counter
   sequence++;                            //next sequence
   if(sequence >= MAX_SEQUENCE) sequence = 0;//restart sequence
   pulse_enable = !(sequence % 2);        //set on for sequence = 0,2,4...
  }                                      //end reached counter
 }
 if (regval & (1 << 0)) //Interrupt flag for match channel 0.
 {
  LPC_TIM0->IR = (1<<0); //reset
  if(pulse_enable)                        //if pulse enabled
  {
   LPC_TIM0->EMR = (1<<0);                //switch on
  }
 }
}
 | 
 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		| 
#define PCTIM0 1
uint32_t cycle_time = 1000;                //cycle time * prescaler * clock
uint32_t on_time    =  250;                //on time
void TIMER0_IRQHandler(void)
{
 uint32_t regval;
 regval = LPC_TIM0->IR;
 if (regval & (1 << 1)) //Interrupt flag for match channel 1.
 {
  LPC_TIM0->IR = (1<<1); //reset
  LPC_TIM0->EMR = (1<<1);
 }
 if (regval & (1 << 0)) //Interrupt flag for match channel 0.
 {
  LPC_TIM0->IR = (1<<0); //reset
  LPC_TIM0->EMR = (1<<0);
 }
}
void timer0_init(void)
{
 LPC_PINCON->PINSEL3 |= (3<<24);        //MAT0.0 P1.28
 LPC_SC->PCONP |= (1 << PCTIM0);        //turn power for TIMER0 on
 LPC_TIM0->PR  = 25-1;                    //(prescaler -1) to 1MHz
 LPC_TIM0->MR1 = cycle_time-1;            //match register 1 = cycle
 LPC_TIM0->MR0 = LPC_TIM0->MR1 - on_time;//match register 0 = off time
 LPC_TIM0->MCR = (1<<0)|(3<<3);            //reset MR1
 LPC_TIM0->EMR = (1<<4)|(2<<6);            //MAT0.0 P1.28
 NVIC_EnableIRQ(TIMER0_IRQn);
 LPC_TIM0->TCR = 1;                        //enable Timer0
} | 
 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		| LPC_PWM1->MR0 = 26.6; //set PWM cycle 37.5kHz LPC_PWM1->MR2 = 13.3; //set duty cycle using channel 2 match register, = 50% | 
 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		 
					
				
		
 lpcware
		
			lpcware
		
		
		
		
		
		
		
		
	
			
		
		
			
					
		