Like Tree3Likes
  • 3 Post By anduril462

Help With library counter programs

This is a discussion on Help With library counter programs within the C Programming forums, part of the General Programming Boards category; I am just learning c language. I have the basics down pat. But I am having trouble writing a C ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Help With library counter programs

    I am just learning c language. I have the basics down pat. But I am having trouble writing a C program fror an 8051 microcontroller. The idea of the program is to count the number of people entering and exiting our schools library. we are using photo eyes. That will be normally high, when someone breaks the beam they go low activating the program. The order in which the photo eyes break detirmens if the person is entering or leaving. I have the program idea in my head I just do not know enough about C to write it fully Here is what I have
    Code:
    #include <reg51.h>
    #include <stdio.h>
    #define LCDData P1
    void main(void)
    char X
    char y
    (
    If(p3.0==0)
    	if(p3.1==0)
    	x++;
    
    if(p3.1==0)
    	if(p3.0==0)
    	y++;
    )
    sbit en=p2^0;
    display(void)
    (
    unsigned char message [ ]= "  "  this is where I want the number that X and Y are currently at
    unsigned char z;
    for(z=0;z<28;z++)
    	{
    	LCDData=message[z];
    	En=1;
    	En=0;
    }
    }
    I know this is not complete, and the LCD display program is from a text book of mine. Any help would be greatly appreciated. I been stuck on the program for sometime.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    What is p3.0 and p3.1 supposed to do/represent? Those are invalid identifier names are you trying to multiply some unknown identifier 'p' by 3.1?

    I don't understand what you mean by the comment "this is where I want the number that X and Y are currently at?" Do you simply want to load the message char array with x and y? if so you can just do:

    Code:
    unsigned char message[2];
    message[0] = x;
    message[1] = y;
    Last edited by nonpuz; 08-11-2011 at 12:21 AM. Reason: spelled 'y' as 'why' doh!

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    There are at least 14 things wrong with what you've posted already, most of which are obvious compile-time errors. If you want to produce a C program then you need to start by having a C compiler. Clearly this piece of 'code' has never seen a C compiler.

    Most of this 'code' makes no sense, and I don't see that you're going to get very far. You cannot really start out learning C and embedded programming, on your own, at the same time. You need far more help than people here are probably going to be able to provide.
    I would suggest that you first start learning C by writing PC programs.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    thats the kicker

    I am just now taking C classes in school and they want me to do this project. I know it wont build its not even close to complete I was looking for pointers. I was never taught how to program a 8051 with c just basic c programs.

    P3.0 and P3.1 are input pins from the 8051 microcontroller they are conected to the photo eyes so when the photo eyes go low, or someone passes through the beam. Pins 3.0 and 3.1 will be equal to 0

    if(P3.0==0)
    if(P3.1==0)
    x++ //this is what i thoought of for checking the direction the person is going if pin3.0 and then p3.1 go low or equal 0 then increment x by 1. That will count people entering.
    if(P3.1==0
    if(P3.0==0)
    y++ // this is for when someone is exiting when P3.1 then P3.0 go low or equal 0 the person is exiting and y is incremented

    then on the LCD display the current count that x is at will be displayed and the current count that y is at will be displayed. The biggest problem is I only know how program an 8051 with assembly

    I appreciate the help, I know I am a newb

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That might work... but your indenting sucks... post code between code /code tags please....
    Code:
    if (p3.0 == 0 && p3.1 == 1)    // going -->
        x++;
    else if (p3.0 == 1 and p3.1 == 0)  // going <--
       x--;

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    That might work...
    Code:
    if (p3.0 == 0 && p3.1 == 1)
    ...if that zero was an "oh", and that one was an "el"


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    ...if that zero was an "oh", and that one was an "el"
    Quzah.
    Some embedded systems identify bits on hardware ports that way... Can't remember exactly where but I have seen this before.

    In any case, even if the variable names are bogus... the concept is right.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    FYI:

    All the 8051 code I have read used "P1" instead of "p1"; so, it could be that it must be upper case to access Port 1 where P1.0 is Port 1 Bit 0.

    Tim S.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Some embedded systems identify bits on hardware ports that way... Can't remember exactly where but I have seen this before.

    In any case, even if the variable names are bogus... the concept is right.
    Hm...

    C and the 8051 - Google Books

    Maybe something like that table, column four? C wouldn't let you use a number as a variable name, which is why I mentioned it. But I suppose if you were writing your own hardware specific compiler you could. But I'm guessing it's really just a way to reference it for discussion, rather than an actual variable name.

    edit - a few seconds and a search engine confirms my suspicions:
    Code:
    /*
    	Program:Lcd4b.C
    	Patchup file:Lcd4ba.c
    	Author: Vineet Kulkarni
    	Date:06-07-2000
    	Modified:07-07-2000
    	Language: C + assembly
    	LCD test program, to be used with Mini51 evaluation/prototyping board
    	LCD connections:
    		p2.0 to p2.3 are connected to 4 MSBits of LCD databus
    		p2.4 is connected to EN of LCD (1 means LCD selected)
    		p2.5 is connected to A0 of LCD (0 means command, 1 means data)
    		WR of LCD is connected to Ground, so we can never read LCD status
    
    	This program has functions to initialize LCD and display a
    	string on it.
    	Here the LCD is port driven and used in 4 bit interface.
    */
    They use the .# for reference, but the actual code does not:
    Code:
    void display_lcd(char arr[32]) {
    
    // displays first 16 characters in the string on line 1 and
    // remaining 16 characters on 2nd line
    // assumed 16 X 2 display
    	unsigned char count, tempchar;
    
    	lcd_cmd(0x80);
    	for(count=0;count<16;count++) { // first line
    		if(arr[count]) {
    			tempchar = arr[count] ;
    			lcd_dat(tempchar);
    		}
    		else return;
    	}
    	lcd_cmd(0xc0);		//addrs for second line
    	for(count=16;count<32;count++) {
    		if (arr[count]) {
    			tempchar = arr[count] ;
    			lcd_dat(tempchar);
    		}
    		else return;
    	}
    }

    Quzah.
    Last edited by quzah; 08-12-2011 at 04:53 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Thank you for the help so far

    I fiddled with making a C program for this that works on the computer. It works heh. But I do not know how to get it to work with 8051. The biggest problem I am having how do I get the contents of the X and Y variable to display on the LCD? here is the program

    Code:
    void main(void)
    {
    int P30; //first photo eye
    int P31; //second photo eye
    int x=0; //entyer count
    int y=0; //exit count
    do
    	{
    printf("\nwhich photo eye broke first? 1 or 2\n");
    scanf("%d", &P30);
    printf("\nwhich photo eye broke second\n");
    scanf("%d", &P31);
    
    //checking to see the order in which the photo eyes are broken the photo eyes will be tied to 
    //high so when they go low this will increment the right count based on which photo eye went
    //low first
    if(P30==1&&P31==2)
    			x++;
    		else if(P30==2&&P31==1)
    			y++;
    printf("\n%d people entered\n", x);
    printf("\n%d people exited\n", y);
    	} while(x>=0);
    }
    Last edited by ssotek; 08-15-2011 at 04:57 PM. Reason: FIX ERRORS

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssotek View Post
    I do not know how to get it to work with 8051. The biggest problem I am having how do I get the contents of the X and Y variable to display on the LCD?
    I assume that whoever gave you this assignment also gave you some instructions on the 8051. Go read them. Failing that, use the internet: "displaying on the 8051 lcd" + your search engine of choice = answers.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Nobody will be able to tell you exactly what's wrong (I would suspect you have a great number of problems with your code), since you don't provide us much useful info. We have no part number for the LCD. We have no schematic to see how the LCD is wired to the uC. We don't have any of the LCD code, so we don't know if you're talking to it correctly. There are timing issues to consider when writing to the LCD (you may need small delays between setting enable pins low and high). You can't safely check which one was triggered first with just if statements, you will need some sort of loop.

    You will need to approach this in several discreet steps. The first thing I recommend in your case is to get the LCD working. This takes a bit of time, but you need to do it anyhow, so get it done first and use the LCD for debugging purposes. Then, make sure you can read P3.0 and P3.1 correctly. Simply display their values on the LCD, 1 for high (beam intact), 0 for low (beam broken). Wire them up to some simple switches for testing (note that without a debounce circuit, you may get some transient bogus data on your LCD, but they should settle out quickly). Then work on the logic for detecting in which order the beams were interrupted and whether somebody entered or exited the library.

    The general idea is this:
    1. Wait for one of the two beams to go low
    2. Check which one is low
    3. Then, wait for the other beam to go low
    4. Record an entry or exit as appropriate
    5. Wait for both beams to go high again
    6. Repeat

    The waits should be some sort of loop, the repeat could be handled with a while(1).
    Adak, quzah and AndrewHunter like this.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This ^^^^^ is the way to go, imo.

    Take each step, code it up, and test it on the 8051. When you get it working right on the 8051, go to the next step. Just putting up code on the PC, without testing it on the controller board, is a rotten idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd/Even counter.
    By fyoung91 in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2010, 12:02 PM
  2. Replies: 1
    Last Post: 11-09-2009, 06:03 AM
  3. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  4. Replies: 19
    Last Post: 01-12-2006, 10:04 AM
  5. Could I create my own library to include in programs?
    By EliasK in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2003, 07:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21