View previous topic :: View next topic |
Author |
Message |
amir6682
Joined: 23 May 2011 Posts: 14
|
problem with detect high with any pin that set to input |
Posted: Fri May 27, 2011 7:26 am |
|
|
I want to write program that detect high for example if I set them to high, some led turn on. The program that was written with myself is:
Code: |
#include <16f887.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
void main()
{
set_tris_C(0xFF); ' input
set_tris_D(0xF0); 'output
OUTPUT_C(0x00);
OUTPUT_D(0x00);
X:
if(input(PIN_C0)==1) // Read the switch pin.
output_high(PIN_D0); // If it's not pressed, turn off the LED
else
output_low(PIN_D0); // If it's pressed, turn on the LED
if(input(PIN_C1)==1) // Read the switch pin.
output_high(PIN_D1); // If it's not pressed, turn off the LED
else
output_low(PIN_D1); // If it's pressed, turn on the LED
goto X;
}
|
It is good with Proteus but have a problem with micro in real hardware. In start some led is on without anything was high and when one of input port set to high, some led have weak light same that turned on. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Fri May 27, 2011 7:34 am |
|
|
You have to read the datasheet and see what internal peripherals(adc,ccp,uart,etc.) are on the same pins that you are using and disable those peripherals. The device.h file will have a list of configuration options listed.
Also, it's best to let the compiler contorl the 'set_tris_...' functions, as one wrong bit set by you will not allow the program to work properly.
Also, Proteus is FULL of errors, as you've found out! |
|
|
amir6682
Joined: 23 May 2011 Posts: 14
|
|
Posted: Fri May 27, 2011 10:03 am |
|
|
Can you help me, and get the sample code to me that turns the led on when its input is high.
LED1---> Pin B0
LED2---> Pin B1
LED3---> Pin B2
LED4---> Pin B3
LED5---> Pin B4
when Pin C0 ---> high ---> LED1 ON
C1---->high---> LED2 ON
....
So thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 27, 2011 11:09 am |
|
|
Quote: | #include <16f887.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
void main()
{
set_tris_C(0xFF); ' input
set_tris_D(0xF0); 'output
OUTPUT_C(0x00);
OUTPUT_D(0x00); |
You are using Port C for input pins for the switches. Don't set it to
be an output port. In CCS, the output_c() function also sets the TRIS
for Port C to be 0x00 (all outputs). Delete the line shown in bold above.
Also, make sure that your external circuit on each switch pin looks like this:
Code: |
+5v
|
<
> 4.7K
< ___ Switch
To | _|_|_
PIC -----------------o o------
pin |
--- GND
- |
|
|
|
amir6682
Joined: 23 May 2011 Posts: 14
|
|
Posted: Fri May 27, 2011 11:29 am |
|
|
Thank you, I migrate from mikrobasic to ccs recently and I think that here is same that but I'm in wrong. We have to clear port before declare it to input or output in there. But my mean is:
Micro <0--------------------0> +4v from Op-amp <0------------0>Sensor
I want to detect output from 10 op-amp to LED light. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 27, 2011 12:33 pm |
|
|
Quote: | Can you help me, and get the sample code to me that turns the led on when its input is high.
LED1---> Pin B0
LED2---> Pin B1
LED3---> Pin B2
LED4---> Pin B3
LED5---> Pin B4
when Pin C0 ---> high ---> LED1 ON
C1---->high---> LED2 ON
.... |
The most simple way to do this is shown below. Just read Port B and
write it out to Port C.
Code: |
#include <16f887.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
//=======================
void main()
{
int8 temp;
while(1)
{
temp = input_b();
output_c(temp);
}
} |
|
|
|
amir6682
Joined: 23 May 2011 Posts: 14
|
help |
Posted: Sun May 29, 2011 2:23 am |
|
|
Thank, i do it but i have problem. Any input port is high but all Led is on. And when i change input port any change see on led. I want when input is low, led off too and reverse. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun May 29, 2011 5:01 am |
|
|
LED wiring might be reversed ?
'positive logic' LED
From port pin----470R resistor----anode of LED--|>|---cathode to GROUND.
This way, a 'high' on the port pin will turn on the LED,a 'low' will turn it off. |
|
|
amir6682
Joined: 23 May 2011 Posts: 14
|
|
Posted: Sun May 29, 2011 1:27 pm |
|
|
anything reversed.LED connected with 320 Ohm to ground.
Micro <0-------0> 320Ohm<0-------0>A LED<0-----0>K LED<0-----0>GND
in start and without any change<LEDs are on.
Last edited by amir6682 on Sun May 29, 2011 2:29 pm; edited 1 time in total |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sun May 29, 2011 2:15 pm |
|
|
A 320 Ohm resistor should be fine. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
amir6682
Joined: 23 May 2011 Posts: 14
|
|
Posted: Sun May 29, 2011 2:32 pm |
|
|
My problem is that inputs don't appear on LED. When any change happen, anything changed on LED, and LED turn on forever. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 29, 2011 4:43 pm |
|
|
Post a schematic of your board. |
|
|
amir6682
Joined: 23 May 2011 Posts: 14
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
amir6682
Joined: 23 May 2011 Posts: 14
|
|
Posted: Sun May 29, 2011 11:12 pm |
|
|
4.120 |
|
|
|