【问题标题】:No output for Embedded application with PIC12, MPLAB and UART带有 PIC12、MPLAB 和 UART 的嵌入式应用没有输出
【发布时间】:2017-02-06 09:17:48
【问题描述】:

我正在开发 RGB LED 项目,该项目由 PIC12F1572 控制。我使用的软件是带有 HiTech C 编译器的 MPLAB IDE。计划是使用串行通信将 LED RGB 组合数据命令发送到 PIC 以存储在一个变量中,使其执行 LED 闪烁和发光我已经能够建立 UART 通信。每个功能或步骤 I 代码都是正确的如果我编译的话,按语法并在 linux 命令行终端上工作。 如果我尝试在 MPLAB 中使用寄存器注入进行模拟,它会失败。我也想在模拟中运行它(任何人都知道寄存器注入在 MPLAB 中实际上是如何工作的?) 我尝试调试时共同面临的问题。它编译但不起作用 这是我的代码: 任何关于该问题的想法或提示都将受到高度赞赏。 我个人认为放置代码[分层方式]可能是错误的 谢谢!

#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;
    }
}

【问题讨论】:

  • 不起作用 .....你能详细说明一下吗?
  • uint8_t RX_Buffer[], RGB_data[], HEX_data[]; ...??????您声明了没有大小的数组.....
  • 目标是通过我从 UART 接收到的数据来点亮 LED。如果我对每个任务执行单独的模块,可以说“UART”-> 它可以工作。我可以接收数据并回显到串行终端..将ASCII转换为HEX工作,LED prog经过测试..我认为一切都很好,当我把代码放在一起..UART停止,没有接收到数据..(系统初始化功能之前测试过)
  • 已更正..应该是这样的->uint8_t RX_Buffer[FRAMESIZE] ,RGB_data[6] ,HEX_data[6];
  • 首先,您没有初始化颜色变量,这是一个错误。

标签: c embedded hierarchy uart mplab


【解决方案1】:

因此,直到接收到 UART 帧并回显并从存储数据中使 LED 闪烁,我才能成功,这就是我想要通过分层方式在这里进行的主要步骤

#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);
          }
        }

  }       

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多