PDA

View Full Version : Input



ECaro03
- 1st December 2004, 05:34
I am a beginner at this, so bare with me please.

I am trying to write code that when I receive an input of roughly around 5 volts into porta.0 in a PIC16F684 that it will blink an LED.

this is what I got:

define osc 4

trisa = %11111111
trisc = %00000000

portc = %00000000

left var byte
left = 0

loop:
left = porta.0
if left = 1 then
portc.0 = 1
pause 2500
portc.0 = 0
endif
goto loop

end

Please help me correct the problem.

mister_e
- 1st December 2004, 06:24
welcome to the forum ECaro03!

Your code is not to far but you must know the followings...

1. PIC16F684 use several oscillator config. Default setting is suppose to be 4MHZ. but it's safe to use this line at the top of your code.

OSCCON=%01100000 'define internal oscillator to 4MHZ


2. you must turn off a/d converter to read digital signal from PORTA by using

ANSEL=0 'turn off a/d


3. you also must turn off analog comparator by using

CMCON0=%00000111


now i sugest the following modify code.



OSCCON=%01100000 'define internal oscillator to 4MHZ
ANSEL=0 'turn off a/d
CMCON0=%00000111

trisa = %11111111
trisc = %00000000

portc = %00000000

left VAR PORTA.0 'define left as PORTA.0 status


loop:
if left then
portc.0 = 1
pause 2500
portc.0 = 0
endif

goto loop


what about now?

ECaro03
- 1st December 2004, 06:44
Thanks for your help...It's getting late here so I will try it first thing tomorrow morning and let you know how it goes.

ECaro03
- 1st December 2004, 16:30
Thank you for your help with the code. I went a little further and added a right to my code to go along with my left, so now I have two LEDs.

I tried the following code:
-------------------------------------------------------------------
osccon = %01100000
ansel = 0
cmcon0 = %00000111

trisa = %11111111
trisc = %00000000

portc = %00000000

left var porta.0
right var porta.1

loop:
if left then
portc.0 = 1
pause 2500
portc.0 = 0
porta.0 = 0
left = 0
endif

if right then
portc.1 = 1
pause 2500
portc.1 = 0
porta.1 = 0
right = 0
endif

portc.3 = 1
pause 120
portc.3 = 0
pause 120

goto loop

end
-------------------------------------------------------------------

The Result:
when porta.0 is triggered, portc.0 turns on 5 times

when porta.1 is triggered, portc.0 turns on 1 and then portc.1 turns on 4 times

Questions:
1. why is it doing that? I just want it to light up once.

2. is there a way I can have two ports light up at the same time? (i.e. porta.0 and porta.2 turn on at the same time if I add more LEDs) what I get when that happens is only one turns on at a time.

I look forward to your reply and thanks for your help again.

ECaro03
- 1st December 2004, 16:51
the portc.3 part is just flashing another led so I know when it is going through the loop

mister_e
- 1st December 2004, 20:55
The Result:
when porta.0 is triggered, portc.0 turns on 5 times

when porta.1 is triggered, portc.0 turns on 1 and then portc.1 turns on 4 times

Questions:
1. why is it doing that? I just want it to light up once.


what do the trigger??? push button, jumper ? The reason is because you have to wait untill the trigger is release. so the following will resolve the problem.




osccon = %01100000
ansel = 0
cmcon0 = %00000111

trisa = %11111111
trisc = %00000000
portc = %00000000

left var porta.0
right var porta.1

loop:
if left then
portc.0 = 1
While left 'wait until left is release to 0V
Wend
pause 2500
portc.0 = 0
porta.0 = 0
endif

if right then
portc.1 = 1
While right 'wait untill right is release to 0V
Wend
pause 2500
portc.1 = 0
porta.1 = 0
endif

portc.3 = 1
pause 120
portc.3 = 0
pause 120

goto loop




2. is there a way I can have two ports light up at the same time? (i.e. porta.0 and porta.2 turn on at the same time


Sure...

let's say you want to turn Porta.0 and Porta.2 at the same time
PORTA=%00000101

that's it

ECaro03
- 1st December 2004, 21:33
The triggering mechanism is a motion sensor. It sends a 5v trigger to the microchip when it is activated.

What is happening with the current code that you just sent me is that when the left sensor sends a pulse to the microchip, the LED at portc.0 turns on for about 8 seconds and then shuts off. When the right sensor sends a pulse to the microchip, the LED at portc.0 flashes, then the LED at portc.1 turns on for about 8 seconds and then the LEDs flash ramdomly a couple times.

Is there a way to reduce the eight second delay? I tried shortening the pause, but that did not have much effect.

The plan is to replace the LEDs and have them lead to an H-bridge that controls a motor. Basically a type of camera tracking system.

mister_e
- 1st December 2004, 21:40
which motion sensor are you using?

what about now if you remove those lines

portc.3 = 1
pause 120
portc.3 = 0
pause 120

is this motion sensor is a rotary encoder??? can you add an simple pulldown resistor on input pin let's say 10K or lower???

let me know
¸
regards

ECaro03
- 1st December 2004, 22:47
it's a PIR sensor.

after playing around with the microchip....it seems that the input channel is super sensitive.

when I tried putting in the resistor with my hand it activated the code

what's going on there?

mister_e
- 2nd December 2004, 01:36
it's because you PIC input is floating when there's no 5volts from PIR at the input. You need to tie it to ground with resistor. This way PIC will see high level and low level(produce by resistor).

BobK
- 2nd December 2004, 01:38
Hi ECaro03,

When a motion sensor is triggered it must wait until the movement it has detected has gone and the logic circuits stabilize and return the output to an off state. This would probably explain why you circuit (LED) remains on for the eight seconds.

The micro triggering from you putting the resistor in place could be static from you body or perhaps you put the resistor to +v instead of ground.

BobK

BobK
- 2nd December 2004, 01:39
Hi ECaro03,

When a motion sensor is triggered it must wait until the movement it has detected has gone and the logic circuits stabilize and return the output to an off state. This would probably explain why your circuit (LED) remains on for the eight seconds.

The micro triggering from you putting the resistor in place could be static from you body or perhaps you put the resistor to +v instead of ground.

BobK

mister_e
- 2nd December 2004, 02:19
i do not agree with the resistor to V+.... trigger works when activated... we detect high level... so in this case when there's no trigger PIR is floating, resistor must be put to ground.

AND the above code i post is already waiting for trigger release.

regards :)

Bruce
- 2nd December 2004, 02:26
If you're using (left var porta.0), and have this pin set to an input, why are you trying to clear the pin with (porta.0 = 0)..?

Just let your sensor handle the logic level transitions.

Some sensors have a certain amount of hysteresis so it may take a while for the sensor to initially trigger or for the output to settle & return to the idle state once the trigger condition is removed.

The PIC input pin will see a logic 1 until the sensor outputs falls below the PIC +V input threshold level.

If you need to speed up the process of returning to the idle state, you can probably add a 10K or more pull-down resistor to help discharge the sensors output, but I would check the datasheet for the sensor you're using first. If the sensors output doesn't hold the PIC input at ground when idle, you'll need the pull-down anyhow.

Using mister_e's WHILE WEND suggestion works really well, but the sensor output needs to fall below the PIC +V input threshold before it will exit the WHILE WEND. That could explain the delay.

ECaro03
- 2nd December 2004, 02:28
I took out the sensor all together and now I am just manually plugging in 5v. I touch the pin for a split second and I am still having problems.

I feel as if I am missing something.

The pin is so sensitive that I can touch a wire to it and it senses input. Figure that one out...I sure don't know. Since digital 1 is 5v.

And the spazzing out of the LEDs is confusing me as well. Why when the right = 1 it runs through the if statement of the left once then goes through the right if statement.

mister_e
- 2nd December 2004, 02:30
Thanks Bruce, simple suggestion here to avoid long delay from PIR

place RC circuit to produce short pulse from PIR output.


if you do it manually... also put pull down resistor.... it's working on my bench here :)

Bruce
- 2nd December 2004, 03:39
Since digital 1 is 5v.

It doesn't necessarily take 5V on an input to register as a logic 1. Only a +V above the PIC logic 1 input threshold level.

If you touch an input pin or wire connected to the input, depending on the voltage level present on the input pin, your environment, body capacitance, static charge, etc,, you may be introducing a signal of sufficient magnitude to allow the input to see a certain voltage level or a brief change in voltage level.

Try something like this;

10K
input pin -------/\/\/\/\-----+5V
|
|
|---o___o---- gnd
switch

If you leave the input floating, then touch the wire or pin, it will definitely spazz-out.

If you hold the input at a certain logic level through a pull-up or pull-down, then the signal or discharge from touching the pin will need to be of sufficient magnitude to over-ride the pull-up or pull-down for the input to see the change.

Even a very fast spike on an input may register since the PIC is operating (and testing input logic) very fast. Even at a lowly 4MHz your PIC is executing single-cycle instructions at 1 million per second.

If you connect a sensor to an input, and the sensor has a high impedance output, it may be allowing the PIC input to float when the sensor output is not actively pulling the PIC input to ground or +V.

ECaro03
- 2nd December 2004, 06:41
Thanks guys for all your help.

I looked over my circuit again and saw that I wasn't using the pull down resistor properly so it was left floating....ugh I feel so stupid now...lol

Anyways I appreciate your help.

ONE LAST THING.....

I am trying to have multiple ports on at the same time....

portc.2 to turn on along with portc.0
portc.3 to turn on along with portc.1

what's the code for that? and can you explain the binary code with it?

Dwayne
- 2nd December 2004, 14:32
>>And the spazzing out of the LEDs is confusing me as well. Why when the right = 1 it runs through the if statement of the left once then goes through the right if statement.<<<

Are you programming your chip with MCLR=OFF?

If you are programming with MCLR=ON, make sure you have the MCLR pin tied high to 5 volts.

I know this sounds "Odd", but I have ran into spazzing chips, and found I forgot to put MCLR=OFF. Thus the chip was floating on and off.

Dwayne

mister_e
- 2nd December 2004, 16:53
Hi ECaro03


I looked over my circuit again and saw that I wasn't using the pull down resistor properly so it was left floating....ugh I feel so stupid now...lol


Hehe it happen to me one but i was using correctly... my sh... breadboard was the trouble. Some of those have supply line who break at the middle... when i discover it... AAARRGH!!!


am trying to have multiple ports on at the same time....

portc.2 to turn on along with portc.0
portc.3 to turn on along with portc.1

what's the code for that? and can you explain the binary code with it?

that's simple when you know how.
PORTC=%00000101 'turn on C.2 and C.0
PORTC=%00001010 'turn on C.3 and C.1

ECaro03
- 2nd December 2004, 19:56
Wohoo!!!

Everything is working great now!!!

I have been stressing out so bad over this project.

This project was for a senior electrical engineering design lab that takes a semester.

There are four of us (two electrical engineering majors and two computer engineering majors) in the group.

What our project is:
A PIR sensing camera that keeps a figure within the boundaries of the sensors using a motor. The camera is also hooked up to a computer that records in a video format.

Uses:
1. Recording a speaker at conferences
2. Security

We demo this system on Monday for the student body in the engineering building here on campus.

Our final report (approx. 100 pages) is due on Wednesday and we should have a website up showing our project. I will make sure to post that link once it comes available.

Thanks for eveybody's help, it was greatly appreciated!!!

Esteban
Computer Engineer '05
Texas A&M University