Hi everyone,
I am trying to use lpc1768 to have 2 pins with Open Drain output (p2.2 and p2.2) and external pull ups. When I set these pins only as output I get pulses with correct timing but when I add this line "LPC_PINCON->PINMODE_OD2 |= ((1<<1)|(1<<2))" I get a weird behavior in these pins, not matching the timing and getting extra pulses.
My code is the next:
void Sensor::init()
{
LPC_PINCON->PINMODE_OD2 |= ((1<<1)|(1<<2));
LPC_GPIO2->FIODIR |= ((1<<1)|(1<<2));
LPC_GPIO2->FIOCLR |= ((1<<1)|(1<<2));
}
void Sensor::measure(void)
{
LPC_GPIO2->FIOSET |= ((1<<1)|(1<<2));
wait_us(50);
LPC_GPIO2->FIOCLR |= ((1<<1)|(1<<2));
wait_us(50);
LPC_GPIO2->FIOSET |= ((1<<1)|(1<<2));
wait_us(50);
LPC_GPIO2->FIOCLR |= ((1<<1)|(1<<2));
}
What I am doing wrong to get OpenDrain output with the correct timing?
Thanks for your help, Regards.
已解决! 转到解答。
Hello,
I noticed that with that line of code you just mentioned, you are writing a 110’b in the PINMODE_OD2 register and I believe that is incorrect, because that register can only be 1 or 0. In this case, the register stays with a value of 0, and that is configuring the register in not open drain mode. Could you please try to change that value to 1?
Regards, Daniel.
@DanielRuvalcaba Thanks for your reply.
I follow your advice and I changed my code to clearly set the bits in 1'b but I still have the same problem. Here you can see the test code that I implemented:
void Sensor::init()
{
Log::info("PINSEL4: %X", LPC_PINCON->PINSEL4);
Log::info("LPC_GPIO2->FIODIR: %X", LPC_GPIO2->FIODIR);
Log::info("PINMODE4: %X", LPC_PINCON->PINMODE4);
Log::info("PINMODE_OD2: %X", LPC_PINCON->PINMODE_OD2);
LPC_PINCON->PINSEL4 &= 0xFFFFFFC3;
LPC_GPIO2->FIODIR |= 0x00000006;
LPC_PINCON->PINMODE4 |= 0x00000028;
LPC_PINCON->PINMODE4 &= 0xFFFFFFEB;
LPC_PINCON->PINMODE_OD2 |= 0x00000006;
Log::info("PINSEL4: %X", LPC_PINCON->PINSEL4);
Log::info("LPC_GPIO2->FIODIR: %X", LPC_GPIO2->FIODIR);
Log::info("PINMODE4: %X", LPC_PINCON->PINMODE4);
Log::info("PINMODE_OD2: %X", LPC_PINCON->PINMODE_OD2);
}
void Sensor::measure(void)
{
LPC_GPIO2->FIOSET |= 0x00000006;
wait_us(50);
LPC_GPIO2->FIOCLR |= 0x00000006;
wait_us(50);
LPC_GPIO2->FIOSET |= 0x00000006;
wait_us(50);
LPC_GPIO2->FIOCLR |= 0x00000006;
wait_us(50);
}
And this is what I get in the logs:
PINSEL4: 0
LPC_GPIO2->FIODIR: 1A0
PINMODE4: 28BFC
PINMODE_OD2: 0
PINSEL4: 0
LPC_GPIO2->FIODIR: 1A6
PINMODE4: 28BE8
PINMODE_OD2: 6
Then I can see that every bit is set correctly but still I got no pulses on the pins p2.1 and p2.2. Every test was made with a 10k resistor to 3.3V for pullups, but nothing happens. But if in the method measure() I put a longer delay as wait_us(1000), I get two pulses but with an incorrect random width.
As I said before, If I set these pins as simpe outputs without opendrain, then I get two pulses with the correct width of 50us and separation of 50us.
Which could be the problem here?