8266 RTOS 红外遥控代码
时间: 2024-03-02 15:45:17 浏览: 188
以下是ESP8266_RTOS_SDK-2.0.0中红外遥控器demo的代码,可以参考:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/rmt.h"
#include "soc/rmt_reg.h"
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define RMT_TX_GPIO_NUM 14
#define NEC_HEADER_HIGH_US 9000
#define NEC_HEADER_LOW_US 4500
#define NEC_BIT_HIGH_US 560
#define NEC_ONE_LOW_US 1690
#define NEC_ZERO_LOW_US 560
#define NEC_REPEAT_PERIOD_US 110000
#define RMT_CLK_DIV 100
#define RMT_TICK_10_US (80000000/RMT_CLK_DIV/100000)
#define RMT_TX_DATA_NUM 16
#define RMT_TX_CARRIER_EN 1
#define RMT_TX_CARRIER_FREQ 38000
#define RMT_TX_CARRIER_DUTY 33
static const uint16_t nec_tx_data[RMT_TX_DATA_NUM * 2] = {
NEC_HEADER_HIGH_US/RMT_TICK_10_US, 1,
NEC_HEADER_LOW_US/RMT_TICK_10_US, 0,
NEC_BIT_HIGH_US/RMT_TICK_10_US, 1,
NEC_ZERO_LOW_US/RMT_TICK_10_US, 0,
NEC_BIT_HIGH_US/RMT_TICK_10_US, 1,
NEC_ZERO_LOW_US/RMT_TICK_10_US, 0,
NEC_BIT_HIGH_US/RMT_TICK_10_US, 1,
NEC_ONE_LOW_US/RMT_TICK_10_US, 0,
NEC_BIT_HIGH_US/RMT_TICK_10_US, 1,
NEC_ZERO_LOW_US/RMT_TICK_10_US, 0,
NEC_BIT_HIGH_US/RMT_TICK_10_US, 1,
NEC_ONE_LOW_US/RMT_TICK_10_US, 0,
NEC_BIT_HIGH_US/RMT_TICK_10_US, 1,
NEC_ZERO_LOW_US/RMT_TICK_10_US, 0,
NEC_BIT_HIGH_US/RMT_TICK_10_US, 1,
NEC_ONE_LOW_US/RMT_TICK_10_US, 0
};
void rmt_tx_init()
{
rmt_config_t rmt_tx;
rmt_tx.channel = RMT_TX_CHANNEL;
rmt_tx.gpio_num = RMT_TX_GPIO_NUM;
rmt_tx.mem_block_num = 1;
rmt_tx.clk_div = RMT_CLK_DIV;
rmt_tx.tx_config.loop_en = 0;
rmt_tx.tx_config.carrier_en = RMT_TX_CARRIER_EN;
rmt_tx.tx_config.idle_output_en = 1;
rmt_tx.tx_config.idle_level = 0;
rmt_tx.tx_config.carrier_freq_hz = RMT_TX_CARRIER_FREQ;
rmt_tx.tx_config.carrier_duty_percent = RMT_TX_CARRIER_DUTY;
rmt_tx.tx_config.carrier_level = 1;
rmt_tx.rmt_mode = 0;
rmt_config(&rmt_tx);
rmt_driver_install(rmt_tx.channel, 0, 0);
}
void nec_tx_task(void *arg)
{
while (1) {
rmt_write_items(RMT_TX_CHANNEL, nec_tx_data, RMT_TX_DATA_NUM * 2, true);
vTaskDelay(NEC_REPEAT_PERIOD_US / portTICK_PERIOD_MS);
}
}
void app_main()
{
rmt_tx_init();
xTaskCreate(nec_tx_task, "nec_tx_task", 2048, NULL, 10, NULL);
}
```
阅读全文