I have a rather strange issue with XC8 on a PIC18F27K40 microcontroller. On a PIC16F1778 it works. I have defined:
void uart_putch(unsigned char byte) {
while (!PIR3bits.TX1IF);
TX1REG = byte;
}
When, in my main
loop, I call uart_putch('a');
, this works fine. However, when I define const char c = 'a';
and call uart_putch(c);
, it does not work. It prints something, though not an a
- I think they are 0x00
characters, which I get from hexdump -x /dev/ttyUSB0
. This is not an issue with the serial port on my computer; I looked with a scope and the signal is different (left works, right does not):
The code is simple:
void main(void) {
init(); // Sets up ports and UART control registers
while (1) {
uart_putch('a'); // or c
}
}
What does not work either is using any of the string functions (puts
, printf
, etc.), which I think is related - so in this question I made a minimal working example with characters.
The generated assembly when I use a variable c
has:
_c:
db low(061h)
global __end_of_c
_main:
; ...
movlw low((_c))
movwf tblptrl
if 1 ;There is more than 1 active tblptr byte
movlw high((_c))
movwf tblptrh
endif
if 1 ;There are 3 active tblptr bytes
movlw low highword((_c))
movwf tblptru
endif
tblrd *
movf tablat,w
call _putch
And with a constant it has in the _main
block:
movlw (061h)&0ffh
call _putch
I'm using MPLAB XC8 C Compiler V1.41 (Jan 24 2017), with part support version 1.41.
The relevant parts of my Makefile:
CC:=xc8
CFLAGS:=-I. --chip=18F27K40 -Q -Wall
SRC:=main.c uart.c
DEP:=uart.h
PRS:=$(subst .c,.p1,$(SRC))
OBJ:=main.hex
all: $(OBJ)
$(OBJ): $(PRS)
$(CC) $(CFLAGS) $^
$(PRS): %.p1: %.c $(DEP)
$(CC) $(CFLAGS) -o$@ --pass1 $<
Any help to get this working would be very much appreciated.
unsigned char
,char
,const unsigned char
andconst char
. \$\endgroup\$byteTx
instead? I'm concerned thatbyte
might be defined elsewhere as a data type. (Seems like that would generate a compiler diagnostic, but clearly something strange is going on here.) And as another test, doesputch(0x61)
misbehave the same way asputch('a')
? I'm wondering whether the table read instruction is reading 8-bit or 16-bit data. PIC W register is only 8 bits though, right? \$\endgroup\$