Author Topic: MPLAB XC8 8 bit pic peripheral access  (Read 2625 times)

0 Members and 1 Guest are viewing this topic.

Offline mrcrud5Topic starter

  • Contributor
  • Posts: 36
  • Country: ca
MPLAB XC8 8 bit pic peripheral access
« on: November 23, 2017, 06:08:15 pm »
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
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3452
  • Country: it
Re: MPLAB XC8 8 bit pic peripheral access
« Reply #1 on: November 23, 2017, 07:42:47 pm »
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 installation
anyway, 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 do
Code: [Select]
uint8_t x;

x = TMR1L;

and if you want to write to a peripheral register you just write to it
Code: [Select]
T1CON |= 0x01;
and of course you can access the individual bits inside the register like so
Code: [Select]
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)

every SFR has an associated structure of the same name + "bits" for accessing the single bits in the register
TRISA <->TRISAbits
CCP1CON <-> CCP1CONbits

and so on.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12807
Re: MPLAB XC8 8 bit pic peripheral access
« Reply #2 on: November 23, 2017, 08:09:08 pm »
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!  |O

...
and of course you can access the individual bits inside the register like so
Code: [Select]
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)

every SFR has an associated structure of the same name + "bits" for accessing the single bits in the register
TRISA <->TRISAbits
CCP1CON <-> CCP1CONbits

and so on.
Not 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 to ADCON0bits.CHS, which on that PIC is a three bit field.
« Last Edit: November 23, 2017, 08:17:13 pm by Ian.M »
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3452
  • Country: it
Re: MPLAB XC8 8 bit pic peripheral access
« Reply #3 on: November 23, 2017, 08:43:08 pm »
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...
 

Offline odessa

  • Regular Contributor
  • *
  • Posts: 113
  • Country: gb
Re: MPLAB XC8 8 bit pic peripheral access
« Reply #4 on: November 24, 2017, 11:14:47 am »
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
When  I die I want to die peacefully in my sleep like my Grandad ... Not all shouting and screaming like the passengers on his bus.
 

Offline mrcrud5Topic starter

  • Contributor
  • Posts: 36
  • Country: ca
Re: MPLAB XC8 8 bit pic peripheral access
« Reply #5 on: November 24, 2017, 12:49:52 pm »
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

Yea 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.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12807
Re: MPLAB XC8 8 bit pic peripheral access
« Reply #6 on: November 24, 2017, 04:07:43 pm »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf