Controlling PIC registers using XC8 C

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
jim_g
Posts: 22
Joined: Tue Jan 13, 2015 12:47 pm
Has thanked: 6 times
Been thanked: 8 times

Controlling PIC registers using XC8 C

Post by jim_g »

Hi

I have developed a battery-powered clock that periodically synchronises with the MSF transmitter at Anthorn. So far, I have not had to resort to the use of C and I enjoy FlowCode's clarity compared with my previous failed C attempt for this project.
I now have to refine the code to provide an acceptable battery life, and I need to be able to do the following (at least):
1) configure the PIC for minimum power consumption
2) enable weak pull-ups for portB, bits 4-7 (for a button interface) and disable them again during sleep
3) switch between a 4MHz clock rate (or greater) oscillator for the duration of time lock-on and update and a 32kHz external clock for timekeeping

All of these, so far as I can see, require the use of the C icon.
Is there any guidance on the coding to be used with the XC8 compiler please?

I have tried to interpret the XC8 compiler User Guide, which suggests (to me!) the use of the #pragma, but this gives a compile error, e.g.,

1217: #pragma INTSRC = 0;
^ (335) unknown pragma "INTSRC" (warning)

using the seemingly deprecated __config() statement gives
1221: __config ("WPUB", 0b11110000);
^ (195) expression syntax

Using just __WPUB = 0b11110000; I get
1159: __WPUB = 0b11110000;
^ (192) undefined identifier "__WPUB"

Using #pragma __WPUB = 0b11110000;, I get
1159: #pragma __WPUB = 0b11110000;
^ (335) unknown pragma "__WPUB" (warning)

By trial and error, I have found that, for example,
INTSRC = 0;
does work; Is this the general rule, i.e.,
a) Use the MPLab data sheet for your pic
b) Find the register definitions for the purpose in hand
c) Refer to the bit or group of bits by the data sheet name and set the value

This seems to work some of the time, e.g., PLLEN = 1 quadruples LED blink rate in a timer loop, but others such as
WPUB 0b11110000; does not change the voltages on portB(4..7), either measured on the pin or sampled using an input icon with the port open circuit; nor does it light any leds on an EB004 board (WPUB4 = 1; does not work either).

I am clearly missing something here, and will feel foolish when I hear the answer. Can you point me at where I should look for this please? Coming back here for each item is likely to prove tedious for all of us!

* I am using the PIC 18F45K22, EB006 with a PIC-KIT3 programmer (Olimex) *

Regards
Jim

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times

Re: Controlling PIC registers using XC8 C

Post by QMESAR »

Hi,
I am not sure if I understood your problem correctly and apologize if I did understood it incorrectly :D
any case a few point from my side
I have tried to interpret the XC8 compiler User Guide, which suggests (to me!) the use of the #pragma, but this gives a compile error, e.g.,
Flowcode support everything that is supported in the XC8 as the C code generated when you press compile is exactly as any one would write it from hand for the XC8 compilers.However the syntax you are using with your #pragmas is incorrect I would advice you to go to MPLABx open a project for your device and then go to windows >> PIC Memory Views >> Configuration bits then select the configuration bits as you would like them to be and press Generate code then look at the generated code and you will see how the Pragma syntax looks with this said you can not use the #pragma directives on the fly meaning while your controller is running (run time ) you can not do #pragmas to change configuration this is only allowed during startup as far as I know and tried in the past :D
During Run time you need to access the register directly as per data sheet as you also have seen it does work

jim_g
Posts: 22
Joined: Tue Jan 13, 2015 12:47 pm
Has thanked: 6 times
Been thanked: 8 times

Re: Controlling PIC registers using XC8 C

Post by jim_g »

Thank you QMESAR, I will try this later today.
Cheers

Jim

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Controlling PIC registers using XC8 C

Post by medelec35 »

Hi Jim,
With weak pull-ups on a several microcontrollers you also need to clear bit 7 of OPTION_REG.
Have you seen this post?

Martin
Martin

jim_g
Posts: 22
Joined: Tue Jan 13, 2015 12:47 pm
Has thanked: 6 times
Been thanked: 8 times

Re: Controlling PIC registers using XC8 C

Post by jim_g »

Hi, Martin

I installed the MPLAB X IDE and that gives instant feedback if the registers are not recognised. As QMESAR suggested just using the register name in upper case does the job.
Yes you are right, for the 18F45K22, it is necessary to use
WPUB = 0b11110000; // weak pull-ups on PORTB.4-7
RBPU = 0; // Switch on pull-ups

as well as to set the port as an output.

It all seems to work fine now, there are many things to switch off! Many thanks

Jim

Post Reply