Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] PIC18F14K50 USART + 433MHz RF

Status
Not open for further replies.

Astralist

Newbie level 5
Joined
Oct 29, 2014
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
134
Hi,

I'm trying to build a simple wireless switch, but i stuck at the UART. I'm not able to run it both on hardware or simulate (transmit) it in ISIS Proteus with this particular MCU. i've tried with PIC16F877A and it works well.
can somebody points out what wrong? here is the UART code, and actually most parts of this code comes from microchip library and still i can't get any result. I'm running on 2400 baud rate.

Code:
#include "system.h"
#include "usart.h"

void USART_Init()
{
        unsigned char c;
     
        ANSELHbits.ANS11 = 0;    // Make RB5 pin digital

        #if defined(REMOTE)
            UART_TRISRx=1;				// RX
        #elif defined(LOCAL)
            UART_TRISTx=0;				// TX
            WPUB7=1;
        #endif
        BAUDCON = 0x08;     	// BRG16 = 1
        SPBRG = 0x87;
        SPBRGH = 0x13;      	// 0x1387 for 48MHz -> 2400 baud

        c = RCREG;				// read
        
        #if defined(LOCAL)
            TXSTA = 0x65;       	// TX enable BRGH=1
        #elif defined(REMOTE)
            RCSTA = 0xD0;       	// Single Character RX
            PIR1bits.RCIF = 0;
            PIE1bits.RCIE= 1;
            INTCONbits.PEIE = 1;
            INTCONbits.GIE = 1;
        #endif
}//end USART_InitializeUSART

void USART_putc(unsigned char c)
{
    while (!TXSTAbits.TRMT);
    TXSTAbits.TX9D = CalcParity(c);
    TXREG = c;
}

unsigned char USART_getc(void)
{
	unsigned char tmp;
        unsigned char c;

	if (RCSTAbits.OERR)  // in case of overrun error
	{                    // we should never see an overrun error, but if we do,
		RCSTAbits.CREN = 0;  // reset the port
        tmp = RCREG;
        if (CalcParity(tmp) == RCSTAbits.RX9D){c = tmp;}
        else{c = 0;}
		RCSTAbits.CREN = 1;  // and keep going.
	}
	else
    {
        tmp = RCREG;
        if (CalcParity(tmp) == RCSTAbits.RX9D){c = tmp;}
        else{c = 0;}
    }
// not necessary.  EUSART auto clears the flag when RCREG is cleared
//	PIR1bits.RCIF = 0;    // clear Flag
	return c;
}

unsigned char CalcParity(unsigned char scancode)
{
    unsigned char parity = 0;
    while (scancode > 0)
    {
        if (scancode & 0x01) // If LSB is 1, we have a parity bit
        {
            parity++;
        }
        scancode = scancode >>  1; // Next bit
    }
    return (parity & 0x01); // We do only need the low bit to determine odd or even
}
 

I am not familiar with either the PIC18F14K50 or "C" however I am familiar with PIC18F CPU'S and the PIC16F877A.
I have found that the UART code for the PIC16F877A will work on all the PIC18F CPU's that I have tried. I would suggest that your problem is probably due to the processor running at a different speed than you think that it is, as setting the processor speed can be tricky.
 
Last edited:

What is supposed to happen and doesn't? How are you determining that?
Your comment shows a 40MHz Fosc but you don't show the config and oscillator setup code. Are you sure that you have the oscillator set up correctly as this is a common cause for UART problems.
You have the BRGH bit setting inside a conditional compilation statement. However the other baud rate generator settings are not. The result is that you will have the wrong SPBRG(H) setting for 2400 BAUD if "LOCAL" is not defined.
Susan
 

Processor running at 48MHz as per config words, and if i use USB framework, it can connect to PC, meaning that the clock was correct.

The preprocessor "LOCAL" and "REMOTE" are defined elsewhere and for this case, let say that "LOCAL" is defined.

what supposed to happen is bit/waveform/freq can be seen on the TX pin (as for LOCAL device's job is to transmit the data)
what happened is i couldn't observe anything on TX, both on simulation (ISIS Proteus) and the board.

My config words are like these:
Code:
    #pragma config CPUDIV   = NOCLKDIV  // CPU System Clock Selection bits (No CPU System Clock divide)
    #pragma config USBDIV   = OFF       // USB Clock Selection bit (USB clock comes directly from the OSC1/OSC2 oscillator block; no divide)
    #pragma config FOSC     = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config PLLEN    = ON        // 4 X PLL Enable bit (Oscillator multiplied by 4)
    #pragma config PCLKEN   = ON        // Primary Clock Enable bit (Primary clock enabled)
    #pragma config FCMEN    = OFF       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor disabled)
    #pragma config IESO     = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
    #pragma config PWRTEN   = OFF       // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN    = OFF       // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
    #pragma config BORV     = 19        // Brown-out Reset Voltage bits (VBOR set to 2.7 V nominal)
    #pragma config WDTEN    = OFF       // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
    #pragma config WDTPS    = 32768     // Watchdog Timer Postscale Select bits (1:32768)
    #pragma config HFOFST   = OFF       // HFINTOSC Fast Start-up bit (HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.)
    #pragma config MCLRE    = OFF       // MCLR Pin Enable bit (RA3 input pin enabled; MCLR disabled)
    #pragma config STVREN   = ON        // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config LVP      = OFF       // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
    #pragma config BBSIZ    = OFF       // Boot Block Size Select bit (1kW boot block size)
    #pragma config XINST    = OFF       // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
    #pragma config CP0      = OFF       // Code Protection bit (Block 0 not code-protected)
    #pragma config CP1      = OFF       // Code Protection bit (Block 1 not code-protected)
    #pragma config CPB      = OFF       // Boot Block Code Protection bit (Boot block not code-protected)
    #pragma config CPD      = OFF       // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
    #pragma config WRT0     = OFF       // Table Write Protection bit (Block 0 not write-protected)
    #pragma config WRT1     = OFF       // Table Write Protection bit (Block 1 not write-protected)
    #pragma config WRTC     = OFF       // Configuration Register Write Protection bit (Configuration registers not write-protected)
    #pragma config WRTB     = OFF       // Boot Block Write Protection bit (Boot block not write-protected)
    #pragma config WRTD     = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    #pragma config EBTR0    = OFF       // Table Read Protection bit (Block 0 not protected from table reads executed in other blocks)
    #pragma config EBTR1    = OFF       // Table Read Protection bit (Block 1 not protected from table reads executed in other blocks)
    #pragma config EBTRB    = OFF       // Boot Block Table Read Protection bit (Boot block not protected from table reads executed in other blocks)
 

You need to also set the SPEN bit in the RCSTA register (see section 16.1.1 in the data sheet) the enable the UART as a whole.
My experience is that some devices need to be fully configured before they are enabled. You are setting TXSTA to a value of 0x65 which means 9-bit transmission, BRGH set high and enable the transmitter. It might be better to set the 9-bit and BRGH configuration bits and THEN enable the transmitter and the UART.
Susan
 

Thanks for reply. I agree that sometimes what you said is happening on some device, it actually happened on me then. Maybe it's related to the code generation of the compiler but i'm not so sure about it.

I did found the problem:
Turns out that the
Code:
USART_Init;
Are not being called by the compiler, but when i used
Code:
USART_Init();
The compiler did call the init function properly.
This is quite stressing as the compiler did not throws an error whatsoever.

As the init function is succeed, i now able to both simulate on ISIS and run the UART on the real device. But then another problem related to the 433MHz module arise.
It just doesn't connected at all.
 

Hi thanks, I actually did read that site before you mention it, but it seems that my problem gone beyond the code.
I've tried debugging it using oscilloscope, but the receiver doesn't receive anything at all. The price was suspicious though, it's really really cheap.

I guess for now i'll just look for another trusted source of these module.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top