5
\$\begingroup\$

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

asked Jun 19, 2016 at 21:01
\$\endgroup\$
12
  • \$\begingroup\$ Try making i of type uintptr_t (the type should be defined in stdint.h). It's possible you are ending up with weird integer overflows - it's not exactly the best compiler in the world. \$\endgroup\$ Commented Jun 19, 2016 at 21:35
  • \$\begingroup\$ do you have optimization turned on ? What happens when you set optimization to 0 (if you have it to something higher than 0) ? \$\endgroup\$ Commented Jun 19, 2016 at 21:51
  • \$\begingroup\$ @TomCarpenter I tried uintptr_t, but I've got the same result. \$\endgroup\$ Commented Jun 19, 2016 at 22:22
  • \$\begingroup\$ @efox29 I can't set a level of optimization, only select mode (free). --opt=default. I typed additional options: --opt=none. The same result. \$\endgroup\$ Commented Jun 19, 2016 at 22:26
  • 1
    \$\begingroup\$ If you use array notation, does that work ? arr[0],arr[1] ? \$\endgroup\$ Commented Jun 19, 2016 at 22:28

2 Answers 2

1
\$\begingroup\$

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.

answered Aug 9, 2018 at 13:25
\$\endgroup\$
2
  • \$\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\$ Commented Aug 12, 2018 at 6:10
  • \$\begingroup\$ That was just a guess since 9600 bps is the most common baud rate. \$\endgroup\$ Commented Aug 12, 2018 at 8:11
0
\$\begingroup\$

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.

answered Aug 12, 2018 at 6:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.