CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

what are the steps to read a digital input like toggle swtch
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

what are the steps to read a digital input like toggle swtch
PostPosted: Sat Apr 19, 2014 12:29 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 2:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 3:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 5:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 8:57 am     Reply with quote

Don't forget you have to debounce the switch or you will get LOTS of switch activations Shocked

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

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 5:29 pm     Reply with quote

If your project is about reading a switch, this thread has links to several
switch debounce routines:
http://www.ccsinfo.com/forum/viewtopic.php?t=42285

If you just want a simple program to change the state of an LED when
you push a button, this thread has a simple example program to do that:
http://www.ccsinfo.com/forum/viewtopic.php?t=47380&start=8
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

is the format rgt to link led ,Lcdand switch? getting error
PostPosted: Mon Apr 28, 2014 6:36 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 6:53 am     Reply with quote

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: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 8:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 8:32 am     Reply with quote

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;
#i
nclude <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

View user's profile Send private message

Re: is the format rgt to link led ,Lcdand switch? getting er
PostPosted: Mon Apr 28, 2014 5:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 11:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 11:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 29, 2014 2:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 30, 2014 12:56 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group