Need Urgent help! , Initialize ports on 8051 microcontroller !!

Discussion in 'Embedded Systems and Microcontrollers' started by nickatnite, Mar 31, 2009.

  1. nickatnite

    Thread Starter Member

    Mar 31, 2009
    I am a complete newbie to the site and to the world of microcontrollers, although I have taken assembly language class during college.
    I need some help immediately with initializing ports on a 8051 microcontroller.
    I am working at this electronics engineering company as a operations manger and I would like to step up to becoming an engineer ( that's what i graduated in ) - But unfortunately due to financial and other reasons I had to take this job , at this engineering company and have to work my way up . .

    My boss just gave me an assignment to "Initialize the ports on the 8051 microcontroller , using C++" AND I told him I can do it, but I have no idea how to do it .

    Can you help me please??
    I have the data sheet and everything ( )

    Someone already helped me with few lines of the code ..
    P0 = 0x0F ; write to port
    var = P0 ; reads the port into a char variable

    Pin0 = 1 ; set port 0 pin 0 bit to 1
    bit = Pin0 ; reads port 0 pin 0 into bit
    P0MDIN = 0xFF ; setting to 1's
    P0MDOUT = 0xFF ; push pull
    P0SKIP = 0xFF ; setting port 0 to skip
    XBR1 |= 0X40 ; setting xbare bit in XBR1 register

    I looked at the data sheet and it seemed to make sense to me..But could you help me finish it please? I am trying to move up within in the company..
    There are 3 ports in total -( p0, p1, p2 )
  2. nickatnite

    Thread Starter Member

    Mar 31, 2009
    anyone ? help ? It's pretty urgent . please.
  3. Papabravo


    Feb 24, 2006
    You have the basic idea. Each of the ports has an addressable register in the Special Function Register Space. Special Function Registers all have addresses in the range 0x80 to 0xFF and require a Direct Address. Register Addresses in the range 0x80 to 0xFF which are addressed indirectly go to RAM.
    Code ( (Unknown Language)):
    2. Register  Name  Address
    3. ----------------------
    4. Port 0     P0     0x80
    5. Port 1     P1     0x90
    6. Port 2     P2     0xA0
    7. Port 3     P3     0xB0
    You also need to understand the structure of an output pin. The technical name for the output pin circuit is "quasi bi-directional output port". In order to use a pin as an input you need to write a '1' to the output latch. When you do this the external circuit connected to the pin can pull it either high or low without drawing any significant current from the 8051 pin. When used as an output '0', you have an active pull-down transistor that can sink several milliamps of current. When used as an output '1' there is only a passive weak pullup that can source only a few tens of microamps in the high state.

    It is this fact which means that youy should prefer active low outputs to active high outputs.
  4. nickatnite

    Thread Starter Member

    Mar 31, 2009
    This is what i am trying to do ,
    I scanned and resized the schematics..
    using the silabs chip.

    Do you have the Configuration wizard ?
    also what is a cross bar ?

    How do I initialize the ports using this ??
  5. Papabravo


    Feb 24, 2006
    The schematic is almost unreadable.
    I have no idea what configuration wizard you are talking about.
    I have no idea what a cross bar is in this context.

    You write a '1' to each input pin, and write a '0' or a '1' to each output bit to establish your initial conditions.

  6. nickatnite

    Thread Starter Member

    Mar 31, 2009
    Hey Papabravo,

    This is what I wrote so far.

    void Port_IO_Init()
    P0MDIN = 0xFF; // assuming digital
    P0MDOUT = 0xFF;
    P1MDIN = 0x5F ; // assuming P1.5 and P1.7 are analog for VBATT monitoring.
    P1MDOUT = 0xE7 ; // assuming SPI to be (master) Push Pull
    P2MDOUT = 0x01; // only one bit used
    P0SKIP = 0x7F; // Skipping p0.0 - p0.6
    P1SKIP = 0x00;
    XBR0 = 0x36; // if a comparator is used to monitor VBatt.
    Do you think that's the correct Header file , which I am using?
    do I need to include this header also -- #include<reg51.h> or?
    I don't know the difference between the two.
    After this is done , i still need to initialize each pin separately if i am not mistaken , or it's not necessary. I am thinking I should . ?
    in the mainschematics(mainscan.jpg) I see a RST/C2CK ( for flash programming ) But it's not going to any ports , Why is that ?

    I am going to show my boss what I have so far ..
    I really appreciate any help you give me.
    It means a lot .Please let me know if I am going in the right direction or anything i should add or missed. Sorry that the schematic scans were not clear. I re-scanned them and here's the link to it.


    Mar 8, 2009
  8. Arm_n_Legs

    AAC Fanatic!

    Mar 7, 2007
    The cross bar in the silicon lab 8051 is used to map the logical I/O to the physical IOs.

    In the standard 8051, a physical pin is assigned to, say Txd.

    In the silab 8051, the Txd pin can be relocated to another pin. You have to look at the cross bar table in the datasheet to determine how the I/Os are assigned.
  9. Papabravo


    Feb 24, 2006
    In the 8051 architecture you can initalize the pins one at a time if you want to -- it may make your intentions crystal clear to anybody who reads your code. You can also initialize all the pins on a port with a single instruction.

    The cross bar sounds like a neat feature. I've seen it on the ARM7 TDMI, but never on an 8051.
  10. nickatnite

    Thread Starter Member

    Mar 31, 2009

    k. yea , the feature is neat..thx.