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 CCS Technical Support

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



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Apr 30, 2014 1:54 am     Reply with quote

You need to add ERRORS to your #use rs232 statement.

The idea of defines is to give things meaningful names and make code more readable.
So this section of code :-
Code:
    //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");
     
   }

Now becomes :-
Code:
    If (input(Button))
    {
       Printf(" Pin Button is high\n\r");
       output_high(Led);
    }
    Else
    {
       Printf("Pin Button is low/n/r");
       output_low(Led);
    }

Does your complete code now work correctly?

Mike
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

RE:
PostPosted: Wed Apr 30, 2014 6:27 am     Reply with quote

hi my code shows error when i put as you said "Led" instead of Pin_B4 for output.

i attach my working code below.
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,Errors)
#define Button Pin_C4 60
#define Led Pin_B4 52
#include <lcd.c>

void main()
{


lcd_init();
delay_ms(1000);
while(0);
 {
   If(input(Pin_C4))
      {
      Printf(lcd_putc,"\fPin Button is high");
      output_high(Pin_B4);
      }
   else
    {
    Printf(lcd_putc,"\fPin Button is low");
    output_low(Pin_B4);
    }
}


}




Link of output screen.
http://s27.postimg.org/4g7vj4lfn/Slide1.jpg

http://s30.postimg.org/q65807ay9/Slide2.jpg

http://s28.postimg.org/9ug9e87b1/Slide3.jpg




In the above code iam getting output when i put not (!) actin before input(PIN_B4),
MY requirement is whenever pin C4 goes high PIN_B4 should also,
whenever C4 is low B4 should be.

BUT now my output is not so,it checks the condition input is on,if not it is doing nothing.

Hope you understand the issue.
help me.
_________________
uma
Application Engineer,
ETPL
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 30, 2014 6:55 am     Reply with quote

hmmm...

while(0);

this says NEVER do the following code...
same as
while(false);

You should have
while(TRUE);
{
//do all of this forever...
}

while(1); is the same.

also
...
#define Button Pin_C4 60
#define Led Pin_B4 52

delete the 60 and 52, not required ,might cause 'problems'.


and
...

insert

delay_ms(500);

before the
lcd_init();

This allows the LCD module time to get 'organized' BEFORE your program tries to access it.

recode/recompile/retest/report back

cheers
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Apr 30, 2014 7:11 am     Reply with quote

Strange!!!!!

My compiler does the same.

The #define for button is accepted with the extra '60' for input
The #define for led generates an error with the '52' for both input and output.

Tried a few different tests, results were not consistent.

Do as Mr T says, remove the numbers following the pin defines, and, of course, change to while(1).

Mike
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

RE:
PostPosted: Thu May 01, 2014 11:13 pm     Reply with 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,Errors)
#define Button Pin_C4
#define Led Pin_B4
#include <lcd.c>

void main()
{

delay_ms(1000);
lcd_init();

while(1);
 {
 
   If(input(Pin_C4))
      {
      Printf(lcd_putc,"\fPin Button is high");
      output_high(Pin_B4);
      }
   else
    {
    Printf(lcd_putc,"\fPin Button is low");
   
    }
   
}


}
   


I have made changes and compiled my code as you people said,but there still is an issue no o/p is coming. My led is not switched on even after latching.

link:
http://s15.postimg.org/no9wm4i5n/attachment.jpg
_________________
uma
Application Engineer,
ETPL
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

any one tell me how to check the input is high or not,
PostPosted: Thu May 01, 2014 11:15 pm     Reply with quote

Anyone tell me how to check the input is high or not, if not high how to make the input high.
_________________
uma
Application Engineer,
ETPL
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 01, 2014 11:24 pm     Reply with quote

You have made a common beginner mistake. Read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=29464
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

RE:
PostPosted: Thu May 01, 2014 11:53 pm     Reply with quote

I read the thread, but still i did not get what i have done. You mean to say that i have missed looping?
_________________
uma
Application Engineer,
ETPL
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Fri May 02, 2014 12:08 am     Reply with quote

A semicolon terminates the instruction.

Code:
While(1);
{

}

Are not the same as
Code:

While(1)
{


}
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

RE:
PostPosted: Fri May 02, 2014 12:33 am     Reply with quote

you are saying that just because iam using while with semicolon,it is checking the condition once ah?
if so how can declare it for looping?
_________________
uma
Application Engineer,
ETPL
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri May 02, 2014 1:25 am     Reply with quote

This really is basic C.

An if statement, while, etc., executes the next 'code block'.

A 'code block', is a single instruction, terminated with a ';', _or_ a group of instructions, contained in '{}' brackets.

So:
while(something) xxx;

executes 'xxx'

while(something)
{
xxx;
yyy;
zzz;
}

executes 'xxx', then 'yyy', then 'zzz', then 'xxx' etc...

while(something) ;

executes nothing (empty statement).

It is continuously checking the 'something', and looping, but the loop does nothing....

You need to get a C primer.
uma



Joined: 17 Apr 2014
Posts: 29
Location: chennai

View user's profile Send private message

i understood the concept..
PostPosted: Sat May 03, 2014 2:06 am     Reply with quote

i understood the concept..thanks for all your responses ,i successfully compiled my code and got output.Especially the last post was very usefull for me..
_________________
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 Previous  1, 2
Page 2 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