8bit PIC, setting individual bits? - Page 1
Forum section: electronics / general

author: step_s
author_rank: Regular Contributor
timestamp: October 02, 2016, 02:39:45 pm
content: 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?

author: aurmer
author_rank: Regular Contributor
timestamp: October 02, 2016, 02:50:21 pm
content: 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 thisT1CONbits.TCKPSI 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, buton this pagethe answer is laid out in section "1.1.2.1 Short Bit Names"

author: Howardlong
author_rank: Super Contributor
timestamp: October 02, 2016, 03:48:49 pm
content: PWMxLDCONbits.LDA

author: dannyf
author_rank: Super Contributor
timestamp: October 02, 2016, 04:51:11 pm
content: QuoteNot 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: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.

author: JPortici
author_rank: Super Contributor
timestamp: October 03, 2016, 10:55:42 am
content: 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

author: step_s
author_rank: Regular Contributor
timestamp: October 03, 2016, 11:25:23 am
content: Ahh cheers, lovely people. Using XC8.The .bits thing is enough for meI 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