I am struggling with the following code:
#include <xc.h>
#include "config.h"
void Init(void);
void uart_send(char*);
void main(void)
{
char arr[2]= {'a','b'};
char i=0;
Init();
uart_send(arr+i);
i++;
uart_send(arr+i);
while(1);
}
void uart_send(char* c)
{
while(PIR1bits.TXIF < 1);
TXREG= *c;
}
void Init(void)
{
OSCCON = 0xef;
TRISBbits.RB7 = 0;
ANSELHbits.ANS11 = 0;
PORTBbits.RB5 = 0;
TRISBbits.RB5 = 1;
BAUDCON = 0x00;
BAUDCONbits.BRG16= 1;
SPBRGH = 0x03;
SPBRG = 0x40;
TXSTA= 0x24;
RCSTA= 0x90;
PIE1 = 0x00;
INTCON = 0x00;
RCSTAbits.CREN = 1;
}
In the first case, I call the function uart_send and I receive 'a'. In the second case, when I call the function again, I also receive 'a' instead of 'b'. I can't figure it out Why. If I modify the code, to use value instead of address:
void uart_send(char);
void main(void)
{
char arr[2]= {'a','b'};
char i=0;
Init();
uart_send(*(arr+i));
i++;
uart_send(*(arr+i));
while(1);
}
void uart_send(char c)
{
while(PIR1bits.TXIF < 1);
TXREG= c;
}
Then, in both cases I receive 0x03.
I use MPLAB X v3.30, XC8 1.37, PIC18F14K50
Please, if You have any idea what do I wrong, share it. Thank you.
UPDATE:
I checked the disassy and it should work. Maybe my mcu partly died or something magic...
2 Answers 2
You don't specify your intended baud rate but I'm assuming it is 9600 bps, if so you are setting the baud rate incorrectly.
By using the internal oscillator, you are setting the microcontroller oscillator frequency to 16MHz, you are also setting the BRG16 and BRGH bits to 1, so the baud rate formula is Fosc/[4*(n+1)], where n is the SPBRG value that you are setting to be 832.
By replacing the values with the ones provided you have: $$ {16000000\over 4\times(832+1)} = {16000000\over 3332} = 4801.92...\approx4802 $$ If you want to set the baud rate to 9600 bps you have to reverse the formula to find n.
$$ baudrate = {Fosc\over{4\times(n+1)}} \Leftrightarrow n+1 = {Fosc\over{4\times baudrate}} \Leftrightarrow n = {Fosc\over{4\times baudrate}} - 1 $$
So: $$ {16000000\over 4 \times 9600}-1={16000000\over 38400}-1=416.66(6)-1=415.66(6) \approx416 $$
Therefore you SPBRG value should be 0x1A0.
-
\$\begingroup\$ You are right, I don't said anything about the baudrate. 4800 is fine. Don't judge me, sometimes I use 1200, too. :) Otherwise, thank you for this hi-quality explanation. \$\endgroup\$PeterPal– PeterPal2018年08月12日 06:10:50 +00:00Commented Aug 12, 2018 at 6:10
-
\$\begingroup\$ That was just a guess since 9600 bps is the most common baud rate. \$\endgroup\$Bruno Ferreira– Bruno Ferreira2018年08月12日 08:11:20 +00:00Commented Aug 12, 2018 at 8:11
2 years ago, but I remember for this annoying issue. Finally, I bought a new PIC, then flashed the same program. Worked correctly.
Conclusion: The first mcu (partly) died.
i
of typeuintptr_t
(the type should be defined instdint.h
). It's possible you are ending up with weird integer overflows - it's not exactly the best compiler in the world. \$\endgroup\$