ESP32的IDF开发学习-驱动ov2640并显示在屏幕上
关键词:ov2640 ESP32S3 LVGL esp32-camera
前言
买的板子还送了一个0v2640的摄像头,今天尝试驱动这个摄像头,之前已经实现了屏幕的驱动了,现在实现一下驱动摄像头显示在屏幕上
下载组件
打开组件注册表,搜索camera组件,点击下载后编译
学习示例
/**
* This example takes a picture every 5s and print its size on serial monitor.
*/
// =============================== SETUP ======================================
// 1. Board setup (Uncomment):
// #define BOARD_WROVER_KIT
// #define BOARD_ESP32CAM_AITHINKER
// #define BOARD_ESP32S3_WROOM
/**
* 2. Kconfig setup
*
* If you have a Kconfig file, copy the content from
* https://github.com/espressif/esp32-camera/blob/master/Kconfig into it.
* In case you haven't, copy and paste this Kconfig file inside the src directory.
* This Kconfig file has definitions that allows more control over the camera and
* how it will be initialized.
*/
/**
* 3. Enable PSRAM on sdkconfig:
*
* CONFIG_ESP32_SPIRAM_SUPPORT=y
*
* More info on
* https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html#config-esp32-spiram-support
*/
// ================================ CODE ======================================
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// support IDF 5.x
#ifndef portTICK_RATE_MS
#define portTICK_RATE_MS portTICK_PERIOD_MS
#endif
#include "esp_camera.h"
#define BOARD_WROVER_KIT 1
// WROVER-KIT PIN Map
#ifdef BOARD_WROVER_KIT
#define CAM_PIN_PWDN -1 //power down is not used
#define CAM_PIN_RESET -1 //software reset will be performed
#define CAM_PIN_XCLK 21
#define CAM_PIN_SIOD 26
#define CAM_PIN_SIOC 27
#define CAM_PIN_D7 35
#define CAM_PIN_D6 34
#define CAM_PIN_D5 39
#define CAM_PIN_D4 36
#define CAM_PIN_D3 19
#define CAM_PIN_D2 18
#define CAM_PIN_D1 5
#define CAM_PIN_D0 4
#define CAM_PIN_VSYNC 25
#define CAM_PIN_HREF 23
#define CAM_PIN_PCLK 22
#endif
// ESP32Cam (AiThinker) PIN Map
#ifdef BOARD_ESP32CAM_AITHINKER
#define CAM_PIN_PWDN 32
#define CAM_PIN_RESET -1 //software reset will be performed
#define CAM_PIN_XCLK 0
#define CAM_PIN_SIOD 26
#define CAM_PIN_SIOC 27
#define CAM_PIN_D7 35
#define CAM_PIN_D6 34
#define CAM_PIN_D5 39
#define CAM_PIN_D4 36
#define CAM_PIN_D3 21
#define CAM_PIN_D2 19
#define CAM_PIN_D1 18
#define CAM_PIN_D0 5
#define CAM_PIN_VSYNC 25
#define CAM_PIN_HREF 23
#define CAM_PIN_PCLK 22
#endif
// ESP32S3 (WROOM) PIN Map
#ifdef BOARD_ESP32S3_WROOM
#define CAM_PIN_PWDN 38
#define CAM_PIN_RESET -1 //software reset will be performed
#define CAM_PIN_VSYNC 6
#define CAM_PIN_HREF 7
#define CAM_PIN_PCLK 13
#define CAM_PIN_XCLK 15
#define CAM_PIN_SIOD 4
#define CAM_PIN_SIOC 5
#define CAM_PIN_D0 11
#define CAM_PIN_D1 9
#define CAM_PIN_D2 8
#define CAM_PIN_D3 10
#define CAM_PIN_D4 12
#define CAM_PIN_D5 18
#define CAM_PIN_D6 17
#define CAM_PIN_D7 16
#endif
static const char *TAG = "example:take_picture";
#if ESP_CAMERA_SUPPORTED
static camera_config_t camera_config = {
.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
.pin_xclk = CAM_PIN_XCLK,
.pin_sccb_sda = CAM_PIN_SIOD,
.pin_sccb_scl = CAM_PIN_SIOC,
.pin_d7 = CAM_PIN_D7,
.pin_d6 = CAM_PIN_D6,
.pin_d5 = CAM_PIN_D5,
.pin_d4 = CAM_PIN_D4,
.pin_d3 = CAM_PIN_D3,
.pin_d2 = CAM_PIN_D2,
.pin_d1 = CAM_PIN_D1,
.pin_d0 = CAM_PIN_D0,
.pin_vsync = CAM_PIN_VSYNC,
.pin_href = CAM_PIN_HREF,
.pin_pclk = CAM_PIN_PCLK,
//XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_RGB565, //YUV422,GRAYSCALE,RGB565,JPEG
.frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates.
.jpeg_quality = 12, //0-63, for OV series camera sensors, lower number means higher quality
.fb_count = 1, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode.
.fb_location = CAMERA_FB_IN_PSRAM,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
};
static esp_err_t init_camera(void)
{
//initialize the camera
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Camera Init Failed");
return err;
}
return ESP_OK;
}
#endif
void app_main(void)
{
#if ESP_CAMERA_SUPPORTED
if(ESP_OK != init_camera()) {
return;
}
while (1)
{
ESP_LOGI(TAG, "Taking picture...");
camera_fb_t *pic = esp_camera_fb_get();
// use pic->buf to access the image
ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
esp_camera_fb_return(pic);
vTaskDelay(5000 / portTICK_RATE_MS);
}
#else
ESP_LOGE(TAG, "Camera support is not available for this chip");
return;
#endif
}
挺短的,短短188行代码,首先配置不同的开发版,根据开发板配置不同的引脚,同时需要打开PSRAM的支持,提高相机的性能
初始化阶段需要配置相机的参数,初始化后进入循环,每5秒钟获取一次图片的参数,并将图片大小打印出来,其后释放图片
下面是一下相关参数的配置
.xclk_freq_hz
这个参数是设置摄像头的时钟频率,默认20MHz,提高频率可以提高帧率
.pixel_format
配置输出的图像格式
PIXFORMAT_JPEG
: 压缩的 JPEG 格式(体积小,适合存储/传输)。
PIXFORMAT_RGB565
: 未压缩的 16 位颜色格式(适合实时处理,如显示屏输出)。PIXFORMAT_YUV422
: YUV 颜色空间格式(某些图像处理算法需要)。
.frame_size
这个是配置输出的分辨率大小
FRAMESIZE_QQVGA
(160x120)
FRAMESIZE_QVGA
(320x240)
FRAMESIZE_SVGA
(800x600)
FRAMESIZE_UXGA
(1600x1200)
.jpeg_quality
这个参数是配置压缩质量,0为最高,63是最低
.fb_count
帧缓冲函数
fb_count = 1
: 单缓冲区模式(捕获一帧后必须手动释放)。
fb_count > 1
: 双缓冲或多缓冲模式(驱动程序自动处理连续捕获)
.fb_location
帧缓冲区内存分配位置
CAMERA_FB_IN_PSRAM
: 优先使用 PSRAM(需硬件支持,适用于大分辨率)。
CAMERA_FB_IN_DRAM
: 使用片上 RAM(仅适用于小分辨率)。
.grab_mode
图像捕获触发模式
CAMERA_GRAB_WHEN_EMPTY
: 当帧缓冲区空闲时自动捕获新帧(稳定,避免数据冲突)。CAMERA_GRAB_LATEST
: 始终保留最新一帧(适合实时性要求高的场景,但可能丢帧)
由于我用的是ESP32-RAM,查看原理图
修改板子引脚,编译程序,下载
能够顺利运行。
在lvgl上显示图片
我们都知道,在lvgl上显示的图片,都是将图片转化为c数组,而数组有一个描述符号
我们在显示图像的时候,也需要给图片创建一个描述符,让LVGL能够识别
void displayPicture() {
camera_fb_t *pic = esp_camera_fb_get();
if (!pic || pic->format != PIXFORMAT_RGB565) {
if (pic) {
esp_camera_fb_return(pic);
}
return;
}
// 动态创建LVGL图像描述符
img_dsc.header.always_zero = 0;
img_dsc.header.w = 320; // 使用实际宽度
img_dsc.header.h = 240; // 使用实际高度
img_dsc.header.cf = LV_IMG_CF_TRUE_COLOR; // 颜色格式为RGB565
img_dsc.data = pic->buf;
img_dsc.data_size = 76800 * 2; // 数据大小,注意这里不能写错了
ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
if(img == NULL) {
img = lv_img_create(scr);
}
lv_img_set_src(img, &img_dsc);
lv_obj_set_height(img, 320);
lv_obj_set_width(img, 240);
// 释放帧缓冲区
esp_camera_fb_return(pic);
}
编译,然后下载
就是这个画质很尴尬,不过240*240还要什么自行车
这里有个问题,在调用这个函数的时候,其实按理应该不能立刻释放这个指针,会造成lvgl崩坏的,原因是lvgl还在使用就不能释放,所以这里需要大家谨慎处理,根据项目需求实现目标
在LVGL上显示视频
既然显示图片可行,那显示视频应该也是可以的,因为视频就是一张张图片在以一定的间隔播放,所以下面修改函数实现
首先我们把单缓冲区改为双缓冲区,剩下的函数不需要修改,只需要加while循环在freertos中调用即可
void displayVideo() {
while(1)
{
camera_fb_t *pic = esp_camera_fb_get();
if (!pic || pic->format != PIXFORMAT_RGB565) {
if (pic) {
esp_camera_fb_return(pic);
}
return;
}
// 动态创建LVGL图像描述符
img_dsc.header.always_zero = 0;
img_dsc.header.w = 320; // 使用实际宽度
img_dsc.header.h = 240; // 使用实际高度
img_dsc.header.cf = LV_IMG_CF_TRUE_COLOR; // 颜色格式为RGB565
img_dsc.data = pic->buf;
img_dsc.data_size = 76800 * 2; // 数据大小,注意这里不能写错了
ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
if(img == NULL) {
img = lv_img_create(scr);
}
lv_img_set_src(img, &img_dsc);
lv_obj_set_height(img, 320);
lv_obj_set_width(img, 240);
// 释放帧缓冲区
vTaskDelay(5 / portTICK_PERIOD_MS);
esp_camera_fb_return(pic);
}
}
编译下载,查看结果
这个帧率真的难蚌,而且屏幕出现了花屏,这可能和数据不完整有关
总结
总的来说,驱动摄像头还是很简单的,下一节我将尝试将摄像头的画面通过TCP传输到电脑