• AVR Freaks

AnsweredHot!(This is for real.) PIC16F18875 + 6 hours of staring = Non-Functional UART. SEE REPLIES!

Author
FileDotZip
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2016/11/30 18:58:55
  • Location: 0
  • Status: offline
2019/06/17 14:44:56 (permalink)
0

(This is for real.) PIC16F18875 + 6 hours of staring = Non-Functional UART. SEE REPLIES!

post was for testing forum posting ability. I can now post. Please ignore.
post edited by FileDotZip - 2019/06/17 15:59:49
#1
FileDotZip
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2016/11/30 18:58:55
  • Location: 0
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 14:46:11 (permalink)
0
Sorry, I was testing posting functionality, for days I couldn't post anything, got the 404 type error page "permission denied" and i was trying to get it again now so i can take a screenshot and show an admin. Turns out the issue went away and i made a trash post I cant delete. If an admin can, can you delete this post?
#2
katela
Super Member
  • Total Posts : 1290
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 14:50:23 (permalink)
0
Just edit it and delete everything and write a simple words like: testing or please ignore or whatever you like. It's unlikely the admin will delete it.

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#3
FileDotZip
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2016/11/30 18:58:55
  • Location: 0
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 15:59:32 (permalink)
0
I cant post, or edit this post. I have a serious question and i need it answered. Please help if you can.
 
I have a GPS module I would like to get UTC time from. For this I need serial RX functionality on my MCU, Good thing it has it. I have been trying to get basic UART functionality working: first sending some data. To make sure the receiving side is ok, I'm using a known working Arduino nano board with the following code https://pastebin.com/MCzNWZh1. It receives code on it's RX pin from TX (RC6) pin on the MCU.

I have manually turned the TX pin on and off by setting LATC6 on and off 50 times per second. Receiving this signal my Arduino says that theoretically I can have Serial functionality.
 
Here is where I got. My code for the MCU is this: https://pastebin.com/ek9VPBxy

Ive been trying to get Serial transmission functionality on the RC6 pin for the past 6 hours. Ive tried various ways of changing BAUDCON, TXSTA, RCSTA and it doesn't appear to change the output of pin RC6 what-so-ever. Pin RC6 measures a constant 0V and no data is received on the other side. I also tried changing TXPPS to output to other RCx pins, but there was no effect by doing that.
#4
ric
Super Member
  • Total Posts : 22758
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 16:12:52 (permalink)
0
You cannot edit a post which contains "live" links (without first "breaking" the links).
See: How to avoid errors when editing posts
Yes, it's damned annoying, but not likely to get fixed in the foreseeable future.
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#5
ric
Super Member
  • Total Posts : 22758
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 16:22:54 (permalink) ☼ Best Answerby FileDotZip 2019/06/17 17:55:41
+1 (1)
Just to simplify things a bit, try replacing
   unsigned brg = getSPBRG(4800, 1, 1);
    SPBRGL = brg & 0xFF;
    SPBRGH = brg >> 8;

with
    SPBRG = 1666;    // 4800 baud @ 32 MHz when BRG16=1 and BRGH=1

 
Now your main problem is that you never assign the TX pin.
That PIC has PPS, and there is NO DEFAULT ASSIGNMENT for the USART TX function.
You must use the PPS facility to connect TX to your desired pin.
 
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
mbrowning
Just a Member
  • Total Posts : 1430
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 16:32:44 (permalink) ☄ Helpfulby FileDotZip 2019/06/17 17:37:57
+2 (2)
You never configured the PPS registers for your UART. You must configure the TX pin. For example if you are using C6 for transmit then RC6PPS = 0x10 per table 13-3. RX defaults to C7 but I never rely on defaults, so RXPPS = 0x17 for C7.
 
This is all pretty clearly spelled out in the datasheet.

Oh well - there's always next year
#7
pcbbc
Super Member
  • Total Posts : 1104
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 16:43:47 (permalink) ☄ Helpfulby FileDotZip 2019/06/17 17:37:55
+2 (2)
Your getSPBRG routine is almost certainly suffering some kind of integer overflow error in its calculations. I suspect it is only using 16 bit arithmetic.
Try calculating your baud rate manually. There are pre-calculated values in the datasheet for many common baud rates and clock frequencies.
Also, set up the TXSTA, RXSTA and BAUDCON registers, and the very last step should be to set SPEN = 1.
Also this...
while (!TRMT);

Is not a great way to check if you can send the next byte. Try using the TXIF flag, which will tell you if there is space in the transmit buffer. This gets set regardless of if you have interrupts enabled or not. Otherwise, if you wait for the shift register empty flag, you will have pauses between characters.
#8
FileDotZip
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2016/11/30 18:58:55
  • Location: 0
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 17:55:21 (permalink)
+1 (1)
Thank you to the 3 helpful users who made my code work.
There is no overflow in getSPBRG, its just dividing a constant, there is nothing to overflow.
The program frankly worked just after adding RC6PPS = 0x10;
But i made a few other changes.

Edit:
SPBRG = getSPBRG(4800, 1, 1); actually doesnt work.

/*
 * File: Main.c
 * Author:
 *
 * Created on June 13, 2019, 2:22 PM
 */

#define _XTAL_FREQ 32000000

// PIC16F18875 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (EC above 8MHz; PFM set to high power)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available // Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF // DataNVM code protection bit (Data EEPROM code protection disabled)

#include <xc.h>

unsigned getSPBRG(unsigned baudrate, char brg16, char brgh) {
    unsigned mult;
    if (brg16 == 0 && brgh == 0) mult = 64;
    else if (brg16 == 1 && brgh == 1) mult = 4;
    else mult = 16;
    
    // baudrate = fOsc / (mult*(spbrg + 1))
    // baudrate * (spbrg + 1) = fOsc / mult
    // spbrg + 1 = (fOsc / mult) / baudrate
    // spbrg = (fOsc / mult) / baudrate - 1
    return (_XTAL_FREQ / mult) / baudrate - 1;
}

void main(void)
{
    TRISA = 0b00000000;
    TRISB = 0b00000000;
    
    // LATA = 1; __delay_ms(1000);

    PORTC = 0;
    TRISC = 0b10000000; // all outputs, except RX on RC7
    ANSELC = 0b00000000; // disable analog
    
    RC6PPS = 0x10;
    
    //init USART
    // SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
    RCSTA = 0b00100000; // TX9D=0 -->8-bit; SPEN=1, CREN=1, addr-det disabled.
    // CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
    TXSTA = 0b00100100; // TX9D=0 -->8-bit; BRGH=1; TXEN=1 --> enable, SYNC=0 --> async
    //ABDOVF RCIDL NOP SCKP BRG16 NOP WUE ABDEN
    BAUDCON = 0b00001000; // SCKP=0 -> not inverted, BRG16=1, WUE=0, ABDEN=0
    SPEN = 1;
    
    // LATA = 2; __delay_ms(1000);

    unsigned brg = getSPBRG(4800, 1, 1);
    SPBRGL = brg & 0xFF;
    SPBRGH = brg >> 8;
    
    // LATA = 3; __delay_ms(1000);

    while(1){
        TXREG = 'X';
        while (!TXIF);
        TXREG = '\n';
        while (!TXIF);
        __delay_ms(500);
        LATA = 4; __delay_ms(1000);
    }
    
    LATA = 7;
    return;
}




post edited by FileDotZip - 2019/06/17 18:00:09
#9
pcbbc
Super Member
  • Total Posts : 1104
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/17 23:55:53 (permalink)
+1 (1)
There is no overflow in getSPBRG, its just dividing a constant, there is nothing to overflow.

But a 16 bit constant which won’t fit into 16 bits. Truncation (instead of overflow) would have been a better choice of words.
“unsigned” is shorthand for “unsigned int” and ints are 16 bit on 8 bit PIC devices with XC8.
TBH I’m surprised the compiler didn’t throw a warning. Perhaps there is something I am missing?

Edit: Regardless of if you can make this code work (by using an unsigned long type, or whatever), I would still NOT recommend using it.
a) It does not pick the best value for the baud rate as it does not do any rounding. You should pick the nearest value which results in least error, instead of always rounding down.
b) It necessitates including a long integer divide library routine, which is not only slow (admittedly not much of a problem as you only do it once at startup) but does use up resources. It is quite possible the rest of your code has no need for long integer divides, so it will not even be reused.
post edited by pcbbc - 2019/06/18 00:05:04
#10
ric
Super Member
  • Total Posts : 22758
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/18 00:02:57 (permalink)
+1 (1)
The relevant line is
    return (_XTAL_FREQ / mult) / baudrate - 1;

which after pre processing becomes
    return (32000000 / mult) / baudrate - 1;

"32000000" is too big for int, so will be treated as a long, so all the divides will be done using 32 bit arithmetic.
It will only be truncated back to 16 bits at the end, by which point it is small enough to fit into 16 bits.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#11
1and0
Access is Denied
  • Total Posts : 9314
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/18 08:16:41 (permalink)
0
ric
Just to simplify things a bit, try replacing
    SPBRG = 1666;    // 4800 baud @ 32 MHz when BRG16=1 and BRGH=1


FileDotZip
 
SPBRG = getSPBRG(4800, 1, 1); actually doesnt work.

That does not work because in Microchip's infinite wisdom
extern volatile unsigned char SPBRG __at(0x11B);

is an 8-bit register (same as SPBRGL) instead of a 16-bit variable. :(  It probably is a bug!?
 
In the past in this forum, I have posted a macro that calculates the values for SPBRGH, SPBRGL, BRGH and BRG16 during compile time, avoiding using any resource at runtime, with rounding to the nearest values and it chooses the settings with the minimum baud rate error.  But, unfortunately I cannot find that thread.



#12
ric
Super Member
  • Total Posts : 22758
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Enabling IO pins high result in unstable voltage on enabled pins 2019/06/18 13:08:57 (permalink)
+1 (1)
1and0
...
That does not work because in Microchip's infinite wisdom
extern volatile unsigned char SPBRG __at(0x11B);

is an 8-bit register (same as SPBRGL) instead of a 16-bit variable. :(  It probably is a bug!?

Someone screwed up at Microchip. They have it right in most enhanced PIC16F header files.
The word definition IS there, but named "SP1BRG"
// Register: SP1BRG
#define SP1BRG SP1BRG
extern volatile unsigned short          SP1BRG              __at(0x11B);
#ifndef _LIB_BUILD
asm("SP1BRG equ 011Bh");
#endif

so
SP1BRG = 1666;    // 4800 baud @ 32 MHz when BRG16=1 and BRGH=1

will work.



I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#13
Jump to:
© 2019 APG vNext Commercial Version 4.5