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

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
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