EmbeddedRelated.com
Forums

MicroC and 8051 pin input

Started by Blaketheturtle November 24, 2004
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 <8051io.h>
#include <8051bit.h>            /* Bit set/clear macros */
#include <8051reg.h>

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

Thanks a lot,
Blake
Blaketheturtle wrote:

> for(;;) > { > setbit(P3.0); > > if(!(P3 & 0xCD)) > { > WriteLCD( "*" ); > delay(2000); > i++;
How "not working" - does it fill the screen with * when you press a button ? Steve
On 24 Nov 2004 13:55:23 -0800, Blaketheturtle wrote:

> 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 <8051io.h> > #include <8051bit.h> /* Bit set/clear macros */ > #include <8051reg.h> > > main() { > int i > i = 0; > InitLCD(); > for(;;) > { > setbit(P3.0); > > if(!(P3 & 0xCD)) > { > WriteLCD( "*" ); > delay(2000); > i++; > } > } > } > > Thanks a lot, > Blake
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
"Blaketheturtle" <blaketheturtle@yahoo.com> schreef in bericht
news:f55076af.0411241355.3cc7a92d@posting.google.com...
> 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 <8051io.h> > #include <8051bit.h> /* Bit set/clear macros */ > #include <8051reg.h> > > main() { > int i > i = 0; > InitLCD(); > for(;;) > { > setbit(P3.0); > > if(!(P3 & 0xCD)) > { > WriteLCD( "*" ); > delay(2000); > i++; > } > } > }
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)
Blaketheturtle wrote:

>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 <8051io.h> >#include <8051bit.h> /* Bit set/clear macros */ >#include <8051reg.h> > >main() { > int i > i = 0; > InitLCD(); > for(;;) > { > setbit(P3.0); > > if(!(P3 & 0xCD)) > { > WriteLCD( "*" ); > delay(2000); > i++; > } > } >} > >Thanks a lot, >Blake
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
Blaketheturtle wrote:
> 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 <8051io.h> > #include <8051bit.h> /* Bit set/clear macros */ > #include <8051reg.h> > > main() { > int i > i = 0; > InitLCD(); > for(;;) > { > setbit(P3.0); > > if(!(P3 & 0xCD)) > { > WriteLCD( "*" ); > delay(2000); > i++; > } > } > } > > Thanks a lot, > Blake
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?