Author Topic: 8bit PIC, setting individual bits?  (Read 2187 times)

0 Members and 1 Guest are viewing this topic.

Offline step_sTopic starter

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
8bit PIC, setting individual bits?
« on: October 02, 2016, 02:39:45 pm »
Hi EEV'ers.

So I have been programming some different 8bit PICs for a while now, and since the datasheet tells you most of what you need to know, setting the different registers is pretty straight forward.
What has been bothering me, is that setting the individual bits isn't always common sense.
Example:

PWMxCON first bit is EN. To just enable to PWM module, you simply write to the first bit by typing PWMxEN. This is pretty simple.
Now the problem occurs when other registers aren't as logical in their approach.
Currently I'm sitting with the PWM reload register, PWMxLDCON, where I only need to write to the first LDA bit, but can't seem to find any short way of writing to this. PWMxLDA, LDAx, PWMxLDLDA so on, nothing seems to work.
I know, I know, I can just write to the entire register, but just wanted to know if there was any simple way of knowing what the individual bits are refered to as?
 

Offline aurmer

  • Regular Contributor
  • *
  • Posts: 84
  • Country: us
  • Insert text here.
Re: 8bit PIC, setting individual bits?
« Reply #1 on: October 02, 2016, 02:50:21 pm »
I don't have/know your chip or compiler, but I am going to guess you will be able to do something like this. My project uses register T1CON and when I want to set the bits called TCKPS, I call it like this

T1CONbits.TCKPS

I use microchip's xc16 compiler, so if you are using a microchip compiler, chances are the convention is consistent.

EDIT:

I picked a random PIC16  here, but on this page the answer is laid out in section "1.1.2.1 Short Bit Names"
« Last Edit: October 02, 2016, 02:52:47 pm by zetharx »
If I just asked the wrong question, shame on me for asking before I was ready for help. Please be kind and direct me to a resource which will teach me the question I SHOULD be asking. Thank you.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5319
  • Country: gb
Re: 8bit PIC, setting individual bits?
« Reply #2 on: October 02, 2016, 03:48:49 pm »
PWMxLDCONbits.LDA
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: 8bit PIC, setting individual bits?
« Reply #3 on: October 02, 2016, 04:51:11 pm »
Quote
but can't seem to find any short way of writing to this.

Not sure what you have tried but take a look at the device header file and there should be things you can use, typically via pre-defined struct.

I prefer bit ops.

For exampke,

Code: [Select]
  PWMxCON = (PWMxCON & ~PWMxCON_LDAMASK) | (LDA & PWMxCON_LDMASK);

the first op zeros out the LDA bits and the 2nd op fill just the LDA bits.

ps: that's just for demo bit pick the right mask from the header file. if not, make your own.
================================
https://dannyelectronics.wordpress.com/
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: 8bit PIC, setting individual bits?
« Reply #4 on: October 03, 2016, 10:55:42 am »
what is your compiler?

in XC8 / mplab C8 you can access individual bits of SFRs using the ***bit structure or in other ways.

for example, to set LATA0 you can do the following:
LATA |= 1;
LATAbits.LATA0 = 1;
_LATA0 = 1 (_LATA0 is actually #define _LATA0 LATAbits.LATA0)

But you can't access inidividual bits in any other register, for every SFR there is also the SFRbits register, mapped to be at the same address, which is of a structure type that mimics the bits position and name of the register. if you check the pic include file you will see how it's done and then you can make a structure in a simillar fashion and declare the variables in the same way, so you can access the individual bits.
the compiler will always try to use single bit operations in these cases
 

Offline step_sTopic starter

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: 8bit PIC, setting individual bits?
« Reply #5 on: October 03, 2016, 11:25:23 am »
Ahh cheers, lovely people. Using XC8.
The .bits thing is enough for me :) I do realize that I might end up with registers with the same individual bit names, but until that, this works just fine.
Never did check that specific page in the 400page datasheet. Thanks for pointing that out :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf