View previous topic :: View next topic |
Author |
Message |
ViserExcizer
Joined: 13 Mar 2011 Posts: 1
|
Really simple input/output LED c code |
Posted: Sun Mar 13, 2011 3:38 am |
|
|
Hello, I need help, this is my first time coding in C
And i wrote the program below :
Code: |
#include <16F877.h>
#use delay(clock=4000000,oscillator)
void main(){
set_tris_a(0xb00011111);
set_tris_b(0b11111111);
while(true){
//testing output_b(0b0000001);
if(input(PIN_A0)==0){
output_b(0b0000000);
}else if(input(PIN_A0)==1){
output_b(0b0000001);
}
}
}
|
The program is supposed to blink the LED connected to RB0 if
the switch to RA0 is pressed.
But It doesnt work. something is wrong with my syntax,
If i comment out the //testing output_b(0b0000001);
nothing happens and if i leave it in, the LED is just on, and never goes off when i press the switch to RA0
However the following program below works, because its a purely PIC output and no input is needed, which lead me to believe that there is something wrong with the code above. Please help. Thanks in advance.
Code: |
#include <16F877.h>
#use delay(clock=4000000,oscillator)
void main(){
set_tris_a(0xb00011111);
set_tris_b(0b11111111);
while(true){
output_b(0b11111111);
delay_ms(1000);
output_b(0b00000000);
delay_ms(1000);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Sun Mar 13, 2011 5:36 am |
|
|
Try without the 'else if' and instead use 'else'
Code: | #include <16F877.h>
#use delay(clock=4000000,oscillator)
void main(){
set_tris_a(0xb00011111);
set_tris_b(0b11111111);
while(true){
if(input(PIN_A0)==0)
{
output_b(0b0000000);
}
else
{
output_b(0b0000001);
}
}
}
|
_________________ Vinnie Ryan |
|
![](templates/subSilver/images/spacer.gif) |
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Sun Mar 13, 2011 9:03 am |
|
|
PORTA powers up as analog inputs. You need to configure it for digital i/o.
This information is in the data sheet for the part. _________________ David |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sun Mar 13, 2011 9:28 am |
|
|
By the way ..You don't need the set tris statements....the CCS compiler handles it. In the rare circumstance you need extremely fast I/O select FAST i/o and use set tris otherwise it is not needed. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 13, 2011 1:43 pm |
|
|
See this post for an example program and schematic:
http://www.ccsinfo.com/forum/viewtopic.php?t=43629&start=4
Also, the CCS compiler sets the TRIS for you. You don't need to set it.
And, please read the PIC data sheet on the correct setting of TRIS bits
to make pins into output pins. You will find a problem with the following:
Quote: | set_tris_b(0b11111111); |
|
|
![](templates/subSilver/images/spacer.gif) |
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Mon Mar 14, 2011 7:00 am |
|
|
Are you using a pullup resistor on the Port A pin, with the switch going to Gnd? If there's no pullup, you won't get reliable operation. Why not have your input going to Port B, and use its built-in pullups, and make Port A be the output? |
|
![](templates/subSilver/images/spacer.gif) |
|