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

Multitask on PIC16F84A

Discussion in 'Homework Help' started by Shen Yi, Dec 29, 2015.

  1. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    The CPU will execute two tasks in parallel. The first task will be a normal program execution loop which continuously tests an active-low switch connected to RB3. When the switch is pressed, an active-high LED connected to RB4 will turn on for 1 second (using software delay). Any switch presses while the LED is on will not be detected as the CPU is busy in the 1 second software delay. After the delay and the LED is turned off, CPU will return to testing the active-low switch. The second task will be to toggle an active-high LED connected to RA1 every 0.25 seconds. Timer0 will be configured to generate an interrupt every 0.25 seconds, and the code to toggle the LED will be placed in the ISR. Timing accuracy requirements is ±1% using 800 kHz RC oscillator.

    Then

    Modify the program above such that another active-low switch connected to RB0 will
    toggle an active-high LED connected to RB7 immediately when pressed, although the CPU is busy in the 1 second software delay at the moment.
    [Hint: Use the external interrupt capability on RB0/INT pin to detect this switch press. The interrupt should be on falling edge of RB0/INT pin. ]
     
  2. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,816
    Likes:
    121
    Location:
    Buenos Aires - Argentina
    Hola Shen,

    This is homework, right? Do not expect anyone doing it full for you.

    Could you show whatever you come up with up to now?

    I do not know how much you know about this but the first part seems quite straightforward. I did not check the second.

    Could you draw a flow diagram? That is what I would do anyway.

    That micro, BTW, is certainly a dated one. Who suggested it?
     
  3. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    i know how to do the 1st task but the 2nd task i dont know how to do it, and the extra part to modify the program i dont know how to do it too
     
  4. dave

    Dave New Member

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


     
  5. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0

    btw i have a few more questions that i dont know how to do it at all can you 'atferrari' help me look through it?(completely dont know how to code them)
     
  6. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,816
    Likes:
    121
    Location:
    Buenos Aires - Argentina
    Hola Shen again,

    Basically you did not replied to my questions in my previous posts. Do not waste your time (I will not mine either) in saying "I know" or "I do not know". Show answers and results, whatever they are.
     
  7. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    loop:
    btfss PORTB, 3
    goto loop
    movlw 1
    movwf PORTB,4
    call delay1s
    movlw 0
    movwf PORTB,4
    goto loop

    this is for the 1st task, but in the 1st task i'm not sure how to set it that ' Any switch presses while the LED is on will not be detected as the CPU is
    busy in the 1 second software delay'
    btw did i code it right?
    then the rest i don't know how to do it

    in the modify code part, i know how to write the code but not sure on how to allow it to still function in between the delay
     
  8. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,060
    Likes:
    520
    Location:
    Cleveland, OH, USA
    Without trying to analyze the question in detail, suffice it to say that any loop like you describe occupies the processor 100% of the time. The question wants parallel tasks to be performed. One way to do that is with interrupts. Thus, you can have a timer running independently of other tasks the MCU is doing. As just one simple example, TMR2 can be set to give 100 us interrupts. Between those interrupts, your processor can complete 20 steps (i.e., 800 kHz = 5 us/instruction cycle) doing something else.

    John

    Edit: The 16F84A does not have TMR2, but the concept just described is equally applicable to TMR0.
     
    Last edited: Dec 29, 2015
  9. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    sorry john but i am horrible at using interrupts would you mind helping me in showing how do i apply the interrupt to make sure it will function as a parallel task in my code as above? i have done interrupt before but dont understand your explanation on how to make it into parallel task code
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
  11. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,060
    Likes:
    520
    Location:
    Cleveland, OH, USA
    Read the pertinent parts of the datasheet. For the 16F84A you will have to do your own "context saving." That code is given in the datasheet. If not, there are many people here, including myself, who will give you that part of the code.

    John
     
  12. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    sry i need your help, dont know where to search for in my data sheet, and dont understand what do you mean by 'context saving'
    just a reminder i am using pic16f84a
     
  13. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,816
    Likes:
    121
    Location:
    Buenos Aires - Argentina
    The statement of work given says "software delay". On line delay to me.
     
  14. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,816
    Likes:
    121
    Location:
    Buenos Aires - Argentina
    Hola Shen

    Could you Google for the datasheet?
     
  15. Inquisitive

    Inquisitive Super Moderator

    Joined:
    Aug 31, 2011
    Messages:
    947
    Likes:
    87
    Location:
    Georgia, USA
  16. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,060
    Likes:
    520
    Location:
    Cleveland, OH, USA
    And, context saving can be found by using "ctrl+f" . Here, I have done it for you:
    upload_2015-12-29_13-21-19.png

    John
     
  17. House0Fwax

    House0Fwax Member

    Joined:
    Nov 15, 2006
    Messages:
    583
    Likes:
    17
    Location:
    Hampshire U.K.
    That's a bit clunky. You are using two lines where you could just use one. Try this, it reduces your 5 lines to 3;
    Code (text):

    BSF PORTB,4 ; This will 'set' the 4th bit of file  PORTB. IE, make it a '1'.  'B'it 'S'et 'F'ile
    call delay1s
    BCF PORTB,4 ; This will 'clear' the 4th bit of file  PORTB. IE, make it a '0'.  'B'it 'C'lear' F'ile
     
     
  18. Shen Yi

    Shen Yi New Member

    Joined:
    Dec 29, 2015
    Messages:
    15
    Likes:
    0
    thx for helping with the context saving but still confuse(dont understand what is it for after reading it) on how to use it in my codes, sry guys
    btw houseofwax thx didnt notice should have done that
     
  19. House0Fwax

    House0Fwax Member

    Joined:
    Nov 15, 2006
    Messages:
    583
    Likes:
    17
    Location:
    Hampshire U.K.
    Context saving. What that means is whilst the PIC is in an interrupt, certain 'variables' can change. So the first thing that happens during an interrupt is you save these 'variables' to temporary files. Then on completion of the interrupt, you move them back to where they were before.
    For example; 'movwf w_temp' and on exit from interrupt; 'Movf w_temp ,w'.
     

Share This Page