This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LPC1768 Timer Interupt

Hello,

Please help confirm if my logic is right regarding timer interupt using NVIC based on the below code.
1) I can only receive or display on LCD 1 value each 3124 counts and the rest of idstring[94] values are blank.
2) Do ADC clock and Timer need to synch?

**Current result from the LCD shows as follow:
1) sometime it displays couple of values at a time (ie.idstring[1]=1, idstring[2]=23, etc.) and hung. If this happened, I need to reset phically.
2) sometime it shows couple of values at a time (ie.idstring[1]=1, idstring[2]=23, etc.) and passed and went back to main()loop.
3) the speed of the LCD display depends on the LPC_TIM0->MR0. If I changed this value higher than 3124, the LCD speeds faster. Why?

#include "lpc17xx.h"
#include "LCD.h"
int idstring[96];                 // data stored here
int j=0;

int main (void){
        int i;

        LCD_Hardware_Initial();
        LCD_initial();
        Signal_Detect_Initial();
        Timer_Hardware_Initial();
        ADC_Hardware_Initial();

  while (1){
   if ((LPC_GPIO0->FIOPIN & (1<<24))==0){     //signal input trigger
     Signal_Detect();
     for (i=1; i<96;i++){                     //display value on LCD
        LCD_send_command(0x01);                 //Clear screen
        LCD_display_int(i);
        LCD_display_string(": ");
        LCD_display_int(idstring[i]);
        delay_mS(50);
     }
   }

/******************subroutines******************************/
void Signal_Detect(void){
        NVIC_EnableIRQ(TIMER0_IRQn);
        LPC_TIM0->TCR = 1;         // Start timer: (1<<0) = bit 0 selected

}

void Timer_Hardware_Initial(void){
        LPC_SC->PCONP         |= (1<<1);       // Enable power to Timer/ Counter 0
        LPC_SC->PCLKSEL0 |= (1<<2);            // Select peripheral clock for Timer/ Counter 0
        LPC_TIM0->TCR          = 0x02;               // reset timer
        LPC_TIM0->PR           = 0x00;               // set prescaler to zero
        LPC_TIM0->MR0          = 3124;            // 1/(8000Hz) = 125 uS = 3125-1 counts @ 40nS/tick
        LPC_TIM0->MCR          = 3;          // Interupt and Reset on MR0: (1<<0) | (1<<1)
}

void TIMER0_IRQHandler (void){
        int val;

        LPC_ADC->ADCR |=  (1<<24);                             // start conversion
         while (!(LPC_ADC->ADGDR & (1UL<<31)));            // Wait for conversion end
         val = ((LPC_ADC->ADGDR >> 4) & 0xFFF);            // read converted value
         LPC_ADC->ADCR &= ~(7<<24);                                // stop conversion

        idstring[j]=val;
         j++;

        LPC_TIM0 ->IR =1;
}

void ADC_Hardware_Initial(void){
  LPC_PINCON->PINSEL1 &= ~(1UL<<14);                       // Reset P0.23 = GPIO
  LPC_PINCON->PINSEL1 |=  (1UL<<14);                   // Config P0.23 is AD0.0
  LPC_SC->PCONP       |=  (1UL<<12);                   // Enable power to ADC block
  LPC_ADC->ADCR        =  (1UL<< 0) |                  // select AD0.0 pin
                          (1UL<< 8) |                  // ADC clock is 18MHz/2
                          (1UL<<21);                   // enable ADC
}


Thanks in advance

0