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

how to check an input

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








how to check an input
PostPosted: Wed Jun 24, 2009 11:38 am     Reply with quote

Hi, I am using PIC16f72 for my project.
I have set port_c as input and port_a digital output (through port_a I operate a Relay), initially port_a is "0xFF" and I want to compare the input of port_c with a desired value.
Code:

#include <16f72.h>
#USE DELAY( CLOCK=4000000 ) /* Using a 4 Mhz clock */
#FUSES XT,NOWDT,NOPROTECT,NOPUT
/* Use XT mode, No Watch Dog, No Code Protect, No Power-up Timer */
//PIC_C2=Low_Level
//PIN_C3=High_Level
#byte port_c=7 /* define the location of register port_b */
#byte port_a=5 /* define the location of register port_b */
#define ALL_DIGITAL 7
#define low 0xF7
void pump_on();
void pump_off();

main()
{
 set_tris_a(0);
 set_tris_c(1);
 setup_adc_ports(ALL_DIGITAL);
 port_a=0;
 //port_c=0;
 output_a(0xFF);
 port_a=0xFF;

  while(1)
  {
   if(port_c==low)
      pump_off();
   else
      pump_on();
  }
 
}


void pump_on()
{
 port_a=0xFF;
}

void pump_off()
{
 port_a=0x00;
}

This is my current code but its not working.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 24, 2009 12:11 pm     Reply with quote

Quote:
set_tris_c(1);

This function takes a byte, in the form of a bitmask, as the parameter.
A '1' in the bitmask, means to make that bit in the port into an input pin.
A '0' means to make it be an output pin. There are typically 8 i/o pins
in a port.

Therefore, you are only setting Pin C0 as an input. All the rest of the
pins (C1-C7) are set as outputs.

Also, you are mixing direct port i/o operations with the built-in CCS
functions. Sometimes you do this:
Code:
output_a(0xFF);

But other times you do this:
Code:
port_a=0xFF;

You should choose one method and stick with it.

Also, you have not specified "#use fast_io" mode. This means the
compiler will operate in the default mode, which is called "Standard i/o"
mode. In this mode, the compiler sets the TRIS for you, whenever you
use the built-in i/o functions, such as output_a(), etc. This will conflict
with the set_tris_x() lines in your program. You'll set the TRIS to one
state, and the compiler will over-write that TRIS state when it executes
one of the i/o functions.

You need to use one method or the other.
1. Specify #use fast_io() for the specific ports, and then call the
set_tris_x() functions.

or,

2. Don't specify fast i/o mode. Leave the compiler in standard i/o mode.
Then use only the built-in i/o functions (of which there are many), and
let the compiler handle the TRIS for you automatically.

For a newbie, method #2 is much easier.
bharatwalia



Joined: 04 May 2009
Posts: 35
Location: India

View user's profile Send private message

PostPosted: Wed Jun 24, 2009 12:48 pm     Reply with quote

Hi,thanks it worked.
After your suggestion I changed the code according to default settings,
but after sometime the program is hanging up.
Please go through the modified code, so that I can rectify it further.
Code:

#include <16f72.h>

#USE DELAY( CLOCK=4000000 ) /* Using a 4 Mhz clock */
#FUSES XT,NOWDT,NOPROTECT,NOPUT
/* Use XT mode, No Watch Dog, No Code Protect, No Power-up Timer */
//PIC_C2=Low_Level
//PIN_C3=High_Level
#byte port_c=7 /* define the location of register port_b */
#byte port_a=5 /* define the location of register port_b */
#define ALL_DIGITAL 7
#define low 0xF3
void pump_on();
void pump_off();

main()
{
 //set_tris_a(0x00);
 //set_tris_c(0xFF);
 setup_adc_ports(ALL_DIGITAL);
 //port_a=0;
 //port_c=0;
 output_a(0xFF);
 input_c();
//port_a=0xFF;
   while(1)
 {

  if(port_c==low)
  pump_off();
else
pump_on();
}
 
}

void pump_on()
{
 port_a=0xFF;
}

void pump_off()
{
 port_a=0x00;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 24, 2009 1:17 pm     Reply with quote

You are still misunderstanding how to use the functions.
Quote:
input_c();

This statement is incorrect. You need to either load a variable with
the result of the input_c() function, or you need to compare the result
to some other expression. Don't use "input_c()" all by itself on a line.
One example of correct usage:
Code:

int8 result;

result = input_c();



Quote:

while(1)
{
if(port_c==low)

Here you are still using direct register i/o. If you want to read Port C
continuously inside the loop (and do it in standard i/o mode), then use
the input_c() function. Example:
Code:

while(1)
 {
  if(input_c() == low)
Guest








PostPosted: Wed Jun 24, 2009 1:54 pm     Reply with quote

Further rectified it is working but the program is hanging up after sometime.
Code:

#include <16f72.h>

#USE DELAY( CLOCK=4000000 ) /* Using a 4 Mhz clock */
#FUSES XT,NOWDT,NOPROTECT,NOPUT
/* Use XT mode, No Watch Dog, No Code Protect, No Power-up Timer */
//PIC_C2=Low_Level
//PIN_C3=High_Level
#byte port_c=7 /* define the location of register port_b */
#byte port_a=5 /* define the location of register port_b */
#define ALL_DIGITAL 7
#define low 0xF3
void pump_on();
void pump_off();

main()
{
 setup_adc_ports(ALL_DIGITAL);
 
while(1)
 {
  if(input_c()==low)
     pump_off();
  else
     pump_on();
 }
 
}

void pump_on()
{
 output_a(0xFF);
}

void pump_off()
{
 output_a(0x00);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 24, 2009 1:59 pm     Reply with quote

What is connected to the pins on Port C ?
Post a detailed explanation of the circuits on each pin of Port C.
bharatwalia



Joined: 04 May 2009
Posts: 35
Location: India

View user's profile Send private message

PostPosted: Thu Jun 25, 2009 2:23 pm     Reply with quote

Hi, thanks a lot. It worked.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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