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

LPC1343 Timer

Hi dears ,

I want to write a program to blink an LED each 1 second. I've wrote below code but after loading into the MCU, it does not work at all. Would you let me know how can I do it, please ?
<
#include "LPC13xx.h"
#define PRESCALE 60000 ;

void delayMS(unsigned int milliseconds);
void initTimer0();

int main()
{

LPC_SYSCON->SYSAHBCLKCTRL = (1<<7); initTimer0(); LPC_GPIO0->DIR |= (1<<3) |(1<<4); LPC_GPIO0->DATA = 0x0;

while(1) { LPC_GPIO0->DATA |= (1<<3);

LPC_GPIO0->DATA |= (1<<3); delayMS(500); LPC_GPIO0->DATA &= ~(1<<3); delayMS(500); LPC_TMR16B0->TCR = 0x0; //disable counter }
}

void initTimer0()
{ //LPC_TMR16B0->CTCR = 0x0; LPC_TMR16B0->PR = PRESCALE-1; LPC_TMR16B0->TCR = 0x02;//reset timer

}

void delayMS(unsigned int milliseconds)
{ LPC_TMR16B0->TCR = 0x02;//reset timer LPC_TMR16B0->TCR = 0x01;//set timer while(LPC_TMR16B0->TC < milliseconds); LPC_TMR16B0->TCR = 0; //disable counter

}>

Parents
  • while(1) {
        LPC_GPIO0->DATA |= (1<<3);
        LPC_GPIO0->DATA |= (1<<3);  <==== why two??? so fast processor one isn't enough???
        delayMS(500);
        LPC_GPIO0->DATA &= ~(1<<3);
        delayMS(500);
        LPC_TMR16B0->TCR = 0x0; //disable counter  <=== why??? your delayMS() already does this
    }
    

    Another thing - your prescaler value of 60000 for 1 millisecond would mean the input frequency to the timer is 60MHz. Is it? Where have you documented that in the source code? Always write your prescaler values so they are meaningful to a reader. The magic value 60000 is not meaningful since it doesn't tell how long timer period it's expected to produce, and it doesn't help the reader to know how you computed the value.

    #define PCLK 60000000 // 60 MHz
    #define PRESCALE_ONE_MS ((PCLK)/1000 - 1)
    

Reply
  • while(1) {
        LPC_GPIO0->DATA |= (1<<3);
        LPC_GPIO0->DATA |= (1<<3);  <==== why two??? so fast processor one isn't enough???
        delayMS(500);
        LPC_GPIO0->DATA &= ~(1<<3);
        delayMS(500);
        LPC_TMR16B0->TCR = 0x0; //disable counter  <=== why??? your delayMS() already does this
    }
    

    Another thing - your prescaler value of 60000 for 1 millisecond would mean the input frequency to the timer is 60MHz. Is it? Where have you documented that in the source code? Always write your prescaler values so they are meaningful to a reader. The magic value 60000 is not meaningful since it doesn't tell how long timer period it's expected to produce, and it doesn't help the reader to know how you computed the value.

    #define PCLK 60000000 // 60 MHz
    #define PRESCALE_ONE_MS ((PCLK)/1000 - 1)
    

Children