EmbDev.net

Forum: ARM programming with GCC/GNU tools LPC2148 Pulse width demultiplexer using CAP0.0


von Noel Le Moult (Guest)


Rate this post
useful
not useful
Hello,

I have an Logomatic-v23 board from Sparkfun electronics. I want to use 
an ADXL213AE accelerometer.

The ADXL send his PWM output value and i want to capture it using the 
CAP0.0 input of the LPC2148. Unfortunately I have tried the following 
code and it don't want to acquire the value.

Initialization :
1
//Enable Interrupts
2
VPBDIV=1;    // Set PCLK equal to the System Clock
3
VICIntSelect = ~(UART1_INT | RTC_INT | TIMER0_INT);
4
VICVectCntl0 = 0x20 | 7;      //Set up the UART1 interrupt
5
VICVectAddr0 = (unsigned int)ISR_RxData1;
6
VICVectCntl1 = 0x20 | 13;      //Set up the RTC interrupt
7
VICVectAddr1 = (unsigned int)ISR_RTC;
8
VICVectCntl2 = 0x20 | 4;      //Timer 0 Interrupt
9
VICVectAddr2 = (unsigned int)ISR_Timer0;
10
11
//TIMER0 setup
12
T0TCR = 0x02;       // TIMER0 Disable; reset T0TC & T0PC
13
T0PR = 0x00000000;  // Reset T0PR
14
T0CTCR = 0x00;      // Timer Mode: every rising PCLK edge
15
T0CCR = 0x06;       // Capture on CAPn.0 falling edge and Interrupt on CAPn.0 event
1
static void ISR_Timer0(void)
2
{
3
    //Interrupt Code Here
4
    T0TCR = 0x02;       // TIMER0 Disable; reset T0TC & T0PC
5
    T0PR = 0x00000000;  // Reset T0PR
6
  
7
    YPacc = T0CR0;     //Get accelerometer captured value
8
    
9
    T0IR = 0xFF;       //Clear the timer interrupt
10
    T0TCR = 0x01;      // TIMER0 Enable 
11
    VICVectAddr =0;    //Update the VIC priorities
12
}

Main code :
1
rprintf_devopen(putc_serial0); //Init rprintf
2
rprintf("ADXL2148 Accelerometer Yp raw data :\n\r");
3
4
VICIntEnable |= TIMER0_INT;  //Enable Timer0 Interrupts
5
T0TCR = 0x01;       // TIMER0 Enable 
6
7
while( 1 )
8
{
9
    rprintf("T0CR0:%i\n\r",T0TC);
10
}

Output on Hyperterminal :
1
ADXL2148 Accelerometer Yp raw data :
2
T0CR0:369396
3
T0CR0:139755
4
T0CR0:271646
5
T0CR0:539757
6
T0CR0:311754
And the value is never the same...

Does anybody could help me to find my problem?

von Noel Le Moult (Guest)


Rate this post
useful
not useful
I have made a mistake in my previous post :

Main code should be :
1
rprintf_devopen(putc_serial0); //Init rprintf
2
rprintf("ADXL2148 Accelerometer Yp raw data :\n\r");
3
4
VICIntEnable |= TIMER0_INT;  //Enable Timer0 Interrupts
5
T0TCR = 0x01;       // TIMER0 Enable 
6
7
while( 1 )
8
{
9
    rprintf("T0CR0:%i\n\r",YPacc);
10
}

And the result output is now :
1
ADXL2148 Accelerometer Yp raw data :
2
T0CR0:4326756
3
T0CR0:4325926
4
T0CR0:4324216
5
T0CR0:4324036
6
T0CR0:4326626
7
T0CR0:4324236

It seems working now.

Thanks

von Noel Le Moult (Guest)


Rate this post
useful
not useful
Ok, now it detect falling edge of pwm signal but it always capture the 
total period and not the duty cycle of my signal.

I need to capture the duty cycle to have a correct value of my ADXL.

von Noel Le Moult (Guest)


Rate this post
useful
not useful
I have solved my problem. Here is my personal solution to get the duty 
cycle of the original pwm incoming signal:

Main code :
1
rprintf_devopen(putc_serial0); //Init rprintf
2
rprintf("ADXL2148 Accelerometer Yp raw data :\n\r");
3
4
while( 1 )
5
{
6
    VICIntEnable |= TIMER0_INT; // Enable Timer0 Interrupts
7
    T0TCR = 0x01;               // TIMER0 Enable
8
    delay_ms(20);               // Do some acquisition
9
    T0TCR = 0x00;               // TIMER0 Disable
10
    VICIntEnClr |= TIMER0_INT;  // Stop Timer0 Interrupts
11
12
    result = T1/((T1+T2)/100);  // Duty cycle calculation in percent
13
14
    if( (result > (oldresult+1)) || (result < (oldresult-1)) ) // If detect movement
15
    {
16
        i=100;
17
        rprintf("%d ", result );
18
    }
19
20
}


Interrupt section :
1
static void ISR_Timer0(void)
2
{
3
    //Interrupt Code Here
4
    T0TCR = 0x02;       // TIMER0 Disable; reset T0TC & T0PC
5
    T0PR = 0x00000000;  // Reset T0PR
6
7
    if(state)
8
    {
9
        T1 = T0CR0;
10
        T0CCR = 0x06;   // Capture on CAPn.0 falling edge and Interrupt on CAPn.0 event
11
        state = 0;
12
    }
13
    else
14
    {
15
        T2 = T0CR0;
16
        T0CCR = 0x05;   // Capture on CAPn.0 rising edge and Interrupt on CAPn.0 event
17
        state = 1;
18
    }
19
20
    T0IR = 0xFF;       //Clear the timer interrupt
21
    T0TCR = 0x01;      // TIMER0 Enable
22
23
    VICVectAddr =0;    //Update the VIC priorities
24
}

It should be working, but adjust the "delay_ms(20);" in the main code 
section according to the period of your pwm signal.

von parth k. (er_parth92)


Rate this post
useful
not useful
hello sir,

i am also try to access capture mode in timer0,but can't get the 
output..i am try to calculate frequency using this mode.. plz send ur 
code so i can get some idea..

thank you....

von Nik (Guest)


Rate this post
useful
not useful
I can print anything with

rprintf("T0CR0:%i\n\r",T0TC); !!!

rprintf is not possible for uVision 4 ?
Can you tell me how I can use this software for rotary encoder

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.