Maker Pro
Maker Pro

16F877 PIC problem

P

Pubudu

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
 
R

Roger Hamlett

Pubudu said:
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.
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
 
S

Spehro Pefhany

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


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
 
P

Pubudu

Spehro said:
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


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
 
J

John Jardine.

Pubudu said:
http://www.speff.com


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
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
 
P

Pubudu

John said:
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

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
 
I

Ignoramus24108

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)

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
 
I

Ignoramus24108

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.

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
 
P

Pubudu

Ignoramus24108 said:
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

Hi all,

I appreciate all your commnts. Thank you very much.

Pubudu.
 
Top