/*
* ili9163b_soft.c
*
* Created on: 2020年2月14日
* Author: chen
*/
#include
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/gpio.h"
#include "ili9163b_soft.h"
#include "Font.h"
void ILI9163B_gpio_init(){
gpio_config_t io_conf;
//disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
io_conf.mode = GPIO_MODE_OUTPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
//disable pull-down mode
io_conf.pull_down_en = 0;
//disable pull-up mode
io_conf.pull_up_en = 0;
//configure GPIO with the given settings
gpio_config(&io_conf);
}
void ILI9163B_write_command(uint8_t cmd)
{
uint8_t i;
gpio_set_level(PIN_NUM_DC, 0);
//ILI9163_A0=0; //A0=0发送命令
gpio_set_level(PIN_NUM_CS, 0);
//ILI9163_CS=0;
for(i=0;i<8;i++)
{
gpio_set_level(PIN_NUM_CLK, 0);
//ILI9163_SCK=0;
// //if((addr1&0x80)==0x80) //高位先发送
// ILI9163_SDA=1;
// else
// ILI9163_SDA=0;
//ILI9163_SDA = (bit)(0x80&cmd);
if((0x80&cmd)==0){
gpio_set_level(PIN_NUM_MOSI, 0);
}else{
gpio_set_level(PIN_NUM_MOSI, 1);
}
gpio_set_level(PIN_NUM_CLK, 1);
//ILI9163_SCK = 1;
cmd = (cmd<<1);
}
gpio_set_level(PIN_NUM_CS, 1);
//ILI9163_CS=1;
}
void ILI9163B_write_para8(uint8_t cmd)
{
uint8_t i;
gpio_set_level(PIN_NUM_DC, 1);
//ILI9163_A0=0; //A0=0发送命令
gpio_set_level(PIN_NUM_CS, 0);
//ILI9163_CS=0;
for(i=0;i<8;i++)
{
gpio_set_level(PIN_NUM_CLK, 0);
//ILI9163_SCK=0;
// //if((addr1&0x80)==0x80) //高位先发送
// ILI9163_SDA=1;
// else
// ILI9163_SDA=0;
//ILI9163_SDA = (bit)(0x80&cmd);
if((0x80&cmd)==0){
gpio_set_level(PIN_NUM_MOSI, 0);
}else{
gpio_set_level(PIN_NUM_MOSI, 1);
}
gpio_set_level(PIN_NUM_CLK, 1);
//ILI9163_SCK = 1;
cmd = (cmd<<1);
}
gpio_set_level(PIN_NUM_CS, 1);
//ILI9163_CS=1;
}
void ILI9163B_write_para16(uint16_t dat)
{
uint8_t i,buf;
gpio_set_level(PIN_NUM_DC, 1);
//ILI9163_A0=0; //A0=0发送命令
gpio_set_level(PIN_NUM_CS, 0);
//ILI9163_CS=0;
buf = (uint8_t)(0xFF&(dat>>8));
for(i=0;i<8;i++)
{
gpio_set_level(PIN_NUM_CLK, 0);
//ILI9163_SCK=0;
// //if((addr1&0x80)==0x80) //高位先发送
// ILI9163_SDA=1;
// else
// ILI9163_SDA=0;
//ILI9163_SDA = (bit)(0x80&cmd);
if((0x80&buf)==0){
gpio_set_level(PIN_NUM_MOSI, 0);
}else{
gpio_set_level(PIN_NUM_MOSI, 1);
}
gpio_set_level(PIN_NUM_CLK, 1);
//ILI9163_SCK = 1;
buf = (buf<<1);
}
gpio_set_level(PIN_NUM_CS, 1);
gpio_set_level(PIN_NUM_CS, 0);
buf = (uint8_t)(0xFF&dat);
for(i=0;i<8;i++)
{
gpio_set_level(PIN_NUM_CLK, 0);
//ILI9163_SCK=0;
// //if((addr1&0x80)==0x80) //高位先发送
// ILI9163_SDA=1;
// else
// ILI9163_SDA=0;
//ILI9163_SDA = (bit)(0x80&cmd);
if((0x80&buf)==0){
gpio_set_level(PIN_NUM_MOSI, 0);
}else{
gpio_set_level(PIN_NUM_MOSI, 1);
}
gpio_set_level(PIN_NUM_CLK, 1);
//ILI9163_SCK = 1;
buf = (buf<<1);
}
gpio_set_level(PIN_NUM_CS, 1);
//ILI9163_CS=1;
}
void ILI9163B_rst(void) {
gpio_set_level(PIN_NUM_RST, 0);
vTaskDelay(100 / portTICK_RATE_MS);
gpio_set_level(PIN_NUM_RST, 1);
vTaskDelay(100 / portTICK_RATE_MS);
}
void ILI9163B_init(void)
{
//ILI9163B_init();
ILI9163B_write_command(0x11); //关闭睡眠,振荡器工作
vTaskDelay(100 / portTICK_RATE_MS);
ILI9163B_write_command(0x3a); //每次传送16位数据(VIPF3-0=0101),每个像素16位(IFPF2-0=101)
ILI9163B_write_para8(0x55);
ILI9163B_write_command(0x26);
ILI9163B_write_para8(0x04);
ILI9163B_write_command(0xf2); //Driver Output Control(1)
ILI9163B_write_para8(0x01);
ILI9163B_write_command(0xe0); //Driver Output Control(1)
ILI9163B_write_para8(0x3f);
ILI9163B_write_para8(0x25);
ILI9163B_write_para8(0x1c);
ILI9163B_write_para8(0x1e);
ILI9163B_write_para8(0x20);
ILI9163B_write_para8(0x12);
ILI9163B_write_para8(0x2a);
ILI9163B_write_para8(0x90);
ILI9163B_write_para8(0x24);
ILI9163B_write_para8(0x11);
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00);
ILI9163B_write_command(0xe1); //Driver Output Control(1)
ILI9163B_write_para8(0x20);
ILI9163B_write_para8(0x20);
ILI9163B_write_para8(0x20);
ILI9163B_write_para8(0x20);
ILI9163B_write_para8(0x05);
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x15);
ILI9163B_write_para8(0xa7);
ILI9163B_write_para8(0x3d);
ILI9163B_write_para8(0x18);
ILI9163B_write_para8(0x25);
ILI9163B_write_para8(0x2a);
ILI9163B_write_para8(0x2b);
ILI9163B_write_para8(0x2b);
ILI9163B_write_para8(0x3a);
ILI9163B_write_command(0xb1); //设置屏幕刷新频率
ILI9163B_write_para8(0x08); //DIVA=8
ILI9163B_write_para8(0x08); //VPA =8,约90Hz
ILI9163B_write_command(0xb4); //LCD Driveing control
ILI9163B_write_para8(0x07); //NLA=1,NLB=1,NLC=1
ILI9163B_write_command(0xc0); //LCD Driveing control
ILI9163B_write_para8(0x0a);
ILI9163B_write_para8(0x02);
ILI9163B_write_command(0xc1); //LCD Driveing control
ILI9163B_write_para8(0x02);
ILI9163B_write_command(0xc5); //LCD Driveing control
ILI9163B_write_para8(0x4f);
ILI9163B_write_para8(0x5a);
ILI9163B_write_command(0xc7); //LCD Driveing control
ILI9163B_write_para8(0x40);
ILI9163B_write_command(0x2a); //配置MCU可操作的LCD内部RAM横坐标起始、结束参数
ILI9163B_write_para8(0x00); //横坐标起始地址0x0000
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00); //横坐标结束地址0x007f(127)
ILI9163B_write_para8(0x7f);
ILI9163B_write_command(0x2b); //配置MCU可操作的LCD内部RAM纵坐标起始结束参数
ILI9163B_write_para8(0x00); //纵坐标起始地址0x0000
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00); //纵坐标结束地址0x009f(159)
ILI9163B_write_para8(0x9f);
ILI9163B_write_command(0x36); //配置MPU和DDRAM对应关系
ILI9163B_write_para8(0xc0); //MX=1,MY=1
ILI9163B_write_command(0xb7); //LCD Driveing control
ILI9163B_write_para8(0x00); //CRL=0
ILI9163B_write_command(0x29); //开启屏幕显示
ILI9163B_write_command(0x29); //设置为LCD接收数据/命令模式
vTaskDelay(100 / portTICK_RATE_MS);
}
void ILI9163B_SetPos(uint8_t xs,uint8_t ys,uint8_t xe,uint8_t ye)
{
ILI9163B_write_command(0x2A); //Colulm addRSTs set
ILI9163B_write_para8(0x00); //行起始坐标高8位,始终为零
ILI9163B_write_para8(xs); //行起始坐标低8位
ILI9163B_write_para8(0x00); //行终止坐标高8位,始终为零
ILI9163B_write_para8(xe); //行终止坐标低8位
ILI9163B_write_command(0x2B); //Colulm addRSTs set
ILI9163B_write_para8(0x00); //列起始坐标高8位,始终为零
ILI9163B_write_para8(ys); //列起始坐标低8位
ILI9163B_write_para8(0x00); //列终止坐标高8位,始终为零
ILI9163B_write_para8(ye); //列终止坐标低8位
ILI9163B_write_command(0x2C); //GRAM接收MCU数据或命令
}
void ILI9163B_address_rst(void)
{
ILI9163B_write_command(0x2a); //配置MCU可操作的LCD内部RAM横坐标起始、结束参数
ILI9163B_write_para8(0x00); //横坐标起始地址0x0000
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00); //横坐标结束地址0x007f(127)
ILI9163B_write_para8(0x7f);
ILI9163B_write_command(0x2b); //配置MCU可操作的LCD内部RAM纵坐标起始结束参数
ILI9163B_write_para8(0x00); //纵坐标起始地址0x0000
ILI9163B_write_para8(0x00);
ILI9163B_write_para8(0x00); //纵坐标结束地址0x009f(159)
ILI9163B_write_para8(0x9f);
ILI9163B_write_command(0x2C); //GRAM接收MCU数据或命令
}
void ILI9163B_display_full()
{
unsigned int i,j;
ILI9163B_address_rst();
for(i=0;i<160;i++)
{
for(j=0;j<128;j++)
{
ILI9163B_write_para16(WHITE);
}
}
}
void Gui_DrawPoint(uint8_t x,uint8_t y,uint16_t Data)
{
ILI9163B_SetPos(x,y,x+1,y+1);
ILI9163B_write_para16(Data);
}
void Gui_DrawFont_Num32(uint8_t x, uint8_t y, uint16_t fc, uint16_t bc, uint16_t num)
{
unsigned char i,j,k,c;
//lcd_text_any(x+94+i*42,y+34,32,32,0x7E8,0x0,sz32,knum[i]);
// w=w/8;
for(i=0;i<32;i++)
{
for(j=0;j<4;j++)
{
c=*(sz32+num*32*4+i*4+j);
for (k=0;k<8;k++)
{
if(c&(0x80>>k)) Gui_DrawPoint(x+j*8+k,y+i,fc);
else {
if (fc!=bc) Gui_DrawPoint(x+j*8+k,y+i,bc);
}
}
}
}
}
void Gui_DrawFont_GBK24(uint8_t x, uint8_t y, uint16_t fc, uint16_t bc, char *s)
{
unsigned char i,j;
unsigned short k;
while(*s)
{
if( *s < 0x80 )
{
k=*s;
if (k>32) k-=32; else k=0;
for(i=0;i<16;i++)
for(j=0;j<8;j++)
{
if(asc16[k*16+i]&(0x80>>j))
Gui_DrawPoint(x+j,y+i,fc);
else
{
if (fc!=bc) Gui_DrawPoint(x+j,y+i,bc);
}
}
s++;x+=8;
}
}
}
void ILI9163B_driver_init(){
ILI9163B_gpio_init();
ILI9163B_rst() ;
ILI9163B_init();
ILI9163B_display_full();
}
/*
* util.c
*
* Created on: 2020年1月4日
* Author: jia
*/
#include "util.h"
#include
#include
#include
/* seconds per day */
#define SPD 24*60*60
/* days per month -- nonleap! */
static const short __spm[13] =
{ 0,
(31),
(31+28),
(31+28+31),
(31+28+31+30),
(31+28+31+30+31),
(31+28+31+30+31+30),
(31+28+31+30+31+30+31),
(31+28+31+30+31+30+31+31),
(31+28+31+30+31+30+31+31+30),
(31+28+31+30+31+30+31+31+30+31),
(31+28+31+30+31+30+31+31+30+31+30),
(31+28+31+30+31+30+31+31+30+31+30+31),
};
int __isleap (int year) {
/* every fourth year is a leap year except for century years that are
* not divisible by 400. */
/* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
return (!(year % 4) && ((year % 100) || !(year % 400)));
}
void rtctime_gmtime ( int32_t stamp, struct rtc_tm *r)
{
int32_t i,week;
stamp=stamp+TIME_ZONE*3600;
week=((stamp-16*3600)%(SPD)+4)%7;
int32_t work = stamp % (SPD);
r->tm_sec = work % 60; work /= 60;
r->tm_min = work % 60; r->tm_hour = work / 60;
work = stamp / (SPD);
r->tm_wday = (4 + work) % 7;
if(r->tm_wday==0){
r->tm_wday=7;
}
for (i = 1970; ; ++i) {
int32_t k = __isleap (i) ? 366 : 365;
if (work >= k) {
work -= k;
} else {
break;
}
}
r->tm_year = i;
r->tm_yday = work;
//r->tm_wday=week;
r->tm_mday = 1;
if (__isleap (i) && (work > 58)) {
if (work == 59) r->tm_mday = 2; /* 29.2. */
work -= 1;
}
for (i = 11; i && (__spm[i] > work); --i) ;
r->tm_mon = i;
r->tm_mday += work - __spm[i];
}
// converts to Julian day number (less the half day offset)
// Caller must ensure valid input
uint32_t jd(int y, int m, int d)
{
return (1461 * (y+4800+(m-14)/12))/4 +(367 * (m-2-12 * ((m-14)/12)))/12 - (3 * ((y + 4900 + (m - 14)/12)/100))/4 + d-32075;
}
// converts a day, month, year and time to a UNIX time-stamp.
// This will overflow in 2038. BSD in 32- and 64-bit machine are now using
// 64-bit integer to avoid the overflow. Linux on 64-bit also returns 64-bit integer
// Caller must ensure valid input
uint32_t unix_time_stamp(int y, int m, int d, int h, int mi, int s)
{
uint32_t j;
j = jd(y, m, d)-jd(1970,1,1);
j = j*24*60*60;
j += h*60*60 + mi*60 + s;
return j+TIME_ZONE*60*60;
}
#include "HX711.h"
#include "esp_log.h"
#include
#define HIGH 1
#define LOW 0
#define CLOCK_DELAY_US 20
#define DEBUGTAG "HX711"
static gpio_num_t GPIO_PD_SCK = GPIO_NUM_0;//D3 // Power Down and Serial Clock Input Pin
static gpio_num_t GPIO_DOUT = GPIO_NUM_16; //D0 // Serial Data Output Pin
static HX711_GAIN GAIN = eGAIN_128; // amplification factor
static unsigned long OFFSET = 0; // used for tare weight
static float SCALE = 1; // used to return weight in grams, kg, ounces, whatever
void HX711_init(gpio_num_t dout, gpio_num_t pd_sck, HX711_GAIN gain )
{
GPIO_PD_SCK = pd_sck;
GPIO_DOUT = dout;
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1UL< OFFSET)
return avg - OFFSET;
else
return 0;
}
float HX711_get_units(char times)
{
return HX711_get_value(times) / SCALE;
}
void HX711_tare( )
{
unsigned long sum = 0;
sum = HX711_read_average(20);
HX711_set_offset(sum);
}
void HX711_set_scale(float scale )
{
SCALE = scale;
}
float HX711_get_scale()
{
return SCALE;
}
void HX711_set_offset(unsigned long offset)
{
OFFSET = offset;
}
unsigned long HX711_get_offset(unsigned long offset)
{
return OFFSET;
}
void HX711_power_down()
{
gpio_set_level(GPIO_PD_SCK, LOW);
ets_delay_us(CLOCK_DELAY_US);
gpio_set_level(GPIO_PD_SCK, HIGH);
ets_delay_us(CLOCK_DELAY_US);
}
void HX711_power_up()
{
gpio_set_level(GPIO_PD_SCK, LOW);
}
#include
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "ds1307.h"
#define CHECK_ARG(ARG) do { if (!ARG) return ESP_ERR_INVALID_ARG; } while (0)
uint8_t bcd2dec(uint8_t val)
{
return (val >> 4) * 10 + (val & 0x0f);
}
uint8_t dec2bcd(uint8_t val)
{
return ((val / 10) << 4) + (val % 10);
}
esp_err_t update_register(i2c_port_t i2c_num, uint8_t reg, uint8_t mask, uint8_t val)
{
//CHECK_ARG(i2c_num);
uint8_t old;
i2c_read(i2c_num,DS1307_ADDR,reg, &old, 1);
//i2c_dev_read_reg(dev, reg, &old, 1);
uint8_t buf = (old & mask) | val;
//esp_err_t res = i2c_dev_write_reg(dev, reg, &buf, 1);
esp_err_t res = i2c_write(i2c_num, DS1307_ADDR,reg, &buf, 1);
return res;
}
esp_err_t ds1307_init_desc( i2c_port_t i2c_num, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
{
int i2c_master_port = i2c_num;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = sda_gpio;
conf.sda_pullup_en = 1;
conf.scl_io_num = scl_gpio;
conf.scl_pullup_en = 1;
conf.clk_stretch_tick = 300; // 300 ticks, Clock stretch is about 210us, you can make changes according to the actual situation.
ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode));
ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
return ESP_OK;
}
esp_err_t ds1307_start(i2c_port_t i2c_num, bool start)
{
return update_register(i2c_num, TIME_REG, CH_MASK, start ? 0 : CH_BIT);
}
esp_err_t ds1307_is_running(i2c_port_t i2c_num, bool *running)
{
//CHECK_ARG(dev);
CHECK_ARG(running);
uint8_t val;
//i2c_dev_read_reg(dev, TIME_REG, &val, 1);
i2c_read(i2c_num,DS1307_ADDR,TIME_REG, &val, 1);
*running = val & CH_BIT;
return ESP_OK;
}
esp_err_t ds1307_get_time(i2c_port_t i2c_num, struct tm *time)
{
//CHECK_ARG(dev);
CHECK_ARG(time);
uint8_t buf[7];
//i2c_dev_read_reg(dev, TIME_REG, buf, 7);
i2c_read(i2c_num,DS1307_ADDR,TIME_REG, &buf, 7);
time->tm_sec = bcd2dec(buf[0] & SECONDS_MASK);
time->tm_min = bcd2dec(buf[1]);
if (buf[2] & HOUR12_BIT)
{
// RTC in 12-hour mode
time->tm_hour = bcd2dec(buf[2] & HOUR12_MASK) - 1;
if (buf[2] & PM_BIT)
time->tm_hour += 12;
}
else
time->tm_hour = bcd2dec(buf[2] & HOUR24_MASK);
time->tm_wday = bcd2dec(buf[3]) ;
time->tm_mday = bcd2dec(buf[4]);
time->tm_mon = bcd2dec(buf[5]) ;
time->tm_year = bcd2dec(buf[6])+2002;
return ESP_OK;
}
esp_err_t ds1307_set_time(i2c_port_t i2c_num, const struct tm *time)
{
//CHECK_ARG(dev);
CHECK_ARG(time);
uint8_t buf[7] = {
dec2bcd(time->tm_sec),
dec2bcd(time->tm_min),
dec2bcd(time->tm_hour),
dec2bcd(time->tm_wday ),
dec2bcd(time->tm_mday),
dec2bcd(time->tm_mon ),
dec2bcd(time->tm_year -2002)
};
//i2c_dev_write_reg(dev, TIME_REG, buf, sizeof(buf));
i2c_write(I2C_NUM_0, DS1307_ADDR,TIME_REG, &buf , sizeof(buf));
return ESP_OK;
}
/*
* e2prom.c
*
* Created on: 2020年1月4日
* Author: jia
*/
/**
* map init 30x8 bit = 240 date
* data start at 0x001E
* startup counter 2byte 0-0xFFFF
*
*
*/
#include"e2prom.h"
static const char *TAG = "E2PROM_DRIVE";
int16_t char2int(char *dchar){
return ((int16_t)((dchar[0] << 8) | dchar[1]) );
}
void int2char(int dint,char *dchar){
dchar[0]=dint/255;dchar[1]=dint%255;
}
int get_init_pointer(){
char reg[2];char data[2];
uint counter;
int2char(30,®);
i2c_read_x_reg(I2C_EXAMPLE_MASTER_NUM, AT24C32_ADDR, ®, 2, &data, 2);
counter=char2int(&data);
if(counter==0xffff){
counter=0;
}
counter++;
ESP_LOGI(TAG, "reg :%d %d date : %d %d",reg[0],reg[1],data[0],data[1]);
int2char(counter,&data);
//data[0]=0;data[1]=1;
i2c_write_x_reg(I2C_EXAMPLE_MASTER_NUM, AT24C32_ADDR, ®, 2, &data, 2);
return counter;
}