/****************************************************************************** * Copyright (C) 2014-2015 HangZhou SiZhu Co.,LTD. * *----------------------------------------------------------------------------- * File: OLED.c * Description: OLED disp code * Author: Lishoujian (867693272@qq.com) * Date: Jan 8, 2015 *****************************************************************************/ /* ----------------------- Platform includes --------------------------------*/ #include "OLED.h" #include "spi.h" #include "delay.h" #include "adc.h" /****************************************** * func: OLED_driver_init * desc: initialise OLED * input: none * output: none * return: none *****************************************/ void OLED_driver_init(void) {//hcs2022 delay_ms(1); OLED_wr_cmd(0x2a); OLED_wr_cmd(0x71); OLED_wr_data(0x00); OLED_wr_cmd(0x28); OLED_wr_cmd(0x08); OLED_wr_cmd(0x2a); OLED_wr_cmd(0x79); OLED_wr_cmd(0xd5); OLED_wr_cmd(0x70); /* Fosc=7 CDLK=1 */ OLED_wr_cmd(0x78); OLED_wr_cmd(0x28); OLED_wr_cmd(0x2a); OLED_wr_cmd(0x08); OLED_wr_cmd(0x28); OLED_wr_cmd(0x2a); OLED_wr_cmd(0x06); /* BCD=1(C0-->C31)DS=0(S99-->S0) */ OLED_wr_cmd(0x28); OLED_wr_cmd(0x2a); OLED_wr_cmd(0x72); OLED_wr_data(0x00); /*ROM=A CGRAM=8 */ OLED_wr_cmd(0x28); OLED_wr_cmd(0x2a); OLED_wr_cmd(0x79); OLED_wr_cmd(0xda); OLED_wr_cmd(0x10); OLED_wr_cmd(0xCD); OLED_wr_cmd(0x00); OLED_wr_cmd(0x81); OLED_wr_cmd(0x40); OLED_wr_cmd(0xd9); OLED_wr_cmd(0xf1); OLED_wr_cmd(0xdb); OLED_wr_cmd(0x30); OLED_wr_cmd(0x78); OLED_wr_cmd(0x28); OLED_wr_cmd(0x01); OLED_wr_cmd(0x80); OLED_wr_cmd(0x0c); } /****************************************** * func: OLED_clear * desc: clear OLED disp * input: none * output: none * return: none *****************************************/ void OLED_clear(void) { OLED_wr_cmd(0x01); delay_ms(1); } /****************************************** * func: OLED_wr_cmd * desc: write command to OLED * input: * cmd - 1 byte command data * cmd - send 0x80~0x93,0xc0~0xd3 corresponding to 2*20 OLED disp postion * output: none * return: none *****************************************/ void OLED_wr_cmd(uint8 cmd) { uint8 OLED_lowbyte,OLED_highbyte,tempdata,i,j; OLED_lowbyte = cmd & 0x0f; OLED_highbyte = (cmd & 0xf0) >> 4; OLED_cs = 0; for(j = 0;j < 3;j ++) { switch(j) { case 0: tempdata = OLED_cmd_startbyte; break; case 1: tempdata = OLED_lowbyte; break; case 2: tempdata = OLED_highbyte; break; default: break; } for(i=0;i<8;i++) { OLED_clk=0; (tempdata&(0x01<> 4; OLED_cs = 0; for(j = 0;j < 3;j ++) { switch(j) { case 0: tempdata = OLED_dat_startbyte; break; case 1: tempdata = lowbyte; break; case 2: tempdata = highbyte; break; default: break; } for(i=0;i<8;i++) { OLED_clk=0; (tempdata&(0x01< 0) { OLED_disponechar(*string); string ++; } } /****************************************** * func: OLED_disp_num * desc: Disp num on OLED * input: * num - data disp on OLED * len - data length on hexadecimal mode. eg:num = 3 len = 1;num = 33 len = 2;num = 333 len = 3 * flag - =(1)fill "0"/=(0) fill " " * output: none * return: none *****************************************/ void OLED_disp_num(uint32 num,uint8 len,uint8 flag) { uint8 i,temp,enshow = 0; for(i = 0;i < len;i ++) { temp = (num / OLED_pow(10, len-i-1)) % 10; if((enshow == 0) && (i < (len - 1))) { if(temp == 0) { if(flag == 1) OLED_wr_data('0'); else OLED_wr_data(' '); continue; } else enshow = 1; } OLED_wr_data(temp + '0'); } } void OLED_disp_double(uint8 x,uint8 y,double num,uint8 integer_len,uint8 decimal_len,uint8 flag) { uint8 i,temp,enshow = 0; uint32 integer,decimal; integer = num; decimal = (num-integer)*OLED_pow(10, decimal_len); OLED_setdispaddr(x,y); for(i = 0;i < integer_len;i ++) { temp = (integer / OLED_pow(10, integer_len-i-1)) % 10; if((enshow == 0) && (i < (integer_len - 1))) { if(temp == 0) { if(flag == 1) OLED_wr_data('0'); else OLED_wr_data(' '); continue; } else enshow = 1; } OLED_wr_data(temp + '0'); } OLED_wr_data('.'); for(i = 0;i < decimal_len;i ++) { temp = (decimal / OLED_pow(10, decimal_len-i-1)) % 10; if((enshow == 0) && (i < (decimal_len - 1))) { if(temp == 0) { OLED_wr_data('0'); continue; } else enshow = 1; } OLED_wr_data(temp + '0'); } } /****************************************** * func: OLED_pow * desc: exponent sign;calculate m^n * input: m,n * output: none * return: m^n *****************************************/ uint32 OLED_pow(uint8 m,uint8 n)//m^n { uint32 result=1; while(n --) { result *= m; } return result; } /****************************************** * func: OLED_cgr_set * desc: creat self define character * input: *s * output: none * return: none *****************************************/ void OLED_cgr_set(u8 *s) { uint8 i,tmp; tmp = 0x40; while(*s != 0xff) { for(i=0;i<8;i++) { OLED_wr_cmd(tmp + i); delay_ms(2); OLED_wr_data(*s); s ++; delay_ms(2); } tmp = tmp + 8; } }