Quantcast
Channel: Question and Answer » uart
Viewing all articles
Browse latest Browse all 57

Error in turning ON the LED using UART in PIC32

$
0
0

I have a PIC32 starter Kit & IO Expansion Board. PIC32 Starter Kit has 3 LEDs on baord LED1 LED2 LED3 . I have made a uart program such that if someone presses 1 on the keyboard, this 1 will be send serially to PIC32 and LED1 will be ON and if someone presses 4, LED1 will be OFF. In the same way, 2 & 5 will turn ON & OFF LED2 & 3 & 6 for LED3. Program is running fine and LED1 & LED3 are responding properly but LED2 is always turn ON whether I press the 2 or not. I dont know why is this happening with this LED2. Is there anything which I am missing. I know LEDs are working but I dont know what is happening in this case. Does anyone have any idea about this thing. Please help.

CODE:

#include <plib.h>
#include <string.h>

#pragma config FNOSC = PRIPLL, POSCMOD = HS, FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPBDIV = DIV_2, FPLLODIV = DIV_1
#pragma config FWDTEN = OFF

#define SYSTEM_FREQUENCY        72000000L
#define BAUDRATE                57600

int main()
{
    int pbFreq;
    TRISDbits.TRISD0 = 0; //output
    TRISDbits.TRISD1 = 0; //output
    TRISDbits.TRISD2 = 0; //output

    char RxBuffer[5];

    pbFreq=SYSTEMConfigPerformance(SYSTEM_FREQUENCY);
    OpenUART2( UART_EN | UART_NO_PAR_8BIT | UART_1STOPBIT, UART_RX_ENABLE | UART_TX_ENABLE, (pbFreq/16/BAUDRATE)-1);

while(1)
    {

        if((U2STAbits.URXDA)!=0)
        {
            getsUART2(5,RxBuffer , 123);

            U2STAbits.URXDA = 0;
            U2STAbits.OERR = 0; 


        }


        if(RxBuffer[0] == '1')
        {
            PORTDbits.RD0 = 1;
            putsUART2(">>LED: 1 ON");

           memset(RxBuffer,0,5*sizeof(char));
    }
    else if(RxBuffer[0] == '4')
    {
        PORTDbits.RD0 = 0;
        putsUART2(">>LED: 1 OFF");

        memset(RxBuffer,0,5*sizeof(char));
    }


    if(RxBuffer[0] == '2')
    {
        PORTDbits.RD1 = 1;
        putsUART2(">>LED: 2 ON");
        memset(RxBuffer,0,5*sizeof(char));
    }
    else if(RxBuffer[0] == '5')
    {
        PORTDbits.RD0 = 0;
        putsUART2(">>LED: 2 OFF");
        memset(RxBuffer,0,5*sizeof(char));
    }

    if(RxBuffer[0] == '3')
    {
        PORTDbits.RD2 = 1;
        putsUART2(">>LED: 3 ON");
        memset(RxBuffer,0,5*sizeof(char));
    }
    else if(RxBuffer[0] == '6')
    {
        PORTDbits.RD2 = 0;
        putsUART2(">>LED: 3 OFF");
        memset(RxBuffer,0,5*sizeof(char));
    }
}
}

Viewing all articles
Browse latest Browse all 57

Trending Articles