找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2136|回复: 2
收起左侧

OLED 0.97 STM8 STVD 菜单

[复制链接]
ID:205182 发表于 2020-5-2 23:49 | 显示全部楼层 |阅读模式
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8s105k4.h"
      
#define u8   unsigned char
#define u16  unsigned short
#define u32  unsigned long
#define uint unsigned int
/*--------------------引脚定义--------------------------*/
#define   OLED_SCLK_Set() (PB_ODR |= 0x01) //PB0(SCL)输出高
#define   OLED_SCLK_Clr() (PB_ODR &= 0xfe) //PB0(SCL)输出低
#define   OLED_SDIN_Set() (PB_ODR |= 0x02) //PB1(SDA)输出高
#define   OLED_SDIN_Clr() (PB_ODR &= 0xfd) //PB1(SDA)输出高
#define   OLED_READ_SDIN() (PB_IDR & 0x02)        //读取PB4(SDA)电平  

//#define   OLED_KEY1()       (PD_IDR
/*definition--------------------------------------------*/
#define OLED_CMD  0 //写命令
#define OLED_DATA 1 //写数据
#define SIZE   16  //显示字符的大小
#define Max_Column 128  //最大列数
#define Max_Row  64  //最大行数
#define X_WIDTH  128  //X轴的宽度
#define Y_WIDTH  64     //Y轴的宽度              
#define IIC_ACK  0  //应答
#define IIC_NO_ACK 1  //不应答


void OLED_GPIO_Init(void);
void delay (u8 num);
void delay_ms(uint ms);
void OLED_IIC_Start(void);
void OLED_IIC_Stop(void);
u8 IIC_Wait_Ack(void);
void Write_IIC_Byte(u8 IIC_Byte);
void Write_IIC_Command(u8 IIC_Command);
void Write_IIC_Data(u8 IIC_Data);
uint OLED_Pow(u8 m,u8 n);
void OLED_WR_Byte(u8 dat,u8 cmd);   //OLED写字节函数
void OLED_Display_On(void); //开显示函数
void OLED_Display_Off(void);//关显示函数
void OLED_Init(void); //OLED初始化函数
void OLED_Clear(void);  //清屏函数
void OLED_ShowChar(u8 x,u8 y,u8 chr); //显示字符函数
void OLED_ShowNum(u8 x,u8 y,uint num,u8 len,u8 size2); //在指定的位置,显示一个指定数的长度大小函数
void OLED_ShowString(u8 x,u8 y, u8 *chr);  //在指定位置开始显示字符串函数
void OLED_Set_Pos(u8 x, u8 y);  //画点函数
void OLED_ShowChinese(u8 x,u8 y,u8 no); //声明在指定位置显示汉字函数
void OLED_DrawBMP(u8 x0, u8 y0,u8 x1, u8 y1,u8 BMP[]); //显示图片函数
void OLED_Scroll(void);//滚动函数
void OLED_menu(u8 cur);
void key_Init(void);
  u8 key1_scan(void);
  u8 key2_scan(void);
void KeyPressed(u8 cur);
  u8 key_scan(void);
const u8 table[]={2,4,6};
const u8 F6x8[][6];
const u8 F8X16[];
const u8 Hzk[][16];
//const u8 BMP1[];

main()
{
CLK_CKDIVR = 0x00;//f_HSI = f_HSI RC 输出    f_CPU = f_MASTER    16M
OLED_Init();//OLED初始化
key_Init();
                KeyPressed(1);



        while (1);
   
}

void delay(u8 num)
{
while(num--);
}

void delay_ms(uint ms)//延迟函数,MS级别
{
uint x,y;
for(x = ms;x>0;x--)
{
  for(y = 1300;y>0;y--);
}
}

void OLED_GPIO_Init(void)
{
PB_DDR |= 0x03; //将 PB0 PB1 口设为输出
PB_CR1 &= 0xfc; //将 PB0 PB1 口设为开漏
PB_CR2 &= 0xfc;//将 PB0 PB1 口设为开漏
OLED_SCLK_Set(); //将PB4(SCL)设为高
OLED_SDIN_Set(); //将PB5(SDA)设为高
}

void OLED_IIC_Start(void)
{
OLED_SCLK_Set(); //时钟线置高
OLED_SDIN_Set(); //信号线置高
delay(1); //延迟1us
OLED_SDIN_Clr(); //信号线置低
delay(1); //延迟1us
OLED_SCLK_Clr(); //时钟线置低
delay(1); //延迟1us
}

void OLED_IIC_Stop(void)
{
OLED_SDIN_Clr(); //信号线置低
delay(1); //延迟1us
OLED_SCLK_Set(); //时钟线置高
delay(1); //延迟1us
OLED_SDIN_Set(); //信号线置高
delay(1); //延迟1us
}
u8 IIC_Wait_Ack(void)
{
u8 ack;
OLED_SCLK_Clr(); //时钟线置低
delay(1); //延迟1us
OLED_SDIN_Set(); //信号线置高
delay(1); //延迟1us
OLED_SCLK_Set(); //时钟线置高
delay(1); //延迟1us
if(OLED_READ_SDIN()) //读取SDA的电平
  ack = IIC_NO_ACK; //如果为1,则从机没有应答
else
  ack = IIC_ACK;  //如果为0,则从机应答
OLED_SCLK_Clr();//时钟线置低
delay(1); //延迟1us
return ack; //返回读取到的应答信息
}


void Write_IIC_Byte(u8 IIC_Byte)
{
u8 i;  //定义变量
for(i=0;i<8;i++) //for循环8次
{
  OLED_SCLK_Clr(); //时钟线置低,为传输数据做准备
  delay(1); //延迟1us
  if(IIC_Byte & 0x80) //读取最高位
     OLED_SDIN_Set(); //最高位为1
  else
   OLED_SDIN_Clr(); //最高位为0
  IIC_Byte <<= 1;  //数据左移1位
  delay(1); //延迟1us
  OLED_SCLK_Set(); //时钟线置高,产生上升沿,把数据发送出去
  delay(1); //延迟1us
}
OLED_SCLK_Clr(); //时钟线置低
delay(1); //延迟1us
while(IIC_Wait_Ack()); //从机应答
}


void Write_IIC_Command(u8 IIC_Command)
{
   OLED_IIC_Start();
   Write_IIC_Byte(0x78);//写入从机地址,SD0 = 0
   Write_IIC_Byte(0x00);//写入命令
   Write_IIC_Byte(IIC_Command);//数据
   OLED_IIC_Stop();  //发送停止信号
}

void Write_IIC_Data(u8 IIC_Data)
{
   OLED_IIC_Start();
   Write_IIC_Byte(0x78); //写入从机地址,SD0 = 0
   Write_IIC_Byte(0x40); //写入数据
   Write_IIC_Byte(IIC_Data);//数据
   OLED_IIC_Stop();  //发送停止信号
}

void OLED_WR_Byte(u8 dat,u8 cmd)
{
if(cmd)
   {
       Write_IIC_Data(dat); //写入数据
   }
else {
       Write_IIC_Command(dat); //写入命令
}
}

void OLED_Set_Pos(u8 x,u8 y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);//写入页地址
OLED_WR_Byte((x&0x0f),OLED_CMD);  //写入列的地址  低半字节
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);//写入列的地址 高半字节
}      

void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD);  //设置OLED电荷泵
OLED_WR_Byte(0X14,OLED_CMD);  //使能,开
OLED_WR_Byte(0XAF,OLED_CMD);  //开显示
}

void OLED_Display_Off(void)
{
OLED_WR_Byte(0XAE,OLED_CMD);  //关显示
OLED_WR_Byte(0X8D,OLED_CMD);  //设置OLED电荷泵
OLED_WR_Byte(0X10,OLED_CMD);  //失能,关
}         

   
void OLED_Clear(void)  
{  
u8 i,n;      //定义变量
for(i=0;i<8;i++)  
{  
  OLED_WR_Byte (0xb0+i,OLED_CMD);    //从0~7页依次写入
  OLED_WR_Byte (0x00,OLED_CMD);      //列低地址
  OLED_WR_Byte (0x10,OLED_CMD);      //列高地址  
  for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA); //写入 0 清屏
}
}

void OLED_ShowChar(u8 x,u8 y,u8 chr)
{      
u8 c=0,i=0;
  c=chr-' '; //获取字符的偏移量
  if(x>Max_Column-1){x=0;y=y+2;} //如果列数超出了范围,就从下2页的第0列开始
  if(SIZE ==16) //字符大小如果为 16 = 8*16
   {
    OLED_Set_Pos(x,y); //从x y 开始画点
    for(i=0;i<8;i++)  //循环8次 占8列
    OLED_WR_Byte(F8X16[c*16+i],OLED_DATA); //找出字符 c 的数组位置,先在第一页把列画完
    OLED_Set_Pos(x,y+1); //页数加1
    for(i=0;i<8;i++)  //循环8次
    OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA); //把第二页的列数画完
   }
  else  //字符大小为 6 = 6*8
   {
    OLED_Set_Pos(x,y+1); //一页就可以画完
    for(i=0;i<6;i++) //循环6次 ,占6列
    OLED_WR_Byte(F6x8[c][i],OLED_DATA); //把字符画完
   }
}

uint OLED_Pow(u8 m,u8 n)
{
uint result=1;  
while(n--)result*=m;   
return result;
}      

   
void OLED_ShowNum(u8 x,u8 y,uint num,u8 len,u8 size)
{         
u8 t,temp;  //定义变量
u8 enshow=0;  //定义变量
for(t=0;t<len;t++)
{
  temp=(num/OLED_Pow(10,len-t-1))%10;//取出输入数的每个位,由高到低
  if(enshow==0&&t<(len-1)) //enshow:是否为第一个数;t<(len-1):判断是否为最后一个数
  {
   if(temp==0) //如果该数为0
   {
    OLED_ShowChar(x+(size/2)*t,y,' ');//显示 0 ;x+(size2/2)*t根据字体大小偏移的列数(8)
    continue; //跳过剩下语句,继续重复循环(避免重复显示)
   }else enshow=1;
  }
   OLED_ShowChar(x+(size/2)*t,y,temp+'0'); //显示一个位;x+(size2/2)*t根据字体大小偏移的列数(8)
}
}

void OLED_ShowString(u8 x,u8 y,u8 *chr)
{
u8 j=0; //定义变量
while (chr[j]!='\0') //如果不是最后一个字符
{  
  OLED_ShowChar(x,y,chr[j]); //显示字符
  x+=8; //列数加8 ,一个字符的列数占8
  if(x>=128){x=0;y+=2;} //如果x大于等于128,切换页,从该页的第一列显示
  j++; //下一个字符
}
}


void OLED_ShowChinese(u8 x,u8 y,u8 no)
{            
u8 t; //定义变量
OLED_Set_Pos(x,y); //从 x y 开始画点,先画第一页
    for(t=0;t<16;t++) //循环16次,画第一页的16列
  {
   OLED_WR_Byte(Hzk[no*2][t],OLED_DATA);//画no在数组位2*no
      }
  OLED_Set_Pos(x,y+1); //画第二页
    for(t=0;t<16;t++)//循环16次,画第二页的16列
  {
   OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);//画no在数组位置的第二页16列的点
        }     
}

void OLED_DrawBMP(u8 x0, u8 y0,u8 x1, u8 y1,u8 BMP[])
{  
  uint j=0; //定义变量
  u8 x,y; //定义变量
  
  if(y1%8==0) y=y1/8;   //判断终止页是否为8的整数倍
   else y=y1/8+1;
  for(y=y0;y<y1;y++) //从起始页开始,画到终止页
  {
   OLED_Set_Pos(x0,y); //在页的起始列开始画
      for(x=x0;x<x1;x++) //画x1 - x0 列
       {
        OLED_WR_Byte(BMP[j++],OLED_DATA); //画图片的点     
       }
  }
}

        
void OLED_Init(void)
{
OLED_GPIO_Init(); //GPIO口初始化

delay_ms(200); //延迟,由于单片机上电初始化比OLED快,所以必须加上延迟,等待OLED上复位完成
OLED_WR_Byte(0xAE,OLED_CMD); //关闭显示
OLED_WR_Byte(0x00,OLED_CMD); //设置低列地址
OLED_WR_Byte(0x10,OLED_CMD); //设置高列地址
OLED_WR_Byte(0x40,OLED_CMD); //设置起始行地址
OLED_WR_Byte(0xB0,OLED_CMD); //设置页地址
OLED_WR_Byte(0x81,OLED_CMD);  // 对比度设置,可设置亮度
OLED_WR_Byte(0xFF,OLED_CMD); //  265  
OLED_WR_Byte(0xA1,OLED_CMD); //设置段(SEG)的起始映射地址;column的127地址是SEG0的地址
OLED_WR_Byte(0xA6,OLED_CMD); //正常显示;0xa7逆显示
OLED_WR_Byte(0xA8,OLED_CMD); //设置驱动路数(16~64)
OLED_WR_Byte(0x3F,OLED_CMD); //64duty

OLED_WR_Byte(0xC8,OLED_CMD); //重映射模式,COM[N-1]~COM0扫描
OLED_WR_Byte(0xD3,OLED_CMD); //设置显示偏移
OLED_WR_Byte(0x00,OLED_CMD); //无偏移

OLED_WR_Byte(0xD5,OLED_CMD); //设置震荡器分频
OLED_WR_Byte(0x80,OLED_CMD); //使用默认值

OLED_WR_Byte(0xD9,OLED_CMD); //设置 Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD); //使用官方推荐值

OLED_WR_Byte(0xDA,OLED_CMD); //设置 com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD); //使用默认值

OLED_WR_Byte(0xDB,OLED_CMD); //设置 Vcomh,可调节亮度(默认)
OLED_WR_Byte(0x40,OLED_CMD); ////使用官方推荐值

OLED_WR_Byte(0x8D,OLED_CMD); //设置OLED电荷泵
OLED_WR_Byte(0x14,OLED_CMD); //开显示

OLED_WR_Byte(0xAF,OLED_CMD); //开启OLED面板显示
OLED_Clear();        //清屏
OLED_Set_Pos(0,0);   //设置数据写入的起始行、列
}  

void OLED_Scroll(void)
{
OLED_WR_Byte(0x2E,OLED_CMD); //关闭滚动
OLED_WR_Byte(0x27,OLED_CMD); //水平向左滚动
OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
OLED_WR_Byte(0x00,OLED_CMD); //起始页 0
OLED_WR_Byte(0x00,OLED_CMD); //滚动时间间隔
OLED_WR_Byte(0x01,OLED_CMD); //终止页 1
OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
OLED_WR_Byte(0xFF,OLED_CMD); //虚拟字节
OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
}


const u8  F6x8[][6] =  
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #
0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $
0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %
0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &
0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '
0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (
0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )
0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *
0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +
0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .
0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2
0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7
0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :
0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <
0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =
0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >
0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?
0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @
0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B
0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C
0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E
0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F
0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I
0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J
0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K
0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L
0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M
0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P
0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R
0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S
0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T
0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W
0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X
0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y
0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z
0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [
0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55
0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]
0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^
0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _
0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b
0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c
0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d
0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i
0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j
0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k
0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s
0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x
0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z
0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines
};
/****************************************8*16的点阵************************************/
const u8  F8X16[]=   
{
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
const u8  Hzk[][16]={        
{0x04,0x04,0x44,0xC4,0x4F,0x44,0x44,0xC4,0x24,0x24,0x2F,0xB4,0x24,0x04,0x04,0x00},
{0x40,0x44,0x24,0x24,0x15,0x0C,0x04,0xFE,0x04,0x0C,0x15,0x24,0x24,0x44,0x40,0x00},/*"菜",0*/
{0x00,0x00,0xF8,0x49,0x4A,0x4C,0x48,0xF8,0x48,0x4C,0x4A,0x49,0xF8,0x00,0x00,0x00},
{0x10,0x10,0x13,0x12,0x12,0x12,0x12,0xFF,0x12,0x12,0x12,0x12,0x13,0x10,0x10,0x00},/*"单",1*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*":",2*/

{0x10,0x10,0x10,0xFF,0x90,0x20,0x98,0x88,0x88,0xE9,0x8E,0x88,0x88,0xA8,0x98,0x00},
{0x02,0x42,0x81,0x7F,0x00,0x00,0x80,0x84,0x4B,0x28,0x10,0x28,0x47,0x80,0x00,0x00},/*"按",3*/
{0x40,0x30,0xEF,0x24,0x24,0x80,0xE4,0x9C,0x10,0x54,0x54,0xFF,0x54,0x7C,0x10,0x00},
{0x01,0x01,0x7F,0x21,0x51,0x26,0x18,0x27,0x44,0x45,0x45,0x5F,0x45,0x45,0x44,0x00},/*"键",4*/
{0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"一",5*/

{0x10,0x10,0x10,0xFF,0x90,0x20,0x98,0x88,0x88,0xE9,0x8E,0x88,0x88,0xA8,0x98,0x00},
{0x02,0x42,0x81,0x7F,0x00,0x00,0x80,0x84,0x4B,0x28,0x10,0x28,0x47,0x80,0x00,0x00},/*"按",6*/
{0x40,0x30,0xEF,0x24,0x24,0x80,0xE4,0x9C,0x10,0x54,0x54,0xFF,0x54,0x7C,0x10,0x00},
{0x01,0x01,0x7F,0x21,0x51,0x26,0x18,0x27,0x44,0x45,0x45,0x5F,0x45,0x45,0x44,0x00},/*"键",7*/
{0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00},
{0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x00},/*"二",8*/

{0x04,0x34,0xC4,0x04,0xC4,0x3C,0x00,0x04,0xFC,0x04,0x04,0x04,0xC4,0x3C,0x00,0x00},
{0x40,0x30,0x0C,0x03,0x0C,0x30,0x80,0x40,0x20,0x13,0x0C,0x13,0x20,0x40,0x80,0x00},/*"双",9*/
{0x10,0x10,0x10,0xFF,0x90,0x20,0x98,0x88,0x88,0xE9,0x8E,0x88,0x88,0xA8,0x98,0x00},
{0x02,0x42,0x81,0x7F,0x00,0x00,0x80,0x84,0x4B,0x28,0x10,0x28,0x47,0x80,0x00,0x00},/*"按",10*/
{0x40,0x30,0xEF,0x24,0x24,0x80,0xE4,0x9C,0x10,0x54,0x54,0xFF,0x54,0x7C,0x10,0x00},
{0x01,0x01,0x7F,0x21,0x51,0x26,0x18,0x27,0x44,0x45,0x45,0x5F,0x45,0x45,0x44,0x00},/*"键",11*/
};
void key_Init(void)
{
  PD_DDR&=0X00;
  PD_CR1=0X18;//PD3,PD4带上拉的输入模式
  PD_CR2&=0X00;
}

u8 key1_scan(void)           //PD3
{
             static u8 key1_up=0;
              u8 num;
              num=PD_IDR;
              num=PD_IDR&0x18;
              if(num==0x10)
                  delay_ms(30);
  if(num==0x10)
  {
          key1_up=0;
  }
  else
{
   key1_up=1;
   return 1;
}
     return 0;
}      
        
        
u8 key2_scan(void)           //PD4
{  
    static u8 key2_up=0;
    u8 num;
    num=PD_IDR;
    num=PD_IDR&0x18;
     if(num==0x08)
         delay_ms(30);
             if(num==0x08)
{
                     key2_up=0;
  }
  else
{
      key2_up=1;
      return 1;
                 }
     return 0;
}      

u8 key_scan(void)
{
  u8 key_code;
  if(key1_scan()==1)key_code=2;
  else if(key2_scan()==1)key_code=3;
  else key_code=0;
  return key_code;
}

void KeyPressed(u8 cur)
{
  u8 flag=0;
OLED_menu(cur);
      while(1)
                         {
  if(flag)
  {
   OLED_menu(cur);
   flag=2;
           }
  switch(key_scan())
  {
      case 0: flag = 0;
      break;
  
      
                     case 3: flag = 1;
                 if(cur==3)
                 cur=1;
               else
                                               cur++;
                OLED_Clear();
               break;
  }
           }
}
void OLED_menu(u8 cur)
  {      
      void   OLED_String(u8 x,u8 y,u8 *chr);
      void   OLED_ShowChinese(u8 x,u8 y,u8  no);
       switch(cur)
{
      
        
case 1:  
                        OLED_ShowString(16,2,"=>");
                        OLED_ShowChinese(0,0,0);//OLED显示      菜
                                       OLED_ShowChinese(16,0,1);//OLED显示    单
                        OLED_ShowChinese(32,0,2);//OLED显示     :
                        OLED_ShowChinese(32,2,3);//OLED显示    按
         OLED_ShowChinese(48,2,4);//OLED显示    键
         OLED_ShowChinese(64,2,5);//OLED显示    一
                        OLED_ShowChinese(32,4,6);//OLED显示    按
         OLED_ShowChinese(48,4,7);//OLED显示    键
                        OLED_ShowChinese(64,4,8);//OLED显示    二
                        OLED_ShowChinese(32,6,9);//OLED显示    双
         OLED_ShowChinese(48,6,10);//OLED显示  按
         OLED_ShowChinese(64,6,11);//OLED显示  键               
                                       break;
            
case 2:
                 
                        OLED_ShowString(16,4,"=>");
                        OLED_ShowChinese(0,0,0);//OLED显示     菜
                                       OLED_ShowChinese(16,0,1);//OLED显示   单
                        OLED_ShowChinese(32,0,2);//OLED显示    :
                        OLED_ShowChinese(32,2,3);//OLED显示   按
         OLED_ShowChinese(48,2,4);//OLED显示   键
         OLED_ShowChinese(64,2,5);//OLED显示   一
                        OLED_ShowChinese(32,4,6);//OLED显示   按
         OLED_ShowChinese(48,4,7);//OLED显示   键
                        OLED_ShowChinese(64,4,8);//OLED显示   二
                        OLED_ShowChinese(32,6,9);//OLED显示   双
         OLED_ShowChinese(48,6,10);//OLED显示 按
                                       OLED_ShowChinese(64,6,11);//OLED显示 键
         break;
            
                 case 3:
                      OLED_ShowString(16,6,"=>");
                        OLED_ShowChinese(0,0,0);//OLED显示          菜
                                       OLED_ShowChinese(16,0,1);//OLED显示        单
                        OLED_ShowChinese(32,0,2);//OLED显示         :
                        OLED_ShowChinese(32,2,3);//OLED显示        按
                        OLED_ShowChinese(48,2,4);//OLED显示        键
                                       OLED_ShowChinese(64,2,5);//OLED显示        一
                        OLED_ShowChinese(32,4,6);//OLED显示        按
         OLED_ShowChinese(48,4,7);//OLED显示        键
                        OLED_ShowChinese(64,4,8);//OLED显示        二
                        OLED_ShowChinese(32,6,9);//OLED显示        双
         OLED_ShowChinese(48,6,10);//OLED显示      按
         OLED_ShowChinese(64,6,11);//OLED显示      键
                         break;     
            
   }
  }

评分

参与人数 1黑币 +15 收起 理由
luliang02003 + 15 共享资料的黑币奖励!补足原理图,再分享给.

查看全部评分

回复

使用道具 举报

ID:108573 发表于 2020-5-4 09:28 | 显示全部楼层
显示汉字有种更好的方法,就是查找汉字的编码方法,那样写程序时就不需要在意汉字在数组中的位置,省去一些麻烦。
回复

使用道具 举报

ID:205182 发表于 2020-5-27 01:01 | 显示全部楼层
谢谢回复,虚心接受
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表