1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

I am having problem to write assembly program for 8051

Discussion in '8051/8951' started by Parth86, Dec 17, 2013.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    512
    Likes:
    5
    I want to ask questions the device like switch , led, motor they have two terminal (positive terminal and negative terminal ) when we interface with controller why we use only single pin like we say switch is connected PO.1 or led is connected P1.1

    I have simple task I have one switch and one motor. the positive edge of switch is connected to port pin PO.1 and negative edge is connected to port pin PO.2 and motor is connected to output the positive edge of motor is connected to port pin P1.1 and negative edge is connected to P1.2

    if switch is on then motor is on
    if switch is of then motor is of

    I am having problem to write code someone please give me idea
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,157
    Likes:
    907
    Location:
    Rochdale UK
    Is this a real scenario? because you can't connect the motor directly to the micro... I say this because the motor will be driven through a transistor of some kind!!

    Pseudo code
    Test switch
    If switch is high
    switch motor on​
    else
    switch motor off​
    goto beginning

    Test
    JB or JNB ( Jump if Bit is on) (Jump if Bit is Not on)
    Code (asm):
    JB P0.1, ON
    Motor on or off
    SETB or CLR
    Code (asm):

    ON:
       SETB P1.1
       CLR  P1.2
    OFF:
       CLR P1.1
       CLR  P1.2
     
     
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    512
    Likes:
    5
    I think we need to give instruction for every pin if we use
    please check out me If i wrong

    org 00000 h
    on:
    SETB P1.1 ,positive ; motor on
    SETB P1.2 ,negative ; motor on
    off:
    CLR P1.1 , positive ; motor off
    CLR P1.2 negative ; motor off
    loop:
    JB Po.1 ; jump if bit is on
    sjmp loop
    End
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,157
    Likes:
    907
    Location:
    Rochdale UK

    You wouldn't write the code like this as it will be very messy

    Code (asm):

       org 00000 h
    on:
       SETB P1.1 ,positive ; motor on
       SETB P1.2 ,negative ; motor on
    off:
       CLR P1.1 , positive ; motor off
       CLR P1.2 negative ; motor off
    loop:
       JB Po.1 ; jump if bit is on
       sjmp loop
    End
     
    You would give it a bit of structure like so
    Code (asm):

        org    0              ; You need a reset vector
        sjmp    main    ; At least 0x30 to clear vector space

        org 0x30
    main:                ; start here
        jb    P0.1,on
        jnb    P0.1,OFF
        sjmp    main

    on:    setb    P1.0      ; On
        clr    P1.1
        sjmp    main

    off:    clr    P1.0       ; Off
        clr    P1.1
        sjmp    main
        end
     
     
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    512
    Likes:
    5
    ON:
    SETB P1.1
    CLR P1.2
    you wrote this in your code what does it mean i think if we set bit it means motor is on but if we clear it means motor is off please clear my confusen
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,157
    Likes:
    907
    Location:
    Rochdale UK
    If you have the motor on two pins P1.1 and P1.2, then one pin needs to be high and one needs to be low..

    motor.png
    Or are you thinking differently!!!

    P.S. I know you can't attach a motor like this or the chip wouldn't last very long... It's for visual reference.
     
  8. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    512
    Likes:
    5
    why we can't directly interface motor to controller
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,157
    Likes:
    907
    Location:
    Rochdale UK
    Motors are very noisy... And they take more current than the humble micro can deliver!!! You need to put relays, catch diodes and smoothing caps so you don't blow the micro up!!
     
  10. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    859
    Likes:
    82
    Location:
    Fresno, CA
    For the same reason a human requires a jack or a hydraulic lift to lift a car.

    Micros are not designed to source load current...ESPECIALLY not an 8051. The 8051 is not "push-pull" output. It uses a weak pull up/strong pull down configuration.

    On the MCS-51 family of microcontrollers (i.e. all 8051/8052 based microcontrollers), with the exception of non-legacy port equipped variants, a pin's source current (i.e. electron current passed from the pin to Vcc when the pin is set high) is very low relative to a pin's sink current (i.e. electron current passed from GND to the pin when the pin is set low). But even the rated sink current is no greater than 15mA per pin, and you cannot sink 15mA on every pin simultaneously.

    Aside from the current demand of a motor, motors are an inductive load. There will be counter voltages as the field expands/collapses, and micros are not designed to deal with this. Even relays are not directly driven from a micro's I/O pins for this very reason.

    Micros are designed to provide high and low voltage signals used to drive high current switching devices such as bipolar transistors, MOSFET's, etc etc. Transistors require very little current to switch them on/off (which the micro can more than source) while they can source/sink LOTS of current on their collectors/drains for driving high current loads. On a standard bipolar transistor, the collector junction would pass the motor current while the micro would simply provide the base current, which is very low in comparison to the motor current. A darlington pair could sink 10+ amps on its collector junction while only requiring maybe 5mA of base current to saturate the base, which a micro can more than deal with.
     
    Last edited: Dec 27, 2013
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,157
    Likes:
    907
    Location:
    Rochdale UK
    Welcome back Jon!!!
    Happy new year!
     
  12. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    859
    Likes:
    82
    Location:
    Fresno, CA
    Likewise Ian. Been super busy with the little one since earlier this year but now that he's learned to crawl and now learning to stand, he's on his way to autonomy, which frees up my time a bit.

    So I'm back here on good ol' ETO. Eventually I'll be back to typing up my tutorial articles, which I highly enjoy doing.
     
    • Like Like x 2

Share This Page