EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
You are here: Home / Topics / PIC24FV16KM202 UART Setup

PIC24FV16KM202 UART Setup

|

Microcontroller › PIC › PIC24FV16KM202 UART Setup

  • This topic has 0 replies, 1 voice, and was last updated 1 year, 10 months ago by Yasein SH.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • April 15, 2019 at 1:44 am #5013
    Yasein SH
    Participant

    Can someone please help with me setting up UART on the PIC24FV16KM202 microcontroller. All I am trying to do is send the character 'A' through UART. I am using a Photon from particle for the receiving end. The photon is not receiving anything. I have tested the Photon with an Arduino and a different microcontroller and they both worked. I am just having trouble setting UART up with the PIC24FV16KM202. I have my code below, any help would be much appreciated.

    //UART Code

    // PIC24FV16KM202 Configuration Bit Settings

    // 'C' source line config statements

    // FBS
    #pragma config BWRP = OFF               // Boot Segment Write Protect (Disabled)
    #pragma config BSS = OFF                // Boot segment Protect (No boot program flash segment)

    // FGS
    #pragma config GWRP = OFF               // General Segment Write Protect (General segment may be written)
    #pragma config GCP = OFF                // General Segment Code Protect (No Protection)

    // FOSCSEL
    #pragma config FNOSC = FRCDIV           // Oscillator Select (8MHz FRC oscillator With Postscaler (FRCDIV))
    #pragma config SOSCSRC = ANA            // SOSC Source Type (Analog Mode for use with crystal)
    #pragma config LPRCSEL = HP             // LPRC Oscillator Power and Accuracy (High Power, High Accuracy Mode)
    #pragma config IESO = ON                // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))

    // FOSC
    #pragma config POSCMOD = NONE           // Primary Oscillator Configuration bits (Primary oscillator disabled)
    #pragma config OSCIOFNC = CLKO          // CLKO Enable Configuration bit (CLKO output signal enabled)
    #pragma config POSCFREQ = HS            // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency greater than 8MHz)
    #pragma config SOSCSEL = SOSCHP         // SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
    #pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Both Clock Switching and Fail-safe Clock Monitor are disabled)

    // FWDT
    #pragma config WDTPS = PS32768          // Watchdog Timer Postscale Select bits (1:32768)
    #pragma config FWPSA = PR128            // WDT Prescaler bit (WDT prescaler ratio of 1:128)
    #pragma config FWDTEN = ON              // Watchdog Timer Enable bits (WDT enabled in hardware)
    #pragma config WINDIS = OFF             // Windowed Watchdog Timer Disable bit (Standard WDT selected(windowed WDT disabled))

    // FPOR
    #pragma config BOREN = BOR3             // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware, SBOREN bit disabled)
    //#pragma config RETCFG = OFF             //  (Retention regulator is not available)
    #pragma config PWRTEN = ON              // Power-up Timer Enable bit (PWRT enabled)
    #pragma config I2C1SEL = PRI            // Alternate I2C1 Pin Mapping bit (Use Default SCL1/SDA1 Pins For I2C1)
    #pragma config BORV = V18               // Brown-out Reset Voltage bits (Brown-out Reset set to lowest voltage (1.8V))
    #pragma config MCLRE = ON               // MCLR Pin Enable bit (RA5 input pin disabled, MCLR pin enabled)

    // FICD
    #pragma config ICS = PGx1               // ICD Pin Placement Select bits (EMUC/EMUD share PGC1/PGD1)

    //============================================================================================================================

    //libraries
    #include "p24Fxxxx.h"
    #include <xc.h>
    #define _XTAL_FREQ 3200000000       //sets frequency
    #include <libpic30.h>               // __delayXXX() functions macros defined here

    #define FP 40000000
    #define BAUDRATE 9600
    #define BRGVAL ((FP/BAUDRATE)/16)-1

    #define _ISR __attribute__((interrupt))
     
    void initUART(){
       
        U2MODEbits.UARTEN = 0; // Disable the UART2
        U2STA = 0; //UARTx STATUS AND CONTROL REGISTER
       
        U2MODE = 0; // 8 bits & no parity & 1 stop
        U2MODEbits.BRGH = 1; //high baud rate enable bit
        U2BRG = 25; //9600
        U2MODEbits.UEN = 0; // UARTx Enable bits
        IFS1bits.U2RXIF = 0; //Disable reception flag interrupt for UART2
        IFS1bits.U2TXIF = 0; //Disable transmission flag interrupt for UART2
        IEC1bits.U2RXIE = 1; //Enable interruptions for UART2 reception
        IEC1bits.U2TXIE = 1; //Enable interruptions for UART2 transmission
        U2MODEbits.UARTEN = 1; //enable UART2
       
    }

    int main()
    {
        char A;
        CLKDIV = 0;
        ANSB = 0;//PORTB is digital
        TRISB = 0b0100110011011110;
       
        initUART();
        while(1)
        {
            U2STAbits.UTXEN = 1; //enable UART2
            U2TXREG = A;
            while(U2STAbits.TRMT==0) // wait here till transmit complete
            {
                Nop();
            }
            U2STAbits.UTXEN = 0; //disable UART2
        }//end of while
     return 0;
    }//end of main
     
    void __attribute__ ((interrupt,no_auto_psv)) _U2RXInterrupt(void){
        IFS1bits.U2RXIF = 0;   //clear flag
        //U2TXREG = U2RXREG;   
    }

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • What is the best way to get 230volts at 50hz? March 9, 2021
  • Appliance recycling centers for minors? March 9, 2021
  • Summer jobs to look for? March 9, 2021
  • Pulse transformers design guide for commercial use March 9, 2021
  • Ideas for a power supply? March 9, 2021

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2021 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools