MicroC and 8051 pin input

Hi. I'm trying to count the number of times P3.3 on the 8051 goes low. The C code I've generated doesn't give me any errors but it isn't working either. Can any of you offer some pointers? The code is as follows:

#include #include /* Bit set/clear macros */ #include

main() { int i i = 0; InitLCD(); for(;;) { setbit(P3.0); if(!(P3 & 0xCD)) { WriteLCD( "*" ); delay(2000); i++; } } }

Thanks a lot, Blake

Reply to
Blaketheturtle
Loading thread data ...

How "not working" - does it fill the screen with * when you press a button ?

Steve

Reply to
Steve Taylor

If you're only trying to monitor P3.3, why the 0xCD mask - as opposed to

0x08? If you're counting '*''s on the screen, why do you need to increment 'i'? If you're keeping the count in 'i', how do you get out of the loop to read it - debugger? You don't mention which 8051 variant you're using, but did you initialise the ports properly? Do you need pullup resistors? Watchdog timer enabled?

Bob

Reply to
Bob Stephens

"Blaketheturtle" schreef in bericht news: snipped-for-privacy@posting.google.com...

It seems that this is one of your very first projects. The first embedded project is to turn on a led. Try that first, forget the LCD for the moment. Change the program to turn it off, test it, change it again to turn on the led, and test it.

The next step is to read an input that has a pushbutton connected. Try to turn on a led when the butten is pressed, and goes off when the button is released.

The third step is to forget the led and switch, and print a text on the LCD, see if you get 'Hello World' on the LCD.

If you have done those 3 experiments, you are ready to combine what you have learnt, and make that counter.

--
Thanks, Frank.
(remove 'x' and 'invalid' when replying by email)
Reply to
Frank Bemelman

In addition to the incorrect mask value (0xCD) I see a couple of problems with this program. First, rather than counting the number of times the port goes low, ie, transitions from the high to low state, it counts the number of times it finds the port pin low. Asterisks will be written to the display and 'i' will be incremented at a rate determined by the time required to traverse the infinite loop rather than by the number of times the pin goes low. To solve this you must also wait for the port to be high before waiting for it to go low.

Second, short pulses from high to low and back to high can be missed if they occur while setbit(), WriteLCD(), delay(), and "i++" are executing. To solve this you would do better to use an interrupt input and count the number of interrupts that occur. There would still be a window of pulses that could be missed but it would be quite a bit shorter.

Oh, and you should probably move the call to setbit() outside the infinite loop, too.

--
========================================================================
          Michael Kesti            |  "And like, one and one don't make
                                   |   two, one and one make one."
          mkesti@gv.net            |          - The Who, Bargain
Reply to
Michael R. Kesti

I don't know the 8051, but the delay doesn't look good to me. What if the pin goes low and high again, you'll have missed a count. You don't give enough information to give a sensible decision, like are these keypresses or what frequency the pulses come in. E.g. key presses need debouncing -like 3 counts with 15mS gaps.

Are you a student?

Reply to
Dave

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.