+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Newbie level 5
    Points: 431, Level: 4

    Join Date
    Oct 2014
    Posts
    10
    Helped
    0 / 0
    Points
    431
    Level
    4

    PIC18F14K50 USART + 433MHz RF

    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
    }

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 1
    Points: 3,273, Level: 13

    Join Date
    May 2012
    Posts
    459
    Helped
    101 / 101
    Points
    3,273
    Level
    13

    Re: PIC18F14K50 USART + 433MHz RF

    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 by pjmelect; 17th April 2016 at 23:47. Reason: spelling



    •   AltAdvertisment

        
       

  3. #3
    Advanced Member level 3
    Points: 6,504, Level: 19

    Join Date
    Jan 2015
    Posts
    946
    Helped
    297 / 297
    Points
    6,504
    Level
    19

    Re: PIC18F14K50 USART + 433MHz RF

    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



    •   AltAdvertisment

        
       

  4. #4
    Newbie level 5
    Points: 431, Level: 4

    Join Date
    Oct 2014
    Posts
    10
    Helped
    0 / 0
    Points
    431
    Level
    4

    Re: PIC18F14K50 USART + 433MHz RF

    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)



  5. #5
    Advanced Member level 3
    Points: 6,504, Level: 19

    Join Date
    Jan 2015
    Posts
    946
    Helped
    297 / 297
    Points
    6,504
    Level
    19

    Re: PIC18F14K50 USART + 433MHz RF

    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



  6. #6
    Newbie level 5
    Points: 431, Level: 4

    Join Date
    Oct 2014
    Posts
    10
    Helped
    0 / 0
    Points
    431
    Level
    4

    Re: PIC18F14K50 USART + 433MHz RF

    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.



    •   AltAdvertisment

        
       

  7. #7
    Advanced Member level 1
    Points: 5,530, Level: 17

    Join Date
    Jul 2004
    Location
    Hungary
    Posts
    474
    Helped
    189 / 189
    Points
    5,530
    Level
    17

    Re: PIC18F14K50 USART + 433MHz RF

    Hi Astralist;
    Look at this site, I hope it will help you:

    http://www.romanblack.com/RF/cheapRFmodules.htm



  8. #8
    Newbie level 5
    Points: 431, Level: 4

    Join Date
    Oct 2014
    Posts
    10
    Helped
    0 / 0
    Points
    431
    Level
    4

    Re: PIC18F14K50 USART + 433MHz RF

    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.



--[[ ]]--