help on 8051 project

Discussion in 'Embedded Systems and Microcontrollers' started by seahcy, Dec 11, 2007.

  1. seahcy

    Thread Starter New Member

    Dec 11, 2007
    5
    0
    Hi
    I am new at C programming. Can you please give me the
    code for writing a program using a 4x3 keypad with a 74c922 encoder
    chip in C? I know that when your DA pin is set high, it tells the
    program that data is available, but how do you tell the code to go
    fetch the data? When the DA pin goes high, it must trigger an external interrupt(INT0) and then it must detect whick key was pressed according the encoder chip.

    Any help would be greatly appreciated!

    Regards and Thanks:D
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,177
    1,799
    The DA(Data Available) pin is connected to an 8051 input. When it goes to the proper state. You can read the data from Data Out A through Data Out E. You need to either wire the Output Enable bar to ground or better yet connect it to an 8051 output and take it low before reading the Data Out pins, then take it high again. This way you can share several devices on the same pins.

    Remember Port 0 has open drain outputs requiring an external pullup resistor if they are used as outputs. The remaining ports 1, 2 and 3 are quasi bi directional. They have waek pullups and can sink way more current than they can source.
     
  3. seahcy

    Thread Starter New Member

    Dec 11, 2007
    5
    0
    Thanks a lot.. But how do i program it? As i am quite new to c programming.. i am using P0.2 for DA and P2.4 to P2.7 for Data out A to Data out D. hw does it capture which key has been pressed?:confused:
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,177
    1,799
    You treat the port bits and other SFRs(special function registers) as if they were memory locations. Now each C compiler handles things a bit differently so you will have to find examples in the compiler's documentation.

    Code ( (Unknown Language)):
    1.  
    2. #define  DA   P1.4    // DA is bit 4 of port 1
    3. #define  OE   P1.0    // OE is bit 0 of port 1
    4. #define  KY   P2      // KY is the keyboard input on bits P2.4 thru P2.0
    5.  
    6. unsigned char getkey(void)
    7. {
    8. unsigned char key ;
    9.  
    10.      if (DA == 0) return '\xFF';  // return out of range value if no key
    11.      OE = 0 ;
    12.      key = KY ;
    13.      OE = 1 ;
    14.      return key ;
    15. }
    16.  
    Be sure to find the compiler option that will generate a listing file so you can check that the compiler generated the correct code. There really is no substitute for this step when you are learning your way around a new processor.
     
  5. mrmeval

    Distinguished Member

    Jun 30, 2006
    833
    2
    I think the key switches themselves don't need pullups. Nice chip BTW it's pretty simple to use.
     
  6. seahcy

    Thread Starter New Member

    Dec 11, 2007
    5
    0
    Thanks for the help.:)

    Hmm.. just to ask.. is there any links to learning c programming to program 8051? or learn hw to apply the codes for 8051?
     
  7. mrmeval

    Distinguished Member

    Jun 30, 2006
    833
    2
  8. seahcy

    Thread Starter New Member

    Dec 11, 2007
    5
    0
    Thank for the info.

    just to ask..
    hw should i go about using timer to do a scan for the 4x3 keyboard every 100ms and what is the code for it?

    Any help would be greatly appreciated!

    Regards and Thanks:)
     
  9. mrmeval

    Distinguished Member

    Jun 30, 2006
    833
    2
    The 74c922 does all that, read page 8 of the datasheet. http://www.octopart.com will lead you to the datasheet. When the data available pin goes high read the data on the encoded outputs (page 2). There are several examples and several more you can find online.

    As to code? You'll have to start studying some language that is compatible with that microcontroller that offers you what you want to do. Or learn several.
     
  10. seahcy

    Thread Starter New Member

    Dec 11, 2007
    5
    0
    Thanks, mrmeval :)

    Just to ask.. how to write the program for the LED to blink 3 times using c language? do i need to declare the variable in the global function?

    Any help would be greatly appreciated!:D
     
Loading...