Jump to content
Sign in to follow this  
Daniel

read out a port

Recommended Posts

Hy,

 

I have a proplem. i'm working with PIC 12F675. These PICs have only one Port. It's named "GPIO". In the C2C-Compiler I cann't find any instructions to read out the port "GPIO".

If anybody knews a way to read the GPIO-port out, please answer me.

 

Thank you,

Daniel

Share this post


Link to post
Share on other sites

I have not used the chip you mention, but your problem

may be as simple as creating your own variables that

represent the port, like:

 

char gpio @ 0x05;   //gpio port

char trisio @ 0x85;   //gpio i/o select

char wpu @ 0x95;   //gpio weak pull-up select

 

void main(void)

{

  char x;

 

  x = gpio;   //read the port

}

 

Give this a try, hopefully it will help.

 

Jim

Share this post


Link to post
Share on other sites
Guest Daniel

Hy Jim,

 

You have helped me a lot. Thank you. Have you also an answer at the question, "How can I read out a single pin from the port?". Is it the same way or another way to read an pin out.

 

Thank you,

Daniel

Share this post


Link to post
Share on other sites

You have to read the entire port, then isolate the pins

you care about; for instance:

 

char gpio @ 0x05

char SwitchPort @ 0x05  //different name for same port...

 

//Set up bit masks for the inputs.

#define SWITCH_1   0x01

#define SWITCH_2   0x02

#define SWITCH_3   0x04

 

void main(void)

{

  char Temp;

 

  while(1)

  {

     //Grab a snapshot of the port.

     Temp = SwitchPort;

 

     //This assumes that the internal pull-up resistors

     //are turned on and the switches go to ground.

     if ( (Temp & SWITCH_1) == 0 )

     {

        //Do switch one stuff.

     }

 

     if ( (Temp & SWITCH_2) == 0 )

     {

        //Do switch two stuff.

     }

 

     if ( (Temp & SWITCH_3) == 0 )

     {

        //Do switch three stuff.

     }

 

 

     //Do other stuff...

 

     //Wait awhile then do it again...

     delay_ms(100);

  }

}

 

In real life you may need to consider switch debouncing

and other issues, but this shows the main idea.

 

Jim

Share this post


Link to post
Share on other sites

Daniel;

here you have an example about your question ....

 

// Example to see how to work with gpio bits (12F629 & 12F675)

// In this example we will see how to set & clear only a pin

// and how to read only one pin of gpio

 

#include "system.h"

 

#define bitmask_0 0x01  // bitmask_0 = 00000001b

#define bitmask_1 0x02  // bitmask_1 = 00000010b

#define bitmask_2 0x04  // bitmask_2 = 00000100b

#define bitmask_3 0x08  // bitmask_3 = 00001000b

#define bitmask_4 0x10  // bitmask_4 = 00010000b

#define bitmask_5 0x20  // bitmask_5 = 00100000b

#define bitmask_6 0x40  // bitmask_6 = 01000000b

#define bitmask_7 0x80  // bitmask_7 = 10000000b

 

char GPV;

 

main()

{

trisio = 000101000b;// ( Output=0;Input=1; XXX,XXX,XXX,GP5,GP4,GP3,GP2,GP1,GP0)

cmcon = 7;     // GP0,GP1,GP2,GP4 are Outputs

gpio = 0x00; // GP3,GP5 are Inputs (GP3 is ONLY INPUT ever)

// XXX are not used bits because GPIO only have 6 I/O pins

 

while (1)

{

GPV = gpio; // GPV is not necessary

 

if (GPV&bitmask_3) // We can do -->  if (gpio&bitmask_3)

{

set_bit(gpio,0);

}

else

{

clear_bit(gpio,0);

}

 

if (GPV&bitmask_5) // We can do --> if (gpio&bitmask_5)

{

set_bit(gpio,4);

}

else

{

clear_bit(gpio,4);

}

}

}

Share this post


Link to post
Share on other sites

I like joancanet idea but instead of writing 8 different #define staments you could use an array. I don't remember the proper syntax because I'm still learning and I keep going back and forth through my references but the idea is something like this.

 

 

arrayname[8]= {'0x01', '0x02', '0x04',....}

int x=0

int find_bit(0);   /* call the find bit function */

.

.

.

find_bit   /*  function   */

{

 if (arrayname[x] & gpio)  /*compare the value in position x*/

                                  /* in the array to gpio               */

    {

      varA  = x      /* store the value of the pin in varA  */

      return varA        /*maybe is return X */

    }

  x=x+1

}

 

I'm pretty sure that the syntax and spelling are wrong but you get the idea. Just call the function whenever you need to find the pin and use the varA (or maybe is x??) to set you port or to do whatever comparison you need.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...