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

SET_TRIS_ for 16F877

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



Joined: 12 Apr 2007
Posts: 7

View user's profile Send private message

SET_TRIS_ for 16F877
PostPosted: Thu Apr 12, 2007 9:59 am     Reply with quote

Hi I'm using PIC16F877 for my project. Can anyone let me know how to SET_TRIS_A(_X__)
SET_TRIS_B etc.

I have pin A0,A1,A2,A3,A4 for input and pin B3,B4,B5 for outputs. I have pin C6 and C7 for RS232. How can I set it? hope some one can help me out.Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 12:34 pm     Reply with quote

Use the built-in CCS pin i/o functions, such as:
Code:

output_high()
output_low()
output_float()
input()

Then the compiler will set the TRIS for you automatically. You don't
have to worry about it. This is the default mode of the compiler.
(Don't enable fast i/o mode).

If you use the CCS library code to setup the UART, the compiler
will automatically handle setting up the correct TRIS for the UART.
Example:
Code:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
TheBeginner



Joined: 10 Apr 2007
Posts: 18

View user's profile Send private message

PostPosted: Thu Apr 12, 2007 4:02 pm     Reply with quote

yeah PCM programmer is right,also if u wanna change the default mode you can do it by somethong like that

SET_TRIS_A(0xff) //all the A ports are input

SET_TRIS_A(0x0f) // A0,A1,A2,A3 are input and the other A4,A5.. are output

the same thing with other ports
teongkia



Joined: 12 Apr 2007
Posts: 7

View user's profile Send private message

PostPosted: Sun Apr 15, 2007 8:47 am     Reply with quote

Thanks.Hi how about I have pins A0,A1,A2 as input and A3,A4,A5 as output.How do I set it?How about port D which I not using it.Shoud I leave it or set it?If I have #USE RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) should SET_TRIS_C?
Ttelmah
Guest







PostPosted: Sun Apr 15, 2007 9:29 am     Reply with quote

Provided you don't change the I/O mode, you don't 'have' to do _anything_!.
Basically if you perform an input operation from a pin, _that pin_gets set as an input. If you use an output operation, that pin gets set as an output.
Now the one 'exception' in what you mention, would be the 'unused' port. Basically, the default (unless you perform an I/O operation), is for a port to wake up as inputs. Now what you should do with an unused port, depends onyour external wiring. If it is physically disconnected from anything at all, then the easiest choice, would be to execute:

output_d(0);

This will set the whole port as 'outputs', and turn on the drivers to pull the pins down. On CMOS logic, it makes little difference, whether you drive down or up, and in one sense, driving 'down' is potentially slightly safer.

The alternative, is to provide an external resistor to each pin, pulling it to +5v, or 0v.
I anything at all is attached to the pins (even if not in 'use'), then you need to find out whether this device draws less current with the pins pulled high or low, or if it drives the pins itself.

Now you mention using 'A4' as an output. One caveat. On a lot of chips, A4, only has a 'pull down' output driver, and requires a pull up resistor, to be used as an output (commonly called an 'open collector' driver, from the earlier designs that used this). The 877 is like this. Personally, I'd use this pin as one of your inputs, and select another pin for output, unless you want this type of driver.

Best Wishes
teongkia



Joined: 12 Apr 2007
Posts: 7

View user's profile Send private message

PostPosted: Sun Apr 15, 2007 10:04 am     Reply with quote

Hi really thanks for your information. Now I am doing a project on car security system which connects PIC16F877 to Sony Ericsson T630 using MAX232. I have this code but couldn't run. Can you check for me what's is going wrong for the code?My basic operation is when i have 5V input on remote pin, it will activate the system and any sensor triggered will send output for siren pin and call the specific number using AT command.Hope you can help me out.Thanks.


Code:
//Preprocessor
#INCLUDE <16F877>
#FUSES HS,NOWDT,NOLVP
#USE DELAY(clock=12000000)
#USE RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

//Define pin
#DEFINE SIREN        PIN_B5
#DEFINE CL1         PIN_B4
#DEFINE CL2          PIN_B3
#DEFINE ULTRA     PIN_A0
#DEFINE TOUCH     PIN_A1
#DEFINE LIGHT     PIN_A2
#DEFINE REMOTE    PIN_A3
#DEFINE DOOR      PIN_A4

//Global declaration
int A,B,C,D,RC,EN;
int RC1[2];
char SENSOR1, SENSOR2, SENSOR3, SENSOR4, TEMP;
char S[51];

//Sound siren
void SIREN_ON()
{
   OUTPUT_HIGH(SIREN);
   DISABLE_INTERRUPTS(INT_RTCC);
}
//Unlock doors
void UNLOCK()
{
   OUTPUT_HIGH(CL1);
   OUTPUT_LOW(CL2);
}
//Lock doors
void LOCK()
{
   OUTPUT_LOW(CL1);
   OUTPUT_HIGH(CL2);
}

//Ultrasonic sensor triggered
void SEND_CALL1()
{
   PRINTF("ATD016456789;\n\r");
}
//Touch sensor triggered
void SEND_CALL2()
{
   PRINTF("ATD016456789;\n\r");   
}
//Light sensor triggered
void SEND_CALL3()
{
   PRINTF("ATD016456789;\n\r");
}
//Door sensor triggered
void SEND_cALL4()
{
   PRINTF("ATD016456789;\n\r");
}
//Central lock triggered
void SEND_CALL5()
{
   PRINTF("ATD016456789;\n\r");
}




#INT_RTCC
T0()
{
      SENSOR1=INPUT(ULTRA);
      SENSOR2=INPUT(TOUCH);
      SENSOR3=INPUT(LIGHT);
      SENSOR4=INPUT(DOOR);
   if(SENSOR1)
      {
         SEND_CALL1();
         SIREN_ON();
      }
      else if(SENSOR2)
      {
         SEND_CALL2();
         SIREN_ON();
      }
      else if(SENSOR3)
      {
         SEND_CALL3();
         SIREN_ON();
      }
      else if(SENSOR4)
      {
         SEND_CALL4();
         SIREN_ON();
      }
}

#INT_RDA
SERIAL()
{
   //Optional   
   if(kbhit())
   {
      TEMP=GETC();
      if(A<50)
      {
         S[A]=TEMP;
         A++;
      }
      else
      {
         A=0;
         S[A]=TEMP;
         A++;
      }
   }
   DELAY_MS(100);
   return A,S;
}

//Main function
void MAIN()
{
   
     SET_RTCC(0);
     SETUP_COUNTERS(RTCC_INTERNAL,RTCC_DIV_64);
     EXT_INT_EDGE(0,H_TO_L);     
     ENABLE_INTERRUPTS(INT_RDA);
     ENABLE_INTERRUPTS(GLOBAL);
     SET_TRIS_A(0b00011111);
     SET_TRIS_B(0x01);
     SET_TRIS_D(0x00);

     DELAY_MS(5000);

   PRINTF("\n\rATZ\n\r");
   DELAY_MS(500);
   PRINTF("\n\rAT\n\r");
   DELAY_MS(500);
   PRINTF("\n\rAT+CNMI=2,3,0,0,0\n\r");
   DELAY_MS(500);
   PRINTF("\n\rAT+CPMS=\"ME\",\"ME\",\"ME\"\n\r");
   
   SENSOR1==0;
     SENSOR2==0;
     SENSOR3==0;
     SENSOR4==0;
   
   A=0;
   B=0;
   C=0;
   D=0;
   EN=0;
     RC=0;
     UNLOCK();
     //Checking system activation
     while(1)
     {
       RC=(INPUT(REMOTE)^0);
      RC1[0]=RC;
      //Checking for rising and falling edge
      if((RC1[0]==1 && RC1[1]==0)||(RC1[0]==0 && RC1[1]==1))
      {
         EN=(EN^1);
         if(EN==1)
         {
             LOCK();
            ENABLE_INTERRUPTS(INT_RTCC);
         }
         else if(EN==0)
         {
            UNLOCK();
            DISABLE_INTERRUPTS(INT_RTCC);
         }     
      }
      RC1[1]=RC;
   }
}
Ttelmah
Guest







PostPosted: Sun Apr 15, 2007 10:15 am     Reply with quote

You cannot 'return' anything from an interrupt.
Your delay in INT_RDA, ensures that if more than two characters come, the UART will overflow.
The same applies to INT_RTCC. It takes a long time to print a string like you are doing, and this will result in both any serial interrupts being missed, and the interrupt already having retriggered, before it returns. You need to rethink, and just set a flag in the interrupt, and do all these slow operations, only in the main. The general rule is _keep interrupt handlers fast_.

Best Wishes
teongkia



Joined: 12 Apr 2007
Posts: 7

View user's profile Send private message

PostPosted: Sun Apr 15, 2007 10:34 am     Reply with quote

Hi Ttelmah thanks for your information.Can you guide me on how to set flag in interrupt in order to make the code work?May I know is there any error on other parts of the code?Thanks.
Guest








PostPosted: Sun Apr 15, 2007 12:31 pm     Reply with quote

Code:

int IntXXXFlag = FALSE;



void main(){
   while (TRUE){
       if (IntXXXFlag == TRUE){
             printf("Int handler was executed");
             IntXXXFlag = FALSE;
       }
   }
}

#INT_XXX //your choice, RDA, INT, TIMER, etc
void IntHandler(){
   //int happened, set flag
   IntXXXFlag = TRUE;
}
Ttelmah
Guest







PostPosted: Sun Apr 15, 2007 2:33 pm     Reply with quote

And, for the serial, look at ex_sisr, which shows how to receive data into a buffer.

Best Wishes
teongkia



Joined: 12 Apr 2007
Posts: 7

View user's profile Send private message

PostPosted: Fri Apr 20, 2007 11:41 am     Reply with quote

Hi I tried on this way on my code but It still didn't work on my PIC.When I switch on the power supply, the siren(PIN_B5) would have output immediately without triggering any sensor.And my remote control does not work too.Anyone can help?Thanks.

Code:
//Preprocessor
#INCLUDE <16F877A.H>
#FUSES HS,NOWDT,NOLVP
#USE DELAY(clock=12000000)
#USE RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

//Define pin
#DEFINE SIREN        PIN_B5
#DEFINE CL1         PIN_B4
#DEFINE CL2          PIN_B3
#DEFINE ULTRA     PIN_A0
#DEFINE TOUCH     PIN_A1
#DEFINE LIGHT     PIN_A2
#DEFINE REMOTE    PIN_A3
#DEFINE DOOR      PIN_A4

//Global declaration
int A,B,C,D,RC,EN;
int RC1[2];
int IntRTCCFlag = FALSE;
char SENSOR1, SENSOR2, SENSOR3, SENSOR4;

void main(){
   while (TRUE){
       if (IntRTCCFlag == TRUE){
             printf("Int handler was executed");
             IntXXXFlag = FALSE;
       }
   }
}

//Sound siren
void SIREN_ON()
{
   OUTPUT_HIGH(SIREN);
   DISABLE_INTERRUPTS(INT_RTCC);
}
//Unlock doors
void UNLOCK()
{
   OUTPUT_HIGH(CL1);
   OUTPUT_LOW(CL2);
}
//Lock doors
void LOCK()
{
   OUTPUT_LOW(CL1);
   OUTPUT_HIGH(CL2);
}

//Ultrasonic sensor triggered
void SEND_CALL1()
{
   PRINTF("ATD0166827272;\n\r");
}
//Touch sensor triggered
void SEND_CALL2()
{
   PRINTF("ATD0166827272;\n\r");
}
//Light sensor triggered
void SEND_CALL3()
{
   PRINTF("ATD0166827272;\n\r");
}
//Door sensor triggered
void SEND_CALL4()
{
   PRINTF("ATD0166827272;\n\r");
}
//Central lock triggered
void SEND_CALL5()
{
   PRINTF("ATD0166827272;\n\r");
}




#INT_RTCC
void IntHandler(){
   //int happened, set flag
   IntRTCCFlag = TRUE;
}

T0()
{
      SENSOR1=INPUT(ULTRA);
      SENSOR2=INPUT(TOUCH);
      SENSOR3=INPUT(LIGHT);
      SENSOR4=INPUT(DOOR);
   if(SENSOR1)
      {
         SEND_CALL1();
         SIREN_ON();
      }
      else if(SENSOR2)
      {
         SEND_CALL2();
         SIREN_ON();
      }
      else if(SENSOR3)
      {
         SEND_CALL3();
         SIREN_ON();
      }
      else if(SENSOR4)
      {
         SEND_CALL4();
         SIREN_ON();
      }
}


//Main function
void MAIN()
{
   
     SET_RTCC(0);
     SETUP_COUNTERS(RTCC_INTERNAL,RTCC_DIV_64);
     EXT_INT_EDGE(0,H_TO_L);     
     ENABLE_INTERRUPTS(GLOBAL);
SET_TRIS_A(0x3F);
     SET_TRIS_B(0x01);
   //SET_TRIS_C(0x0F);
   SET_TRIS_D(0x00);

     DELAY_MS(5000);

   PRINTF("\n\rATZ\n\r");
   DELAY_MS(500);
   PRINTF("\n\rAT\n\r");
   DELAY_MS(500);
   PRINTF("\n\rAT+CNMI=2,3,0,0,0\n\r");
   DELAY_MS(500);


   
   A=0;
   B=0;
   C=0;
   D=0;
   EN=0;
     RC=0;
     UNLOCK();
     //Checking system activation
     while(1)
     {
       RC=(INPUT(REMOTE)^0);
      RC1[0]=RC;
      //Checking for rising and falling edge
      if((RC1[0]==1 && RC1[1]==0)||(RC1[0]==0 && RC1[1]==1))
      {
         EN=(EN^1);
         if(EN==1)
         {
             LOCK();
            ENABLE_INTERRUPTS(INT_RTCC);
         }
         else
         {
            UNLOCK();
            DISABLE_INTERRUPTS(INT_RTCC);
         }     
      }
      RC1[1]=RC;
   }
}

andys



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

SET_TRIS_A(0XFF)
PostPosted: Tue May 01, 2007 1:31 pm     Reply with quote

when all ports A is input i write
SET_TRIS_A(0XFF)
when all ports A is outpus
how to write that????
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 01, 2007 1:54 pm     Reply with quote

Download the CCS manual here:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf

Look in the section on "Set_Tris_A". Read the sub-section called
"Function". There are two sentences near the end, which explain
exactly what parameters are used to set the pins as inputs or outputs.
tiryaka



Joined: 01 May 2007
Posts: 8

View user's profile Send private message

Re: SET_TRIS_A(0XFF)
PostPosted: Fri May 04, 2007 4:19 am     Reply with quote

andys wrote:
when all ports A is input i write
SET_TRIS_A(0XFF)
when all ports A is outpus
how to write that????

SET_TRIS_A(0X00);
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