需要你仔细看器件的SPI时序图
我给你来个程序试试
- #include <stc15.h> //本例程为SSD1329驱动芯片的96x96OLED屏
- sbit RSDC=P1^0; //即D/C 0是command 1是data
- sbit CS=P1^1; //片选 低有效
- struct hanzi // 汉字字模数据结构
- {
- unsigned char index[2]; // 汉字内码索引
- unsigned char msk[32]; // 点阵码数据
- };
- struct hanzi code hz[]=
- {
- "床", 0x01,0x00,0x00,0x84,0x3F,0xFE,0x20,0x80,
- 0x20,0x80,0x20,0x88,0x3F,0xFC,0x20,0x80,
- 0x21,0xC0,0x21,0xA0,0x22,0x90,0x24,0x88,
- 0x48,0x8E,0x50,0x84,0x80,0x80,0x00,0x80,
- "前", 0x10,0x10,0x0C,0x30,0x04,0x44,0xFF,0xFE,
- 0x00,0x00,0x3E,0x08,0x22,0x48,0x22,0x48,
- 0x3E,0x48,0x22,0x48,0x22,0x48,0x3E,0x48,
- 0x22,0x08,0x22,0x08,0x2A,0x28,0x24,0x10,
- "明", 0x00,0x04,0x04,0xFE,0x7E,0x84,0x44,0x84,
- 0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,
- 0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,
- 0x01,0x04,0x01,0x04,0x02,0x14,0x04,0x08,
- "月", 0x00,0x10,0x0F,0xF8,0x08,0x10,0x08,0x10,
- 0x08,0x10,0x0F,0xF0,0x08,0x10,0x08,0x10,
- 0x08,0x10,0x0F,0xF0,0x08,0x10,0x08,0x10,
- 0x10,0x10,0x10,0x10,0x20,0x50,0x40,0x20,
- "光", 0x01,0x00,0x21,0x08,0x11,0x0C,0x09,0x10,
- 0x09,0x20,0x01,0x04,0xFF,0xFE,0x04,0x40,
- 0x04,0x40,0x04,0x40,0x04,0x40,0x08,0x40,
- 0x08,0x42,0x10,0x42,0x20,0x3E,0x40,0x00,
- "疑", 0x20,0x00,0x26,0xFC,0x38,0x08,0x22,0x50,
- 0x1E,0x20,0x21,0xFE,0x3E,0x22,0x48,0x24,
- 0x08,0xA0,0xFE,0xB8,0x08,0xA0,0x08,0xA0,
- 0x14,0xE0,0x22,0xA0,0x41,0x1E,0x02,0x04,
- "是", 0x0F,0xE0,0x08,0x20,0x08,0x20,0x0F,0xE0,
- 0x08,0x20,0x08,0x20,0x0F,0xE0,0x00,0x04,
- 0xFF,0xFE,0x01,0x00,0x09,0x20,0x09,0xF0,
- 0x09,0x00,0x15,0x00,0x23,0x06,0x40,0xFC,
- "地", 0x00,0x40,0x20,0x40,0x22,0x40,0x22,0x40,
- 0x22,0x48,0x22,0x7C,0xFB,0xC8,0x26,0x48,
- 0x22,0x48,0x22,0x48,0x22,0x68,0x22,0x50,
- 0x3A,0x42,0xE2,0x02,0x41,0xFE,0x00,0x00,
- "上", 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
- 0x01,0x10,0x01,0xF8,0x01,0x00,0x01,0x00,
- 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
- 0x01,0x00,0x01,0x04,0xFF,0xFE,0x00,0x00,
- "霜", 0x1F,0xF0,0x01,0x00,0x7F,0xFE,0x41,0x02,
- 0x9D,0x74,0x01,0x00,0x1D,0x70,0x08,0x04,
- 0x08,0xFE,0x7E,0x84,0x08,0xFC,0x1C,0x84,
- 0x2A,0xFC,0x48,0x84,0x08,0xFC,0x08,0x84,
- "举", 0x22,0x08,0x19,0x18,0x09,0x20,0x00,0x44,
- 0xFF,0xFE,0x04,0x40,0x09,0x20,0x11,0x10,
- 0x2F,0xEE,0xC1,0x04,0x01,0x10,0x3F,0xF8,
- 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
- "头", 0x00,0x80,0x00,0x80,0x08,0x80,0x06,0x80,
- 0x22,0x80,0x18,0x80,0x08,0x80,0x00,0x84,
- 0xFF,0xFE,0x00,0x80,0x01,0x00,0x01,0x60,
- 0x02,0x10,0x04,0x08,0x18,0x0C,0x60,0x04,
- "望", 0x10,0x04,0x0A,0x7E,0xFF,0x44,0x20,0x7C,
- 0x20,0x44,0x26,0x7C,0x38,0x44,0x20,0x8C,
- 0x00,0x00,0x3F,0xF8,0x01,0x00,0x1F,0xF0,
- 0x01,0x00,0x01,0x04,0xFF,0xFE,0x00,0x00,
- "低", 0x10,0x08,0x10,0x3C,0x17,0xC0,0x24,0x40,
- 0x24,0x40,0x64,0x40,0xA4,0x44,0x27,0xFE,
- 0x24,0x20,0x24,0x20,0x24,0x20,0x24,0x90,
- 0x25,0x12,0x26,0x4A,0x24,0x2A,0x20,0x04,
- "思", 0x00,0x08,0x3F,0xFC,0x21,0x08,0x21,0x08,
- 0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,
- 0x21,0x08,0x3F,0xF8,0x20,0x08,0x02,0x00,
- 0x51,0x88,0x50,0x96,0x90,0x12,0x0F,0xF0,
- "故", 0x10,0x40,0x10,0x40,0x10,0x40,0x14,0x44,
- 0xFE,0xFE,0x11,0x08,0x12,0x88,0x10,0x88,
- 0x7C,0x88,0x44,0x88,0x44,0x50,0x44,0x20,
- 0x44,0x20,0x7C,0x50,0x44,0x8E,0x03,0x04,
- "乡", 0x02,0x00,0x02,0x00,0x04,0x20,0x08,0x20,
- 0x10,0x40,0x3F,0x80,0x01,0x00,0x02,0x08,
- 0x04,0x08,0x3F,0xF0,0x00,0x20,0x00,0x40,
- 0x00,0x80,0x03,0x00,0x0C,0x00,0x70,0x00,
- "静", 0x08,0x40,0x08,0x40,0x7E,0xF8,0x09,0x10,
- 0x3E,0x24,0x08,0xFE,0xFF,0x24,0x00,0x24,
- 0x3F,0xFE,0x22,0x24,0x3E,0x24,0x22,0xFC,
- 0x3E,0x24,0x22,0x20,0x22,0xA0,0x26,0x40,
- "夜", 0x02,0x00,0x01,0x04,0xFF,0xFE,0x08,0x80,
- 0x08,0x80,0x10,0xF8,0x11,0x08,0x32,0x88,
- 0x56,0x50,0x99,0x10,0x10,0xA0,0x10,0x40,
- 0x10,0xA0,0x11,0x10,0x12,0x0E,0x1C,0x04,
- "唐", 0x01,0x00,0x00,0x88,0x3F,0xFC,0x20,0x80,
- 0x2F,0xF8,0x20,0x88,0x3F,0xFE,0x20,0x88,
- 0x2F,0xF8,0x20,0x80,0x2F,0xF8,0x28,0x08,
- 0x48,0x08,0x48,0x08,0x8F,0xF8,0x08,0x08,
- "李", 0x01,0x00,0x01,0x08,0x7F,0xFC,0x01,0x00,
- 0x05,0x40,0x09,0x30,0x31,0x0E,0xC1,0x04,
- 0x0F,0xE0,0x00,0x40,0x01,0x88,0x7F,0xFC,
- 0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,
- "白", 0x02,0x00,0x04,0x00,0x08,0x08,0x3F,0xFC,
- 0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,
- 0x3F,0xF8,0x20,0x08,0x20,0x08,0x20,0x08,
- 0x20,0x08,0x3F,0xF8,0x20,0x08,0x00,0x00,
- "。", 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x3C,0x00,0x66,0x00,
- 0x66,0x00,0x66,0x00,0x3C,0x00,0x00,0x00,
- ",", 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x0C,0x00,0x1E,0x00,0x1E,0x00,0x0C,0x00,
- 0x04,0x00,0x08,0x00,0x10,0x00,0x00,0x00
- };
- void init_oled(); //屏初始化,各种datasheet的设置,官方参考数据
- void WriteComm(unsigned char com); //单片机写命令
- void WriteData(unsigned char dat); //单片机写数据
- void init_SPI(); //单片机SPI初始化
- void Delayms(unsigned int t);
- void sawhz(unsigned char x,unsigned char y,unsigned char c[2]); //设置起止坐标并写汉字数据
- void clear(); //清屏
- void write(unsigned dat); //最终写data函数,最高亮度,由4bit变1bit
- //因为本驱动是一个Byte的其中4位控制一个led像素点,共16种灰度模式,1111为亮 0000为熄灭
- //比如一个字节的低四位是D0像素,高四位是D1像素,依次类推
- //void writehz(unsigned char c[2]);
- unsigned char m;
- unsigned int i;
- void main()
- {
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- P6M0 = 0x00;
- P6M1 = 0x00;
- P7M0 = 0x00;
- P7M1 = 0x00; //全设置为准双向口
- init_SPI();
- init_oled();
- clear();
- sawhz(12,0,"静");
- sawhz(20,0,"夜");
- sawhz(28,0,"思");
- sawhz(8,16,"唐");
- sawhz(20,16,"李");
- sawhz(28,16,"白");
- sawhz(0,32,"床");
- sawhz(8,32,"前");
- sawhz(16,32,"明");
- sawhz(24,32,"月");
- sawhz(32,32,"光");
- sawhz(40,32,",");
- sawhz(0,48,"疑");
- sawhz(8,48,"是");
- sawhz(16,48,"地");
- sawhz(24,48,"上");
- sawhz(32,48,"霜");
- sawhz(40,48,"。");
- sawhz(0,64,"举");
- sawhz(8,64,"头");
- sawhz(16,64,"望");
- sawhz(24,64,"明");
- sawhz(32,64,"月");
- sawhz(40,64,",");
- sawhz(0,80,"低");
- sawhz(8,80,"头");
- sawhz(16,80,"思");
- sawhz(24,80,"故");
- sawhz(32,80,"乡");
- sawhz(40,80,"。");
- while(1);
- }
- void clear() //清屏
- {
- for(i=4608;i>0;i--)
- WriteData(0x00);
- }
- /*
- void writehz(unsigned char c[2])
- {
- unsigned char i,k;
- for(k=0;k<23;k++) //23 代表字库中的个数 轮询方式查询
- {
- if((hz[k].index[0]==c[0])&&(hz[k].index[1]==c[1])) //数据结构查询
- {
- for(i=0;i<32;i++)
- write(hz[k].msk);
- }
- }
- }
- */
- void sawhz(unsigned char x,unsigned char y,unsigned char c[2]) //setxy and writehz 头字母saw 设置起止坐标并写汉字
- {
- unsigned char i,k;
- WriteComm(0x15);//SET COLUMN ADDR
- WriteComm(0x10+x);//第 16+x 列开始 即本屏幕的第一列
- WriteComm(0x10+x+7); //第16+x+3列结束
- WriteComm(0x75);//SET ROW ADDR
- WriteComm(0x00+y);//第 0+y 行开始
- WriteComm(0x00+y+15);//第0+y+15行结束 共16行
- for(k=0;k<25;k++)
- {
- if((hz[k].index[0]==c[0])&&(hz[k].index[1]==c[1]))
- {
- for(i=0;i<32;i++)
- write(hz[k].msk);
- }
- }
- }
- void Delayms(unsigned int t) //stc15@11.0592 1ms延时
- {
- unsigned int a,b;
- for(a=t;a>0;a--)
- for(b=850;b>0;b--);
- }
- void WriteComm(unsigned char com) //单片机写命令SPI函数
- {
- RSDC=0;
- SPSTAT=0xc0; //SPIstat clear
- SPDAT=com; //upper eight bits
- while((SPSTAT&0x80)==0); //wait for transfer finished
- SPSTAT=0xc0; //SPIstat clear
- RSDC=1;
- }
- void WriteData(unsigned char dat) //单片机写data,SPI函数
- {
- RSDC=1;
- SPSTAT=0xc0; //SPIstat clear
- SPDAT=dat; //upper eight bits
- while((SPSTAT&0x80)==0); //wait for transfer finished
- SPSTAT=0xc0; //SPIstat clear
- RSDC=0;
- }
- void write(unsigned dat) //最终写data函数,最高亮度,由4bit变1bit
- {
- unsigned char n;
- for(n=0;n<4;n++)
- {
- if((dat&0xc0)==0xc0) //与1100 0000与,留高两位
- WriteData(0xff);
- else if((dat&0xc0)==0x80)
- WriteData(0xf0);
- else if((dat&0xc0)==0x40)
- WriteData(0x0f);
- else
- WriteData(0x00);
- dat<<=2;
- }
- }
- void init_SPI()
- {
- SPDAT=0;
- SPSTAT=0xc0; //SPIstat clear
- SPCTL=0xdc; //master dc高到低 f8低到高 SSIG=1 SPEN=1 MSTR =1 CPOL=1 CPHA=1
- }
- void init_oled()
- {
- CS=0;
- Delayms(25);
- WriteComm(0x15);//SET COLUMN ADDR
- WriteComm(0x10);//第16列开始
- WriteComm(0x3f); //第63列结束 共(63-16+1)*2=96列
- WriteComm(0x75);//SET ROW ADDR
- WriteComm(0x00);//第0行开始
- WriteComm(0x5f);//第95行结束 共96行
- WriteComm(0xA0);//SET RE-MAP
- WriteComm(0x51);
- WriteComm(0xA1);//SET DISPLAY START LINE
- WriteComm(0x00);
- WriteComm(0xA2);//SET DISPLAY OFFSET
- WriteComm(0x60);
- WriteComm(0xA4);//SET DISPLAY MODE
- WriteComm(0xA8);//SET MUX RADIO
- WriteComm(0x5F);
- WriteComm(0xB2);//SET FRAME FREQUENCY
- WriteComm(0x23);
- WriteComm(0xB3);//SET FRONT CLOCK DIVIDER & OSCILLATOR FREQUENCY
- WriteComm(0xF0);
- WriteComm(0x81);//SET CONTRAST CURRENT
- WriteComm(0x7F);//
- WriteComm(0xBC); //first pre_charge voltage
- WriteComm(0x1F);
- Delayms(10);
- WriteComm(0x82); //second pre_charge speed
- WriteComm(0xFE);
- Delayms(10);
- WriteComm(0xB1); //first pre_charge phase length
- WriteComm(0x21);
- WriteComm(0xBB);//SET SECONDE PRE_CHARGE PERIOD
- WriteComm(0x0F);
- WriteComm(0xbe);//SET VCOMH
- WriteComm(0x1F);
- WriteComm(0xB8); //SET GS
- WriteComm(0x04); //GS1
- WriteComm(0x06); //GS2
- WriteComm(0x08); //GS3
- WriteComm(0x0A); //GS4
- WriteComm(0x0C); //GS5
- WriteComm(0x0E); //GS6
- WriteComm(0x10); //GS7
- WriteComm(0x12); //GS8
- WriteComm(0x14); //GS9
- WriteComm(0x16); //GS10
- WriteComm(0x18); //GS11
- WriteComm(0x1A); //GS12
- WriteComm(0x1C); //GS13
- WriteComm(0x1E); //GS14
- WriteComm(0x20); //GS15
- Delayms(10);
- WriteComm(0xAF); //DSPLAY ON
- }
复制代码 |