• AVR Freaks

Hot!UART Communication Troubles

Author
jack.fining
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/06/17 07:36:49
  • Location: 0
  • Status: offline
2019/07/10 06:27:51 (permalink)
0

UART Communication Troubles

Hi, I'm really new to microcontrollers and C in general, so please bear with me here. I'm just trying to set up a simple LED system to test the functionality of UART with the OS I'm working with . It looks relatively simple because it is. All I want to do here is initialize the UART functionality, and then blink the LED. I don't have a main.c here because it is listed somewhere else but all it does is call this function. Any recommendations would be appreciated. Below I'll list out the specs of what I'm working with:
 
PIC24FJ256GB210
xc16 compiler v1.25
MPLAB X IDE 3.15
 
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "xc.h"
#include <math.h>

void takePicture(int n) {
    initUART();
    UARTblinkLED(5);
}

void initUART(void) {
    //Set Pin RP16 to be an output.
    RPOR8bits.RP16R = 3;
    //Initialize UART.
    U1MODE = 0;
    U1STA = 0;
    //Set Baud Rate to 9600.
    U1BRG = 25;
    //Turn on UART functionality.
    U1MODEbits.UARTEN = 1;
    //Turn on TX functionality.
    U1STAbits.UTXEN = 1;
}

void UARTblinkLED(int x) {
    //Blink an LED x number of times. i is used as a counter.
    int i;
    for (i = 1; i <= x; i++) {
        //Turn on the LED.
        putUART(1);
        //Wait 2 seconds.
        delay(2000);
        //Turn off the LED.
        putUART(0);
        //Wait 2 seconds.
        delay(2000);
        printf("Blinked ;o \n");
    }
}

void putUART(char c) {
    U1TXREG = c;
}

#1

14 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24593
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART Communication Troubles 2019/07/10 06:47:47 (permalink)
    0
    Comes from https://www.microchip.com/forums/m1104617.aspx
    I see you have followed the request to post in a more relevant forum, but ignored the request for the config bits and ALL the code.
    post edited by ric - 2019/07/10 06:49:19

    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!
    #2
    rodims
    Super Member
    • Total Posts : 1532
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: UART Communication Troubles 2019/07/10 06:49:54 (permalink)
    0
    Duplicate yes, but Susan asked him to post it here again ...
     
    @jack
    You forgot to describe any problem or ask a question.
     
    The shown code itself is likely not OK, but where to begin, if you did not even test it yourself ?
    And as Susan said, please show all your code, including your main function, you cannot know what is ok or not ok, if you are asking for help (do you ? )
    Does it compile ? Does it do something ? What do you get ? What do you expect ?
    How do you think to receive byte values like 0 and 1 on your PC ?
    Does your LED work at least ?
    post edited by rodims - 2019/07/10 06:52:37
    #3
    LdB_ECM
    Senior Member
    • Total Posts : 162
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: online
    Re: UART Communication Troubles 2019/07/10 07:03:22 (permalink)
    0
    He does not appear to have set RP16 tristate to output, I think it RF3
    So I think it will need
    _TRISF3 = 0;

    The PutChar will also simply overrun at the moment should be something like
    void putUART(char c)
    {
        while (U1STAbits.UTXBF == 1);
        U1TXREG = c;
    }

     
    I would also offer a warning that character 0 and 1 probably won't display on some terminal programs at best they are smiley face thing. Try '0' and '1' might be better.
    #4
    jack.fining
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/06/17 07:36:49
    • Location: 0
    • Status: offline
    Re: UART Communication Troubles 2019/07/10 08:02:45 (permalink)
    0
    @rodims
    Sorry about not stating the problem, the problem is I do not see any transmission to the LED. I have a logic analyzer connected to the LED, so I should be able to see when it is sent a 1 or a 0, but nothing is sent at all. The code compiles and runs. WHat I want it to do it sent the 1 or 0 to the LED through UART. I know that it gets to that point in the code because when I run the code on the simulator, it steps into the UARTblinkLED function and runs a printf statement I have there. The LED does work, and my wiring is correct, as when I connect it to +5V and DGRND it will turn on.
     
    @LdB_ECM
    Thanks for catching that. When I add it to the code I still get no indication of transmission, but nevertheless it was needed. I think my problem might be found somewhere else instead.
     
    Below I will post my main.c, as well as the configuration bits. The only file that I am really running is the captureimage.c file, where all of the original code I posted is located.
     
    #include "xc.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include "salvo.h"
    #include "checkconnection.c"
    #include "commandhandler.c"
    #include "commandreceiver.c"
    #include "captureimage.c"
    #include "sendpayload.c"

    //MAIN.C
    //Determine the number of ticks on the system timer.
    //Greater the number of ticks, the greater the delay.
    #define LOOPS_PER_TICK 100 /* <64K */

    int main(void) {
        //Set the number of ticks for the system timer.
        unsigned int ticks;
        ticks = LOOPS_PER_TICK;

        //Used for debugging.
        printf("\n\nIn main...\n\n");

        //Initiate the Salvo RTOS.
        OSInit();

        //Create all needed tasks.
        OSCreateTask(CheckConnection, CHECK_CONNECTION_P, 2);
        OSCreateTask(CommandHandler, COMMAND_HANDLER_P, 5);
        OSCreateTask(CommandReceiver, COMMAND_RECEIVER_P, 3);
        OSCreateTask(SendPayload, SEND_PAYLOAD_P, 5);
        OSCreateTask(CaptureImage, CAPTURE_IMAGE_1_P, 5);
        OSCreateTask(CaptureImage, CAPTURE_IMAGE_2_P, 6);

        //Create semaphore for the camera.
        OSCreateBinSem(CAPTURE_IMAGE_SEM_P, 1);

        //Create message queue.
        OSgltypeMsgQP MsgQBuff[SIZEOF_MSGQ]; //Allocate memory for buffers.
        OSCreateMsgQ(MSG_COMMAND_RECEIVED_P, MQCB_P, MsgQBuff, SIZEOF_MSGQ);

        //Continuously runs as its own thread.
        while (1) {
            //Creates a system timer without interrupts.
            if (ticks-- == 0) {
                OSTimer();
                ticks = LOOPS_PER_TICK;
            }

            //Schedules tasks. Takes into account delays and priorities.
            OSSched();
        }
    }

    //CONFIG BITS
    // PIC24FJ256GB210 Configuration Bit Settings

    // 'C' source line config statements

    #include <xc.h>

    // CONFIG4

    // CONFIG3
    #pragma config WPFP = WPFP255 // Write Protection Flash Page Segment Boundary (Highest Page (same as page 170))
    #pragma config SOSCSEL = SOSC // Secondary Oscillator Power Mode Select (Secondary oscillator is in Default (high drive strength) Oscillator mode)
    #pragma config WUTSEL = LEG // Voltage Regulator Wake-up Time Select (Default regulator start-up time is used)
    #pragma config ALTPMP = ALPMPDIS // Alternate PMP Pin Mapping (EPMP pins are in default location mode)
    #pragma config WPDIS = WPDIS // Segment Write Protection Disable (Segmented code protection is disabled)
    #pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Last page (at the top of program memory) and Flash Configuration Words are not write-protected)
    #pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Protected code segment upper boundary is at the last page of program memory; the lower boundary is the code page specified by WPFP)

    // CONFIG2
    #pragma config POSCMOD = NONE // Primary Oscillator Select (Primary oscillator is disabled)
    #pragma config IOL1WAY = ON // IOLOCK One-Way Set Enable (The IOLOCK bit (OSCCON<6>) can be set once, provided the unlock sequence has been completed. Once set, the Peripheral Pin Select registers cannot be written to a second time.)
    #pragma config OSCIOFNC = OFF // OSCO Pin Configuration (OSCO/CLKO/RC15 functions as CLKO (FOSC/2))
    #pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor (Clock switching and Fail-Safe Clock Monitor are disabled)
    #pragma config FNOSC = FRCDIV // Initial Oscillator Select (Fast RC Oscillator with Postscaler (FRCDIV))
    #pragma config PLL96MHZ = ON // 96MHz PLL Startup Select (96 MHz PLL is enabled automatically on start-up)
    #pragma config PLLDIV = DIV12 // 96 MHz PLL Prescaler Select (Oscillator input is divided by 12 (48 MHz input))
    #pragma config IESO = ON // Internal External Switchover (IESO mode (Two-Speed Start-up) is enabled)

    // CONFIG1
    #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
    #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
    #pragma config ALTVREF = ALTVREDIS // Alternate VREF location Enable (VREF is on a default pin (VREF+ on RA9 and VREF- on RA10))
    #pragma config WINDIS = OFF // Windowed WDT (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
    #pragma config FWDTEN = ON // Watchdog Timer (Watchdog Timer is enabled)
    #pragma config ICS = PGx1 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1)
    #pragma config GWRP = OFF // General Segment Write Protect (Writes to program memory are allowed)
    #pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled)
    #pragma config JTAGEN = ON // JTAG Port Enable (JTAG port is enabled)

    #5
    rodims
    Super Member
    • Total Posts : 1532
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: UART Communication Troubles 2019/07/10 08:40:15 (permalink)
    0

    the problem is I do not see any transmission to the LED ...
    WHat I want it to do it sent the 1 or 0 to the LED through UART
    The LED does work, and my wiring is correct, as when I connect it to +5V and DGRND it will turn on.

    I really do not understand what you are trying to do.
    Where is the LED connected ? Are we talking about a light emitting diode ?
    How is it connected to your hardware (PIC, PC )  ? (schematic ?) 
    I did not ask, whether your LED is damaged, but whether you succesfully ever could let it blink (on / off / on / off ...), if you are not using this additional UART stuff.
     
    I dont' see calls to the functions you showed in your original post.
    #6
    ric
    Super Member
    • Total Posts : 24593
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART Communication Troubles 2019/07/10 13:11:36 (permalink)
    0
    I'm going to take a wild guess here, and assume the OP has connected the LED to the UART Tx pin.
    He never gets around to actually stating this, which is a fundamental part of asking questions clearly.
     
    I also assume that he is watching this same pin with a logic analyser. That would make sense, as UART transmisssions are way too fast to see on an LED with your eye anyway, so I don't see why the LED even gets a mention.
     
    Jack, please stop leaving us in the dark, anbd actually tell us what you are doing to test this.
    What exactly have you connected you logic analyser too?
    You still have not posted an entire program, and what you have posted does not show all the relevant calls. You are making it hard to answer by keeping essential details to yourself.
    Best strip your program down to just what you are testing now, and post THE WHOLE LOT.
     

    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!
    #7
    Aussie Susan
    Super Member
    • Total Posts : 3636
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: UART Communication Troubles 2019/07/10 22:01:58 (permalink)
    5 (1)
    With the config settings, you have JTAG on which, unless you really are using a JTAG programmer/debugger, is not a good idea. In your case all it is going is taking out of action RA0, RA1, RA4 and RA5 but if you want to use those later on then you will have issues.
    You have IOL1WAY set to on which means that the first time you lock the PPS registers, they will stay locked until a device reset. Again, this may or may not be an issue for you now but there is a whole stack of RTOS calls that you don't show and a PPS lock could be hidden in any of them. If there is on and it occurs before the call to 'initUART' then the setting of the PPS register will not work.
    One of the first things you do in the main function is to call 'printf' but that requires that the UART be configured (the 'putUART' function might be called but if the UART is not configured it will not do anything useful).
    However your main problem is that the UART wil not control the LED the way you probably think it will - at least going by your comments. when you write a value (which is 8-bits in length) to the UART buffer, it will start sending a 'start' bit to the Tx line, followed by each of the 8 bits and finally a stop bit. The line will then return to the 'idle' state. Now at 9600 baud, each bit will take approximately 100uS to send (9600 baud = 9600 bits a second in this configuration which means each bit will take 100uS to send).
    This also means that the complete time to send each 'character' will be about 1mS (8 bits of the character plus 1 start and 1 stop bit = 10 bits). Therefore the total time between when the Tx line leaves its idle state to when it returns to idle will be 1mSec. Even if you had the LED lit (or off) for that entire time, you eye cannot react to anything shorter than about 30mS. You will see something on the logic analyser (as long as it is set up correctly to capture the 1mS long burst).
    You actually send 2 characters to the UART with 2 seconds in between - but you also send all of your text strings to the same UART which means that things will get hopelessly confused.
    Put the LED on a different pin and write to the LAT register for that pin to toggle it.
    Susan
    #8
    LdB_ECM
    Senior Member
    • Total Posts : 162
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: online
    Re: UART Communication Troubles 2019/07/11 04:51:40 (permalink)
    0
    Can I also suggest you dumb the code to the bare minimum .. this is a little easier to debug :-)
    int main(void)
    {
         char ch = 'A';
         initUART();
          while (1){
                putUART(ch);
                if (ch == 'Z') ch = 'A'; else ch++;      
          }
    }

    #9
    jack.fining
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/06/17 07:36:49
    • Location: 0
    • Status: offline
    Re: UART Communication Troubles 2019/07/11 05:59:44 (permalink)
    0
    @Ric
    You can tell in my code I originally posted that I assigned it to a Tx pin. And per your comment on it being too fast, I have a 2 second delay in there. You must not have looked at the code.

    The LED and logic analyzer are both connected to a breadboard. The LED is grounded on my development board's DGND, and the input to the LED is from pin RP16, where I send it a 1. 

    I have stripped my program down and that is just what you're seeing here. That's why I didn't post ALL of my code because nearly none of it is relevant to what I'm doing HERE. All I am doing is trying to send the LED a 1 through the UART Tx pin. I have added the suggestions of @LdB_ECM
     
    @rodims
    I am connected to my development board through an ICD 3. In my original code my calls to the LED are near the very top, listed under takePicture because I'm trying to scale this up to a JPEG camera module once I get the LED working. The calls are this part of the code:


    void takePicture(int n) {
        initUART();
        UARTblinkLED(5);
    }
    #10
    rodims
    Super Member
    • Total Posts : 1532
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: UART Communication Troubles 2019/07/11 08:10:55 (permalink)
    5 (2)
    Last try.  I think in this whole thread we have more a communication problem here in the first place. The technical problem probably is easy to solve. But for some reason it is not possible to get all the information from you which we would need to help you.
     
    If you can't explain, we can't help.
     
    In my original code my calls to the LED are near the very top, listed under takePicture ...

    Meanwhile you changed your code several times.  It does make no sense at all to look at code, which you do not really use. In the last version you showed, your main function never calls takePicture respectively initUart,- how should that work ?
     
    Susan explained very well, what might be your problem (of understanding), but you ignored that completely.
    LdB_ECM proposed to reduce everything to a very small example project, so that it is possible to find out the reason for any problem. But it was not meant to be added to your code, you should ONLY use this code. Once you have learned about your problem, you then would return to your current code.
     
    -------------------------------------------------
    So let me summarize that very briefly so that there is little room for misunderstandings.
     
    It does not make sense to use the UART to send a byte value of 0 and a 1 to a LED.  
    If you still need help, please explain, why you think it would make any sense, i.e. why your are doing that.
     
    post edited by rodims - 2019/07/11 08:20:18
    #11
    jack.fining
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/06/17 07:36:49
    • Location: 0
    • Status: offline
    Re: UART Communication Troubles 2019/07/11 08:25:11 (permalink)
    0
    I think I fixed the problem. Instead of using pin RP16, I used RP2, and it worked. The LED turns on and I pick up the bits on the Logic Analyzer. Maybe RP16 wasn't working because it's used for USB applications? I've seen another post on here before where changing the pin fixed their problems. Pretty sure it's the comment by @jjmontero9 here:
    https://www.microchip.com/forums/m473875.aspx
     
     
    @Susan I will look into each of the config settings a bit more to see what I need to have turned on and off. I appreciate you point out some particular ones for me to change.
    #12
    ric
    Super Member
    • Total Posts : 24593
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART Communication Troubles 2019/07/11 13:22:34 (permalink)
    5 (1)
    jack.fining
    @Ric
    You can tell in my code I originally posted that I assigned it to a Tx pin.

    Nope. You never explicitly state that.
    You do call a routine with a name "UARTblinkLED" which sends values to the UART, with no explanation of how this controls the LED.

    And per your comment on it being too fast, I have a 2 second delay in there. You must not have looked at the code.

    The pause BETWEEN transmissions is irrelevant.
    If you send 0x01, then the bit pattern (showing the start and stop bits) is 0000000011 0100000001 (edit: corrected bit order)
    If you send 0x00, then the bit pattern is 0000000001
    Either way, the TX pin is low for eight or nine bit times, and is left high at the end of the transmission.
    There's no way you can see the difference with your eye.
     

    The LED and logic analyzer are both connected to a breadboard. The LED is grounded on my development board's DGND, and the input to the LED is from pin RP16, where I send it a 1.

    This indicates a total lack of understanding of what a UART transmission looks like.
    post edited by ric - 2019/07/11 13:43:10

    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
    mbrowning
    USNA79
    • Total Posts : 1564
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: UART Communication Troubles 2019/07/11 13:38:17 (permalink)
    0
    ricIf you send 0x01, then the bit pattern (showing the start and stop bits) is 0000000011

    I think it would actually be 0100000001 since UART is LSB first, but of course your main points are all spot on.

    Go Navy! Beat Army!
    #14
    ric
    Super Member
    • Total Posts : 24593
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART Communication Troubles 2019/07/11 13:40:28 (permalink)
    0
    mbrowning
    I think it would actually be 0100000001 since UART is LSB first, but of course your main points are all spot on.

    Agree. Been looking at too much SPI recently.
    Also agree that it doesn't change the argument. You won't see the difference on an LED at 9600 baud.

    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!
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5