The MPLAB® XC8 C Compiler is a free-standing, optimizing ANSI C compiler. It supports all 8-bit PIC® microcontrollers: PIC10, PIC12, PIC16 and PIC18 series devices, as well as the PIC14000 device.
Questions tagged [xc8]
188 questions
7
votes
3 answers
compiler optimizes out pointer to function when address is assigned manually
I need to call a function at address 0xDD2:
// foo.h
void foo(void) __at(0xDD2);
// foo.c
#include "foo.h"
void foo(void)
{
// some code
}
This code works:
#include "foo.h"
void main(void)
{
void (*a)(void) = &foo;
a();
}
However,…

AmirSina Mashayekh
- 498
- 1
- 5
- 21
7
votes
3 answers
Why does this code break when -O2 or higher is enabled?
I tried to fit an implementation of NSA's SPECK in a 8-bit PIC microcontroller.
The free version of their compiler (based on CLANG) won't enable optimizations so I ran out of memory. I tried the "trial" version that enables -O2, -O3 and -Os…

hjf
- 453
- 5
- 16
5
votes
2 answers
Program exits endless loop (PIC microcontroller)?
I wrote a program for the PIC 16f690 microcontroller after noticing my programs seemed to be exiting an endless loop.
There are LEDs on pins 3,4,5 of PORTC.
#include
#define _XTAL_FREQ 4000000
void main(void) {
TRISC = 0x00;
PORTC =…

user3893595
- 53
- 5
5
votes
8 answers
PIC16 C compiler
I am looking for a good C compiler for the PIC 16 family, working on Windows.
A couple of colleagues use bknudc but some seem to dislike it. Apparently you cannot use the linker efficiently, and it turns out that the best is to set all code in…

Gauthier
- 40,309
- 11
- 63
- 97
4
votes
3 answers
xc8 random number before programming
im producing 100 remote controls using pic16f1823 and i need unique id for each remote but it should be constant over time so i think its better to generate a random before programming in mplab compiler and then compile these 100 remotes
i want not…

Shahriar Sarmast
- 41
- 1
3
votes
0 answers
Best way to unset a flag in C when using Enum that is signed
I'm trying to use enum in C to create a flags variable, as discussed here: Flags, enum (C)
How should I reset a single enum flag, when the compiler represents the enum as signed?
Problem:
Say I declare my flags type like this:
typedef enum {
…

Luminaire
- 334
- 6
- 11
3
votes
2 answers
C (XC8) implicit signed to unsigned conversion with ternary operator
I am trying to set a color to either foreground or background (fg / bg) according to a mask.
uint8_t bitmap, mask, bg, fg = ;
This code works:
uint8_t color = bg;
if (bitmap & mask)
{
color = fg;
}
This code works too but throws a…

Kay
- 123
- 8
3
votes
2 answers
Vector address starts in the middle of program memory
I'm using MPLabX IDE 5.4 with XC8 compiler (a C/MPASM hybrid compiler that has a driver named pic-as v.2.2 as its assembler) to compile/assemble a simple piece of assembly code and to output a listing file.
My entire assembly code:
PROCESSOR…

KMC
- 19,548
- 58
- 164
- 253
3
votes
2 answers
How can I use C macros to define PIC microcontroller pin names?
PIC microcontrollers have 3 basic registers to set GPIO (General Purpose I/O) pin status. These are:
TRIS (Tri-status, the direction register. Sets the pin as in or out)
PORT (The input buffer)
LAT (Latch, the output burffer).
Ports can be A, B,…

hjf
- 453
- 5
- 16
3
votes
3 answers
Is there a literal suffix for 8 Bit values?
I am working with the xc8 compiler and want to tell him that my literal is only 8 bit wide.
123 :no suffix, default int (16 bit in xc8)
123U :unsigned int also 16 bit wide
Any idea for a clean solution to describe a 8 bit literal?

Mike
- 4,041
- 6
- 20
- 37
3
votes
1 answer
xc8 : please detailed the (((unsigned)&(REGISTER_NAME))*8) + BIT_NUM
i want defining macros for set pin direction and level.their argumans must be port name and pin number.i dont want use TRIS Register or LAT register name,only port name(PORTA e.g).i do this with pointer and bit mask like:
set pin dir:
#define…

daryooosh
- 57
- 8
3
votes
0 answers
Writing to a PORT does not change its state
I have the following program - Using XC8 C Compiler on MPLAB and the microcontroller is PIC16F877.
int main()
{
TRISB = 0x00;
while(1)
{
PORTB = 0xFF;
__delay_ms(1000);
PORTB = 0x00;
__delay_ms(1000);
}
return 0;
}
I have…

Nujufas
- 676
- 1
- 9
- 19
3
votes
3 answers
mplab xc8/16 builtin_constant_p
I was searching for this in the mplab compiler users guide but haven't found anything. I am asking it here to confirm that I am not blind or anything:
The GCC compiler provides some very interesting and useful built-in functions like…

jwsc
- 867
- 8
- 15
3
votes
2 answers
Receiving "undefined symbols" error with XC8 concerning plib I2C functions
Hey there StackOverflow!
My question concerns errors being reporting within the program pasted (far) below. The target device is the PIC12LF1552, it has one serial peripheral on it that I assumed could be used in conjunction with the library…

James Lui
- 195
- 1
- 7
2
votes
1 answer
Initial EEPROM contents in Atmega644
I am working on a project using a Atmega644 and want to include EEPROM data in the initial programming.
I am using MPLAB X and the XC8 compiler (version 2.31) and for programming I am using a PICkit4.
I know that the xc8 compiler for PIC MCUs has…

XPModder
- 303
- 4
- 16