Continue to Site

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.

  • 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.

Multitask on PIC16F84A

Status
Not open for further replies.

Shen Yi

New Member
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. ]
 
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?
 
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
 
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)
 
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.
 
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
 
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:
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
 
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
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
 
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
 
The statement of work given says "software delay". On line delay to me.
 
Hola Shen

Could you Google for the datasheet?
 
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
 
movlw 1
movwf PORTB,4
call delay1s
movlw 0
movwf PORTB,4
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:
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
 
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
 
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'.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top