voidrtc_init(void){/* RTC Reset */RTC_Init();RTC_Reset();RTC_Init();/* RTC Enable Init */RTC_EnableInit();RTC_ConfigDateStructInit(&DateStruct);/* RTC Disable Init */RTC_DisableInit();}
voidrtc_read_time(unsignedchar*hours,unsignedchar*minutes,unsignedchar*seconds){/* Read time */RTC_ReadTime(RTC_FORMAT_BIN, &TimeStruct);RTC_Delay();*hours = TimeStruct.hours;*minutes = TimeStruct.minutes;*seconds = TimeStruct.seconds;}voidrtc_read_data(unsignedchar*year,unsignedchar*month,unsignedchar*day,unsignedchar*weekday){/* Read Date */RTC_ReadDate(RTC_FORMAT_BIN, &DateStruct);RTC_Delay();*year = DateStruct.year;*month = DateStruct.month;*day = DateStruct.date;*weekday = DateStruct.weekday;}
为了设置时间和日期,需要相关的写入函数:
voidrtc_write_time(unsignedcharhour,unsignedcharminute,unsignedcharsecond){TimeStruct.H12 =12;TimeStruct.hours = hour;TimeStruct.minutes = minute;TimeStruct.seconds = second;RTC_ConfigTime(RTC_FORMAT_BIN, &TimeStruct);RTC_Delay();}voidrtc_write_date(unsignedcharyear,unsignedcharmonth,unsignedcharday,unsignedcharweekday){DateStruct.year = year;DateStruct.month = month;DateStruct.date = day;DateStruct.weekday = weekday;RTC_ConfigDate(RTC_FORMAT_BIN, &DateStruct);RTC_Delay();}
SCK - PB3 时钟
CS - PB4 片选
DC - PC12 数据/命令控制
RST - PB8 复位
BUSY - PB9 繁忙检测
voidepaper_gpio_write_cs(unsignedcharlevel){if(level)GPIO_SetBit(EPAPER_CS_GPIO, EPAPER_CS_PIN);elseGPIO_ClearBit(EPAPER_CS_GPIO, EPAPER_CS_PIN);}voidepaper_gpio_write_rst(unsignedcharlevel){if(level)GPIO_SetBit(EPAPER_RST_GPIO, EPAPER_RST_PIN);elseGPIO_ClearBit(EPAPER_RST_GPIO, EPAPER_RST_PIN);}voidepaper_gpio_write_dc(unsignedcharlevel){if(level)GPIO_SetBit(EPAPER_DC_GPIO, EPAPER_DC_PIN);elseGPIO_ClearBit(EPAPER_DC_GPIO, EPAPER_DC_PIN);}voidepaper_gpio_write_mosi(unsignedcharlevel){if(level)GPIO_SetBit(EPAPER_MOSI_GPIO, EPAPER_MOSI_PIN);elseGPIO_ClearBit(EPAPER_MOSI_GPIO, EPAPER_MOSI_PIN);}voidepaper_gpio_write_sck(unsignedcharlevel){if(level)GPIO_SetBit(EPAPER_SCK_GPIO, EPAPER_SCK_PIN);elseGPIO_ClearBit(EPAPER_SCK_GPIO, EPAPER_SCK_PIN);}unsignedcharepaper_gpio_busy_read(){if(GPIO_ReadInputBit(EPAPER_BUSY_GPIO, EPAPER_BUSY_PIN) == BIT_RESET)return0;elsereturn1;}//SPI写字节voidepaper_spi_wrtie(unsignedcharvalue){unsignedchari;__disable_irq();EPAPER_SPI_DELAY;for(i =0; i <8; i++){epaper_gpio_write_sck(0);EPAPER_SPI_DELAY;if(value &0x80)epaper_gpio_write_mosi(1);elseepaper_gpio_write_mosi(0);value = (value <<1);EPAPER_SPI_DELAY;EPAPER_SPI_DELAY1;epaper_gpio_write_sck(1);EPAPER_SPI_DELAY;}__enable_irq();}//写命令voidepaper_write_cmd(unsignedcharcommand){EPAPER_SPI_DELAY;epaper_gpio_write_cs(0);epaper_gpio_write_dc(0);// command writeepaper_spi_wrtie(command);epaper_gpio_write_cs(1);}//写数据voidepaper_write_data(unsignedchardata){EPAPER_SPI_DELAY;epaper_gpio_write_cs(0);epaper_gpio_write_dc(1);// command writeepaper_spi_wrtie(data);epaper_gpio_write_cs(1);}////等待电子纸空闲,超时后会退出unsignedcharepaper_wait_busy(void){unsignedinti =400;while(i--){if(epaper_is_busy() ==0)return0;//空闲退出epaper_delay_xms(10);}return-1;//超时退出}
在每一行数据写入前设置起点epaper_driver_set_cursor(0, y),然后一次写入整行数据,重复多行,完成整幅图片的写入。
//全填充 刷整个屏幕voidepaper_driver_fill(unsignedcharbuffer[]){unsignedshortx, y;unsignedinti =0;epaper_driver_set_window(0,0, EPAPER_WIDTH_PIXEL, EPAPER_HEIGHT_PIXEL);for(y =0; y < EPAPER_HEIGHT_PIXEL; y++){epaper_driver_set_cursor(0, y);epaper_write_cmd(0x24);for(x =0; x < EPAPER_WIDTH_BYTES; x++){epaper_write_data(buffer[i++]);}}epaper_driver_refresh();}
//刷新显示voidepaper_driver_refresh(void){epaper_write_cmd(0x22);// DISPLAY_UPDATE_CONTROL_2epaper_write_data(0xC4);epaper_write_cmd(0X20);// MASTER_ACTIVATIONepaper_write_cmd(0xFF);// TERMINATE_FRAME_READ_WRITEepaper_wait_busy();}
涉及的驱动代码很多,更详细的代码可以参考上面链接中的微雪示例代码。
- 局部刷新的方法
//更新LUT, 设置全刷或局刷voidepaper_set_lut_table(unsignedcharmode){epaper_write_cmd(0x32);unsignedshorti;if(mode == EPAPER_MODE_FULL)//全刷{for(i =0; i <30; i++){epaper_write_data(epaper_lut_full_update[i]);}}elseif(mode == EPAPER_MODE_PART)//局刷{for(i =0; i <30; i++)epaper_write_data(epaper_lut_partial_update[i]);}else;}
- 自定义字体的制作
typedefstruct{unsignedcharfirst;//GB2312编码unsignedcharsecond;//GB2312编码unsignedintindex;//在字库文件中的索引} FontCode;
把所有要用的字的映射关系存放进该结构体数组中:
FontCode DZ_simkai24_code[]={{0x20,0x00,0},//" "0200{0xc4,0xea,1},//"年"c4ea{0xd4,0xc2,2},//"月"d4c2{0xc8,0xd5,3},//"日"c8d5{0xd2,0xbb,4},//"一"d2bb{0xb6,0xfe,5},//"二"b6fe{0xc8,0xfd,6},//"三"c8fd{0xcb,0xc4,7},//"四"cbc4{0xce,0xe5,8},//"五"cee5{0xc1,0xf9,9},//"六"c1f9{0xcc,0xec,10},//"天"ccec{0,0}};
//搜索汉字在数组中的索引staticinlineunsignedintepaper_font_search_gb2312(unsignedcharcode[2]){unsignedinti =0;while(curFont.code[i].first >0){if((curFont.code[i].first == code[0]) && (curFont.code[i].second == code[1])){returni;}i++;}return0;}//绘制文字voidepaper_draw_text(unsignedshortx0,unsignedshorty0,char*text){unsignedshortx;unsignedintindex;unsignedchar*ptr;unsignedshortfirst;x = x0;while(*text !=0){if(*text <0x7F)//小于127(0x7F)是ASCII{index = epaper_font_search_ascii(*text);ptr = &curFont.data[index * curFont.size];epaper_clear_windows(x, y0, x + curFont.width, y0 + curFont.height);if(curPage.area_refresh)epaper_draw_icon_area(x, y0, curFont.width, curFont.height, ptr);elseepaper_draw_icon(x, y0, curFont.width, curFont.height, ptr);x += (curFont.width);text++;;}else{index = epaper_font_search_gb2312(text);ptr = &curFont.data[index * curFont.size];epaper_clear_windows(x, y0, x + curFont.width, y0 + curFont.height);if(curPage.area_refresh)epaper_draw_icon_area(x, y0, curFont.width, curFont.height, ptr);elseepaper_draw_icon(x, y0, curFont.width, curFont.height, ptr);x += curFont.width;text +=2;}}}
epaper_font_set(&simkai24);epaper_draw_text(0,0," 年 月 日 ");
//显示实时时间:时、分、秒unsignedcharis_refresh_date=0;voidgui_page_real_time(){staticunsignedcharold_hour=0xff,old_minute=0xff,old_second=0xff;unsignedcharhour,minute,second;chartemp_str[8];rtc_read_time(&hour,&minute,&second);epaper_font_set(&Digiface64);if(hour!=old_hour){sprintf(temp_str,"%02d",hour);epaper_draw_text(TIME_POS_X,TIME_POS_Y,temp_str);}if(minute!=old_minute){sprintf(temp_str,"%02d",minute);epaper_draw_text(TIME_POS_X+32*3-16,TIME_POS_Y,temp_str);}if(second!=old_second){sprintf(temp_str,"%02d",second);epaper_draw_text(TIME_POS_X+32*6-16,TIME_POS_Y,temp_str);}if((hour!=old_hour)||(minute!=old_minute)||(second!=old_second)){if(curPage.area_refresh==0)epaper_driver_fill(curPage.buffer);if(hour==0&& minute==0&& second==0)is_refresh_date=1;}old_hour =hour;old_minute =minute;old_second =second;}//显示实时日期:年、月、日voidgui_page_real_date(){staticunsignedcharold_year=0xff,old_month=0xff,old_day=0xff,old_weekday=0xff;unsignedcharyear,month,day,weekday;chartemp_str[8];rtc_read_data(&year,&month,&day,&weekday);epaper_font_set(&Digiface24);if(year!=old_year){sprintf(temp_str,"20%02d",year);epaper_draw_text(DATE_POS_X,DATE_POS_Y,temp_str);}if(month!=old_month){sprintf(temp_str,"%02d",month);epaper_draw_text(DATE_POS_X+12*6,DATE_POS_Y,temp_str);}if(day!=old_day){sprintf(temp_str,"%02d",day);epaper_draw_text(DATE_POS_X+12*10,DATE_POS_Y,temp_str);}if(weekday!=old_weekday){epaper_font_set(&simkai24);memcpy(temp_str,&WeekdayTab[weekday*2],2);strcat(temp_str,"\0");epaper_draw_text(DATE_POS_X+12*16,DATE_POS_Y,temp_str);}if((year!=old_year)||(month!=old_month)||(day!=old_day)||(weekday!=old_weekday)){if(curPage.area_refresh==0)epaper_driver_fill(curPage.buffer);}old_year =year;old_month =month;old_day =day;old_weekday=weekday;}
,
---------------------
作者:shanyuxiang
链接:https://bbs.21ic.com/icview-3466572-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。