Author Topic: Microchip woes!  (Read 17239 times)

0 Members and 1 Guest are viewing this topic.

Offline bktemp

  • Super Contributor
  • ***
  • Posts: 1616
  • Country: de
Re: Microchip woes!
« Reply #25 on: January 28, 2016, 08:37:00 am »
The same type of definitions are defined in the xc.h headers.  Not many use them, though.
Most likely because they have added them recently and most people do not know about it (like me). When I started with dsPICs/PIC24 only the bitfields were available in the C30 header files.
Good to know, this will make most init codes much smaller.

The names are quite long (having the format registername_bitname_POSITION), so you either get a long line or have to split it into several lines like you did. Your solution is probably the best and every reasonable compiler should assemble the bits into a single 8/16/32bit word.
It often takes a long time, but at the end Microchip makes many things right.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Microchip woes!
« Reply #26 on: January 28, 2016, 12:11:13 pm »
It is straight forward to cast a struct to a byte and get the bits defined.

Has been around since the picc days and can be done with avrs too.

I prefer masks personally. I think luminary has an app note that compares the varies approaches to operate on bits and pieces and how effiecint they are.
================================
https://dannyelectronics.wordpress.com/
 

Offline Xenoamor

  • Regular Contributor
  • *
  • Posts: 83
  • Country: wales
Re: Microchip woes!
« Reply #27 on: January 28, 2016, 01:06:52 pm »
It is straight forward to cast a struct to a byte and get the bits defined.

A million times yes. Changing:
Code: [Select]
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADIE)|(1<<ADFR)|6;to:
Code: [Select]
ADEN = 1;
ADSC = 1;
ADIE = 1;
ADFR = 1

Makes working across multiple control registers a lot simpler.
 

Offline Daving

  • Contributor
  • Posts: 34
Re: Microchip woes!
« Reply #28 on: January 28, 2016, 04:53:41 pm »
It is straight forward to cast a struct to a byte and get the bits defined.

A million times yes. Changing:
Code: [Select]
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADIE)|(1<<ADFR)|6;to:
Code: [Select]
ADEN = 1;
ADSC = 1;
ADIE = 1;
ADFR = 1

Makes working across multiple control registers a lot simpler.

The problem is that this:
Code: [Select]
ADEN = 1;
ADSC = 1;
ADIE = 1;
ADFR = 1;

And this:
Code: [Select]
ADSC = 1;
ADIE = 1;
ADFR = 1;
ADEN = 1;

Have different behavior in the chip.  Especially since in your example, you set the enable bit before anything else.
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 2001
  • Country: 00
Re: Microchip woes!
« Reply #29 on: January 28, 2016, 06:35:28 pm »
I prefer bitfields. Code is far more readible. I ran some tests, but posting it is a bit beyond the scope of this thread (I can post my findings in it's own thread if anyone is interested), but I only found very minor variations. I ran a simple timer init sequence and saw only 1-3 instructions difference between 8-bit PIC, 16-bit PIC, 32-bit PIC and Cortex m4 ARM with GCC 4.8 (all with PRO or -O1 optimisations obviously). Winner? Bit masks obviously.. but IMHO the code is less readable.

Yes you need to watch out those order of execution. The biggest pain on 32-bit CPUs is they generate a lot of code for 1 RMW. Many 8 and 16-bit CPUs can perform AND/OR directly on I/O, like PIC24 has: AND f, WREG -> store in WREG. Or bitset/clr. If you first need to disable a peripheral, then change bits around, and then re-enable it.. you can see a big difference between 2 platforms.

If you do want to bit-bang directly, I recommended working on a local variable first (e.g. to clear some bits of a field in order to write a new value to it). This variable does not have to be volatile. This has the advantage of the compiler being able to be very aggressive and generating exactly the same or even more efficient code than bitfields.
« Last Edit: January 28, 2016, 06:39:37 pm by hans »
 

Offline captbill

  • Contributor
  • Posts: 37
  • Country: us
Re: Microchip woes!
« Reply #30 on: February 02, 2016, 06:26:30 pm »
Here's a novel idea:
Take a look at PICL which is a compiler for the PIC16Cxx written in Oberon. The whole compiler is written in <800 lines of code and that includes the uploader/flasher as well. Heck, you could quite possibly study 5 pages of code and roll up your sleeves and fix or implement whatever you might need with less hassle than mucking with these overly complicated C compilers.

https://www.inf.ethz.ch/personal/wirth/PICL/index.html

How about this for a days lab assignment:
https://www.inf.ethz.ch/personal/wirth/PICL/PIC.pdf

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->