Bài 3 - Blink LED
Chương trình của chúng ta hoạt động như sau: LED sẽ sáng 500ms sau đó tắt
500ms, cứ thế lặp lại.
Sơ đồ mạch như sau, LED sẽ nối với GPIO12:
Để thực hiện việc bật tắt LED 500ms, chúng ta sẽ dùng software timer đếm hết
500ms sẽ gọi hàm thực hiện việc nháy LED.
Trình tự cài đặt software timer như sau:
- Gọi os_timer_disarm trước
để dừng các timer.
- Cài đặt hàm được gọi khi timer đếm xong bằng os_timer_setfn.
- Enable timer bằng os_timer_arm.
Trong chương trình nháy LED 500ms của chúng ta, trình tự như sau:
//Stop
led_timer
os_timer_disarm(&led_timer);
//cài
đặt hàm được gọi khi led_timer đếm xong
os_timer_setfn(&led_timer,
(os_timer_func_t *)led_timer_blink, NULL);
//kích
hoạt led_timer với 500ms, true có nghĩa là lặp
đi lặp lại timer 500ms này
os_timer_arm(&led_timer, 500, true);
Hàm được gọi khi timer đếm hết
500ms như sau:
void ICACHE_FLASH_ATTR //hàm có ICACHE_FLASH_ATTR phía trước là
hàm chứa trong FLASH
led_timer_blink(void)
{
led_state
= !led_state;// đổi
trạng thái LED
GPIO_OUTPUT_SET(LED_PIN,
led_state);//xuất
ra LED
}
BIÊN DỊCH
VÀ NẠP VÀO ESP8266
Copy
thư mục HelloWorld, đổi tên thành 3-blinkLED sau đó mở Eclipse import vào (xem hướng dẫn
import ở bài
trước).
Chỉnh sửa file user_main.c trong thư mục 3-blinkLED/app/user có nội dung toàn bộ như sau:
#include "osapi.h"
#include "user_interface.h"
#include "gpio.h"
#include "ets_sys.h"
#define LED_PIN 12
static bool led_state = false;
static os_timer_t led_timer;
void ICACHE_FLASH_ATTR //hàm có ICACHE_FLASH_ATTR phía trước là
hàm chứa trong FLASH
led_timer_blink(void)
{
led_state
= !led_state;// đổi
trạng thái LED
GPIO_OUTPUT_SET(LED_PIN,
led_state);//xuất
ra LED
}
void ICACHE_FLASH_ATTR
user_init(void)
{
uart_init(115200,115200);
os_printf("\n=======================================\n");
os_printf("\t\t SDK version: %s \n", system_get_sdk_version());
os_printf("\n=======================================\n");
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); //khai báo chức năng
GPIO12 cho chân 12
GPIO_OUTPUT_SET(LED_PIN, 0); //set output cho GPIO12
os_timer_disarm(&led_timer); //Stop led_timer
os_timer_setfn(&led_timer, (os_timer_func_t *)led_timer_blink, NULL);//cài đặt hàm được gọi khi led_timer đếm xong
os_timer_arm(&led_timer, 500, true);//kích hoạt led_timer với 500ms, true có nghĩa là lặp đi lặp lại timer 500ms này
}
/******************************************************************************
* FunctionName
: user_rf_cal_sector_set
*
Description : SDK just reversed 4
sectors, used for rf init data and paramters.
* We add this function to force
users to set rf cal sector, since
* we don't know which sector is
free in user's application.
* sector map for last several
sectors : ABCCC
* A : rf cal
* B : rf init data
* C : sdk parameters
*
Parameters : none
* Returns : rf cal sector
*******************************************************************************/
uint32
ICACHE_FLASH_ATTR
user_rf_cal_sector_set(void)
{
enum flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 - 5;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 - 5;
break;
case FLASH_SIZE_16M_MAP_512_512:
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 - 5;
break;
case FLASH_SIZE_32M_MAP_512_512:
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec
= 1024 - 5;
break;
case FLASH_SIZE_64M_MAP_1024_1024:
rf_cal_sec = 2048 - 5;
break;
case FLASH_SIZE_128M_MAP_1024_1024:
rf_cal_sec = 4096 - 5;
break;
default:
rf_cal_sec = 0;
break;
}
return rf_cal_sec;
}
void ICACHE_FLASH_ATTR
user_rf_pre_init(void)
{
}
Biên dịch vào nạp vào ESP8266,
xem bài
trước có hướng dẫn nạp.
Nhận xét
Đăng nhận xét