Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

passing an AVR port's bit pointer to to a function

passing an AVR port's bit pointer to to a function

2010-08-06 by Jeff Blaine

I have an encoder service function that reads a couple of bits from an AVR and increments a counter if a turn was decoded.

To make the function generic, I would like to pass the port bits as arguments to the function.

Unfortunately, I cannot quite figure out how to pass the port bits to the function.  Appreciate advice as to what I'm overlooking...

function prototype looks like this:

char encoder_service(char *avr_port_bit_A, char *avr_port_bit_B) {

}

In the main program, I would have something like this to call the function

x = char pointer to PORTA0
y = char pointer to PORTA1

encoder_service(   x   ,   y   );

Using standard C.

73/jeff/ac0c
www.ac0c.com


[Non-text portions of this message have been removed]

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-06 by erikc

Will the port bits  both be on the same port or can they be on 
different ports?  If so, it seems to me that the best way to go would 
be to pass the port bits as a bit mask (i.e. pin7 = 0x80 rather than 
pin7 = 7) and do it that way.

erikc

Jeff Blaine wrote:
Show quoted textHide quoted text
> I have an encoder service function that reads a couple of bits from an AVR and increments a counter if a turn was decoded.
> 
> To make the function generic, I would like to pass the port bits as arguments to the function.
> 
> Unfortunately, I cannot quite figure out how to pass the port bits to the function.  Appreciate advice as to what I'm overlooking...
> 
> function prototype looks like this:
> 
> char encoder_service(char *avr_port_bit_A, char *avr_port_bit_B) {
> 
> }
> 
> In the main program, I would have something like this to call the function
> 
> x = char pointer to PORTA0
> y = char pointer to PORTA1
> 
> encoder_service(   x   ,   y   );
> 
> Using standard C.
> 
> 73/jeff/ac0c
> www.ac0c.com

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-06 by Jeff Blaine

erikc,

In this project, yes they are together.  But that's not always the case.  If I can't find a better solution, then I will just set the routeene to work with a 2x4 pair and provide it an "encoder number" which would resolve to A0/A1 for encoder 1, A2/A3 for encoder 2, etc.  That's easy.  But given I use a rotary in almost everything now, I'm trying to move toward a function which would not have any prespecified index.  

Now that I think of it, I suppose I could set it up with arguments PORT, pin A, pin B.  

Something like this:  char encoder_service(char *avr_port, unsigned char bit_mask_A, unsigned char bit_mask_B) 

Which would then be called as (example):

x = encoder_service(&PORTA, 0x01, 0x02);

Think that would work?

73/jeff/ac0c
www.ac0c.com
Show quoted textHide quoted text
From: erikc 
Sent: Friday, August 06, 2010 7:45 AM
To: AVR-Chat@yahoogroups.com 
Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function


  
Will the port bits both be on the same port or can they be on 
different ports? If so, it seems to me that the best way to go would 
be to pass the port bits as a bit mask (i.e. pin7 = 0x80 rather than 
pin7 = 7) and do it that way.

erikc

Jeff Blaine wrote:
> I have an encoder service function that reads a couple of bits from an AVR and increments a counter if a turn was decoded.
> 
> To make the function generic, I would like to pass the port bits as arguments to the function.
> 
> Unfortunately, I cannot quite figure out how to pass the port bits to the function. Appreciate advice as to what I'm overlooking...
> 
> function prototype looks like this:
> 
> char encoder_service(char *avr_port_bit_A, char *avr_port_bit_B) {
> 
> }
> 
> In the main program, I would have something like this to call the function
> 
> x = char pointer to PORTA0
> y = char pointer to PORTA1
> 
> encoder_service( x , y );
> 
> Using standard C.
> 
> 73/jeff/ac0c
> www.ac0c.com





[Non-text portions of this message have been removed]

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-06 by erikc

I think so.  Are you trying to do something like this?
The code compiles under WINAVR.
(apologies for being a smartass  :D )

// code begins //

#include <avr\pgmspace.h>
#include <avr\io.h>
#include <avr\interrupt.h>

#define true 1
#define false 0

char    last_one;	// previous encoder sample
char    fault;		// goes high when both lines change at once
int     count;		// motion accumulator

void idle( void) {}     // dummy for encoder not moving

void encoder( char * port, char pin1, char pin2)
{
     char    this_one;	// current encoder sample
     char    bit_3, bit_2, bit_1, bit_0;

     last_one = this_one;
     this_one = *port;

     bit_3 = this_one & pin1;
     bit_2 = this_one & pin2;
     bit_1 = last_one & pin1;
     bit_0 = last_one & pin2;

     if( bit_3)
     {
         if( bit_2)
         {
             if( bit_1)
             {
                 if( bit_0) count++;
                 else idle();
             }
             else
             {
                 if( bit_0) fault = true;
                 else count--;
             }
         }
         else
         {
             if( bit_1)
             {
                 if( bit_0) idle();
                 else count--;
             }
             else
             {
                 if( bit_0) count++;
                 else fault = true;
             }
         }
     }
     else
     {
         if( bit_2)
         {
             if( bit_1)
             {
                 if( bit_0) fault = true;
                 else count++;
             }
             else
             {
                 if( bit_0) count--;
                 else idle();
             }
         }
         else
         {
             if( bit_1)
             {
                 if( bit_0) count--;
                 else fault = true;
             }
             else
             {
                 if( bit_0) idle();
                 else count++;
             }
         }
     }
}

int main( void)
{
     encoder( &PORTB, 0x08, 0x04);
}

// code ends //



Jeff Blaine wrote:
Show quoted textHide quoted text
> erikc,
> 
> In this project, yes they are together.  But that's not always the case.  If I can't find a better solution, then I will just set the routeene to work with a 2x4 pair and provide it an "encoder number" which would resolve to A0/A1 for encoder 1, A2/A3 for encoder 2, etc.  That's easy.  But given I use a rotary in almost everything now, I'm trying to move toward a function which would not have any prespecified index.  
> 
> Now that I think of it, I suppose I could set it up with arguments PORT, pin A, pin B.  
> 
> Something like this:  char encoder_service(char *avr_port, unsigned char bit_mask_A, unsigned char bit_mask_B) 
> 
> Which would then be called as (example):
> 
> x = encoder_service(&PORTA, 0x01, 0x02);
> 
> Think that would work?
> 
> 73/jeff/ac0c
> www.ac0c.com
> 
> 
> 
> From: erikc 
> Sent: Friday, August 06, 2010 7:45 AM
> To: AVR-Chat@yahoogroups.com 
> Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function
> 
> 
>   
> Will the port bits both be on the same port or can they be on 
> different ports? If so, it seems to me that the best way to go would 
> be to pass the port bits as a bit mask (i.e. pin7 = 0x80 rather than 
> pin7 = 7) and do it that way.
> 
> erikc
> 
> Jeff Blaine wrote:
>> I have an encoder service function that reads a couple of bits from an AVR and increments a counter if a turn was decoded.
>>
>> To make the function generic, I would like to pass the port bits as arguments to the function.
>>
>> Unfortunately, I cannot quite figure out how to pass the port bits to the function. Appreciate advice as to what I'm overlooking...
>>
>> function prototype looks like this:
>>
>> char encoder_service(char *avr_port_bit_A, char *avr_port_bit_B) {
>>
>> }
>>
>> In the main program, I would have something like this to call the function
>>
>> x = char pointer to PORTA0
>> y = char pointer to PORTA1
>>
>> encoder_service( x , y );
>>
>> Using standard C.
>>
>> 73/jeff/ac0c
>> www.ac0c.com

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-06 by Robert Adsett

On 8/6/2010 3:16 AM, Jeff Blaine wrote:
> I have an encoder service function that reads a couple of bits from an AVR and increments a counter if a turn was decoded.
>
> To make the function generic, I would like to pass the port bits as arguments to the function.
>
> Unfortunately, I cannot quite figure out how to pass the port bits to the function.  Appreciate advice as to what I'm overlooking...
>
> function prototype looks like this:
>
> char encoder_service(char *avr_port_bit_A, char *avr_port_bit_B) {
>
> }
>
> In the main program, I would have something like this to call the function
>
> x = char pointer to PORTA0
> y = char pointer to PORTA1
>
> encoder_service(   x   ,   y   );
>
> Using standard C.

Why not just pass the state of the pins?

Robert

-- 
http://www.aeolusdevelopment.com/

  From the Divided by a Common Language File (Edited to protect the guilty)
ME - "I'd like to get Price and delivery for connector Part # XXXXX"
Dist./Rep - "$X.XX Lead time 37 days"
ME - "Anything we can do about lead time?  37 days seems a bit high."
Dist./Rep - "that is the lead time given because our stock is live....
we currently have stock."

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-07 by Jeff Blaine

Erikc,

Yes, exactly like that.  Thanks!

73/jeff/ac0c
www.ac0c.com
Show quoted textHide quoted text
From: erikc 
Sent: Friday, August 06, 2010 6:20 PM
To: AVR-Chat@yahoogroups.com 
Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function


  
I think so. Are you trying to do something like this?
The code compiles under WINAVR.
(apologies for being a smartass :D )

// code begins //

#include <avr\pgmspace.h>
#include <avr\io.h>
#include <avr\interrupt.h>

#define true 1
#define false 0

char last_one; // previous encoder sample
char fault; // goes high when both lines change at once
int count; // motion accumulator

void idle( void) {} // dummy for encoder not moving

void encoder( char * port, char pin1, char pin2)
{
char this_one; // current encoder sample
char bit_3, bit_2, bit_1, bit_0;

last_one = this_one;
this_one = *port;

bit_3 = this_one & pin1;
bit_2 = this_one & pin2;
bit_1 = last_one & pin1;
bit_0 = last_one & pin2;

if( bit_3)
{
if( bit_2)
{
if( bit_1)
{
if( bit_0) count++;
else idle();
}
else
{
if( bit_0) fault = true;
else count--;
}
}
else
{
if( bit_1)
{
if( bit_0) idle();
else count--;
}
else
{
if( bit_0) count++;
else fault = true;
}
}
}
else
{
if( bit_2)
{
if( bit_1)
{
if( bit_0) fault = true;
else count++;
}
else
{
if( bit_0) count--;
else idle();
}
}
else
{
if( bit_1)
{
if( bit_0) count--;
else fault = true;
}
else
{
if( bit_0) idle();
else count++;
}
}
}
}

int main( void)
{
encoder( &PORTB, 0x08, 0x04);
}

// code ends //

Jeff Blaine wrote:
> erikc,
> 
> In this project, yes they are together. But that's not always the case. If I can't find a better solution, then I will just set the routeene to work with a 2x4 pair and provide it an "encoder number" which would resolve to A0/A1 for encoder 1, A2/A3 for encoder 2, etc. That's easy. But given I use a rotary in almost everything now, I'm trying to move toward a function which would not have any prespecified index. 
> 
> Now that I think of it, I suppose I could set it up with arguments PORT, pin A, pin B. 
> 
> Something like this: char encoder_service(char *avr_port, unsigned char bit_mask_A, unsigned char bit_mask_B) 
> 
> Which would then be called as (example):
> 
> x = encoder_service(&PORTA, 0x01, 0x02);
> 
> Think that would work?
> 
> 73/jeff/ac0c
> www.ac0c.com
> 
> 
> 
> From: erikc 
> Sent: Friday, August 06, 2010 7:45 AM
> To: AVR-Chat@yahoogroups.com 
> Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function
> 
> 
> 
> Will the port bits both be on the same port or can they be on 
> different ports? If so, it seems to me that the best way to go would 
> be to pass the port bits as a bit mask (i.e. pin7 = 0x80 rather than 
> pin7 = 7) and do it that way.
> 
> erikc
> 
> Jeff Blaine wrote:
>> I have an encoder service function that reads a couple of bits from an AVR and increments a counter if a turn was decoded.
>>
>> To make the function generic, I would like to pass the port bits as arguments to the function.
>>
>> Unfortunately, I cannot quite figure out how to pass the port bits to the function. Appreciate advice as to what I'm overlooking...
>>
>> function prototype looks like this:
>>
>> char encoder_service(char *avr_port_bit_A, char *avr_port_bit_B) {
>>
>> }
>>
>> In the main program, I would have something like this to call the function
>>
>> x = char pointer to PORTA0
>> y = char pointer to PORTA1
>>
>> encoder_service( x , y );
>>
>> Using standard C.
>>
>> 73/jeff/ac0c
>> www.ac0c.com




[Non-text portions of this message have been removed]

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-07 by Steven Holder

On 06/08/2010 22:55, Jeff Blaine wrote:
>
> erikc,
>
> In this project, yes they are together. But that's not always the 
> case. If I can't find a better solution, then I will just set the 
> routeene to work with a 2x4 pair and provide it an "encoder number" 
> which would resolve to A0/A1 for encoder 1, A2/A3 for encoder 2, etc. 
> That's easy. But given I use a rotary in almost everything now, I'm 
> trying to move toward a function which would not have any prespecified 
> index.
>
> Now that I think of it, I suppose I could set it up with arguments 
> PORT, pin A, pin B.
>
> Something like this: char encoder_service(char *avr_port, unsigned 
> char bit_mask_A, unsigned char bit_mask_B)
>
> Which would then be called as (example):
>
> x = encoder_service(&PORTA, 0x01, 0x02);
>
> Think that would work?
>
> 73/jeff/ac0c
> www.ac0c.com
>
> From: erikc
> Sent: Friday, August 06, 2010 7:45 AM
> To: AVR-Chat@yahoogroups.com <mailto:AVR-Chat%40yahoogroups.com>
> Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function
>
> Will the port bits both be on the same port or can they be on
> different ports? If so, it seems to me that the best way to go would
> be to pass the port bits as a bit mask (i.e. pin7 = 0x80 rather than
> pin7 = 7) and do it that way.
>
> erikc
>
> Jeff Blaine wrote:
> > I have an encoder service function that reads a couple of bits from 
> an AVR and increments a counter if a turn was decoded.
> >
> > To make the function generic, I would like to pass the port bits as 
> arguments to the function.
> >
> > Unfortunately, I cannot quite figure out how to pass the port bits 
> to the function. Appreciate advice as to what I'm overlooking...
> >
> > function prototype looks like this:
> >
> > char encoder_service(char *avr_port_bit_A, char *avr_port_bit_B) {
> >
> > }
> >
> > In the main program, I would have something like this to call the 
> function
> >
> > x = char pointer to PORTA0
> > y = char pointer to PORTA1
> >
> > encoder_service( x , y );
> >
> > Using standard C.
> >
> > 73/jeff/ac0c
> > www.ac0c.com
>
> [Non-text portions of this message have been removed]
>
> 
Make sure you define the port as volatile in the function as volatile, 
(gcc lib 1.6.6 p320)

Regards



[Non-text portions of this message have been removed]

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-07 by erikc

Mind if I ask what you are doing with this kind of code?

In my case, I'm trying to come up with a digital readout for my mini-mill.

erikc

Jeff Blaine wrote:
Show quoted textHide quoted text
> Erikc,
> 
> Yes, exactly like that.  Thanks!
> 
> 73/jeff/ac0c
> www.ac0c.com
> 
> 
> 
> From: erikc 
> Sent: Friday, August 06, 2010 6:20 PM
> To: AVR-Chat@yahoogroups.com 
> Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function
> 
> 
>   
> I think so. Are you trying to do something like this?
> The code compiles under WINAVR.
> (apologies for being a smartass :D )

Re: [AVR-Chat] passing an AVR port's bit pointer to to a function

2010-08-07 by Jeff Blaine

Erikc,

The AVR provides clock sources for a set of switched capacitor filters used in a communications project.

The AVR takes the 3 rotary encoder and 5 buttons as inputs, runs some switching control lines and provides these clock signals which determine what frequency the filter is tuned too.  And it provides an LCD output to the user so you can know what the filter configuration is.

There are some pics and a topology shot here:

http://ac0c.com/main/page_ft2k_roofing_filters_add_apf_to_the_sub_rx.html

73/jeff/ac0c
www.ac0c.com
Show quoted textHide quoted text
From: erikc 
Sent: Saturday, August 07, 2010 7:42 AM
To: AVR-Chat@yahoogroups.com 
Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function


  
Mind if I ask what you are doing with this kind of code?

In my case, I'm trying to come up with a digital readout for my mini-mill.

erikc

Jeff Blaine wrote:
> Erikc,
> 
> Yes, exactly like that. Thanks!
> 
> 73/jeff/ac0c
> www.ac0c.com
> 
> 
> 
> From: erikc 
> Sent: Friday, August 06, 2010 6:20 PM
> To: AVR-Chat@yahoogroups.com 
> Subject: Re: [AVR-Chat] passing an AVR port's bit pointer to to a function
> 
> 
> 
> I think so. Are you trying to do something like this?
> The code compiles under WINAVR.
> (apologies for being a smartass :D )




[Non-text portions of this message have been removed]

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.