MPLAB XC8 8 bit pic peripheral access - Page 1
Forum section: electronics / general

author: mrcrud5
author_rank: Contributor
timestamp: November 23, 2017, 06:08:15 pm
content: I just switched compilers from Picbasicpro to XC8 so keep in mind I am a newb at this point. Its going to be tough for the next little while I get used to using C again but I am up for the challenge.Anywho! I want to know if there are any libraries out there that I can use for accessing the peripherals on the 8 bit pics. I know I can just make them myself(infact that would probably help the learning process). I know there is PLIB.h but from what I understand that was only included on versions earlier than XC8 v1.34 and also it is only used for 16bit pics. At the moment I am using the latest version which I think is 1.44. They are encouraging the use of the code configurator which seems really neat, however they have a limited amount of pics that it will work with. I currently have a lot of PICF616s, PIC16F88, and PIC18F2550, and PIC18F2585 so I'd like to continue to use those. None of these seem to be on the list of pics that will work with the code configurator.I have a feeling I am going to have to make my own libraries to use. Its mostly just the UART, ADC, I2C, and PWM stuff I'd like to have functions written for. Can someone point me in the right direction? Thanks

author: JPortici
author_rank: Super Contributor
timestamp: November 23, 2017, 07:42:47 pm
content: plib was for pic18 only (which is an 8 bit mcu... the instructions are 16 bit wide but the core operates on 8 bit data)even if plib isn't included you can always add it to your installationanyway, i never felt the need for peripheral libraries (well with exceptions.. canbus for example, but i wrote my own, because the time needed to understand another one and see if it could do what i wanted in an efficient way would have been at least the same, plus i get to keep the same prototypes if i switch between architectures)anyway, you can access the registers directly, they have the same name in the datasheet. for example, if you want to read the content of TMR1L (low byte of TMR1 counter) you just have to doCode:uint8_t x;x = TMR1L;and if you want to write to a peripheral register you just write to itCode:T1CON |= 0x01;and of course you can access the individual bits inside the register like soCode:T1CONbits.T1ON = 1(name of the bit could be different... they are doing a bit of a mess in the new parts, newer versions of peripherals using different names for bits.. ugh)everySFR has an associated structure of the same name + "bits" for accessing the single bits in the registerTRISA <->TRISAbitsCCP1CON <-> CCP1CONbitsand so on.

author: Ian.M
author_rank: Super Contributor
timestamp: November 23, 2017, 08:09:08 pm
content: PLIB sucked! It can sometimes be helpful to look at its source if you REALLY dont understand how to do something but it was (mostly) a really thin wrapper over the hardware registers, didn't even try to present a consistent interface to different versions of each peripheral, and had the general look & feel of being written by a summer intern part-way through a C programming course! Also, its documentation sucked even worse!Quote from: JPortici on November 23, 2017, 07:42:47 pmNot quite *every* SFR, but certainly every one for which access to individual bits is meaningful.For all built-in peripherals *EXCEPT* CAN, USB and Ethernet (which require a protocol stack) its easier to hit the datasheet and translate the example code given in assembler into C. Study the assembler code with the XC8 device specific header for your PIC open beside the datasheet as there are names for many functionally useful groups of bits e.g PIC16F88 ADC channel selection can be done simply be writing the desired ANx pin number toADCON0bits.CHS, which on that PIC is a three bit field.

author: JPortici
author_rank: Super Contributor
timestamp: November 23, 2017, 08:43:08 pm
content: just to be pedantic, last time i checked an include file i could find many registers with just a single or no field to have their "bits" counterpart. Maybe for completeness, maybe they are auto-generated i don't know...

author: odessa
author_rank: Regular Contributor
timestamp: November 24, 2017, 11:14:47 am
content: Use the MCC add on. It will configure and build all the functions you need. I use it all the time, very handy.Look up Microchip Code Configurator

author: mrcrud5
author_rank: Contributor
timestamp: November 24, 2017, 12:49:52 pm
content: Quote from: odessa on November 24, 2017, 11:14:47 amYea it seems pretty neat. I just ordered some Pics that I can use it with. Its one of the things I originally mentioned in my post though.

author: Ian.M
author_rank: Super Contributor
timestamp: November 24, 2017, 04:07:43 pm
content: MCC generates a lot of bloat and results in an over-complex project structure. Its good if you haven't finalised your MCU requirements or are new to the device in question, but is less than ideal for any device you are already familiar with.