lpc used as comparator

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

lpc used as comparator

676 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CroesJeroen on Mon Aug 27 04:03:54 MST 2012
Hello sir/madam.

I'm trying to use a input (gpio) as input for the comparator0 (+ input) and the - input of the comparator0 from the voltage divider ( set to Vdd/2). So the output of the comp go's high if I apply a voltage higher than vdd/2 and an interrupt is thrown.

The output of the comparator is always high even when no voltage is applied at the + input. The used microcontroller = LPC1227.

Here is the code :
compare.h
#define PullUpDis  (1<<4) // default enabled
#define AnalogMode  (1<<7) // enable analog mode

#defineGPIO0_Clock  (1<<31)
#define Compare_CLK       (1<<20)// enable clock for comparator module
#define PowerCMP   ((1<<3)|(1<<15))// enable BOD en Comparator module in PDRUNCFG

#define CMP0_EN (1)// enable comparator module 0

#define CMPIEV (1<<3)// activate interrupt on rising edge
#define ACMP0_I0 (1<<8)// use ACMP0_O input

#define INTCOMPCLR (1<<20)// bit to reset the interrupt status
#defineCMP0STAT (1<<21)//interrupt status of the comparator 0
#defineCMP1STAT (1<<22)//interrupt status of the comparator 1

#defineVLADEN (1)// enable ladder module ( 32 levels, 0 to 3V3)
#define VSEL (10000<<1)

#define VLADVDDREF (1<<6)// use internal 3V3 voltage to take a level from


compare.c

void InitCMP(void){ 

LPC_SYSCON->SYSAHBCLKCTRL|=Compare_CLK; 
LPC_SYSCON->SYSAHBCLKCTRL|=GPIO0_Clock; 
LPC_IOCON->PIO0_19|=0x2; // select as input for comparator(ACMP0_O) 
LPC_IOCON->PIO0_19&=(~PullUpDis);//disable pullup 
LPC_IOCON->PIO0_19&=(~AnalogMode);//enable analog mode 
LPC_SYSCON->PDRUNCFG&=(~PowerCMP); // power the comparator module 
LPC_ACOMP->VLAD|=(VLADEN|VSEL|VLADVDDREF); // enable voltage ladder, select reference 3.3V, set voltage to 1.66V 
LPC_ACOMP->CMP|=(CMP0_EN|CMPIEV|ACMP0_I0|INTCOMPCLR); // enable comparator, use rising edge, positieve input opamp = input ACMP0_I0 , negative = voltage ladder 

//NVIC_ClearPendingIRQ(CMP_IRQn); 
NVIC_EnableIRQ(CMP_IRQn); 
} 

void COMP_IRQHandler(void){ 

uint8_t dummy=0; 
LPC_ACOMP->CMP|=INTCOMPCLR; 

} 


Hopefully you guys can point me out why the output of comparator 0 is high even when no voltage is applied to the input and also when a voltage is applied, the interrupt doesnt hit..
0 Kudos
Reply
13 Replies

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXP_Europe on Fri Aug 31 04:56:40 MST 2012
Hello Jeroen,

did you look at the Keil example?

There is an output-interrupt and an output-polling version.

see our website:
http://www.lpcware.com/content/nxpfile/lpc12xx-cmsis-driver-library-keil
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CroesJeroen on Thu Aug 30 07:07:41 MST 2012

Quote: graynomad
Not me, bit 23 isn't even a valid bit.:confused:

Just to prove there's nothing screwy with the define I'd try using (1<<20) directly, but I can't see that making a difference.



Hi,

swapped it with
LPC_ACOMP->CMP|=(1<<20);
. No improvements.

Yea indeed, the bit isnt even valid.. . Well here is something I tested :

1) Start lpc with input (0.19) to gnd, interrupthandler isn't invoked.
2) Start lpc with input (0.19) to vdd, interrupthandler is  invoked. After executing the interrupt clear,the interrupthandler isn't invoked any more.
3) Start lpc with input (0.19) to vdd, interrupthandler is  invoked. Now the instruction to clear the interruptflag is removed and the interrupthandler is still invoked.

So I think that we wont see bit 20 go high but it works, clearing the interruptflag for the comparator.
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CroesJeroen on Thu Aug 30 06:29:11 MST 2012

Quote: NXP_Europe
Hello Jeroen,

did you read out the content of address 0x40044008 after initCMP?




Offcourse here you are : [img]http://i47.tinypic.com/2lwoeva.jpg[/img]
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXP_Europe on Wed Aug 29 15:18:15 MST 2012
Hello Jeroen,

did you read out the content of address 0x40044008 after initCMP?


Quote:

[LEFT][FONT=Arial][COLOR=#005042][B][FONT=Arial][COLOR=#005042]Table 63. PIO0_19 register (PIO0_19, address 0x4004 4008) bit description[/COLOR][/FONT][/B][/COLOR][/FONT][/LEFT]
[FONT=Arial]
[LEFT][FONT=Arial][COLOR=#005042][B][COLOR=#005042][FONT=Arial][SIZE=1][FONT=Arial][SIZE=1]2:0 FUNC Selects pin function. 000[/SIZE][/FONT][/SIZE][/FONT][/COLOR][/B][/LEFT]

[LEFT][SIZE=1][FONT=Arial][B][SIZE=1][FONT=Arial][COLOR=#005042]0x0 Selects function PIO0_19.[/COLOR][/FONT][/SIZE][/B][/FONT][/SIZE][SIZE=1][FONT=Arial]
[B][SIZE=1][FONT=Arial][COLOR=#005042]0x1 Reserved. Do not use.[/COLOR][/FONT][/SIZE][/B]
[B][SIZE=1][FONT=Arial][COLOR=#005042]0x2 Select function ACMP0_I0.[/COLOR][/FONT][/SIZE][/B]
[B][SIZE=1][FONT=Arial][COLOR=#005042]0x3 Select function CT32B0_CAP1.[/COLOR][/FONT][/SIZE][/B]
[B][SIZE=1][FONT=Arial][COLOR=#005042]0x4 Select function CT32B0_MAT1.[/COLOR][/FONT][/SIZE][/B][/LEFT]
[/FONT][/SIZE][/COLOR][/FONT][/FONT]

0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Wed Aug 29 07:39:05 MST 2012

Quote:
Hopefully someone knows whats going on.

Not me, bit 23 isn't even a valid bit.:confused:

Just to prove there's nothing screwy with the define I'd try using (1<<20) directly, but I can't see that making a difference.
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CroesJeroen on Tue Aug 28 10:48:27 MST 2012

Quote: graynomad
Meaning that the input is floating? Have you tried forcing it to VCC and GND?

I don't see where you set the comparator's output pin (PIO0:27) as an output, is that required?




Indeed I don't set PIO0:27 as an output, because I don't want to use that output. I want to use that pin for other purposes.

Ok now, so when I start and attach the input to GND, the interrupthandler isn't thrown, than I unleash it ( floating input), it hits the handler, than I apply the Vdd and it gets hit again, so thats works. Also the output of the comp is zero when 0V is applied and 1 when Vdd is applied.

But I'm facing some little problem now :

[IMG]http://i46.tinypic.com/2wftsa8.jpg[/IMG]

why the bit INTCLR ( 20) is not set but bit 23 is set instead???

I also tried to change the instruction into :
LPC_ACOMP->LPC_ACOMP->CMP+INTCOMPCLR;


Hopefully someone knows whats going on.

Kind regards!!
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Mon Aug 27 17:57:09 MST 2012

Quote:
The output of the comparator is set high even when no voltage is applied.


Meaning that the input is floating? Have you tried forcing it to VCC and GND?

I don't see where you set the comparator's output pin (PIO0:27) as an output, is that required?
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CroesJeroen on Mon Aug 27 12:19:39 MST 2012

Quote: Zero
What's strange there :confused::confused:

10000  << 1  =  10000 * 2 = 20000 = 0b100111000[COLOR=Red]10000[/COLOR]0

Now cut out bit 1..5 and voila: 0b10000 :eek:



OK thanks, I see what you mean.

Changed it to :
#define VSEL (0x10<<1)


But still it doesn't work properly. The output of the comparator is set high even when no voltage is applied. So the + input of the comparator ( pin from the GPIO) is also 0.8V when no voltage is applied. The - input for the comp comes from the voltage ladder. As in the picture below the COMP0STAT bit is high, states that + input> - input.

[img]http://i49.tinypic.com/15pq55g.jpg[/img]

Someone has an idea??
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Aug 27 11:23:41 MST 2012
What's strange there :confused::confused:

10000  << 1  =  10000 * 2 = 20000 = 0b100111000[COLOR=Red]10000[/COLOR]0

Now cut out bit 1..5 and voila: 0b10000 :eek:
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CroesJeroen on Mon Aug 27 11:09:52 MST 2012

Quote: Zero
But your compiler doesn't :)

10000 = 10000 :confused:

Use binary notation for binary values:

0b10000 = 16 = 0x10



Ok, thanks for the remark.

It's strange because I debugged it and it states that the value 10000 ( binairy) is in the vsel register.

[IMG]http://i50.tinypic.com/nmyf87.jpg[/IMG]

thanks!
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Aug 27 10:59:02 MST 2012

Quote: CroesJeroen
... I know it has to be binairy...



But your compiler doesn't :)

10000 = 10000 :confused:

Use binary notation for binary values:

0b10000 = 16 = 0x10
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CroesJeroen on Mon Aug 27 10:38:33 MST 2012

Quote: graynomad
#define VSEL         (10000<<1)


This should be binary not 10,000 decimal.



Hello graynomad, I know it has to be binairy.

I just set the MSBit high so I would have Vdd/2.

0000 = VSS
0001 = 1 * Vref/31
0010 = 2 * Vref/31
...
11111 = Vref

10000=16*Vref/31 ~ Vref/2.

Thanks for reply!
0 Kudos
Reply

609 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Mon Aug 27 08:19:39 MST 2012
#define VSEL         (10000<<1)


This should be binary not 10,000 decimal.
0 Kudos
Reply