Page 12 of 16
Page 292
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include "driver/gptimer.h"
#include <unistd.h>
void delay_us(int t)
{
usleep(t);
}
gptimer_handle_t tick_us_start(void) {
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000,
};
gptimer_handle_t gptimer_us = NULL;
gptimer_new_timer(&timer_config, &gptimer_us);
gptimer_enable(gptimer_us);
gptimer_start(gptimer_us);
return gptimer_us;
}
int64_t tick_us(gptimer_handle_t gptimer_us, int64_t offset) {
uint64_t count;
gptimer_get_raw_count(gptimer_us, &count);
return count + offset;
}
void app_main(void)
{
gptimer_handle_t timer = tick_us_start();
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_set_level(2, 1);
delay_us(1000);
gpio_set_level(2, 0);
delay_us(1000);
gpio_set_direction(2, GPIO_MODE_INPUT);
while (gpio_get_level(2) == 1)
{
};
while (gpio_get_level(2) == 0)
{
};
while (gpio_get_level(2) == 1)
{
};
int64_t t2;
uint32_t data = 0;
int64_t t1 = tick_us(timer, 0);
for (int i = 0; i < 32; i++)
{
while (gpio_get_level(2) == 0)
{
};
while (gpio_get_level(2) == 1)
{
};
t2 = tick_us(timer, 0);
data = data << 1;
data = data | ((t2 - t1) > 100);
t1 = t2;
}
uint8_t checksum = 0;
for (int i = 0; i < 8; i++)
{
while (gpio_get_level(2) == 0)
{
};
while (gpio_get_level(2) == 1)
{
};
t2 = tick_us(timer, 0);
checksum = checksum << 1;
checksum = checksum | ((t2 - t1) > 100);
t1 = t2;
}
printf("data %ld\n", data);
uint8_t byte1 = (data >> 24 & 0xFF);
uint8_t byte2 = (data >> 16 & 0xFF);
uint8_t byte3 = (data >> 8 & 0xFF);
uint8_t byte4 = (data & 0xFF);
printf("Checksum %X %X\n", checksum,
(byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float)((byte1 << 8) | byte2) / 10.0;
printf("Humidity= %f %%\n", humidity);
float temperature;
int neg = byte3 & 0x80;
byte3 = byte3 & 0x7F;
temperature = (float)(byte3 << 8 | byte4) / 10.0;
if (neg > 0)
temperature = -temperature;
printf("Temperature= %f C\n", temperature);
}
Page 294 from fragment
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include <unistd.h>
void delay_us(int t)
{
usleep(t);
}
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_set_level(2, 1);
delay_us(1000);
gpio_set_level(2, 0);
delay_us(1000);
gpio_set_direction(2, GPIO_MODE_INPUT);
while (gpio_get_level(2) == 1)
{
};
while (gpio_get_level(2) == 0)
{
};
while (gpio_get_level(2) == 1)
{
};
uint32_t data = 0;
for (int i = 0; i < 32; i++)
{
while (gpio_get_level(2) == 0)
{
};
delay_us(50);
data = data << 1;
data = data | gpio_get_level(2);
while (gpio_get_level(2) == 1)
{
};
}
uint8_t checksum = 0;
for (int i = 0; i < 8; i++)
{
while (gpio_get_level(2) == 0)
{
};
delay_us(50);
checksum = checksum << 1;
checksum = checksum | gpio_get_level(2);
while (gpio_get_level(2) == 1)
{
};
}
printf("data %ld\n", data);
uint8_t byte1 = (data >> 24 & 0xFF);
uint8_t byte2 = (data >> 16 & 0xFF);
uint8_t byte3 = (data >> 8 & 0xFF);
uint8_t byte4 = (data & 0xFF);
printf("Checksum %X %X\n", checksum,
(byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float)((byte1 << 8) | byte2) / 10.0;
printf("Humidity= %f %%\n", humidity);
float temperature;
int neg = byte3 & 0x80;
byte3 = byte3 & 0x7F;
temperature = (float)(byte3 << 8 | byte4) / 10.0;
if (neg > 0)
temperature = -temperature;
printf("Temperature= %f C\n", temperature);
}
Page 298
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include "driver/rmt_rx.h"
#include <unistd.h>
TaskHandle_t taskhandle;
void delay_us(int t)
{
usleep(t);
}
bool rmtrxDone(rmt_channel_handle_t channel,
const rmt_rx_done_event_data_t *edata, void *user_data)
{
xTaskResumeFromISR(*(TaskHandle_t *)user_data);
return true;
}
rmt_symbol_word_t raw_symbols[100];
void app_main(void)
{
rmt_rx_channel_config_t rmtrxconfig = {
.gpio_num = 2,
.resolution_hz = 1000000,
.mem_block_symbols = 100,
.clk_src = RMT_CLK_SRC_DEFAULT,
.flags.invert_in = false,
.flags.with_dma = false,
};
rmt_channel_handle_t rmtrxHandle;
rmt_new_rx_channel(&rmtrxconfig, &rmtrxHandle);
rmt_rx_event_callbacks_t cbs = {
.on_recv_done = rmtrxDone};
taskhandle = xTaskGetCurrentTaskHandle();
rmt_rx_register_event_callbacks(rmtrxHandle, &cbs, &taskhandle);
rmt_enable(rmtrxHandle);
rmt_receive_config_t receive_config = {
.signal_range_min_ns = 2000,
.signal_range_max_ns = 900000,
};
while (true)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_set_level(2, 1);
delay_us(1000);
gpio_set_level(2, 0);
delay_us(1000);
gpio_set_direction(2, GPIO_MODE_INPUT);
while (gpio_get_level(2) == 1)
{
};
while (gpio_get_level(2) == 0)
{
};
while (gpio_get_level(2) == 1)
{
};
rmt_receive(rmtrxHandle, raw_symbols, sizeof(raw_symbols),
&receive_config);
vTaskSuspend(taskhandle);
uint8_t byte1 = 0;
uint8_t byte2 = 0;
uint8_t byte3 = 0;
uint8_t byte4 = 0;
uint8_t checksum = 0;
for (int i = 0; i < 40; i = i + 1)
{
switch (i / 8)
{
case 0:
byte1 = byte1 << 1;
byte1 = byte1 | (raw_symbols[i].duration0 > 50);
break;
case 1:
byte2 = byte2 << 1;
byte2 = byte2 | (raw_symbols[i].duration0 > 50);
break;
case 2:
byte3 = byte3 << 1;
byte3 = byte3 | (raw_symbols[i].duration0 > 50);
break;
case 3:
byte4 = byte4 << 1;
byte4 = byte4 | (raw_symbols[i].duration0 > 50);
break;
case 4:
checksum = checksum << 1;
checksum = checksum | (raw_symbols[i].duration0 > 50);
break;
}
}
printf("Checksum %X %X\n", checksum,
(byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float)((byte1 << 8) | byte2) / 10.0;
printf("Humidity= %f %%\n", humidity);
float temperature;
int neg = byte3 & 0x80;
byte3 = byte3 & 0x7F;
temperature = (float)(byte3 << 8 | byte4) / 10.0;
if (neg > 0)
temperature = -temperature;
printf("Temperature= %f C\n", temperature);
delay_us(1000000);
}
}
Page 311
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include <unistd.h>
void delay_us(int t)
{
usleep(t);
}
uint8_t readBit(uint8_t pin)
{
gpio_set_level(pin, 0);
delay_us(2);
gpio_set_level(pin, 1);
delay_us(5);
uint8_t b = gpio_get_level(pin);
delay_us(60);
return b;
}
void writeBit(uint8_t pin, int b)
{
int delay1, delay2;
if (b == 1)
{
delay1 = 6;
delay2 = 64;
}
else
{
delay1 = 60;
delay2 = 10;
}
gpio_set_level(pin, 0);
delay_us(delay1);
gpio_set_level(pin, 1);
delay_us(delay2);
}
int readByte(uint8_t pin)
{
int byte = 0;
for (int i = 0; i < 8; i++)
{
byte = byte | readBit(pin) << i;
};
return byte;
}
void writeByte(uint8_t pin, int byte)
{
for (int i = 0; i < 8; i++)
{
if (byte & 1)
{
writeBit(pin, 1);
}
else
{
writeBit(pin, 0);
}
byte = byte >> 1;
}
}
int presence(int pin)
{
gpio_reset_pin(pin);
gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
gpio_set_level(pin, 1);
delay_us(1000);
gpio_set_level(pin, 0);
delay_us(480);
gpio_set_level(pin, 1);
delay_us(70);
int res = gpio_get_level(pin);
delay_us(410);
return res;
}
int convert(uint8_t pin)
{
writeByte(pin, 0x44);
int i;
for (i = 0; i < 500; i++)
{
delay_us(10000);
if (readBit(pin) == 1)
break;
}
return i;
}
uint8_t crc8(uint8_t *data, uint8_t len)
{
uint8_t temp;
uint8_t databyte;
uint8_t crc = 0;
for (int i = 0; i < len; i++)
{
databyte = data[i];
for (int j = 0; j < 8; j++)
{
temp = (crc ^ databyte) & 0x01;
crc >>= 1;
if (temp)
crc ^= 0x8C;
databyte >>= 1;
}
}
return crc;
}
float getTemperature(uint8_t pin)
{
if (presence(pin) == 1)
return -1000;
writeByte(pin, 0xCC);
if (convert(pin) == 500)
return -3000;
presence(pin);
writeByte(pin, 0xCC);
writeByte(pin, 0xBE);
uint8_t data[9];
for (int i = 0; i < 9; i++)
{
data[i] = readByte(pin);
}
uint8_t crc = crc8(data, 9);
if (crc != 0)
return -2000;
int t1 = data[0];
int t2 = data[1];
int16_t temp1 = (t2 << 8 | t1);
float temp = (float)temp1 / 16;
return temp;
}
void app_main(void)
{
while (true)
{
float temp = getTemperature(2);
printf("temperature=%f\n", temp);
delay_us(1000000);
}
}
Page 318 ESP32 S3
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void writeGRB(int pin, int GRB)
{
int mask = 0x800000;
volatile int duty = 0;
for (int j = 0; j < 24; j++)
{
gpio_set_level(pin, 1);
if ((GRB & mask))
{
for (volatile int i = 0; i < 3; i++)
{
}
duty = 0;
duty = 0;
gpio_set_level(pin, 0);
duty++;
duty = 0;
duty = 0;
duty = 0;
}
else
{
duty = 0;
gpio_set_level(pin, 0);
for (volatile int i = 0; i < 5; i++)
{
}
}
mask = mask >> 1;
}
vTaskDelay(60 / portTICK_PERIOD_MS);
}
void app_main(void)
{
int pin = 48;
gpio_reset_pin(pin);
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
gpio_set_level(pin, 0);
vTaskDelay(10 / portTICK_PERIOD_MS);
int color = 0x0000FF;
writeGRB(pin, color);
vTaskDelay(1000 / portTICK_PERIOD_MS);
color = 0x00FF00;
writeGRB(pin, color);
vTaskDelay(1000 / portTICK_PERIOD_MS);
color = 0xFF0000;
writeGRB(pin, color);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
Page 319 complete program
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void writeGRB(int pin, int GRB)
{
int mask = 0x800000;
volatile int duty = 0;
for (int j = 0; j < 24; j++)
{
gpio_set_level(pin, 1);
if ((GRB & mask))
{
for (volatile int i = 0; i < 2; i++)
{
}
duty = 0;
gpio_set_level(pin, 0);
duty = 0;
duty = 0;
duty = 0;
duty = 0;
}
else
{
gpio_set_level(pin, 0);
for (volatile int i = 0; i < 3; i++)
{
}
duty = 0;
}
vTaskDelay(60 / portTICK_PERIOD_MS);
}
}
void app_main(void)
{
int pin = 48;
gpio_reset_pin(pin);
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
gpio_set_level(pin, 0);
vTaskDelay(10 / portTICK_PERIOD_MS);
int color = 0x0000FF;
writeGRB(pin, color);
vTaskDelay(1000 / portTICK_PERIOD_MS);
color = 0x00FF00;
writeGRB(pin, color);
vTaskDelay(1000 / portTICK_PERIOD_MS);
color = 0xFF0000;
writeGRB(pin, color);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
Page 321
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "driver/rmt_tx.h"
void convertGRB(int GRB, rmt_symbol_word_t *rawdata)
{
int mask = 0x800000;
for (int j = 0; j < 24; j++)
{
if ((GRB & mask))
{
rawdata[j].level0 = 1;
rawdata[j].duration0 = 7;
rawdata[j].level1 = 0;
rawdata[j].duration1 = 12 - 7;
}
else
{
rawdata[j].level0 = 1;
rawdata[j].duration0 = 3;
rawdata[j].level1 = 0;
rawdata[j].duration1 = 12 - 3;
}
mask = mask >> 1;
}
}
rmt_symbol_word_t raw_symbols[100];
void app_main(void)
{
int pin = 2;
rmt_channel_handle_t led_chan = NULL;
rmt_tx_channel_config_t tx_chan_config = {
.clk_src = RMT_CLK_SRC_DEFAULT,
.gpio_num = pin,
.mem_block_symbols = 64,
.resolution_hz = 10000000,
.trans_queue_depth = 4,
.flags.with_dma = false,
};
rmt_new_tx_channel(&tx_chan_config, &led_chan);
rmt_copy_encoder_config_t config = {};
rmt_encoder_handle_t encoder = NULL;
rmt_new_copy_encoder(&config, &encoder);
rmt_enable(led_chan);
rmt_transmit_config_t txconf = {
.loop_count = 1};
int color = 0x00FFFF;
convertGRB(color, raw_symbols);
rmt_transmit(led_chan, encoder, raw_symbols, 24 * sizeof(rmt_symbol_word_t), &txconf);
rmt_tx_wait_all_done(led_chan, 10000);
}