【MAX7800与ESP8266mcu串口通讯点灯】

【MAX7800与ESP8266mcu通讯】

  • 1. 前言
  • 2. 实验条件
    • 2.1 硬件条件
    • 2.2 软件条件
  • 3. 程序编写
    • 3.1 ESP8266程序解剖
    • 3.2 MAX7800程序解剖
  • 4. 实验效果
    • 4.1 esp8266打印如下
    • 4.2 max7800打印如下
  • 5. 小结

在这里插入图片描述

1. 前言

前期搭好MAX7800 的eclipse和ESP82666的Arduino开发环境,现在开始慢慢实现这两者的通讯,目前MAX7800 羽毛板可以发送字符串,但是ESP8266无法连续接收,因此采用简单的单个字符的串口通讯实现点灯功能
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

不要问我D0为什么不行,波特率在9600以下都可以,因为它本身不适合

2. 实验条件

2.1 硬件条件

本次采用ESP8266node MCU开发板和MAX7800羽毛板,利用ESP8266软串口通讯与MAX7800 UART2硬串口搭建单字符通讯,采用关键字符实现开关灯功能

名称 端口
LED +接D2,-接GND
ESP8266 P1_1接D3,P1_0接D1,GND接GND,mcro数据线
MAX7800 P1_1接D3,P1_0接D1,GND接GND,mcro数据线

在这里插入图片描述

2.2 软件条件

ESP8266-NodeMCU物联网开发之Arduino环境搭建
【window下配置Maxim SDK环境】

3. 程序编写

3.1 ESP8266程序解剖

三个子程序

  1. 读取软硬串口字符
  2. 打印两次不同的字符
  3. 测试ok字符

主程序

  1. 读取软硬串口
  2. 判别是否为ok
  3. 是则灯亮,进而判别是否为stop,退出
    在这里插入图片描述
    源代码

#include 

volatile int item;
volatile int item1;
SoftwareSerial mySerial(0,5);

void procedure() {
  if (item != item1) {
    Serial.print("item:  ");
    Serial.println(item);
    item1 = item;

  }
}

void procedure2() {
  if (Serial.available() > 0) {
    item = Serial.read();

  }
  if (mySerial.available() > 0) {
    item = mySerial.read();

  }
}

void procedure3() {
  Serial.println("ok");
}

void setup(){
  item = 0;
  item1 = 0;
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(4, OUTPUT);
  digitalWrite(4,LOW);
  Serial.println(item);
  mySerial.println(item);
}

void loop(){
  delay(100);
  procedure2();
  procedure();
  while (item == '1') {
    procedure3();
    delay(100);
    digitalWrite(4,HIGH);
    procedure2();
    procedure();
    if (item == '0') {
      digitalWrite(4,LOW);
      break;

    }
  }

}

3.2 MAX7800程序解剖

采用uart实例教程改写而成
按照1:4时间比例发送01,对此esp8266按照此间隔刷新LED灯亮灭

******************************************************************************/

/**
 * @file    main.c
 * @brief   UART!
 * @details This example demonstrates the UART Loopback Test.
 */

/***** Includes *****/
#include 
#include 
#include 
#include "mxc_device.h"
#include "led.h"
#include "board.h"
#include "mxc_delay.h"
#include "uart.h"
#include "dma.h"
#include "nvic_table.h"
#include "mxc_delay.h"
/***** Definitions *****/
#define DMA

#define UART_BAUD 9600
//#define UART_BAUD 115200
#define BUFF_SIZE 1024
#define BUFF1_SIZE 12

/***** Globals *****/
volatile int READ_FLAG;
volatile int DMA_FLAG;

#if defined(BOARD_EVKIT_V1)
#define READING_UART 1
#define WRITING_UART 2
#elif defined(BOARD_FTHR_REVA)
//#define READING_UART 2
#define READING_UART 2
#define WRITING_UART 2
#else
#warning "This example has been written for the MAX78000 Ev Kit or FTHR board."
#endif

/***** Functions *****/
#ifdef DMA
void DMA_Handler(void)
{
    MXC_DMA_Handler();
    DMA_FLAG = 0;
}
#else
void UART_Handler(void)
{
    MXC_UART_AsyncHandler(MXC_UART_GET_UART(READING_UART));
}
#endif

void readCallback(mxc_uart_req_t *req, int error)
{
    READ_FLAG = error;
}

void writemsg(uint8_t *TxData,int BUFF)
{
    mxc_uart_req_t write_req;
    write_req.uart = MXC_UART_GET_UART(WRITING_UART);
    write_req.txData = TxData;
    write_req.txLen = BUFF;
    write_req.rxLen = 0;
    write_req.callback = NULL;

    MXC_UART_Transaction(&write_req);
    MXC_Delay(100000);
}
/******************************************************************************/
int main(void)
{
    int error,  fail = 0;
//    uint8_t TestTxData[BUFF1_SIZE];
    uint8_t RxData[BUFF_SIZE];

    printf("\n\n**************** UART Example ******************\n");
    printf("This example sends data from one UART to another.\n");
    printf("\nConnect the TX pin of UART%d to the RX pin of UART%d for this example.\n",
           WRITING_UART, READING_UART);

    printf("\n-->UART Baud \t: %d Hz\n", UART_BAUD);
    printf("\n-->Test Length \t: %d bytes\n", BUFF_SIZE);

    memset(RxData, 0x0, BUFF_SIZE);

#ifdef DMA
    MXC_DMA_Init();
    MXC_DMA_ReleaseChannel(0);
    MXC_NVIC_SetVector(DMA0_IRQn, DMA_Handler);
    NVIC_EnableIRQ(DMA0_IRQn);
#else
    NVIC_ClearPendingIRQ(MXC_UART_GET_IRQ(READING_UART));
    NVIC_DisableIRQ(MXC_UART_GET_IRQ(READING_UART));
    MXC_NVIC_SetVector(MXC_UART_GET_IRQ(READING_UART), UART_Handler);
    NVIC_EnableIRQ(MXC_UART_GET_IRQ(READING_UART));
#endif

    // Initialize the UART
    if ((error = MXC_UART_Init(MXC_UART_GET_UART(READING_UART), UART_BAUD, MXC_UART_APB_CLK)) !=
        E_NO_ERROR) {
        printf("-->Error initializing UART: %d\n", error);
        printf("-->Example Failed\n");
        while (1) {}
    }

    if ((error = MXC_UART_Init(MXC_UART_GET_UART(WRITING_UART), UART_BAUD, MXC_UART_APB_CLK)) !=
        E_NO_ERROR) {
        printf("-->Error initializing UART: %d\n", error);
        printf("-->Example Failed\n");
        while (1) {}
    }

    printf("-->UART Initialized\n\n");


    READ_FLAG = 1;
    DMA_FLAG = 1;
    uint8_t *firstName = "0\n";
    uint8_t *lastName = "1\n";
    if (fail == 0) {
		LED_On(LED1);
		printf("\n-->EXAMPLE SUCCEEDED\n");
		int count=0;
		while(1){
			printf("count : %d\n", count++);
//			sprintf(TestTxData, "%s%d\n", firstName, count);
//			writemsg( TestTxData, BUFF1_SIZE);
			MXC_Delay(200000);
			if(count%5==0){
				writemsg( lastName, 1);
			}
			else
				writemsg( firstName, 1);
			MXC_Delay(500000);
		}
    }
    return fail;
}

下面给出max7800输出字符串代码,无法与esp8266通讯,暂时没有找到原因

/**
 * @file    main.c
 * @brief   UART!
 * @details This example demonstrates the UART Loopback Test.
 */

/***** Includes *****/
#include 
#include 
#include 
#include "mxc_device.h"
#include "led.h"
#include "board.h"
#include "mxc_delay.h"
#include "uart.h"
#include "dma.h"
#include "nvic_table.h"
#include "mxc_delay.h"
/***** Definitions *****/
#define DMA

#define UART_BAUD 9600
//#define UART_BAUD 115200
#define BUFF_SIZE 1024
#define BUFF1_SIZE 12

/***** Globals *****/
volatile int READ_FLAG;
volatile int DMA_FLAG;

#if defined(BOARD_EVKIT_V1)
#define READING_UART 1
#define WRITING_UART 2
#elif defined(BOARD_FTHR_REVA)
//#define READING_UART 2
#define READING_UART 2
#define WRITING_UART 2
#else
#warning "This example has been written for the MAX78000 Ev Kit or FTHR board."
#endif

/***** Functions *****/
#ifdef DMA
void DMA_Handler(void)
{
    MXC_DMA_Handler();
    DMA_FLAG = 0;
}
#else
void UART_Handler(void)
{
    MXC_UART_AsyncHandler(MXC_UART_GET_UART(READING_UART));
}
#endif

void readCallback(mxc_uart_req_t *req, int error)
{
    READ_FLAG = error;
}

void writemsg(uint8_t *TxData,int BUFF)
{
    mxc_uart_req_t write_req;
    write_req.uart = MXC_UART_GET_UART(WRITING_UART);
    write_req.txData = TxData;
    write_req.txLen = BUFF;
    write_req.rxLen = 0;
    write_req.callback = NULL;

    MXC_UART_Transaction(&write_req);
}
/******************************************************************************/
int main(void)
{
    int error,  fail = 0;
    uint8_t TestTxData[BUFF1_SIZE];
    uint8_t RxData[BUFF_SIZE];

    printf("\n\n**************** UART Example ******************\n");
    printf("This example sends data from one UART to another.\n");
    printf("\nConnect the TX pin of UART%d to the RX pin of UART%d for this example.\n",
           WRITING_UART, READING_UART);

    printf("\n-->UART Baud \t: %d Hz\n", UART_BAUD);
    printf("\n-->Test Length \t: %d bytes\n", BUFF_SIZE);

    memset(RxData, 0x0, BUFF_SIZE);

#ifdef DMA
    MXC_DMA_Init();
    MXC_DMA_ReleaseChannel(0);
    MXC_NVIC_SetVector(DMA0_IRQn, DMA_Handler);
    NVIC_EnableIRQ(DMA0_IRQn);
#else
    NVIC_ClearPendingIRQ(MXC_UART_GET_IRQ(READING_UART));
    NVIC_DisableIRQ(MXC_UART_GET_IRQ(READING_UART));
    MXC_NVIC_SetVector(MXC_UART_GET_IRQ(READING_UART), UART_Handler);
    NVIC_EnableIRQ(MXC_UART_GET_IRQ(READING_UART));
#endif

    // Initialize the UART
    if ((error = MXC_UART_Init(MXC_UART_GET_UART(READING_UART), UART_BAUD, MXC_UART_APB_CLK)) !=
        E_NO_ERROR) {
        printf("-->Error initializing UART: %d\n", error);
        printf("-->Example Failed\n");
        while (1) {}
    }

    if ((error = MXC_UART_Init(MXC_UART_GET_UART(WRITING_UART), UART_BAUD, MXC_UART_APB_CLK)) !=
        E_NO_ERROR) {
        printf("-->Error initializing UART: %d\n", error);
        printf("-->Example Failed\n");
        while (1) {}
    }

    printf("-->UART Initialized\n\n");


    READ_FLAG = 1;
    DMA_FLAG = 1;
    char *firstName = "count: ";

    if (fail == 0) {
        LED_On(LED1);
        printf("\n-->EXAMPLE SUCCEEDED\n");
        int count=0;
        while(1){
            printf("count : %d\n", count++);
            sprintf(TestTxData, "%s%d\n", firstName, count);
            writemsg( TestTxData, BUFF1_SIZE);
            MXC_Delay(500000);
        }
    }
    return fail;
}

4. 实验效果

4.1 esp8266打印如下

在这里插入图片描述

已打开串口: COM12
0
item:  49
ok
ok
ok
ok
ok
ok
ok
ok
item:  48
item:  49

4.2 max7800打印如下

在这里插入图片描述

19:28:30.829 -> 
19:28:30.829 -> 
19:28:30.832 -> **************** UART Example ******************
19:28:30.834 -> This example sends data from one UART to another.
19:28:30.838 -> 
19:28:30.838 -> Connect the TX pin of UART2 to the RX pin of UART2 for this example.
19:28:30.844 -> 
19:28:30.844 -> -->UART Baud 	: 9600 Hz
19:28:30.846 -> 
19:28:30.846 -> 
19:28:30.846 -> -->Test Length 	: 1024 bytes
19:28:30.848 -> -->UART Initialized
19:28:30.850 -> 
19:28:30.850 -> 
19:28:30.850 -> -->EXAMPLE SUCCEEDED
19:28:30.853 -> count : 0
19:28:31.651 -> count : 1
19:28:32.448 -> count : 2
19:28:33.245 -> count : 3
19:28:34.042 -> count : 4
19:28:34.839 -> count : 5

5. 小结

通过对这篇文章我们掌握了MAX7800与ESP8266mcu串口通讯点灯,接下来会有许多有趣的实验,尝试与Arduino通讯做语音小车,进而丰富我们的生活。从而实现对外部世界进行感知,充分认识这个有机与无机的环境,科学地合理地进行创作和发挥效益,然后为人类社会发展贡献一点微薄之力。
再次非常感谢大赛支持和胡同学
参考文献:
window10下配置Maxim SDK
数据手册
MAX78000板卡项目汇总
应用笔记
【window下配置Maxim SDK环境】
【MAX78000基础案例演示】
MAX78000 关键字定位演示 v.3

你可能感兴趣的:(arduino学习,MAX78000,单片机,嵌入式硬件)