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 problem with timer0

Hi, this is my first thread in this forum and at the beginning i want to apologize for my English OK, I've just written small program with timer0 and interrupt. I want to toggle one of the LED on board, but...It doesn't work and I really don't know what's wrong with this code. Each LED is lit and nothing more... Could you help with it ? Thank you in advance and best regards :)

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

// TODO: insert other include files here

// TODO: insert other definitions and declarations here
int main(void)
{
    LPC_GPIO2->FIODIR |= 1 << 29;
    LPC_SC->PCONP |= 1 << 1;
    LPC_SC->PCLKSEL0 |= 1 << 3;
    LPC_TIM0->TCR |= 0x00000002;
    LPC_TIM0->MCR |= 0x00000003;
    LPC_TIM0->MR0 |= 6000000;
    NVIC_EnableIRQ(TIMER0_IRQn);
    LPC_TIM0->TCR |= 0x00000001;
    while(1){}
    return 0 ;
}

void TIMER0_IRQHandler (void)
{
    if((LPC_TIM0->IR & 0x01) == 0x01)
    {
        LPC_TIM0->IR |= 1 << 0;
        LPC_GPIO2->FIOPIN ^= 1 << 29;
    }
}

  • OK, I've just corrected my code and now works better. I know how to read ADC register, I make it in main loop but the logic of LEDs is still bad. It works only one way - you talked about it. The LEDs don't turn off but turning on works very well. Any snippet ? And small prompt ? Thank you for all answers and advices... :)

    My code:

    /*
    ===============================================================================
     Name        : main.c
     Author      :
     Version     :
     Copyright   : Copyright (C)
     Description : main definition
    ===============================================================================
    */
    
    #ifdef __USE_CMSIS
    #include "LPC17xx.h"
    #endif
    
    #include <cr_section_macros.h>
    #include <NXP/crp.h>
    
    // Variable to store CRP value in. Will be placed automatically
    // by the linker when "Enable Code Read Protect" selected.
    // See crp.h header for more information
    __CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
    
    // TODO: insert other include files here
    #include <math.h>
    #include <stdio.h>
    // TODO: insert other definitions and declarations here
    int adc_config(void);
    uint16_t prevVal,curVal,errCnt;
    uint16_t readADC();
    
    int main(void)
    {
            int i;
            errCnt = 0;
            const unsigned int led_state[] = {
                                        0x01, 0x03, 0x07, 0x0f,
                                        0x1f, 0x3f, 0x7f, 0xff
                                    };
            LPC_GPIO2->FIODIR = 0x000000FF;
            adc_config();
    
            while(1)
            {
                    i = readADC();
                    //printf("i = %d",i);
                    LPC_GPIO2->FIOSET = led_state[(i >> 9) & 7];
            }
            return 0 ;
    }
    
    int adc_config(void)
    {
            LPC_SC->PCONP |= 1 << 12; // power for adc
            LPC_ADC->ADCR |= (1 << 5) | (1 << 21); // bit for ADC0.5 input and PDN mode is operational
            LPC_SC->PCLKSEL0 |= 0; //clock for ADC - adcclk = pclk/4 = 3Mhz
            LPC_PINCON->PINSEL3 |= (1 << 30) | (1 << 31); //activate adc function on pin P1.31
            //LPC_ADC->ADINTEN = 1 << 8; //enable interrupts for channel 0
            //NVIC_EnableIRQ(ADC_IRQn);
    }
    
    /*void ADC_IRQHandler(void)
    {
    
    }*/
    
    uint16_t readADC()
    {
            LPC_ADC->ADCR |= 1 << 24;// start conversion.
            while(!(LPC_ADC->ADGDR & (1 << 31))); // wait for completion of coversion
            curVal = (LPC_ADC->ADGDR >> 4) & 0x0fff;
    
            if(errCnt >= 3)
            {
                    prevVal = curVal;
            }
    
            if(abs((curVal - prevVal) > 20))
            {
                    curVal = prevVal;
                    errCnt++;
            }
            else
            {
                    errCnt = 0;
            }
    
            prevVal = curVal;
            return curVal;
    }
    
    
    

  • Not sure if you actually used the processor user manual, or if you just looked at any blinky application.

    But when you did find the FIOSET register - didn't you then also find any FIOCLR register?

    By the way - I see you wrote a hysterese function to reduce problems with noise from the potentiometer. But why did you select a variable name errCnt? Where is the error?

    Another thing - why is curVal a global variable? You give it a value the first thing you do in the readADC() function - and then you return the value of curVal - so no need to retain the value of it until next call...

  • OK, I did it. Now it works good :) LEDs turns off too :) I only added one line:

    LPC_GPIO2->FIOCLR =~(led_state[(i >> 9) & 7]);

    When I negate table's value and send it to FIOCLR register LEDs turns off.
    I knew about this register, but as I said I have many habits from AVR...I forgot that, if I want to turn off LEDs I must write "1" to the bits of FIOCLR.

  • But AVRs also have SET and CLEAR registers for their port bits - don't they?!

  • In a previous post he mentioned ATmega32 so no, there is no set/clr.
    There is only a direction , port and input read register (DDR, PORT,PIN)

    Alex