1
votes

I am working on RGB LED project and that's controlled by a PIC12F1572. The software that I am using is MPLAB IDE with the HiTech C compiler. The plan is to use serial communication to send LED RGB combination data commands to the PIC to be stored in a variable that will make it perform the LED blink and glowing I have been able to establish UART communication.Every function or step I code is right by syntax and works on linux command line terminal if I compile.. And it fails if I try to simulate using register injection in MPLAB.I wanted to run it in simulation also (anyone knows how register injection actuallly works in MPLAB?) The problem I face together when I try to debug . it compiles but doesn't work here is my code : Any idea or hint about the problem will be highly appreciated. I personally fee that placing the code [hierarchical way] may be wrong Thanks!

#include <xc.h>
#include "mcc.h"
#include "LED.h"
#include "tmr0.h"
#include "interrupt_manager.h"

void SetLedColor(uint16_t R_color, uint16_t G_color, uint16_t B_color);

void main(void)
{
    uint8_t data, i, j;

    uint16_t R_value, G_value, B_value;
    uint8_t value;
    uint8_t RX_Buffer[FRAMESIZE] ,RGB_data[6] ,HEX_data[6];

    // initialize the device
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();           // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable();   // Enable the Peripheral Interrupts

    while (1)
    {
        // EUSART_Write(0x61);
        while (!RCIF)
        {
            data = EUSART_Read();                     // Read received character
            for (i = 0; i < FRAMESIZE; i++)
            {
                RX_Buffer[i] = data;
            }

            EUSART_Write(data);
        }

        //check if any data is received

        for (j = 0; j = 5; j++)       // get the RGB value in the separate array
        {
            RGB_data[j] = RX_Buffer[j + 3];
            HEX_data[value] = RGB_data[j] / 16;
        }

        if (RX_Buffer[0] == 'R' && RX_Buffer[FRAMESIZE - 1] == '\n')
        {
            //ASCII to HEX separate values

            // uint32_t number = (uint32_t)strtol(HEX_data, NULL, 16);
            // R_value = number >>16;
            // G_value = (number & 0xffff) >> 8;
            // B_value = (number & 0x0000FF);

            R_value = (uint16_t) atoh(HEX_data[0], HEX_data[1]);
            G_value = (uint16_t) atoh(HEX_data[2], HEX_data[3]);
            B_value = (uint16_t) atoh(HEX_data[4], HEX_data[5]);

        }

        SetLedColor(R_value, G_value, B_value);
    }

}

void SetLedColor(uint16_t R_color, uint16_t G_color, uint16_t B_color)
{
    if (R_color == 0xFF)
    {
        LATAbits.LATA2 = 1;
    }
    else
    {
        LATAbits.LATA2 = 0;
    }

    if (G_color == 0xFF)
    {

        LATAbits.LATA4 = 1;
    }
    else
    {
        LATAbits.LATA4 = 0;
    }
    if (B_color == 0xFF)
    {

        LATAbits.LATA5 = 1;
    }
    else
    {
        LATAbits.LATA5 = 0;
    }
}
1
doesn't work ..... Could you elaborate a bit?LPs
uint8_t RX_Buffer[], RGB_data[], HEX_data[]; ...?????? You declared arrays without size.....LPs
the objective is to GLOW the LED by the data which I receive from the UART..and if I do separate modules of each task , lets say " UART"-> it works..I can receive the data and echod back to serial terminal too..Conversion ASCII to HEX works , LED prog is tested ..everything I believe is fine and when I put the code together..UART stops , not receiving the data..(system initialization function is tested before)Garryp
Corrected..It should be like this->uint8_t RX_Buffer[FRAMESIZE] ,RGB_data[6] ,HEX_data[6];Garryp
To begin with, you don't initialize the color variables, which is a bug.Lundin

1 Answers

0
votes

So till the receiving the UART frame and echoed back and from the storing data make LED blink , I am able to succeed and this is what I wanted for primary step here by hierarchical way

#include "mcc_generated_files/mcc.h"
#include <stdlib.h>
#include <stdio.h>
#include "atoh.h"
#include "LED.h"
#define _XTAL_FREQ 16000000
#define FRAMESIZE 19

void main(void)
{
   uint8_t data,i,j,got_char;

   uint8_t R_value, G_value ,B_value;
   uint8_t value;
   uint8_t RX_Buffer[FRAMESIZE];
   uint8_t RGB_data[6] ,HEX_data[6];

    // initialize the device
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();                       // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable();                   // Enable the Peripheral Interrupts

  while (1)
 {

        if (EUSART_DataReady)
        {
            for (i = 0; i<FRAMESIZE; i++)
            {
                RX_Buffer[i] = EUSART_Read();
                if (RX_Buffer[i] == '\n')
                   break;
            }

            RX_Buffer[i] = '\n';                        //append '\n' at the end of stoaring array for detection of frame
            RX_Buffer[i+1] = '\0';                      // End of an array
            EUSART_WriteAnArrayOfBytes(RX_Buffer);


        if(RX_Buffer[0]=='R' && RX_Buffer[FRAMESIZE-2] == '\n')   //check for correct frame
          {

            LATAbits.LATA2 = 1;
            __delay_ms(2000);
            LATAbits.LATA2 = 0;
            __delay_ms(1000);
          }
        }

  }