Yahoo Groups archive

AVR-Chat

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

Message

RE: [AVR-Chat] Struct ?

2006-06-04 by Colin Paul Gloster

Kathy Quinlan wrote on Saturday, June 03, 2006:

"I am trying to find the best way to work around a few problems.

[..]"

You did not really describe an actual problem.

"As this chip works in 16 bit words on SPI, The first byte is a constant
for each instruction, I would like to use in my subroutines the
assembler equivalent of Master_Volume (low) and (high), with the high
byte containing the actual volume for the master Smile

Some of these pairs the second pair is more of a 4bit variable and a few
bits. Can I access these parts as Master_Volume,8 etc ?

Can I assign a new name like Master_Volume.8 = BSW ?"

That is not legal C. Bit 8 of Master_Volume is Bit 0 of Master_Volume (high)
so if you wanted to change just Bit 8 you could have for example
uint16_t Master_Volume;
const uint8_t BSW = 8;

uint8_t new_value_for_a_single_bit = 1; /*Alternatively 0.*/

Master_Volume &= 0xffff & (new_value_for_a_single_bit << BSW);

If the four bit field is located in Bits 9; 10; 11; and 12 you could have
const uint16_t the_bits_of_some_four_bit_field_in_Master_Volume = 
(1<<12)|(1<<11)|(1<<10)|(1<<9);
const uint16_t 
the_least_significant_bit_of_some_four_bit_field_in_Master_Volume = 9;

uint8_t new_value_for_a_four_bit_field = 0xf; /*Alternatively an 
integer from 14 down to 0.*/
Master_Volume &= 0xffff & 
(the_bits_of_some_four_bit_field_in_Master_Volume &
(new_value_for_a_four_bit_field<<
the_least_significant_bit_of_some_four_bit_field_in_Master_Volume));

On Sat, 3 Jun 2006, Larry Barello wrote an example with bitfields. 
Bitfields provide better protection against accidentally accessing 
irrelevant bits but are discouraged by some, consult e.g. Kernighan, B. W. 
and Ritchie, D. M.: 1988, "The C Programming Language", 2nd edn, Prentice 
Hall, ISBN 0131103628; Gloster, Colin Paul, ``Re: C question'', 27
   Jul 2005 14:55:15 +0200,
Message-ID: <86hdegtnm4.fsf@Delta-Utec_PC5_FreeBSD.lan> ,
news:comp.arch.embedded

Erasmus, Anton, ``Re: C question'', Wed, 27
   Jul 2005 21:31:48 +0200,
   Message-ID: <1122492497.df9eec6125c7704a9631ca6a430e37c9@teranews> ,
news:comp.arch.embedded

Ashley, David T., ``Re: C question'',
Wed, 27 Jul 2005 23:30:19 -0400,
   Message-ID: <edYFe.14487$ES5.6088@fe32.usenetserver.com> ,
news:comp.arch.embedded

``accu-general: saving global bit
   fields to query later'',
Message-ID: <31d4d63b05080913265232501b@mail.gmail.com> ,
Tue, 9 Aug 2005 16:26:52 -0400, the accu-general email list of the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists

``Re: accu-general: saving global
   bit fields to query later'', Message-Id:
   <20751E28-54DA-462D-9BBC-B9BFCC207CC8@boiler.home> ,
   Thu, 11 Aug 2005 05:57:19 +0100, the accu-general email list of the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists

``Re: accu-general: saving global
   bit fields to query later'', Message-ID: 
<31d4d63b05081110134c721084@mail.gmail.com>
   , Thu, 11 Aug 2005 13:13:21 -0400, the accu-general email list of
   the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists

Gloster, Colin Paul, ``Re: accu-general: saving global
   bit fields to query later'', Date: Fri, 12 Aug 2005 10:09:42 +0200,
Message-ID: <42F3087F000071CA@hawk.dcu.ie> ,
the accu-general email list of
   the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists

``Re: accu-general:
   saving global
   bit fields to query later'',
Message-ID: <31d4d63b05081205201ecd999e@mail.gmail.com> ,
Fri, 12 Aug 2005 08:20:20 -0400, the accu-general email list of
   the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists

Gloster, Colin Paul, ``Re: accu-general:
   saving global
   bit fields to query later'', Fri, 12 Aug 2005 15:14:38 +0200,
Message-ID: <42F3087F00007A98@hawk.dcu.ie>
, the accu-general email list of
   the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists

``Re: accu-general:
   saving global
   bit fields to query later'', Message-ID: 
<31d4d63b05081207155b75367f@mail.gmail.com>
, Fri, 12 Aug 2005 10:15:09 -0400, the accu-general email list of
   the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists

``Re: accu-general:
   saving global
   bit fields to query later'', the accu-general email list of
   the
   Association of C & C++ Users, ACCU.org/index.php/mailinglists ,
Message-Id: <A996A965-51AD-4B8F-9A2A-73A7D501FD06@boiler.home>
, Fri, 12 Aug 2005 14:48:40 +0100.

If you like the lexical convenience or the protection of bitfields, 
perhaps you would prefer the well defined nature of Ada. E.g.
type Some_Datatype_for_the_New_Japan_Radio_NJW1157B is
   record
     Some_Eight_Bit_Field : Unsigned_8;
     BSW : Bits_1;
     some_four_bit_field_in_Master_Volume : Bits_4;
     Another_Flag : Bits_1;
     Yet_Another_Flag : Bits_1;
     Still_Yet_Another_Flag : Bits_1;
   end record;

--Two dashes start a comment. The definition of 
--Some_Datatype_for_the_New_Japan_Radio_NJW1157B above is sufficient for 
--ordinary use, but the optional next few lines are needed to guarantee 
--the exact layout of the bits. Consult 
-- http://www.adaic.org/standards/95lrm/html/RM-13-5-1.html

for Some_Datatype_for_the_New_Japan_Radio_NJW1157B use
   record
     Some_Eight_Bit_Field at 0 range 0..7;
     BSW at 0 range 8..8;
     some_four_bit_field_in_Master_Volume at 0 range 9..12;
     Another_Flag at 0 range 13..13;
     Yet_Another_Flag at 0 range 14..14;
     Still_Yet_Another_Flag at 0 range 15..15;
   end record;

new_value_for_a_single_bit : Bits_1 := 1;
Master_Volume : Some_Datatype_for_the_New_Japan_Radio_NJW1157B;

Master_Volume.BSW := new_value_for_a_single_bit;

new_value_for_a_four_bit_field : Bits_4 := 16#f#;
Master_Volume.some_four_bit_field_in_Master_Volume := 
new_value_for_a_four_bit_field;

It is safer than anything possible in C.

Kathy Quinlan wrote on Saturday, June 03, 2006:

"What is the best way to describe this so I can just shunt it out the SPI
port, also can I send an int to the SPI port and have CVAVR break it
into 2 8 bit values, to send and receive back my 2 8 bit variables ?"

You will not lose a variable's value by using the SPI (the SPI returns 
a value from the slave (your New Japan Radio NJW1157B), which is probably 
not the same value as Master_Volume which the AVR wrote to the SPI).

I do not know whether CodeVisionAVR provides a function for writing 
sixteen bits to the SPI, but you could write it yourself so that you could 
use it if you use a different vendor later.

uint16_t write_16_bits_to_the_SPI_and_read_16_bits_from_the_SPI (uint16_t 
from_the_AVR_to_the_SPI_only)
{
   uint16_t response;
   SPDR = 0x00ff & from_the_AVR_to_the_SPI_only; /*I got the impression 
that you want to send Master_Volume (low) first.*/ /*Function DF_SPI_RW in 
AVR-Ada.*/
   response = 0x00ff & SPDR;
   SPDR &= from_the_AVR_to_the_SPI_only >> 8;
   response |= SPDR << 8;
   return response;
}

uint16_t answer_from_the_New_Japan_Radio_NJW1157B;
answer_from_the_New_Japan_Radio_NJW1157B = 
write_16_bits_to_the_SPI_and_read_16_bits_from_the_SPI (Master_Volume);

Attachments

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.