Page 3 of 16
Page 56
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
while (1)
{
gpio_set_level(2, 0);
gpio_set_level(2, 1);
}
}
Page 57
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
volatile int waste = 0;
while (1) {
gpio_set_level(2, 0);
waste++;
gpio_set_level(2, 1);
}
}
Page 59
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.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);
while (1)
{
gpio_set_level(2, 0);
delay_us(1);
gpio_set_level(2, 1);
delay_us(1);
}
}
Page 59
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
volatile int i=0;
int n=1;
while (1) {
gpio_set_level(2, 0);
for(i=0;i<n;i++){}
gpio_set_level(2, 1);
for(i=0;i<n;i++){}
}
}
Page 62
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
#include <sys/time.h>
int64_t tick_us(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec + offset;
}
void delay_until(int64_t t)
{
static struct timeval tv_now;
do
{
gettimeofday(&tv_now, NULL);
} while (t > (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec);
}
void app_main(void)
{
int pin = 2;
gpio_reset_pin(pin);
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
volatile int i = 0;
int n = 100;
int64_t t = 0;
int64_t width = 1000;
while (1)
{
t = tick_us(width);
gpio_set_level(pin, 1);
for (i = 0; i < n; i++)
{
}
delay_until(t);
t = tick_us(width);
gpio_set_level(pin, 0);
for (i = 0; i < n; i++)
{
}
delay_until(t);
}
}
Page 64
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_OUTPUT);
while (1) {
gpio_set_level(2, 1);
gpio_set_level(4, 0);
gpio_set_level(2, 0);
gpio_set_level(4, 1);
}
}
Page 66
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "esp_rom_sys.h"
#include "soc/gpio_reg.h"
void gpio_set(int32_t value, int32_t mask)
{
int32_t *OutAdd = (int32_t *)GPIO_OUT_REG;
*OutAdd = (*OutAdd & ~mask) | (value & mask);
}
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_OUTPUT);
int32_t mask = 1 << 2 | 1 << 4;
int32_t value1 = 0 << 2 | 1 << 4;
int32_t value2 = 1 << 2 | 0 << 4;
while (1)
{
gpio_set(value1, mask);
gpio_set(value2, mask);
}
}