16F877 PIC problem

Hi,

I am a beginner with PIC projects. I am using 16F877A with a 20 MHz oscillator. This is a example of what I want to do. suppose there are two activities.

  1. Blink a LED once a second.
  2. Read the status of a line continously. (I dont want to use interrupts right now)

E.g.

While (1) {

x := Read_input(RA.0) if x = 1 then { .................. ................. } else { ..................... ................. } Write_output(RB.1,1); Delay_ms(500); Write_output(RB.1,0); Delay_ms(500);

}

This process is controlled by the dealy loop. Thus it is not reading the read line continuously. How can I do this ?. I need to run those two indipendantly.

In desktop programming we use something called threads. Is there any possibility to use such thing here ? Please let me know.

Pubudu

Reply to
Pubudu
Loading thread data ...

Basically, you need to use interrupts. Threads are generated, by having a 'state', which is changed at a regular interval (by the interrupt), and then in your main code, executing different routines, according to the value of 'state'. At heart, this is what is happening in your PC... There has to be some form of timer, to generate the switching, and the easiest way to do this is an interrupt.

Best Wishes

Reply to
Roger Hamlett

You can either use interrupts, or write your own delay function that regularly polls your input. You could use a hardware timer or make it isochronous or live with some error in the 500msec if it is not critical.

It's up to you to evaluate the best way, and to make sure that the worst-case interval between polls of the input is short enough that nothing will get missed.

All small microcomputers can do only *one* thing at a time, it's only an illusion (or abstraction) that threads execute simultaneously.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
 Click to see the full signature
Reply to
Spehro Pefhany

Hi Guys,

Thanks for all. I thought there might be other methods than interrupts and it is now confirmed nothing other than using interupts. I will use interrupts.

Pubudu

Reply to
Pubudu

reward"

formatting link

formatting link

Interupts are the way to go but not absolutely vital. Can't you just integrate the pin reading as an intrinsic part of the delay routine?. john

Reply to
John Jardine.

Hi,

It is possible but it is not all the time. If I doo it that way my things get really complicated. I ready tried with Interrupts and working fine.

Pubudu

Reply to
Pubudu

This is a basic programming 201 question that has precious little with embedded stuff as such. You need to use a state machine and keep track of how long your LED is lit or unlit, if that time is exceeded, change the LED state from unlit to lit or vice versa and keep going.

The need for threads is vastly overstated, especially on single CPU machines.

i

Reply to
Ignoramus24108

You can and should do it without interrupts and without threads. Do not use sleep for timing real time stuff, it will make things impossible. See my other post about state machine. I will post more soon.

i
Reply to
Ignoramus24108

Hi all,

I appreciate all your commnts. Thank you very much.

Pubudu.

Reply to
Pubudu

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.