Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] devce deaign using polling for switch scann seems useless "waching machine "

Status
Not open for further replies.

libyantiger

Member level 5
Joined
Aug 10, 2016
Messages
90
Helped
0
Reputation
0
Reaction score
3
Trophy points
1,288
Activity points
2,269
devce deaign using polling for switch scann seems useless "waching machine "

Code:
Btfsc    port b,0
Goto   job          /   washer  

Btfsc  portb ,1
Goto  second  2thjob    /  spinner

If I wanted to design for example washing machine where the 0 button is flush ..second is drying spinner

It seems that pulling is of no value …by the time we reach goto job and start
The motor and water inside the machine we have to make some sort of delay so
That the machine stays in this condition till spectfic time

After that it go to drying automatically

But if the user press buttom while the machine is doing the wash action

the machine wont respond because is still busy making first job and it never reach to the line btfsc portb,1

i mean it is making job that is too long so the scanning action is now disabled !

If wanted to make the job using a pic16f84 which have only one pin of interrupt it seems that the waching machine turn into a “mission impossible “ sort of thing


Any hints?!
 
Last edited by a moderator:

Re: devce deaign using polling for switch scann seems useless "waching machine

Providing a snippet of the code in addition to the confusing information as given is almost useless; it seems to be just the entry point of each routine, and nothing else. Another detail is that once being programmed in assembly language, it is expected some lack of structuring in the program which is by itself a discouragement to assess what comes next. Anyway, one could guess with no risk of being wrong that your code is not subdivided into functions dedicated to specific tasks, such as oftentimes reading pressed buttons, as well as not implementing a state machine which would help you decide which parts of the program that should run and when. In short, I'd be surprised if someone can understand your problem and give an applicable solution. Take time to elaborate the question more clearly explained.
 

Re: devce deaign using polling for switch scann seems useless "waching machine

ok here i made things more clearer i am talking about the unabilities of polling methods to address problem where perferming job takes alot of time here is simple healthy and strong code of scanning keys and turning on ports it quickly scans and turn on or off ports accoding to switch state and quickly goes to other key to examin if it pressed or not and again and gain

Switch 1 Btfss port b,1
Goto job ,,key is pressed
Pcf,porta,0 key is not pressed

Switch 2 Btfss portb ,0
Goto second 2thjob key pressed
Pcf porta,1 key not pressed
Go to switch 1

job
Bsf port a,0
Goto switch 2


2thjob
Bsf porta,1
Goto swhich1



ths will work ok quickly scans and send command to ports to turn led on or off but if we need process that takes time ...for example you dont have to trun led briefly ....you have to turn it for 30 minutes

this code wont do because by the time you say make bsf porta and try to make dealy for led to stay for 30 miutes you wont reach to the line goto switch2 you stuck in that delay comand that now your mcu cant accepts commands any more ....is there any way around it !
 

Re: devce deaign using polling for switch scann seems useless "waching machine

One strategy is to set a variable which tells what action the machine is currently doing (Call the variable Current_action). All routines of your program make decisions based on its value. Its value never changes until you detect that the action is finished.

Then you set 'Current_Action to the value of whatever action is 'coming down the chute' (that is, the latest button-press). Your program logic must decide whether certain button-presses override others.
 

Re: devce deaign using polling for switch scann seems useless "waching machine

Hi,

Here is where interrupts come into account.
I personally do just the IO in the ISR...and process all data - timed - in the main loop as a dedicated control loop..

ISR:
I set up a timer to run ans ISR maybe every 10ms.
Here i just output all calculated values and read the inputs.
And set a flag to show the main loop that new data is available.

Main loop:
* just does fast jobs... that surely take much less than 10ms
* polls the flag ... if set: clears the fkag and starts the control_job

Control loop: (runs every 10ms = exactly 100 times a second but with some jitter)
(Example: blinking LED1 with 1Hz and at the same time blinking LED2 with 2Hz
For 1Hz blink the LED needs to be 500ms ON and 500ms OFF
500ms = 50 × 10ms)
Decrement LEDcounter1; if 0 then toggle LED1 and set LEDcounter = 50)
Decrement LEDcounter2; if 0 then toggle LED2 and set LEDcounter = 25)
... do other jobs
(The values for LED1 and LED2 are not output in main loop, just prepared to be output in the ISR
Thus you get LED timings with jitter below 1uS....While the main loop may have jitter in the range of milliseconds)

Mind:
Although there is no busy_wait you can blink many LEDs "at the same time" with exact timing.
And because there are no busy_waits ... you have plenty of processing power available.
You can do motor control, UART communication, key press processing, disply control and many other jobs "at the same time"

*****
Many people think this is complicated ... until they tried it the first time ... then they will use it from this day on

You have just to divide your software in three major sections.
* Init ( you already should have, but here you need to add about 4 instructions to set up the interrupt)
* ISR just for I/O handling and setting the flag
* main loop (you already should have, but its way smaller now)
* control_loop ( a very fast, clear and straight forward processing function)

Klaus
 

Re: devce deaign using polling for switch scann seems useless "waching machine

Klaus is quite right about ISRs. They are incredibly useful and not at all complicated but many people are afraid of them. As he says, once you have tried them you never look back.

Also consider 'calling' the individual routines instead of 'goto' them. A 'goto' changes the course of the program permanently but a call allows you to resume when your routine has finished, it makes a loop of switch checks much simpler. In each of the individual routines you start with "am I busy? if yes just return" so there is no hold up while you wait for something to finish.

Brian.
 

Re: devce deaign using polling for switch scann seems useless "waching machine

One strategy is to set a variable which tells what action the machine is currently doing (Call the variable Current_action). All routines of your program make decisions based on its value. Its value never changes until you detect that the action is finished.

Then you set 'Current_Action to the value of whatever action is 'coming down the chute' (that is, the latest button-press). Your program logic must decide whether certain button-presses override others.


nice idea man ...would please but some simple generic code that explains the principle .thanks

- - - Updated - - -

Klaus is quite right about ISRs. They are incredibly useful and not at all complicated but many people are afraid of them. As he says, once you have tried them you never look back.

Also consider 'calling' the individual routines instead of 'goto' them. A 'goto' changes the course of the program permanently but a call allows you to resume when your routine has finished, it makes a loop of switch checks much simpler. In each of the individual routines you start with "am I busy? if yes just return" so there is no hold up while you wait for something to finish.

Brian.

hi brian i dont think it is possible using pic 16f84 cause it has one pin for external interrupts
 

Re: devce deaign using polling for switch scann seems useless "waching machine

Hi,

hi brian i dont think it is possible using pic 16f84 cause it has one pin for external interrupts
For sure this PIC can do this.
I can´t remember a single microcontroller that can not do this.

It just needs one quick view on the first page of the datasheet:
• Four interrupt sources:
- External RB0/INT pin
- TMR0 timer overflow
- PORTB<7:4> interrupt-on-change
- Data EEPROM write complete


from post#5:
ISR:
I set up a timer to run an ISR maybe every 10ms.

Klaus
 

Re: devce deaign using polling for switch scann seems useless &amp;quot;waching machine

Hi,

Here is where interrupts come into account.
I personally do just the IO in the ISR...and process all data - timed - in the main loop as a dedicated control loop..

ISR:
I set up a timer to run ans ISR maybe every 10ms.
Here i just output all calculated values and read the inputs.
And set a flag to show the main loop that new data is available.

Main loop:
* just does fast jobs... that surely take much less than 10ms
* polls the flag ... if set: clears the fkag and starts the control_job

Control loop: (runs every 10ms = exactly 100 times a second but with some jitter)
(Example: blinking LED1 with 1Hz and at the same time blinking LED2 with 2Hz
For 1Hz blink the LED needs to be 500ms ON and 500ms OFF
500ms = 50 × 10ms)
Decrement LEDcounter1; if 0 then toggle LED1 and set LEDcounter = 50)
Decrement LEDcounter2; if 0 then toggle LED2 and set LEDcounter = 25)
... do other jobs
(The values for LED1 and LED2 are not output in main loop, just prepared to be output in the ISR
Thus you get LED timings with jitter below 1uS....While the main loop may have jitter in the range of milliseconds)

Mind:
Although there is no busy_wait you can blink many LEDs "at the same time" with exact timing.
And because there are no busy_waits ... you have plenty of processing power available.
You can do motor control, UART communication, key press processing, disply control and many other jobs "at the same time"

*****
Many people think this is complicated ... until they tried it the first time ... then they will use it from this day on

You have just to divide your software in three major sections.
* Init ( you already should have, but here you need to add about 4 instructions to set up the interrupt)
* ISR just for I/O handling and setting the flag
* main loop (you already should have, but its way smaller now)
* control_loop ( a very fast, clear and straight forward processing function)

Klaus




looks tempting hehehe but with pic16f84 only one external interrupt pin .....all i want is that each button press be responded to and served immedately by the mcu

most of the buttons have contradicting actions that is why i have to made them using one mcu ...if they dont contradicting actions i would have gone by using 2 mcus


my main idea is that when buttom of wash is pressed the mcu does the following

open water outlet to start from 0 water level wait one minute
close water out let
open water inlet

sense the water have fillled to specific level

close water inlet

start spinning motor for wash cycle


do that for 1 hour

after that open the water outlet

wait 1 mintue

start the sping action for drying the clothers




second buttom i called low level wash it is same as first button yet it have "new water level" it is useful when you dont have many clother to wash

note that all wash button start by open the out let for one minute so that water sensing start from 0 water

3 button i use as dryer button

it immediately open the water out let wait for 1 minute

start spining clother basket for 1 hour to finish the dryer action

- - - Updated - - -

Hi,


For sure this PIC can do this.
I can´t remember a single microcontroller that can not do this.

It just needs one quick view on the first page of the datasheet:
• Four interrupt sources:
- External RB0/INT pin
- TMR0 timer overflow
- PORTB<7:4> interrupt-on-change
- Data EEPROM write complete


from post#5:


Klaus



got 3 buttons in my design so interrupt for each one of them can be done using the pic16f84? and shoud i use interrupt for each button?

- - - Updated - - -

Hi,

Here is where interrupts come into account.
I personally do just the IO in the ISR...and process all data - timed - in the main loop as a dedicated control loop..

ISR:
I set up a timer to run ans ISR maybe every 10ms.
Here i just output all calculated values and read the inputs.
And set a flag to show the main loop that new data is available.

Main loop:
* just does fast jobs... that surely take much less than 10ms
* polls the flag ... if set: clears the fkag and starts the control_job

Control loop: (runs every 10ms = exactly 100 times a second but with some jitter)
(Example: blinking LED1 with 1Hz and at the same time blinking LED2 with 2Hz
For 1Hz blink the LED needs to be 500ms ON and 500ms OFF
500ms = 50 × 10ms)
Decrement LEDcounter1; if 0 then toggle LED1 and set LEDcounter = 50)
Decrement LEDcounter2; if 0 then toggle LED2 and set LEDcounter = 25)
... do other jobs
(The values for LED1 and LED2 are not output in main loop, just prepared to be output in the ISR
Thus you get LED timings with jitter below 1uS....While the main loop may have jitter in the range of milliseconds)

Mind:
Although there is no busy_wait you can blink many LEDs "at the same time" with exact timing.
And because there are no busy_waits ... you have plenty of processing power available.
You can do motor control, UART communication, key press processing, disply control and many other jobs "at the same time"

*****
Many people think this is complicated ... until they tried it the first time ... then they will use it from this day on

You have just to divide your software in three major sections.
* Init ( you already should have, but here you need to add about 4 instructions to set up the interrupt)
* ISR just for I/O handling and setting the flag
* main loop (you already should have, but its way smaller now)
* control_loop ( a very fast, clear and straight forward processing function)

Klaus



flash of a lamp inside my mind!!!!!


looks like you are not using the interrupt to process the buton press but rather you use timer that cuase interrupt after some small imterval

the idea of which is that it looks for button change every timers overflow interval thus we have mechanism by which we look for


button state change .....and for that even the pic16f84 is useful it have timer over flow interupt simply briliant


would you please post code that tell me how to do it on pic16f84 just for me to grasp the idea more and more


briliant!
 

Re: devce deaign using polling for switch scann seems useless "waching machine

This sounds like an application for a timer interrupt triggering a matrix key scan. You can (theoretically) scan 40 switches (8 x 5) with just one interrupt although that would use up all the pins.

Think of your program like this:

1. a timer (TMR0) generates an interrupt every 50mS. (20 times per second)
2. the ISR counts the number of times it is entered (clue: a count of 20 means 1 second has elapsed)
3. the same ISR starts a scan of your switches and reads back whether they are pressed or not.
4. the ISR ends

Now you have a periodic scan of several switches and an exact timing routine that can easily be scaled to one minute.

Brian.
 

Re: devce deaign using polling for switch scann seems useless "waching machine

The replies to this thread (except this one) are the best advice the OP will ever receive on the subject.
 

Re: devce deaign using polling for switch scann seems useless "waching machine

Hi,

flash of a lamp inside my mind!!!!!
looks like you are not using the interrupt to process the buton press but rather you use timer that cuase interrupt after some small imterval
the idea of which is that it looks for button change every timers overflow interval thus we have mechanism by which we look for
button state change .....and for that even the pic16f84 is useful it have timer over flow interupt simply briliant
would you please post code that tell me how to do it on pic16f84 just for me to grasp the idea more and more
briliant!
You´ve got it.

I can´t give you code, because I don´t work with PICs. (But I should be able to read code, especially when documented)

* you will find many examples how to set up the timer to generate an interrupt every 10ms (or Brian´s 50ms).... Maybe 3 instructions maybe 5 ..
* within the ISR you just have to: * write outputs, * read inputs, * set a flag .... in best case 3 instructions, maybe 6

Klaus
 

Re: devce deaign using polling for switch scann seems useless "waching machine

The replies to this thread (except this one) are the best advice the OP will ever receive on the subject.

thanks sir yes we learned much of stuffs ...it is all about ideas i spent 1 day thinking of AWAY to do it in normal polling

but it was complicated and not easy now the main idea is clear ...but the exacet code and details still need some work !
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top