【瑞康原创,转载需说明】
效果图
文件结构
SPI.H头文件
#ifndef __SPI__H__
#define __SPI__H__
#include "reg52.h"
//OLED自带复位,OLED显示屏和字库的时钟和数据线共用
sbit SSD1306_GT20_CLK=P1^0; //显示屏和字库共用时钟线
sbit SSD1306_GT20_MOSI=P1^1; //显示屏和字库共用MOSI线
sbit SSD1306_DC=P1^2; //显示屏命令/数据线,低电平表示传输的命令,高电平表示传输的数据
sbit SSD1306_CS=P1^3; //显示片选线,低电平有效
//字库信号线
sbit GT20_MISO=P1^4; //字库数据输出线
sbit GT20_CS=P1^5; //字库片选线
//所用到的函数
void Write_Byte(unsigned char wDat); //写1byte
static unsigned char Read_Byte(); //读1byte
void Write_To_1306(unsigned char wDat,unsigned char cmd_data); //向1306写数据
void Read_Nbyte_fromROM(unsigned char HSB,unsigned char MSB,unsigned char LSB,unsigned char *sBuffer,unsigned char Data_Len); //从字库读数据
#endif
SPI.C文件
#include "reg52.h"
#include "spi.h"
//SPI写一个byte
void Write_Byte(unsigned char wDat)
{
unsigned char i; //通用计数器
for (i=0;i<8;i++) //先发送高位,再发送低位
{
SSD1306_GT20_MOSI=(bit)(wDat&0x80); //取最高位
SSD1306_GT20_CLK=0; //产生时钟
SSD1306_GT20_CLK=1;
wDat<<=1; //左移一位
}
}
//SPI读一个byte
static unsigned char Read_Byte(void)
{
unsigned char i,result_data=0;
for (i=0;i<8;i++)
{
GT20_MISO=1;
SSD1306_GT20_CLK=0;
result_data<<=1;
result_data|=GT20_MISO;
SSD1306_GT20_CLK=1;
}
return(result_data); //返回数据
}
//向1306写命令/数据
void Write_To_1306(unsigned char wDat,unsigned char cmd_data)
{
SSD1306_CS=0; //选中显示屏
SSD1306_DC=(bit)cmd_data; //0写入命令,1写入数据
Write_Byte(wDat); //写入
SSD1306_CS=1; //显示屏脱离总线
}
//从ROM中指定地址读取连续N字节到Buffer中
void Read_Nbyte_fromROM(unsigned char HSB,unsigned char MSB,unsigned char LSB,unsigned char *sBuffer,unsigned char Data_Len)
{
unsigned char i;
GT20_CS=0; //选中字库
Write_Byte(0x03); //发送读命令
Write_Byte(HSB); //发送地址
Write_Byte(MSB);
Write_Byte(LSB);
for (i=0;i<Data_Len;i++)
*(sBuffer+i)=Read_Byte(); //将读回数据放在Buffer缓冲区中
GT20_CS=1; //字库离线
}
I2C.H文件
#ifndef __I2C__H__
#define __I2C__H__
//定义PAJ7620连接
sbit PAJ7620_SCK=P3^0;
sbit PAJ7620_SDA=P3^1;
sbit PAJ7620_INT=P3^2;
//定义I2C操作函数
void I2C_Start(void); //I2C启动
void I2C_Stop(void); //I2C停止
void I2C_Send_Ack(unsigned char ACK); //发送应答信号(用于接收)
bit I2C_Wait_Ack(void); //等待应答信号
bit I2C_Write_Byte(unsigned char wDat); //写1byte
unsigned char I2C_Read_Byte(); //读1byte
void Write_One_Reg(unsigned char I2C_Write_Add,unsigned char Reg_Address,unsigned char wDat); //写一个寄存器
unsigned char Read_One_Reg(unsigned char I2C_Add,unsigned char Reg_Address); //读一个寄存器
void Multi_Write(unsigned char I2C_Add,unsigned char Reg_Add,unsigned char *dp,unsigned char len); //写多个寄存器
void Multi_Read(unsigned char I2C_Add,unsigned char Reg_Add,unsigned char *dp,unsigned char len); //读多个寄存器
#endif
I2C.C文件
#include "reg52.h"
#include "i2c.h"
#include "intrins.h"
//I2C启动
void I2C_Start(void)
{
PAJ7620_SDA=1; //拉高数据
PAJ7620_SCK=1; //接高时钟
_nop_(); //延时
PAJ7620_SDA=0; //SCK高电平时数据由高变低表示总线启动
PAJ7620_SCK=0; //拉低时钟,在通信期间,非时钟状态SCK为低电平
}
//I2C停止
void I2C_Stop(void)
{
PAJ7620_SDA=0; //拉低数据
PAJ7620_SCK=1; //拉高时钟
_nop_(); //延时
PAJ7620_SDA=1; //SCK高电平时数据由低变高表示总线结束
}
//发送应答信号
void I2C_Send_Ack(unsigned char ACK)
{
PAJ7620_SDA=(bit)ACK; //写应答数据 0:应答 1:非应答
PAJ7620_SCK=1; //拉高时钟
_nop_(); //延时
PAJ7620_SCK=0; //拉低时钟
}
//等待应答,返回值 0:应答 1:非应答
bit I2C_Wait_Ack(void)
{
unsigned char TimeOut=0; //超时计时器
PAJ7620_SDA=1; //拉高数据线,作好输入准备
PAJ7620_SCK=1; //拉高时钟线
_nop_(); //延时
while(PAJ7620_SDA) //如果数据线一直为高电平就等待,要么收到应答,要么超时
{
TimeOut++;
if (TimeOut>250)
{
PAJ7620_SCK=0;
return 1; //返回未应答
}
}
PAJ7620_SCK=0;
return 0; //返回应答信号
}
//发送1byte,返回值 1:写入成功 0:写入失败
bit I2C_Write_Byte(unsigned char wDat)
{
unsigned char i; //计数器
bit ACK; //应答信号
for (i=0;i<8;i++)
{
PAJ7620_SDA=(bit)(wDat&0x80); //发送最高位
_nop_(); //延时让数据稳定
PAJ7620_SCK=1; //拉高时钟
_nop_(); //延时
PAJ7620_SCK=0; //产生时钟
wDat<<=1; //左移一位
}
ACK=!I2C_Wait_Ack(); //获取应答信号
return ACK; //返回应答信号
}
//接收1ybyte
unsigned char I2C_Read_Byte()
{
unsigned char i,rDat; //计数器,返回数据
PAJ7620_SDA=1; //上拉数据线,为接收作准备
for (i=0;i<8;i++)
{
rDat<<=1; //数据左移
PAJ7620_SCK=1; //拉高时钟线
_nop_(); //延时
rDat|=PAJ7620_SDA; //读数据
PAJ7620_SCK=0; //拉低时钟线
_nop_(); //延时
}
return rDat;
}
//向PAJ7620写1byte
void Write_One_Reg(unsigned char I2C_Write_Add,unsigned char Reg_Address,unsigned char wDat)
{
I2C_Start(); //总线启动
I2C_Write_Byte(I2C_Write_Add); //发送器件
I2C_Write_Byte(Reg_Address); //发送寄存器地址
I2C_Write_Byte(wDat); //发送数据
I2C_Stop(); //总线停止
}
//从PAJ7620读1byte
unsigned char Read_One_Reg(unsigned char I2C_Add,unsigned char Reg_Address)
{
unsigned char rDat; //定义读出的数据
I2C_Start(); //启动总线
I2C_Write_Byte(I2C_Add); //写器件地址
I2C_Write_Byte(Reg_Address); //写寄存器地址
I2C_Start(); //重新启动
I2C_Write_Byte(I2C_Add|1); //发送器件地址+读信号
rDat=I2C_Read_Byte(); //读数据
I2C_Send_Ack(1); //发送非应答信号
I2C_Stop(); //总线停止
return rDat; //返回数据
}
/*==========以下是未使用函数=========================
//向PAJ7620写入多字节
//入口参数 I2C_Add:器件地址 Reg_Add:寄存器地址 *dp:待写入数据指针 len:写入长度
void Multi_Write(unsigned char I2C_Add,unsigned char Reg_Add,unsigned char *dp,unsigned char len)
{
unsigned char i; //计数器
I2C_Start(); //启动总线
I2C_Write_Byte(I2C_Add); //发送器件地址
I2C_Write_Byte(Reg_Add); //发送寄存器地址
for (i=0;i
{
I2C_Write_Byte(*dp); //发送数据
dp++; //移动指针
}
I2C_Stop(); //总线停止
}
//从PAJ7620读入多字节
//入口参数 I2C_Add:器件地址 Reg_Add:寄存器地址 *dp:数据缓冲区 len:读出长度
void Multi_Read(unsigned char I2C_Add,unsigned char Reg_Add,unsigned char *dp,unsigned char len)
{
unsigned char i; //计数器
I2C_Start(); //启动总线
I2C_Write_Byte(I2C_Add); //发送器件地址
I2C_Write_Byte(Reg_Add); //发送寄存器地址
I2C_Start(); //重新启动
I2C_Write_Byte(Reg_Add|1); //发送寄存器地址+读信号
for (i=0;i
{
*dp++=I2C_Read_Byte(); //读取数据
I2C_Send_Ack(0); //发送应答信号
}
*dp=I2C_Read_Byte(); //读取最后1byte
I2C_Send_Ack(1); //发送非应答信号
I2C_Stop(); //总线停止
}
*/
1306.H文件
#ifndef __SSD1306__H__
#define __SSD1306__H__
//显示屏参数常量定义
#define SSD1306_SetRow_Cmd 0xb0 //设置页号
#define SSD1306_SetColHi_Cmd 0x10 //设置列高位
#define SSD1306_SetColLo_Offset 0x00 //列底位偏移量,SH1106为0x02,1306为0x00
#define SSD1306_CMD 0x00 //写命令
#define SSD1306_DATA 0x01 //写数据
#define SSD1306_Width 128 //显示屏宽度
#define SSD1306_Height 64 //显示屏高度
#define SSD1306_Pages 8 //页数
//unsigned char SSD1306_Bright=0xff; //显示屏亮度
void delay_ms(unsigned int n_ms); //延时
//void SSD1306_Rst(); //未用
void SSD1306_init(); //初始化
void SSD1306_Set_Pos(unsigned char x,unsigned char y); //定位
void SSD1306_Clear_Screen(); //清屏
void SSD1306_Adjust_Light(unsigned char XDB_Value,unsigned char X81_Value); //调亮度
void SSD1306_Show_12864img(unsigned char *dp); //显示128X64全屏图像
void SSD1306_ShowX16(unsigned char x,unsigned char y,unsigned char *dp,unsigned char Size); //显示8X16和16X16文字、字符
void SSD1306_Show0507(unsigned char x,unsigned char y,unsigned char *dp); //显示5X7字符
#endif
1306.C文件
unsigned char code SSD1306_init_Value[]={0xAE,0xD5,0x80,0xAD,0x8B,0x32,0xDB,0x33,0x81,0xFF,0xDA,
0x12,0x8D,0xA8,0x3F,0x20,0x02,0xD9,0x1F,0x40,0x14,0xA4,0xD3,0x00,0xA1,0xC8,0xA6,0xAF}; //复位数据
/*
//延时函数
void delay_ms(unsigned int n_ms)
{
int j,k;
for(j=0;j
for(k=0;k<1002;k++);
}
//OLED复位,这个DEMO中因为OLED是硬件复位,所以不用
void SSD1306_Rst()
{
SSD1306_RST=0;
delay_ms(10);
SSD1306_RST=1;
}
*/
//初始化函数
void SSD1306_init()
{
unsigned char i;
//SSD1306_Rst(); //因芯片有硬件复位,所以不用
for (i=0;i<28;i++)
Write_To_1306(SSD1306_init_Value[i],SSD1306_CMD); // 写配置
SSD1306_Clear_Screen(); //清屏
}
//定位函数
void SSD1306_Set_Pos(unsigned char x,unsigned char y)
{
Write_To_1306(SSD1306_SetRow_Cmd+y,SSD1306_CMD); //设置页面
Write_To_1306(SSD1306_SetColHi_Cmd|(((x+SSD1306_SetColLo_Offset)&0xf0) >>4),SSD1306_CMD); //设置列坐标高4位
Write_To_1306((x+SSD1306_SetColLo_Offset)&0x0f,SSD1306_CMD); //设置列坐标低4位
}
//清屏函数
void SSD1306_Clear_Screen()
{
unsigned char x,y; //列页计数器
for (y=0;y<SSD1306_Pages;y++)
{
SSD1306_Set_Pos(0,y);
for (x=0;x<SSD1306_Width;x++)
{
Write_To_1306(0x00,SSD1306_DATA);
}
}
}
/*
//改变SSD1306亮度函数,此函数在此工程中未用。
void SSD1306_Adjust_Light(unsigned char XDB_Value,unsigned char X81_Value)
{
//先改变0xDB寄存器的值
Write_To_1306(0xDB,SSD1306_CMD);
//0.96屏值可以为0x10,0x20,0x30
//1.3屏的值可以为0x30,0x31,0x32,0x33
Write_To_1306(XDB_Value,SSD1306_CMD);
//然后改变0x81寄存器的值
Write_To_1306(0x81,SSD1306_CMD);
//值可以从00-0xFF
Write_To_1306(X81_Value,SSD1306_CMD);
}
*/
/*
//显示128X64图像,图像数据格式,列行式,此函数在此工程中未用
void SSD1306_Show_12864img(unsigned char *dp)
{
unsigned char x,y; //列页计数器
for (y=0;y
{
SSD1306_Set_Pos(0,y);
for (x=0;x
{
Write_To_1306(*dp,SSD1306_DATA);
dp++;
}
}
}
*/
//显示XX16点阵,可以显示16X16或8X16点阵
//x,y:显示坐标 *dp 显示数据缓冲区指针 Size:文字大小 16:16X16 8:8X16)
void SSD1306_ShowX16(unsigned char x,unsigned char y,unsigned char *dp,unsigned char Size)
{
unsigned char i,j; //计数器,i计点阵的上下两部分,j计每部分的16byte
for (i=0;i<2;i++)
{
SSD1306_Set_Pos(x,y);
for (j=0;j<Size;j++)
{
Write_To_1306(*dp,SSD1306_DATA);
dp++;
}
y++; //上半部分分写完写下半部分
}
}
//显示0507点阵
void SSD1306_Show0507(unsigned char x,unsigned char y,unsigned char *dp)
{
unsigned char j;
SSD1306_Set_Pos(x,y);
for (j=0;j<7;j++)
{
Write_To_1306(*dp,SSD1306_DATA);
dp++;
}
}
GT20.H文件
#ifndef __GT20__H__
#define __GT20__H__
void ShowText(unsigned char x,unsigned char y,unsigned char *Text); //显示0816和1616汉字、字符
void Show5X7Text(unsigned char x,unsigned char y,unsigned char *Text); //显示0507字符
#endif
GT20.C文件
#include "reg52.h"
#include "gt20.h"
#include "SSD1306.h"
#include "spi.h"
//从字库读数据显示在OLED上
void ShowText(unsigned char x,unsigned char y,unsigned char *Text)
{
unsigned char i=0;
unsigned char HSB,MSB,LSB;
unsigned char Buffer[32];
unsigned long Font_Address;
while ((Text[i]>0x00)) //遇到就表示字符串结束
{
if (((Text[i]>=0xb0) && (Text[i]<=0xf7)) && (Text[i+1]>=0xa1)) //汉字
{
//Address = ((MSB - 0xB0) * 94 + (LSB - 0xA1)+ 846)*32+ BaseAdd; BaseAdd=0
Font_Address=(unsigned long)((Text[i]- 0xb0)*94+(Text[i+1]-0xa1)+846)*32;
HSB=(Font_Address&0xff0000)>>16;
MSB=(Font_Address&0xff00)>>8;
LSB=Font_Address&0xff;
Read_Nbyte_fromROM(HSB,MSB,LSB,Buffer,32); //从字库中读取点阵数据
SSD1306_ShowX16(x,y,Buffer,16); //将点阵数据显示到OLED上
i+=2; //一个汉字编码占两字节
x+=16;
} else if (((Text[i]>=0xa1) &&(Text[i]<=0xa3))&&(Text[i+1]>=0xa1)) //16X16字符
{
//Address = ((MSB - 0xa1) * 94 + (LSB - 0xA1))*32+ BaseAdd;BaseAdd=0
Font_Address=(unsigned long)((Text[i]- 0xa1)*94+(Text[i+1]-0xa1))*32;
HSB=(Font_Address&0xff0000)>>16;
MSB=(Font_Address&0xff00)>>8;
LSB=Font_Address&0xff;
Read_Nbyte_fromROM(HSB,MSB,LSB,Buffer,32); //从字库中读取点阵数据
SSD1306_ShowX16(x,y,Buffer,16); //将点阵数据显示到OLED上
i+=2;
x+=16;
} else if((Text[i]>=0x20) &&(Text[i]<=0x7e)) //8X16 ASCII码
{
//Address = ((MSB - 0x20) * 16 + 0X3CF80
Font_Address=(unsigned long)((Text[i]- 0x20)*16+0x3cf80);
HSB=(Font_Address&0xff0000)>>16;
MSB=(Font_Address&0xff00)>>8;
LSB=Font_Address&0xff;
Read_Nbyte_fromROM(HSB,MSB,LSB,Buffer,16); //从字库中读取点阵数据
SSD1306_ShowX16(x,y,Buffer,8); //将点阵数据显示到OLED上
i+=1;
x+=8;
} else
i++;
}
}
//从字加读取5X7数据显示在OLED上
void Show5X7Text(unsigned char x,unsigned char y,unsigned char *Text)
{
unsigned char i=0;
unsigned char HSB,MSB,LSB;
unsigned char Buffer[8];
unsigned long Font_Address;
while ((Text[i]>0x00)) //遇到就表示字符串结束
{
if ((Text[i]>=0x20) && (Text[i]<=0x7e))
{
//Address = ((MSB - 0x20) * 8 + 0X3bfc0
Font_Address=(unsigned long)((Text[i]- 0x20)*8+0x3bfc0);
HSB=(Font_Address&0xff0000)>>16;
MSB=(Font_Address&0xff00)>>8;
LSB=Font_Address&0xff;
Read_Nbyte_fromROM(HSB,MSB,LSB,Buffer,16); //从字库中读取点阵数据
SSD1306_Show0507(x,y,Buffer); //将点阵数据显示到OLED上
i+=1;
x+=6;
} else
i++;
}
}
PAJ7620.H文件
#ifndef __PAJ7620__H__
#define __PAJ7620__H__
#define PAJ7620_Add 0x73 //PAJ7620 7位地址
#define PAJ7620_wAdd ((PAJ7620_Add<<1)|0) //PAJ7620 8位写地址
//#define PAJ7620_rAdd Paj7620_Add<<1|1 //PAJ7620 8位读地址
#define PAJ7620_REGITER_BANK_SEL 0xEF //区块选择
#define PAJ7620_ADDR_GES_PS_DET_FLAG_0 0x43
#define PAJ7620_ADDR_GES_PS_DET_FLAG_1 0x44
#define PAJ7620_BANK0 0
#define PAJ7620_BANK1 1
#define RIGHT 1 //右
#define LEFT 2 //左
#define UP 4 //上
#define DOWN 8 //下
#define FORWARD 16 //前进
#define BACKWARD 32 //后退
#define CLOCKWISE 64 //顺时针
#define UNCLOCKWISE 128 //逆时针
#define WAVE 1 //挥手
#define INIT_REG_ARRAY_SIZE (sizeof(PAJ7620_init_Value)/sizeof(PAJ7620_init_Value[0]))
#define PAJ7620_REACTION_TIME 10
#define PAJ7620_ENTRY_TIME 20
#define PAJ7620_QUIT_TIME 50
void Delay_us(unsigned int us); //延时程序
void PAJ7620_Select_Bank(unsigned char bank); //选对7620寄存器区块
void PAJ7620_init(void); //7620初始化
#endif
PAJ7620.C文件
#include "reg52.h"
#include "PAJ7620.h"
#include "i2c.h"
#include "intrins.h"
void Delay_us(unsigned int us);
void PAJ7620_Select_Bank(unsigned char bk);
//寄存器初始化数据
unsigned char code PAJ7620_init_Value[][2] = {
{0xEF,0x00},{0x32,0x29},{0x33,0x01},{0x34,0x00},{0x35,0x01},{0x36,0x00},{0x37,0x07},
{0x38,0x17},{0x39,0x06},{0x3A,0x12},{0x3F,0x00},{0x40,0x02},{0x41,0xFF},{0x42,0x01},
{0x46,0x2D},{0x47,0x0F},{0x48,0x3C},{0x49,0x00},{0x4A,0x1E},{0x4B,0x00},{0x4C,0x20},
{0x4D,0x00},{0x4E,0x1A},{0x4F,0x14},{0x50,0x00},{0x51,0x10},{0x52,0x00},{0x5C,0x02},
{0x5D,0x00},{0x5E,0x10},{0x5F,0x3F},{0x60,0x27},{0x61,0x28},{0x62,0x00},{0x63,0x03},
{0x64,0xF7},{0x65,0x03},{0x66,0xD9},{0x67,0x03},{0x68,0x01},{0x69,0xC8},{0x6A,0x40},
{0x6D,0x04},{0x6E,0x00},{0x6F,0x00},{0x70,0x80},{0x71,0x00},{0x72,0x00},{0x73,0x00},
{0x74,0xF0},{0x75,0x00},{0x80,0x42},{0x81,0x44},{0x82,0x04},{0x83,0x20},{0x84,0x20},
{0x85,0x00},{0x86,0x10},{0x87,0x00},{0x88,0x05},{0x89,0x18},{0x8A,0x10},{0x8B,0x01},
{0x8C,0x37},{0x8D,0x00},{0x8E,0xF0},{0x8F,0x81},{0x90,0x06},{0x91,0x06},{0x92,0x1E},
{0x93,0x0D},{0x94,0x0A},{0x95,0x0A},{0x96,0x0C},{0x97,0x05},{0x98,0x0A},{0x99,0x41},
{0x9A,0x14},{0x9B,0x0A},{0x9C,0x3F},{0x9D,0x33},{0x9E,0xAE},{0x9F,0xF9},{0xA0,0x48},
{0xA1,0x13},{0xA2,0x10},{0xA3,0x08},{0xA4,0x30},{0xA5,0x19},{0xA6,0x10},{0xA7,0x08},
{0xA8,0x24},{0xA9,0x04},{0xAA,0x1E},{0xAB,0x1E},{0xCC,0x19},{0xCD,0x0B},{0xCE,0x13},
{0xCF,0x64},{0xD0,0x21},{0xD1,0x0F},{0xD2,0x88},{0xE0,0x01},{0xE1,0x04},{0xE2,0x41},
{0xE3,0xD6},{0xE4,0x00},{0xE5,0x0C},{0xE6,0x0A},{0xE7,0x00},{0xE8,0x00},{0xE9,0x00},
{0xEE,0x07},{0xEF,0x01},{0x00,0x1E},{0x01,0x1E},{0x02,0x0F},{0x03,0x10},{0x04,0x02},
{0x05,0x00},{0x06,0xB0},{0x07,0x04},{0x08,0x0D},{0x09,0x0E},{0x0A,0x9C},{0x0B,0x04},
{0x0C,0x05},{0x0D,0x0F},{0x0E,0x02},{0x0F,0x12},{0x10,0x02},{0x11,0x02},{0x12,0x00},
{0x13,0x01},{0x14,0x05},{0x15,0x07},{0x16,0x05},{0x17,0x07},{0x18,0x01},{0x19,0x04},
{0x1A,0x05},{0x1B,0x0C},{0x1C,0x2A},{0x1D,0x01},{0x1E,0x00},{0x21,0x00},{0x22,0x00},
{0x23,0x00},{0x25,0x01},{0x26,0x00},{0x27,0x39},{0x28,0x7F},{0x29,0x08},{0x30,0x03},
{0x31,0x00},{0x32,0x1A},{0x33,0x1A},{0x34,0x07},{0x35,0x07},{0x36,0x01},{0x37,0xFF},
{0x38,0x36},{0x39,0x07},{0x3A,0x00},{0x3E,0xFF},{0x3F,0x00},{0x40,0x77},{0x41,0x40},
{0x42,0x00},{0x43,0x30},{0x44,0xA0},{0x45,0x5C},{0x46,0x00},{0x47,0x00},{0x48,0x58},
{0x4A,0x1E},{0x4B,0x1E},{0x4C,0x00},{0x4D,0x00},{0x4E,0xA0},{0x4F,0x80},{0x50,0x00},
{0x51,0x00},{0x52,0x00},{0x53,0x00},{0x54,0x00},{0x57,0x80},{0x59,0x10},{0x5A,0x08},
{0x5B,0x94},{0x5C,0xE8},{0x5D,0x08},{0x5E,0x3D},{0x5F,0x99},{0x60,0x45},{0x61,0x40},
{0x63,0x2D},{0x64,0x02},{0x65,0x96},{0x66,0x00},{0x67,0x97},{0x68,0x01},{0x69,0xCD},
{0x6A,0x01},{0x6B,0xB0},{0x6C,0x04},{0x6D,0x2C},{0x6E,0x01},{0x6F,0x32},{0x71,0x00},
{0x72,0x01},{0x73,0x35},{0x74,0x00},{0x75,0x33},{0x76,0x31},{0x77,0x01},{0x7C,0x84},
{0x7D,0x03},{0x7E,0x01},
};
//延时程序
void Delay_us(unsigned int us)
{
while((us--)!= 0)
{
_nop_();
}
}
//选择寄存器区块
void PAJ7620_Select_Bank(unsigned char bk)
{
Write_One_Reg(PAJ7620_wAdd,PAJ7620_REGITER_BANK_SEL,bk);
}
//PAJ7620初始化
void PAJ7620_init(void)
{
unsigned int i; //计数器
Delay_us(1000); //延时等待
PAJ7620_Select_Bank(PAJ7620_BANK0); //选择寄存器0区
for (i=0;i<INIT_REG_ARRAY_SIZE;i++)
{
Write_One_Reg(PAJ7620_wAdd,PAJ7620_init_Value[i][0],PAJ7620_init_Value[i][1]); //写初始化参数
}
PAJ7620_Select_Bank(PAJ7620_BANK0); //选择寄存器0区
}
Main.c文件
#include "reg52.h"
#include "SSD1306.h"
#include "gt20.h"
#include "paj7620.h"
#include "i2c.h"
sbit LED=P2^1;
unsigned char PAJ7620_Value=0; //从PAJ7620获取的数据
unsigned char result_Code; //返回结果
//延时函数
void delay_ms(unsigned int n_ms)
{
int j,k;
for(j=0;j<n_ms;j++)
for(k=0;k<1002;k++);
}
//获取7620数据
void Get_7620_Value(void)
{
PAJ7620_Value=Read_One_Reg(PAJ7620_wAdd,PAJ7620_ADDR_GES_PS_DET_FLAG_0);
if (PAJ7620_Value!=0xFF)
{
switch (PAJ7620_Value)
{
case RIGHT:
delay_ms(PAJ7620_REACTION_TIME); //延时
PAJ7620_Value=Read_One_Reg(PAJ7620_wAdd,PAJ7620_ADDR_GES_PS_DET_FLAG_0);
if (PAJ7620_Value==FORWARD)
{
result_Code=5;
delay_ms(PAJ7620_QUIT_TIME);
}
else if(PAJ7620_Value==BACKWARD)
{
result_Code=6;
delay_ms(PAJ7620_QUIT_TIME);
}
else
{
result_Code=4;
delay_ms(PAJ7620_QUIT_TIME);
}
break;
case LEFT:
delay_ms(PAJ7620_REACTION_TIME); //延时
PAJ7620_Value=Read_One_Reg(PAJ7620_wAdd,PAJ7620_ADDR_GES_PS_DET_FLAG_0);
if (PAJ7620_Value==FORWARD)
{
result_Code=5;
delay_ms(PAJ7620_QUIT_TIME);
}
else if(PAJ7620_Value==BACKWARD)
{
result_Code=6;
delay_ms(PAJ7620_QUIT_TIME);
}
else
{
result_Code=3;
delay_ms(PAJ7620_QUIT_TIME);
}
break;
case UP:
delay_ms(PAJ7620_REACTION_TIME); //延时
PAJ7620_Value=Read_One_Reg(PAJ7620_wAdd,PAJ7620_ADDR_GES_PS_DET_FLAG_0);
if (PAJ7620_Value==FORWARD)
{
result_Code=5;
delay_ms(PAJ7620_QUIT_TIME);
}
else if(PAJ7620_Value==BACKWARD)
{
result_Code=6;
delay_ms(PAJ7620_QUIT_TIME);
}
else
{
result_Code=1;
delay_ms(PAJ7620_QUIT_TIME);
}
break;
case DOWN:
delay_ms(PAJ7620_REACTION_TIME); //延时
PAJ7620_Value=Read_One_Reg(PAJ7620_wAdd,PAJ7620_ADDR_GES_PS_DET_FLAG_0);
if (PAJ7620_Value==FORWARD)
{
result_Code=5;
delay_ms(PAJ7620_QUIT_TIME);
}
else if(PAJ7620_Value==BACKWARD)
{
result_Code=6;
delay_ms(PAJ7620_QUIT_TIME);
}
else
{
result_Code=2;
delay_ms(PAJ7620_QUIT_TIME);
}
break;
case FORWARD:
result_Code=5;
delay_ms(PAJ7620_QUIT_TIME);
break;
case BACKWARD:
result_Code=6;
delay_ms(PAJ7620_QUIT_TIME);
break;
case CLOCKWISE:
result_Code=7;
delay_ms(PAJ7620_QUIT_TIME);
break;
case UNCLOCKWISE:
result_Code=8;
delay_ms(PAJ7620_QUIT_TIME);
break;
default:
delay_ms(PAJ7620_REACTION_TIME); //延时
PAJ7620_Value=Read_One_Reg(PAJ7620_wAdd,PAJ7620_ADDR_GES_PS_DET_FLAG_1);
if (PAJ7620_Value==WAVE)
{
result_Code=9;
} else
result_Code=0;
delay_ms(PAJ7620_QUIT_TIME);
break;
}
}
switch (result_Code)
{
case 1:
ShowText(82,6,"上 ");
break;
case 2:
ShowText(82,6,"下 ");
break;
case 3:
ShowText(82,6,"左 ");
break;
case 4:
ShowText(82,6,"右 ");
break;
case 5:
ShowText(82,6,"前进 ");
break;
case 6:
ShowText(82,6,"后退 ");
break;
case 7:
ShowText(82,6,"顺时针");
break;
case 8:
ShowText(82,6,"逆时针");
break;
case 9:
ShowText(82,6,"挥动 ");
break;
}
}
void main (void)
{
SSD1306_init(); //OLED初始化
ShowText(16,0,"手势识别程序");
ShowText(0,3,"状态:");
ShowText(48,3,"开机中");
ShowText(0,6,"识别结果:");
delay_ms(1000); //延时函数
PAJ7620_init(); //7620初始化
delay_ms(200); //延时
ShowText(48,3,"准备就绪");
while(1)
{
Get_7620_Value(); //获取7620数据
LED=~LED;
}
}