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

LEDS

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







LEDS
PostPosted: Thu Apr 28, 2005 7:27 am     Reply with quote

Hello,

i wanted to know how i could do if i wanted to select the pin i want to switch on.

I know that i could do it simply but it is just a test to do something more so
How to have a pin as a variable ?

Code:

void test(byte const x)
{
output_high(x);
}
void main(void)
   {
      while(1)
         {
         test(LED_1);
         test(LED_2);
         }
   }


Thanks for your help
JohnKennedy



Joined: 12 May 2004
Posts: 38

View user's profile Send private message

PostPosted: Thu Apr 28, 2005 8:27 am     Reply with quote

I use the define statement as below - works well

Code:
#define ledred (Pin_B4)


Hope this helps Very Happy

JFK
JohnKennedy



Joined: 12 May 2004
Posts: 38

View user's profile Send private message

PostPosted: Thu Apr 28, 2005 8:28 am     Reply with quote

I use the define statement as below - works well

Code:
#define ledred (Pin_B4)


You can the use the output_high or output_low statements to turn on or off

Hope this helps Very Happy

JFK
Homer
Guest







PostPosted: Thu Apr 28, 2005 8:40 am     Reply with quote

i think you didn't understand my question ...

How do i have to set a function to have a pin as a variable ?

function(pin_1);
function(pin_2);
Ttelmah
Guest







PostPosted: Thu Apr 28, 2005 8:48 am     Reply with quote

The answer in part depends on whether the pins are on the same port. The 'easiest' way if they are, is simply to develop a bit mask. The problem is that the pin values, and the internal input and output functions, translate directly to individual bit set/reset functions, which use a different instruction for each bit, which means these can't use a variable to control the access. Using a variable requires a different approach.
Something like:
Code:

void bit_set_A(int8 bit_num) {
   int8 mask;
   mask=1<<bit_num;
   output_B(input_B() | mask);
}


Best Wishes
Homer
Guest







PostPosted: Thu Apr 28, 2005 8:56 am     Reply with quote

I am not sure to understand ...

I just want to use different functions that i have that i should use for other components in the programm.

The PIC has different components on its pins and the use for example 1 wire interface, so functions used are exactly the same, excepts for the pins that are considered .

How to ue one function for 2 differents pins ?
Ttelmah
Guest







PostPosted: Thu Apr 28, 2005 9:25 am     Reply with quote

These functions, are just like the input and output ones, internal to the compiler. Hence the compiler can just substitute the operations needed at compile time. You can to some extent get the same effect, by using the macro ability, and tests. The 'key' with this, is that only the code for the required pin, is actually implemented when the code is compiled. Arithmetically, the low three bits of the pin number correspond to the bit to be used inside an address, and the rest of the number (/8), is the memory address to be used. So you have to decompose the pin number into these two values, and then define a byte based on the code for the instruction, combined with these values, to give the final code.
Generally, it is easier to just test for the byte required, and use a mask as shown.

Best Wishes
Homer
Guest







PostPosted: Thu Apr 28, 2005 9:58 am     Reply with quote

Something else to add, please ?
Because it doesn't really help me.
Any suggestion ?
Thanks you all anyway !
Ttelmah
Guest







PostPosted: Thu Apr 28, 2005 10:16 am     Reply with quote

If you said what you actually needed to do, somebody could probably help more. If you just need to make a decision as to which pin to use, then if you define 'LED_1' and 'LED_2', as numbers (<255) then in the test routine, something like:
Code:

void test(int8 LEDnum) {
   switch (LEDnum) {
   case LED_1 :
       //Do what you want for LED_1 here
       break;
   case LED_2 :
       //Do what you want for LED_2 here
       break;
   }
}

These are all dead basic 'C' techniques...

Best Wishes
Homer
Guest







PostPosted: Fri Apr 29, 2005 1:52 am     Reply with quote

Thank you very much but nearly what i needed !
Ifi want for example to do in the "Do what you want for LED_1 or LED_2 here" the same function but for each different LED ?

Like this :

Code:

void test(int8 LEDnum) {
   switch (LEDnum) {
   case LED_1 :
       function(LED_1);//Do what you want for LED_1 here
       break;
   case LED_2 :
       function(LED_2);//Do what you want for LED_2 here
       break;
   }
}



It should be a way for me to write function() only once !

Thank you !
Ttelmah
Guest







PostPosted: Fri Apr 29, 2005 2:19 am     Reply with quote

Yes.
You just use exactly the same switch inside the function.
Understand, that you cannot use the same _code_ for the two pins, unless you use a mask and read the whole port, then select the required bit (as I have already described). The switch allows you to select which I/O bits to use.
As an example of doing it 'generically', if (for instance), both pins are on portA, as bits 1, and 2:
Code:

#define LED_1 (2) //bit mask for bit 1
#define LED_2 (4) //bit mask for bit 2
int8 dosomething(int8 LEDreqd) {
   int8 temp;
   temp=input_A();
   temp=temp | LEDreqd;  //This will turn on the selected LED
   output_A(temp);
   //Now check the LED is 'on' - allow a moment for the pin to
   //settle.
   delay_us(4);
   //Return the state of the pin
   return (input_A() & LEDregd);
}

So you have the two choices, of using the pin number to select which code to use inside the function, or of selecting the pin with a mask.

Best Wishes
RobM



Joined: 29 Apr 2005
Posts: 13
Location: Santa Cruz, CA

View user's profile Send private message

PostPosted: Sun May 01, 2005 6:26 pm     Reply with quote

Homer wrote:
i think you didn't understand my question ...

How do i have to set a function to have a pin as a variable ?

function(pin_1);
function(pin_2);


I read your discussion, it is interesting. I have a bunch of assembler experience, and zero c experience. I assembler, I would use a table for this problem.
This is a bar graph,
enter with the Bar Graph State Number in the W reg
Code:
      bcf   STATUS, RP0
      movwf   LedIndex
      movlw   LEDMASK
      iorwf   PORTA      ; LED off

      movlw    high ($+4)
      movwf   PCLATH   
      bcf   STATUS, C   ; multiply by 2
      rlf   LedIndex, w   ; and put in w
      addwf   PCL
      incf   countFlash      ; state 0
      goto   thrash_flash
      bcf   LED_RED                   ;  state 1
      goto   endLED
      incf   countFlash      ; state  2
      goto   flash
      bcf   LED_YEL   ;  state 3
      goto   endLED
      bcf   LED_GR    ;  state 4
      goto   endLED

In C, I guess this is a case statement. Why can't we make the Pin numbers enumerated types and then just use a case statement to steer the output?

These types of problems are why we design TOP DOWN and attempt to encapsulate the interface between objects. If a driver (for example) is written in that manner, then when it needs to be modified to work in run time with multiple set of hardware, only part of the code must be re-written. It is a sad fact that sometimes we have to duplicate blocks of code. Everything can't be done with switches if the code is to be readable

Regards

Rob
_________________
Rob
_______
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