View previous topic :: View next topic |
Author |
Message |
uma
Joined: 17 Apr 2014 Posts: 29 Location: chennai
|
what are the steps to read a digital input like toggle swtch |
Posted: Sat Apr 19, 2014 12:29 am |
|
|
give me the basic idea to write code to read a digital input using 16F877a in CCS compiler and to display a message as that particular" DI is high" in LCD . waiting for the reply. _________________ uma
Application Engineer,
ETPL |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Apr 19, 2014 2:33 am |
|
|
Start by doing an LED flasher.
When it flashes at the correct speed you know the compiler & PIC are working properly.
Progress to making a switch control the LED.
Next get the LCD to do "Hello World".
Finally merge the switch and LCD codes.
CCS provides schematics for the hardware, and examples for all the code.
Mike |
|
|
uma
Joined: 17 Apr 2014 Posts: 29 Location: chennai
|
|
Posted: Sat Apr 19, 2014 3:05 am |
|
|
OK sure ,i will come back with the concept again.thank you. _________________ uma
Application Engineer,
ETPL |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sat Apr 19, 2014 5:34 am |
|
|
Hi,
Look at the 'input' function....
Code: |
If (input(Pin_A0))
Printf("Pin A0 is high\n\r");
Else
Printf("Pin A0 is low\n\r");
|
John |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sat Apr 19, 2014 8:57 am |
|
|
Don't forget you have to debounce the switch or you will get LOTS of switch activations
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
uma
Joined: 17 Apr 2014 Posts: 29 Location: chennai
|
is the format rgt to link led ,Lcdand switch? getting error |
Posted: Mon Apr 28, 2014 6:36 am |
|
|
I am getting error as numeric expression is expected. I want to on/off switch and corresponding LED as a do should be on/off. Msg is respectively displayed in LCD.
Code: |
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define Button Pin_C4;
#define Led Pin_B4;
#include <lcd.c>
void main()
{
while(1);
{
lcd_init();
//lcd_putc,("/f welcome to ETPL");
//lcd_putc,("/n LCD interface");
If(input(Pin_C4))
Printf(" Pin C4 is high\n\r");
output_B(0);
output_high(Pin_B4);//PIN_B4=1;
Else
Printf("Pin C4 is low/n/r");
|
_________________ uma
Application Engineer,
ETPL |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Mon Apr 28, 2014 6:53 am |
|
|
Code: |
If(input(Pin_C4))
{
Printf(" Pin C4 is high\n\r");
output_B(0);
output_high(Pin_B4);//PIN_B4=1;
}
Else
Printf("Pin C4 is low/n/r");
|
You need to add the brackets as shown. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Apr 28, 2014 8:17 am |
|
|
For what it's worth,
I'm a HUGE fan of ALWAYS encapsulating conditional statements with brackets/braces (whatever the language uses).
I never use:
Code: |
If ()
statement;
else
statement;
|
I ALWAYS use:
Code: |
If () {
statement;
} else {
statement;
}
|
I've seen too many bugs of people adding things to conditionals that didn't use braces and when they added stuff, the braces didn't go in, the extra code didn't run right. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Mon Apr 28, 2014 8:32 am |
|
|
Quote: |
[/code]
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define Button Pin_C4;
#define Led Pin_B4;
#include <lcd.c>
|
Remove the ; from the PIN_C4 and PIN_B4 defines. _________________ David |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Re: is the format rgt to link led ,Lcdand switch? getting er |
Posted: Mon Apr 28, 2014 5:09 pm |
|
|
uma wrote: | I am getting error as numeric expression is expected. I want to on/off switch and corresponding LED as a do should be on/off. Msg is respectively displayed in LCD.
Code: |
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define Button Pin_C4;
#define Led Pin_B4;
#include <lcd.c>
void main()
{
while(1);
{
lcd_init();
//lcd_putc,("/f welcome to ETPL");
//lcd_putc,("/n LCD interface");
If(input(Pin_C4))
Printf(" Pin C4 is high\n\r");
output_B(0);
output_high(Pin_B4);//PIN_B4=1;
Else
Printf("Pin C4 is low/n/r");
|
|
1) Code offered is neither complete nor compilable.
2) lcd_init(); should not be inside while loop. Only needs to happen once.
3) Insert a 1 second delay before initialising LCD to allow device to settle.
4) You have defined Button and Led, then do not use them!!!
Mike |
|
|
uma
Joined: 17 Apr 2014 Posts: 29 Location: chennai
|
|
Posted: Mon Apr 28, 2014 11:09 pm |
|
|
I am connecting the button switch to pin C4 and LED to Pin B4, then how can i mention that in the code mr. mike, especially to link everything in the main code? _________________ uma
Application Engineer,
ETPL |
|
|
uma
Joined: 17 Apr 2014 Posts: 29 Location: chennai
|
|
Posted: Mon Apr 28, 2014 11:28 pm |
|
|
After putting Brace,
my code is,
Code: |
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define Button Pin_C4;
#define Led Pin_B4;
#include <lcd.c>
void main()
{
lcd_init();
delay_ms(1000);
while(1);
{
//lcd_putc,("/f welcome to ETPL");
//lcd_putc,("/n LCD interface");
If(input(Pin_C4))
{
Printf(" Pin C4 is high\n\r");
output_B(0);
output_high(Pin_B4);
//PIN_B4=1;
}
Else
{
Printf("Pin C4 is low/n/r");
}
|
link for screen shot:
http://s28.postimg.org/rdpepndel/screen_with_connection.jpg _________________ uma
Application Engineer,
ETPL |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Apr 29, 2014 2:23 am |
|
|
Your code is still neither complete nor compilable.
1) Count the opening and closing braces.
2) I said put the delay BEFORE the lcd_init().
3) You are still not using the Button and Led defines in your code.
Mike |
|
|
uma
Joined: 17 Apr 2014 Posts: 29 Location: chennai
|
|
Posted: Wed Apr 30, 2014 12:56 am |
|
|
sorry i put the braces correctly,the mistake occurred in the above code while copying.but i didnot understand that u have told me i have not used the defines LED and button,i used it as pin C4 and B4, then how to use it? _________________ uma
Application Engineer,
ETPL |
|
|
|