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

I/O pin management

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







I/O pin management
PostPosted: Fri Apr 09, 2004 2:58 am     Reply with quote

Hello,

I'm trying to use unused pin of my PIC to do 8 bit DATA bus I/O.

for example, if I have RA0,RA1,RA4,RB2,RB5,RB7,RC0,RC5 pin unused, how can I outpout/inpout a byte using this pin ?

0) is it possible ?
1) how to do correct pin affectation (something like a struct ????)
2) how to correctly set TRIS registers for read and write operation

Hope I'm clear enought ?!


Thanks in advance
Xavier
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Fri Apr 09, 2004 3:43 am     Reply with quote

Of course it is possible. The best way to do it would be using structs. Look at the LCD.C file in the \drivers directory for more details.
Userrr



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

Haplo answered me
PostPosted: Fri Oct 15, 2004 1:31 pm     Reply with quote

answered me:
this LCD.c struct

struct lcd_pin_map { // This structure is overlayed
BOOLEAN enable; // on to an I/O port to gain
BOOLEAN rs; // access to the LCD pins.
BOOLEAN rw; // The bits are allocated from
BOOLEAN unused; // low order up. ENABLE will
int data : 4; // be pin B0.
} lcd;

But how with 2 ports

CAN I Do something as it
struct port_new {
int1 PORTB.0;
int1 PORTB.1;
int1 PORTA.5;
int1 PORTA.6;
int1 PORTB.3;
int1 PORTB.5;
int1 PORTA.1;
int1 PORTA.0;
} ports;

how I must do it
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 15, 2004 1:35 pm     Reply with quote

What you want is Mark's method. He posted it in the Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=20182
Userrr



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

Thanks PCM Programmer
PostPosted: Fri Oct 15, 2004 3:05 pm     Reply with quote

Thanks PCM Programmer
Userrr



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

But
PostPosted: Fri Oct 15, 2004 3:21 pm     Reply with quote

But here I can't get this:

PORTNEW=0xFF - function
and that is mean that as example
PORTA=0x0F and PORTB=0x0F

PORTNEW { RA0,RA1,RA2,RA3,RB0,RB1,RB2,RB3}

PCM programmer please answ. me
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 15, 2004 5:30 pm     Reply with quote

There are many ways to do it. It's a question of which way
is the most efficient. It's also a question of how much time
do you (or I) want to spend on it. Here is one way:
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// We want to be able to write a byte value to the following spare pins.
// RA0,RA1,RA4,RB2,RB5,RB7,RC0,RC5

// Define the bit addresses of the spare pins.
// The addresses shown below are for the 16F877.
#bit RA0 = 5.0
#bit RA1 = 5.1
#bit RA4 = 5.4
#bit RB2 = 6.2
#bit RB5 = 6.5
#bit RB7 = 6.7
#bit RC0 = 7.0
#bit RC5 = 7.5

// The following macro defines the "set_spare_bits()" function.
// This will be implemented by the compiler as eight sequential
// BSF or BCF instructions.
#define set_spare_bits(x)  \
RA0 = x & 1;         \           
RA1 = (x >> 1) & 1;  \
RA4 = (x >> 2) & 1;  \
RB2 = (x >> 3) & 1;  \
RB5 = (x >> 4) & 1;  \
RB7 = (x >> 5) & 1;  \
RC0 = (x >> 6) & 1;  \
RC5 = (x >> 7) & 1 

//==================================
void main()
{

// The TRIS must be set for the spare pins before the function is
//  called.   The spare pins must be set as outputs.
set_tris_a(0b11101100);
set_tris_b(0b01011011);
set_tris_c(0b11011110);

set_spare_bits(0x55);

while(1);
}

The program above compiles to the following code:
Code:

0000                00309 ..... set_tris_a(0b11101100); 
0014 30EC       00310 MOVLW  EC
0015 1683       00311 BSF    03.5
0016 0085       00312 MOVWF  05
0000                00313 ..... set_tris_b(0b01011011); 
0017 305B       00314 MOVLW  5B
0018 0086       00315 MOVWF  06
0000                00316 ..... set_tris_c(0b11011110); 
0012 30FF       00317 MOVLW  FF
0013 00A1       00318 MOVWF  21
0019 30DE       00319 MOVLW  DE
001A 0087       00320 MOVWF  07
001B 1283       00321 BCF    03.5
001C 00A1       00322 MOVWF  21
0000                00323 .....   
0000                00324 ..... set_spare_bits(0x55); 
001D 1405       00325 BSF    05.0
001E 1085       00326 BCF    05.1
001F 1605       00327 BSF    05.4
0020 1106       00328 BCF    06.2
0021 1686       00329 BSF    06.5
0022 1386       00330 BCF    06.7
0023 1407       00331 BSF    07.0
0024 1287       00332 BCF    07.5


--------------

Edited to fix the mistake in the last line of the set_spare_bits()
macro, as noted in the post below.
Edited to put code in a Code Block.


Last edited by PCM programmer on Thu Apr 07, 2011 10:03 am; edited 3 times in total
aaaaamartin



Joined: 17 Apr 2005
Posts: 39
Location: Germany Stuttgart

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 1:43 pm     Reply with quote

Hello,

what if I need input and output ?

Regards Martin
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 14, 2006 5:00 pm     Reply with quote

Quote:
what if I need input and output ?

The demo program below shows how to read eight spare bits and pack
them into a byte variable.
You don't need to set the TRIS if you're running the compiler in Standard
i/o mode. The compiler will handle it, since I'm using CCS i/o functions
in the macro. ("Standard i/o" mode is the default mode of the compiler).
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// This macro reads the value of the following
// pins and packs the result into a single byte.
// RA0,RA1,RA4,RB2,RB5,RB7,RC0,RC5

#define read_spare_bits(x) \
x = 0; \                 
if(input(PIN_A0)) bit_set(x, 0); \
if(input(PIN_A1)) bit_set(x, 1); \
if(input(PIN_A4)) bit_set(x, 2); \
if(input(PIN_B2)) bit_set(x, 3); \
if(input(PIN_B5)) bit_set(x, 4); \
if(input(PIN_B7)) bit_set(x, 5); \
if(input(PIN_C0)) bit_set(x, 6); \
if(input(PIN_C5)) bit_set(x, 7);


//==================================
void main()
{
int8 result;

while(1)
  {
   // Read the value of the spare bits and put
   // it into the 'result' variable.
   read_spare_bits(result);

   printf("Result = %X \n\r", result);

   delay_ms(500);
  }

}
Guest








PostPosted: Mon Jan 08, 2007 5:21 am     Reply with quote

I've just tried implementing the above macro set_spare_bits() and instead of turning on a static image on the 7-seg it cycles through the segments, one at a time, starting with one segment and then two, three, four, etc.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 08, 2007 11:17 am     Reply with quote

Read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=18183&highlight=leds+read+modify+write

Questions:
1. What PIC are you using ?

2. Can you post a list of the Port pins that you're using ?
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