Generating a 5MHz clock signal using LPC1768

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Generating a 5MHz clock signal using LPC1768

2,736 Views
perdrix
Contributor II

The processor is running with a processor clock of 100MHz.

I only have the following GPIO pins available for use:

 P0.2, P0.3, P0.23, P0.24, P0.25 and P0.26

I tried to use a timer interrupt to generate a 5MHz clock signal, but AFAICT there just aren't enough processor cycles available to handle a timer interrupt every 10 clocks (which makes sense) and I only achieved a 1MHz signal out and there were probably not many processor cycles available to do any other work!

Is there a neat way to get a 5MHz clock out those pins using hardware to toggle it (perhaps using one of the peripherals)?  Ideally I'd like to use P0.23 and P0.26 for the clock signal (but not both at the same time).

Thanks, David

 

0 Kudos
Reply
7 Replies

2,638 Views
perdrix
Contributor II

I've done this the old fashioned way by bit-banging the clock and data at a lower rate (1MHz) which the receiving device can handle and I can generate.  Thanks,David

0 Kudos
Reply

2,698 Views
perdrix
Contributor II

Please remember

I only have the following GPIO pins available for use:

P0.2, P0.3, P0.23, P0.24, P0.25 and P0.26

0 Kudos
Reply

2,646 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @perdrix 

For match function, there is External Match Output pins, while not for those pins(P0.2, P0.3, P0.23, P0.24, P0.25 and P0.26). 

Alice_Yang_0-1691485170144.png

 

For you those pins, I think only can use timer interrupt to control GPIO. 

 

BR

Alice

0 Kudos
Reply

2,717 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @perdrix 

Do you just want to toggle GPIO? How about use match function of timer?

Alice_Yang_0-1691052888415.png

 

BR

Alice

 

0 Kudos
Reply

2,705 Views
perdrix
Contributor II

Maybe because I didn't quite understand how that works!

Remember that for a 5MHz clock on a GPIO port that needs to be toggled every 100ns or every ten processor cycles.    If by using the match registers I can do that w/o consuming processor cycles, I am all ears!!!

Please could you help me with some example code.

The code I used looked like:

void TIMER1_IRQHandler (void)
{
  if((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt
  {
        LPC_TIM1->IR |= (1 << 0); // Clear MR1 interrupt flag
	if (enablePGC)
	{
	  LPC_GPIO0->FIOPIN ^= PGC; // Toggle PGC (programming clock)
	}
	else LPC_GPIO0->FIOPIN &= ~(PGC);// Force PGC low		
  }
}

LPC_SC->PCONP |= 1 << 1;      // Power up Timer 1
LPC_SC->PCONP |= 1 << 22;     // Power up Timer 2
LPC_SC->PCLKSEL0 |= 1 << 4;   // Clock for Timer 1 = CCLK
LPC_SC->PCLKSEL1 |= 1 << 12;  // Clock for Timer 2 = CCLK

LPC_TIM1->MR0 = 10;           // 5MHz
LPC_TIM1->MCR |= (1 << 0) | (1 << 1); // Interrupt and Reset on MR0

// Reset Timers 1 and 2
LPC_TIM1->TCR = 0x02; 
LPC_TIM1->TCR = 0x00;
LPC_TIM2->TCR = 0x02; 
LPC_TIM2->TCR = 0x00;

// Clear pending interrupts for Timer 1 and enable timer interrupts
NVIC_ClearPendingIRQ(TIMER1_IRQn);
NVIC_EnableIRQ(TIMER1_IRQn);  // Enable timer interrupt
LPC_TIM1->TCR |= 1 << 0;      // Start timer 1


If you could show me how to do that using the match stuff I'd be most grateful - I got quite confused reading about EMR and MAT0.n registers

 

0 Kudos
Reply

2,689 Views
perdrix
Contributor II

Woohoo! I think I got Match working:

I user Timer0 and MR1, setting up as follows:

LPC_PINCON->PINSEL7 = (2<<20); // P3.26 becomes MAT0.1
LPC_PINCON->PINMODE7 |= (2 << 20); // Pin 3.26 == MAT0.1 no pull or pull down
LPC_SC->PCONP |= 1 << 1;      // Power up Timer 1
LPC_SC->PCLKSEL0 |= 1 << 2;   // Clock for Timer 0 = CCLK

LPC_TIM0->MR1 = 9;     // Approx 100ns
LPC_TIM0->MCR = (1 << 4);			// Reset on MR1 (no interrupt)
LPC_TIM0->EMR = (1 << 1) | (3<<6);	// Drive MAT0.1
	
// Reset Timer 1
LPC_TIM0->TCR = 0x02; 
LPC_TIM0->TCR = 0x00;
LPC_TIM0->TCR |= 1 << 0;      // Start timer 1

The only odd things are:

  1. I needed to set MR1 to 9 for an interval or 100ns when I expected to have to use 10.
  2. The amplitude of the signal on that output pin was 0.4Vp-p when I expected 3.3V.

Can you clarify those issues for me please?

Thanks, David

0 Kudos
Reply

2,680 Views
perdrix
Contributor II

Hmmm I generate the clock signal, but how do I clock data out using that clock? Maybe I need to rethink to use one of the peripherals

David

0 Kudos
Reply