Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
BadGranola
PostPosted: Jul 04, 2010 - 12:40 AM
Rookie


Joined: Sep 16, 2009
Posts: 39


Hi, I'm trying to build a 16-bit word made up from three integer variables; address, control (both in range 0x00-0x03), and data (0x00-0xFF).

The most significant four bits of the word are irrelevant, the next two hold the address, the next two the control information, and the final 8 hold the data.

Will something like this allow me to contruct the word from these variables?

Code:

word = (address<<10) | (control<<8) | data
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jul 04, 2010 - 01:15 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden

Have you tried it out? (For this kind of stuff the simulator in AVR Studio is excellent.)

But yes, that looks reasonable.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Koshchi
PostPosted: Jul 04, 2010 - 04:57 AM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13853
Location: Vancouver, BC

Or you could do this:
Code:
typefef struct
{
    unsigned int data :8;
    unsigned int control :2;
    unsigned int address :2;
} whateverYouWantToCallIt;
or:
Code:
typefef struct
{
    unsigned int :4; //4 bits of fiiler
    unsigned int control :2;
    unsigned int address :2;
    unsigned int data :8;
 } whateverYouWantToCallIt;
depending on endianness.
Code:
whateverYouWantToCallIt word;
...
word.address = address;
word.control = control;
word.data = data;

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
qli029
PostPosted: Jul 21, 2010 - 04:47 AM
Newbie


Joined: Jun 30, 2010
Posts: 13


This one doesnot make sense to me,

To set bit 0 in foo and then store the result back into foo:
Code:
Code:
foo = foo | 0x01;


bit OR"|" with 0x01, we will set bit 1 in foo,,
why is it to set bit 0?
I am a newbie, forgive me if i am wrong. please explain it to me.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jul 21, 2010 - 09:16 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden

[Removed as I totally mis-understood the post above. Snigelen nailed the correct interpretation and answer below.]


Last edited by JohanEkdahl on Jul 21, 2010 - 10:08 AM; edited 1 time in total
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
snigelen
PostPosted: Jul 21, 2010 - 09:55 AM
Posting Freak


Joined: Jan 08, 2009
Posts: 1160
Location: Lund, Sweden

qli029 wrote:
This one doesnot make sense to me,

To set bit 0 in foo and then store the result back into foo:
Code:
Code:
foo = foo | 0x01;


bit OR"|" with 0x01, we will set bit 1 in foo,,
why is it to set bit 0?

This is because bits in a byte are usually numbered from 0 to 7. So the first bit is number 0.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jul 21, 2010 - 10:53 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

Just to add a little more to that:
Code:
bit     shift     mask

0       (1<<0) =  0x01
1       (1<<1) =  0x02
2       (1<<2) =  0x04
3       (1<<3) =  0x08
4       (1<<4) =  0x10
5       (1<<5) =  0x20
6       (1<<6) =  0x40
7       (1<<7) =  0x80

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
iA7med
PostPosted: Jul 24, 2010 - 10:22 AM
Newbie


Joined: Feb 08, 2010
Posts: 12


That was useful , Thanks .
 
 View user's profile Send private message  
Reply with quote Back to top
violin_rules
PostPosted: Jan 09, 2011 - 02:01 AM
Newbie


Joined: Jan 02, 2011
Posts: 1


Thanks!, i was looking for this information.
 
 View user's profile Send private message  
Reply with quote Back to top
yaswanth2008
PostPosted: Mar 02, 2011 - 01:27 PM
Wannabe


Joined: Jun 05, 2009
Posts: 67
Location: bangalore, India

how should i access the registers R0-R32? using c programming.
Just give me an example..

Thank you
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
abcminiuser
PostPosted: Mar 02, 2011 - 01:32 PM
Moderator


Joined: Jan 23, 2004
Posts: 9832
Location: Trondheim, Norway

Quote:

how should i access the registers R0-R32? using c programming.
Just give me an example..


You don't - the point of C is that you don't need to directly access the CPU temporary registers. They are handled by the compiler in order to implement your code.

The I/O and peripheral registers (like the timer control registers) are manipulated by you, but not the CPU scratch registers.

- Dean Twisted Evil

_________________
Atmel Studio 6.1 is now released, grab it here.
Report AS6/ASF bugs here.
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Mar 02, 2011 - 02:12 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

To illustrate what Dean has said consider this program:
Code:
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
   uint8_t mask = 0x55;

   DDRB = 0xFF;
    while(1)
    {
         PORTB ^= mask;
       if (PIND & 1<<PD0) {
          mask ^= 0xFF;
       }
    }
}

when built by the compiler it generates:
Code:
00000080 <main>:

int main(void)
{
   uint8_t mask = 0x55;

   DDRB = 0xFF;
  80:   8f ef          ldi   r24, 0xFF   ; 255
  82:   84 b9          out   0x04, r24   ; 4
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
   uint8_t mask = 0x55;
  84:   85 e5          ldi   r24, 0x55   ; 85

   DDRB = 0xFF;
    while(1)
    {
         PORTB ^= mask;
  86:   95 b1          in   r25, 0x05   ; 5
  88:   98 27          eor   r25, r24
  8a:   95 b9          out   0x05, r25   ; 5
       if (PIND & 1<<PD0) {
  8c:   48 9b          sbis   0x09, 0   ; 9
  8e:   fb cf          rjmp   .-10        ; 0x86 <main+0x6>
          mask ^= 0xFF;
  90:   80 95          com   r24
  92:   f9 cf          rjmp   .-14        ; 0x86 <main+0x6>

In this code the compiler initially uses R24 to output 0xFF to DDRB. It then reuses R24 as 'mask' and initialises it to be 0x55. It reads PORTB into R25 then exclusive-or's this with 'mask' (R24). It then checks bit 0 of PIND and if that is set it complements the contents of 'mask' (so 0x55 switches to 0xAA).

You, the programmer, need never be aware which registers the compiler has chosen to put values or variables into.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
ghost800
PostPosted: Mar 30, 2011 - 06:51 PM
Newbie


Joined: Mar 30, 2011
Posts: 1


What this program does not work help me

Code:
//***********************************************************
// Project:  Analog Comparator example   
// Author: winavr.scienceprog.com   
// Module description: Analog comparator example with positive comparator
// input connected to Vref 1.23V. When compared voltage exceed 1.23V LED lighst on.
// When voltage drops bellow - LED turns OFF. Comparator inputis connected to ADC3 pin.
// ***********************************************************
#include <avr\io.h>       
#include <avr\interrupt.h> 
#define AINpin PA3
#define LED PD0
void Init(){
   DDRA&=~(1<<AINpin);//as input
   PORTA&=~(1<<AINpin);//no Pull-up
   DDRD|=(1<<LED); //Led pin as output
   PORTD|=(1<<LED);//Initally LED is OFF
   SFIOR|=(1<<ACME);//enable multiplexer
   ADCSRA&=~(1<<ADEN);//make sure ADC is OFF
   ADMUX|=(0<<MUX2)|(1<<MUX1)|(1<<MUX0); //select ADC3 as negative AIN
   ACSR|=
   (0<<ACD)|   //Comparator ON
   (1<<ACBG)|   //Connect 1.23V reference to AIN0
   (1<<ACIE)|   //Comparator Interrupt enable
   (0<<ACIC)|   //input capture disabled
   (0<<ACIS1)| //set interrupt on output toggle
   (0<<ACIS0);
   sei();//enable global interrupts
}
// Interrupt handler for ANA_COMP_vect
//
ISR(ANA_COMP_vect) {
if bit_is_clear(ACSR, ACO)
   PORTD&=~(1<<LED);//LED is ON
   else    PORTD|=(1<<LED);//LED is OFF
}
// ***********************************************************
// Main program
//
int main(void) {
     Init();
    while(1) {     // Infinite loop; interrupts do the rest
   }
}
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 30, 2011 - 07:54 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

ghost800,

How does your post extend the discussion of the article in the first post of this thread?

Moderator.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Mar 30, 2011 - 09:10 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden

Cliff!

When those post totally unrelated to the thread occur I think we should tossin an explanation of the difference between the "new topic" and the "new reply" buttons. I have a feeling that the distinction is not clear for many newcomers, or thet they even do not see the buttons and think that the only way to post on the forum is the "Quick Reply" area.

ghost800!

If you want to start a discussion on a new topic, use the "new topic" button at the top or bottom thread pages and forum listings here at AVRfreaks. Each discussion should commence in a separate "thread" (topic) or things become very confused and out of order. When creating a new thread please take the time to think out a really smart title (subject) so that it reflects the content of your post (extremely bad subjects are things like "Help!", "Why does my code not work" etc).

Hope this helps!
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
reaper420
PostPosted: Apr 01, 2011 - 07:27 AM
Newbie


Joined: Jul 21, 2010
Posts: 5


can the macros be used with ports?
for eg
Code:
int totalcomplete()
{
int retval;
if ((bit_get(PORTC, BIT(1))) && (bit_get(PORTC, BIT(0)))==0)
{
retval = 1;
}
else retval = 0;

return retval;
}
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Apr 01, 2011 - 10:33 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

I'm curious. What would make you want to type:
Code:
if ((bit_get(PORTC, BIT(1))) && (bit_get(PORTC, BIT(0)))==0)
{

when you could have typed:
Code:
if ((PORTC & 3) == 0)
{

Do the tortuous macros really help?

(BTW do you mean to read PORTC or PINC?)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
reaper420
PostPosted: Apr 01, 2011 - 08:15 PM
Newbie


Joined: Jul 21, 2010
Posts: 5


thanks! ya my mistake it should have been PINC .sorry but am new to programming an ya thanks i guess i could use
Code:
if ((PORTC & 3) == 0)
{
but what i meant was does this macro
Code:
#define bit_get(p,m) ((p) & (m))
work when a pin is specified in place of variable p. Or do we have to use macro such as
Code:
#define READ(port,pin) ((PIN ## port & (1<<pin))
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Apr 01, 2011 - 08:44 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

Yes and my question is why obfuscate code with layers of macros when the plain C to achieve the same would actually be simpler. There seems to be a recent trend to try and wrap code in N levels of macro at the drop of a hat. As someone who has to read/maintain other programmers code I find that it very rarely aids the mainatainability/readability of the code but instead just makes it more difficult to follow. Perhaps you can explain what how your bit_get() macro makes the code easier to follow. ALL programmers know what an AND operation is!

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
reaper420
PostPosted: Apr 01, 2011 - 09:02 PM
Newbie


Joined: Jul 21, 2010
Posts: 5


thanks, and well as u said it will be simpler to use plain c but the reason is that, i am working on a project and the panel where i have to present it to has a few members who have well little or no knowledge of programming so i am hoping the macros will help in making them understand more easily.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits