register definition in MCU header file: PIC vs AVR

Go To Last Post
40 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pic18f14k50.h:

// Register: PORTC
#define PORTC PORTC
extern volatile unsigned char           PORTC               __at(0xF82);
#ifndef _LIB_BUILD
asm("PORTC equ 0F82h");
#endif
// bitfield definitions
typedef union {
    struct {
        unsigned RC0                    :1;
        unsigned RC1                    :1;
        unsigned RC2                    :1;
        unsigned RC3                    :1;
        unsigned RC4                    :1;
        unsigned RC5                    :1;
        unsigned RC6                    :1;
        unsigned RC7                    :1;
    };
    struct {
        unsigned AN4                    :1;
        unsigned AN5                    :1;
        unsigned AN6                    :1;
        unsigned AN7                    :1;
        unsigned                        :2;
        unsigned AN8                    :1;
        unsigned AN9                    :1;
    };
    struct {
        unsigned                        :6;
        unsigned NOT_SS                 :1;
    };
    struct {
        unsigned C12INP                 :1;
        unsigned C12IN1M                :1;
        unsigned C12IN2M                :1;
        unsigned C12IN3M                :1;
        unsigned C12OUT                 :1;
        unsigned CCP1                   :1;
        unsigned nSS                    :1;
    };
    struct {
        unsigned INT0                   :1;
        unsigned INT1                   :1;
        unsigned INT2                   :1;
        unsigned PGM                    :1;
        unsigned SRQ                    :1;
        unsigned T0CKI                  :1;
        unsigned T13CKI                 :1;
        unsigned T1OSCO                 :1;
    };
    struct {
        unsigned VREFP                  :1;
        unsigned VREFM                  :1;
        unsigned CVREF                  :1;
        unsigned                        :3;
        unsigned T1OSCI                 :1;
    };
    struct {
        unsigned                        :2;
        unsigned P1D                    :1;
        unsigned P1C                    :1;
        unsigned P1B                    :1;
        unsigned P1A                    :1;
        unsigned SS                     :1;
        unsigned SDO                    :1;
    };
    struct {
        unsigned                        :1;
        unsigned CCP2                   :1;
        unsigned PA1                    :1;
    };
    struct {
        unsigned                        :1;
        unsigned PA2                    :1;
    };
} PORTCbits_t;
extern volatile PORTCbits_t PORTCbits __at(0xF82);
// bitfield macros
// Lots of macros...

main.c:

if (PORTCbits.RC0 == 1)
{
    LATCbits.LATC1 = 0;
}

iom328p.h:

#define PINC _SFR_IO8(0x06)
#define PINC0 0
#define PINC1 1
#define PINC2 2
#define PINC3 3
#define PINC4 4
#define PINC5 5
#define PINC6 6

main.c:

if (PINC & (1 << PINC0))
{
    PORTC |= (1 << PORTC1);
}

I personally prefer the way PIC implemented ports. It is more human-readable and easier to use.

Why AVR didn't implement registers this way?

Slow and Steady!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why ask us?  We didn't make the decision! Besides, it's not set in stone - you can do it you way if you like.

 

You might want to look at the xmegas and later model AVRs where the bit field/structure  thing is used.

 

Personally I preferred the 8051 way that was implemented in CodeVision in that every port bit is a variable.

Last Edited: Tue. Oct 27, 2020 - 06:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

This is nothing to do with how the chip works; it's a function of the compiler header files. Nothing stops you writing your own headers.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
I preferred the 8051 way that was implemented in CodeVision in that every port bit is a variable.

Sadly, standard 'C' doesn't really allow that.

 

pajuhesh80 wrote:
Why AVR didn't implement registers this way?

As already noted, it is nothing to do with the AVR - it is simply the way the code was written.

 

It could be historical - maybe there weren't efficient implementations of bitfields at the time?

 

As K&R wrote:
Almost everything about [bit] fields is implementation-dependent.

Whether a field may overlap a word boundary is implementation-defined ...

Fields are assigned left-to-right on some machines, and right-to-left on others.

 

Maybe there wasn't one dominant compiler back then to make it worth the effort of trying to devise a representation that worked on all?

 

etc, etc, ...

 

You should try googling for some of the arguments against it; eg, https://blogs.sw.siemens.com/emb...

 

As the others have said, there's nothing to stop you writing your own code this way if you wish.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
there's nothing to stop you writing your own code this way

In fact, I think @clawson has done this already ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

In fact, I think @clawson has done this already ... ?

Indeed I have:

 

https://www.avrfreaks.net/forum/...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I agree 100%.   This is a far more intuitive way to access Special Function Registers.

if (PORTCbits.RC0 == 1)
{
    LATCbits.LATC1 = 0;
}

The AVR way is largely historic.

if (PINC & (1 << PINC0))
{
    PORTC |= (1 << PORTC1);
}

Both are perfectly legal C.   Modern Compilers will generate efficient code for both.

Early compilers were not as good.   Bitfields are implementation dependent.   If your Compiler packed bitfields differently,  you would need a different set of header files to match.

 

I suggest that you just live with what you have got.   But if it really upsets you,   generate your own header files.    Or pre-process your coding style to suit the AVR convention.

 

Introducing extra "translation stages" is likely to produce strange results occasionally.    Which makes debugging difficult.

 

Incidentally,  accessing register bits with a number e.g. PORTC.1 is not legal C.    But looks nicer than PORTC.PORTC1 or PORTC.b1

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
accessing register bits with a number e.g. PORTC.1 is not legal C.   

although CodeVision, I think, does allow it as a proprietary language extension ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that the utility I linked to will define things such as:

	union {
		uint8_t all; // (@ 0x37) Port B Data Direction Register
		struct {
			unsigned int b0:1;
			unsigned int b1:1;
			unsigned int b2:1;
			unsigned int b3:1;
			unsigned int b4:1;
			unsigned int b5:1;
			unsigned int b6:1;
			unsigned int b7:1;
		} bits;
	} _DDRB;

and

	union {
		uint8_t all; // (@ 0x4e) Timer/Counter1 Control Register B
		struct {
			unsigned int _CS10:1; // b0 Prescaler source of Timer/Counter 1
			unsigned int _CS11:1; // b1 Prescaler source of Timer/Counter 1
			unsigned int _CS12:1; // b2 Prescaler source of Timer/Counter 1
			unsigned int _WGM12:1; // b3 Waveform Generation Mode
			unsigned int _WGM13:1; // b4 Waveform Generation Mode
			unsigned int unused5:1; // b5
			unsigned int _ICES1:1; // b6 Input Capture 1 Edge Select
			unsigned int _ICNC1:1; // b7 Input Capture 1 Noise Canceler
		} bits;
	} _TCCR1B;

(and the same for every bit in every register mentioned in the ATDF file)

 

Also it further defines for each such structure things like:

#define ddrb pSFR->_DDRB.all
#define ddrb_b0 pSFR->_DDRB.bits.b0
#define ddrb_b1 pSFR->_DDRB.bits.b1
#define ddrb_b2 pSFR->_DDRB.bits.b2
#define ddrb_b3 pSFR->_DDRB.bits.b3
#define ddrb_b4 pSFR->_DDRB.bits.b4
#define ddrb_b5 pSFR->_DDRB.bits.b5
#define ddrb_b6 pSFR->_DDRB.bits.b6
#define ddrb_b7 pSFR->_DDRB.bits.b7

and

#define tccr1b pSFR->_TCCR1B.all
#define tccr1b_cs10 pSFR->_TCCR1B.bits._CS10
#define cs10 (1 << 0)
#define cs10_bp 0
#define tccr1b_cs11 pSFR->_TCCR1B.bits._CS11
#define cs11 (1 << 1)
#define cs11_bp 1
#define tccr1b_cs12 pSFR->_TCCR1B.bits._CS12
#define cs12 (1 << 2)
#define cs12_bp 2
#define tccr1b_wgm12 pSFR->_TCCR1B.bits._WGM12
#define wgm12 (1 << 3)
#define wgm12_bp 3
#define tccr1b_wgm13 pSFR->_TCCR1B.bits._WGM13
#define wgm13 (1 << 4)
#define wgm13_bp 4
#define tccr1b_ices1 pSFR->_TCCR1B.bits._ICES1
#define ices1 (1 << 6)
#define ices1_bp 6
#define tccr1b_icnc1 pSFR->_TCCR1B.bits._ICNC1
#define icnc1 (1 << 7)
#define icnc1_bp 7

then you could use:

#include "generatedHeader.h"

USE_SFRS();

int main(void) {
    ddrb = 0x55;
    ddrb_b7 = 1;
    tccr1b_wgm12 = 1;
    tccr1b |= cs10 | cs11;

}

 

Last Edited: Tue. Oct 27, 2020 - 10:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pajuhesh80 wrote:
I personally prefer the way PIC implemented ports.

and others detest it; eg, https://community.atmel.com/comm...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

It's like a variant of the Endian Wars from the good ol' days!

 

Steve

 

Maverick Embedded Technologies Ltd. Home of Maven and wAVR.

Maven: WiFi ARM Cortex-M Debugger/Programmer

wAVR: WiFi AVR ISP/PDI/uPDI Programmer

https://www.maverick-embedded.co...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

scdoubleu wrote:
It's like a variant of the Endian Wars from the good ol' days!

It depends which brand of compiler is used for the header files.  [and indentation -- tabs or spaces]

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

theusch wrote:
indentation -- tabs or spaces

Well, ...

 

cheeky

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I personally prefer the way PIC implemented ports.

Are you talking about having structures mapped onto the io registers in general, or the massive union of all possible names for the bits that represent pins?

The latter is less common, and a bit weird.

I'm not at all sure how I feel about

   val = PORTC.AN1;

giving a digital value.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Brian Fairchild wrote:
This is nothing to do with how the chip works;

This is one the things I wanted to make sure. I thought it might be this way because of instruction set or etc. I've seen some bit instructions in AVR instruction set manual.

 

Brian Fairchild wrote:
Nothing stops you writing your own headers.

Maybe I I'll try it.

 

clawson wrote:

Indeed I have:

https://www.avrfreaks.net/forum/...

yes

 

david.prentice wrote:

I suggest that you just live with what you have got.

Most inventions were created because inventor didn't want to live with what he/she have got! cheeky

 

theusch wrote:

indentation -- tabs or spaces

I don't understand why some editors use spaces as default indentation instead of tabs. Tab looks neater (if you enable show all characters for an unknown reason!) and they are just one character; so basically they should occupy some less bytes than multiple spaces!

 

Slow and Steady!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

theusch wrote:
indentation -- tabs or spaces

 

Tabs in the Makefile, spaces for everything else. I never know if the editor at hand will treat a tab as 8 spaces or 4 so I do my best to avoid Tabs. VScode (default) uses spaces rather than tabs (except when it is a Makefile), and shows tabs as 4 spaces, which is just the way I want it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:

Are you talking about having structures mapped onto the io registers in general, or the massive union of all possible names for the bits that represent pins?

No - the Microchip headers for PIC don't do that: Here's the PORTC struct for example:

#define PORTC PORTC
extern volatile uint16_t  PORTC __attribute__((__sfr__));
typedef struct tagPORTCBITS {
  uint16_t :1;
  uint16_t RC1:1;
  uint16_t RC2:1;
  uint16_t RC3:1;
  uint16_t RC4:1;
  uint16_t :7;
  uint16_t RC12:1;
  uint16_t RC13:1;
  uint16_t RC14:1;
  uint16_t RC15:1;
} PORTCBITS;
extern volatile PORTCBITS PORTCbits __attribute__((__sfr__));

Note the use of uint16_t because it's a 16-bit microcontroller and the unimplemented pins in the 16-bit PORT register.

 

 

I believe if this scheme had been implemented on AVR many years ago the number of questions on port bit twiddling here on AVRfeaks would be far fewer.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pajuhesh80 wrote:
I don't understand why some editors use spaces as default indentation instead of tabs

because tabs have no standard meaning; there is no standard definition of what a TAB character should expand to.

 

This is why the indentation is so often messed up in code posted to forums - because the forum has a different TAB expansion to the poster's original editor.

 

Spaces are entirely predictable,  portable, and consistent: if you use spaces, you are guaranteed that your indentation will look the same irrespective of what is used to view it, and how that viewer is configured.

 

and they are just one character; so basically they should occupy some less bytes than multiple spaces!

That is true, but entirely irrelevant in these days of Terabyte storage!

 

The exception, as  ron_sutherland  says, is makefiles - which specifically require TABs.

 

I think Python (or some implementations?) needs you to be consistent - use either TABs, or spacces, but not a mixture of both.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

awneil wrote:
This is why the indentation is so often messed up in code posted to forums - because the forum has a different TAB expansion to the poster's original editor.

For example; here is some code in Notepad++

Note how everything is nicely lined-up:

 

To show that I am using TABs:

 

If I copy & paste that text into the forum:

void func( void )
{
	int		a;					// blah blah
	uint8_t	unsigned_thingy;	// blah blah
	int8_t	singed_thingy;		// blah blah
}

You can see that the alignments is all messed-up - because the TAB interpretation is different.

 

I rest my case.

 

 

EDIT

 

If I "fix" the text to display correctly here:

void func( void )
{
	int	a;			// blah blah
	uint8_t	unsigned_thingy;	// blah blah
	int8_t	singed_thingy;		// blah blah
}

and then copy & paste that to my Notepad++:

it's messed up again!

 

 

All of this would be avoided by just using spaces!

 

QED.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Oct 28, 2020 - 09:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ron_sutherland wrote:
I never know if the editor at hand will treat a tab as 8 spaces or 4

Or, in fact, anything else!

 

It's not uncommon for people to set TABs to 2 or 3 spaces.

 

Some editors allow you to set TABs at completely arbitrary positions - not just regular increments along the line.

 

Of course, any decent editor can be set to insert the appropriate number of spaces when you hit the TAB button on the keyboard

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


 

if using AS7 (or any form of Visual Studio) the first port of call after you have just installed it should generally be:

 

 

As others have said, if you don't do this then viewing someone else's code is a bit of crapshoot with the indentation if it's full of /t and they had tabs set to something other than the thing you are viewing it with interprets tabs then indentation will be all over the place. With a definite number of spaces all is right in the world.

 

One other really useful option in Visual Studio derived tools:

 

 

That will convert any rogue tabs that "got in" to more acceptable space indentation.

 

Oh and while I'm at it, this:

 

 

Do not create "messy" code with trailing whitespace. It serves no purpose (apart from to sometimes upset "diff" tools!)

 

EDIT: forgot to say that another good one on the Edit-Advanced menu is the "View white space" option. In the file I was looking at:

 

it reveals the editor was mistakenly set to insert tabs. But after using Untabify...

which is much more satisfying.

 

(philosophical question - why is the option to "View white space" not on the View menu - I always have to go hunting to find it when it's not in the obvious place!)

Last Edited: Wed. Oct 28, 2020 - 09:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Corresponding features in Notepad++:

 

 

 

 

 

EDIT

 

Eclipse has similar facilities, but with the added confusion of whether the setting is global, per-project, per-language, or whatever ... surprise

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Oct 28, 2020 - 09:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
I rest my case.

Sheesh -- I just tossed that in along with Endian and Compiler Wars.  Noone went off on those.

 

Remember that using Whitesmith's correct indentation convention, along with tabs, in the source code fed to the CodevisionAVR C compiler, makes for a better quality AVR app.

 

A matter of national security needing the attention of NCIS:

 

NCIS: Los Angeles season 9 episode 20 review: Out of the woods

Tonight’s NCIS: Los Angeles episode had it all: Sam and Callen getting a meaty case alongside Nell, a tabs-vs-spaces argument that brought to mind Silicon Valley, and then also a more personal, emotional story that suggested that Hetty may be in trouble.

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Wed. Oct 28, 2020 - 01:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

awneil wrote:

I rest my case.

Sheesh -- I just tossed that in along with Endian and Compiler Wars.  Noone went off on those.

Go on - you were poking at a wasps nest

 

cheeky

 

Note #13 - it was the OP who decided to pick it up & run with it!

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Oct 28, 2020 - 01:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Well, there is this

Computer programmers who use spaces as part of their coding earn $15,370 (£12,000) more per year than those who use tabs, a survey of developers has revealed.

https://www.bbc.com/news/technology-40302410  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use tabs for indentation and spaces for alignment. Then everyone can happily use the tab size of their choice in their own editor and the code still looks OK.

 

See, for example, Indent with tabs, align with spaces.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Finding a couple mentions on the Internet, and right away y'all take it as universal axioms.  [My health-care professional calls it "consulting Dr. Google".]  For example, three people in the world claim that Donald Trump is worthy of a Nobel Peace Prize.  I saw it on the Internet so it must be a worthy claim. 

 

Aren't we all going to quote those that agree with us? 

 

Think of how many hours and days and months of development effort that I saves over my life in programming by not fussing with countless piles of spaces.

 

BTW, and y'all can convince me one way or another, but the past decades I've gotten into the habit of not necessarily indenting the top level, e.g. variable definitions inside functions, and the top level inside functions.  You kids need to remember that punch cards only had 80 columns, and most terminals and PC screens as well.  "Wasting" that precious ~10% and forcing the onerous shifting that screen view left-and-right seemed wrong to me.  The perhaps obvious drawback is having function declarations camouflaged.  But how many do you have that aren't first code in the file, or can easily be jumped to with your smartish editor?

 

And Whitesmiths is the proper way to indent.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Wed. Oct 28, 2020 - 08:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But you don't type any more than you did before? It's just that when you hit Tab it inserts between 1 and 4 (or whatever you set it to) spaces for you.??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
But you don't type any more than you did before? It's just that when you hit Tab it inserts between 1 and 4 (or whatever you set it to) spaces for you.??

You've lost me. My Tab key inserts [wait for it] the HT character, 011 in ASCII.  My usual editors for the past n decades have had an "indent block" and corresponding unindent feature.

 

Part of this is tongue-in-cheeck, but it would really be interesting if the TAB people had different genetic markers than the SPACE people. [edit] yah, you space people are far out, man.

 

Is it just because I'm an old fuddy duddy and it really did matter if the source file was 10KB or 23KB on an 8-inch floppy?  And I should adjust with the times?

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Wed. Oct 28, 2020 - 08:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


theusch wrote:
You've lost me.

You weren't paying attention in #20  & #21, then?

 

in #20, I wrote:
any decent editor can be set to insert the appropriate number of spaces when you hit the TAB button on the keyboard

 

in #21, clawson wrote:

 

EDIT

 

#21

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Oct 28, 2020 - 09:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VScode...

 

 

Nice it will also sync those settings with the remote (there is a local git repo on the VScode remote machine, it is remote from my local git, how are my not confused with all this).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One of the biggest downsides to using spaces for indentation is that it overrides the personal preferences of anyone else who works on the code! I prefer an indentation width of 8 spaces, but others might prefer 2, 3, 4, 5, or some other width. Tab lets you customize it in your editor. Hardcoded spaces don't.

 

(If your code formatting looks goofy when the width of a tab changes, then you probably (mis)used tabs for alignment, and you should fix that rather than condemn tab for your own abuse of tabs.)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can't leave vim out while we're at it :)

set softtabstop=4
set expandtab

 

Tabs are just plain evil.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the massive union of all possible names for the bits that represent pins?

No - the Microchip headers for PIC don't do that

It's what the example quoted in the original post did, unless I seriously miss-read it.

 

 

because I'm an old fuddy duddy and it really did matter if the source file was 10KB or 23KB on an 8-inch floppy?  And I should adjust with the times?

Yeah, that, to a large degree.  "It's important to indent properly for clarity" passed "it's important to save disk space" a long time ago.   And "I can insist that the people who will see my code have their editor properly configured for a standard type of tab spacing" has become unreasonable more recently.  I converted my emacs init from "tabs are at every 8 spaces, use an optimized combination of such tabs and spaces for whitespace" to "just uses spaces" within the last month.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:

It's what the example quoted in the original post did, unless I seriously miss-read it.

AT last we're back on track instead of all this spaces vs tabs rubbish.

 

The original example was fro 8-Bit PIC18 which obviously uses a different compiler to the 16-bit PIC example I posted. Perhaps Microchip decided it was unnecessary and didn't do that for PIC24.

 

For GPIO PORT bits I think unions are unnecessary for alternate pin names, but the configuration registers I find it really helps.

 

Here's a better example for AD1CON1: A/D CONTROL REGISTER 1:

extern volatile uint16_t  AD1CON1 __attribute__((__sfr__));
__extension__ typedef struct tagAD1CON1BITS {
  union {
    struct {
      uint16_t DONE:1;
      uint16_t SAMP:1;
      uint16_t ASAM:1;
      uint16_t :2;
      uint16_t SSRC:3;
      uint16_t FORM:2;
      uint16_t :3;
      uint16_t ADSIDL:1;
      uint16_t :1;
      uint16_t ADON:1;
    };
    struct {
      uint16_t :5;
      uint16_t SSRC0:1;
      uint16_t SSRC1:1;
      uint16_t SSRC2:1;
      uint16_t FORM0:1;
      uint16_t FORM1:1;
    };
  };
} AD1CON1BITS;
extern volatile AD1CON1BITS AD1CON1bits __attribute__((__sfr__));

Yes there is a union but it serves to allow access to grouped configuration bitfields and also splits those up to allow access to the individual bits.

 

Last Edited: Thu. Oct 29, 2020 - 08:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:
Yes there is a union but it serves to allow access to grouped configuration bitfields and also splits those up to allow access to the individual bits.
I'm still not getting it - how is this used or beneficial? A union suggests "two different interpretations of the same memory layout" but surely the register/bits group this typedef is used for has one fixed register layout? Or is this something like the UCSRC/UBRRH register in ATmega16 where the same location is either 7 bits of parity/bit-length control or 7 bits of baud rate divisor (with the 8th URSEL bit determining which layout to use) ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Btw, the “anonymous” structures/unions in the original example are a relatively new (C11) feature.  It would not be so nice if you had to use:

if (PORTCbits.rc.bits.RC0 == 1)

 Avr-libc is older than that, and it takes a certain amount of confidence and chutzpah to update such things.  Microchip seems to have that (witness mega-0, and the massive changes in arm cmsis files recently), perhaps because they own a compiler company.  Atmel didn’t, and there are still people using winavr (2010) as their C development environment.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:
it takes a certain amount of confidence and chutzpah to update such things.
The exact reason for doing:

 

https://www.avrfreaks.net/forum/...

 

and doing it in a way that avoids name pollution with AVR-LibC - so you can use a mix of both if you like.

 

BTW the code is held at:

 

https://github.com/wrightflyer/a...

 

(it requires Python 3)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

You weren't paying attention in #20  & #21, then?

 

 

in #20, I wrote:

...

But I don't WANT that.  The lead-in on #21 says "it should generally be" or similar, right?  An opinion, IMO. 

 

Does all of this right-thinking help with OP's choice of chip-include file?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


source: t.me/programmerjokes

Slow and Steady!