本帖最后由 384998430 于 2016-12-16 11:28 编辑
使用GPIO模拟SPI是单片机开发中常用的一种手段,GPIO还可以模拟IIC等总线的时序,可以用在没有相应外设的处理器上,节省成本。 下面的代码是软模SPI写LCD5110的程序:
- void WriteByte(unsigned char byte,unsigned char dc)
- {
- int i;
- CE(0);
- DC(dc);
- for(i=0;i<8;i++)
- {
- if(byte & 0x80)
- DIN(1);
- else
- DIN(0);
- CLK(0);
- // usleep(10);//LCD5110的响应速度很快,不加延时也行!!!
- CLK(1);
- // usleep(10);
- byte <<= 1;
- }
- CE(1);
- }
复制代码
下面的代码是刷新LCD的两个函数,第一个是使用84*48点阵的数组,第二个是使用84*6点阵的数组:
- int LCD_Buffer[48 * 84] =
- { 0x00,};
- void LCD_Refresh()
- {
- unsigned int i,j,k;
- unsigned char data;
- WriteByte(0x40,0);
- WriteByte(0x80,0);
- for(i=0;i<6;i++)
- {
- for(j=0;j<84;j++)
- {
- for(k=0;k<8;k++)
- {
- data >>= 1;
- if( LCD_Buffer[ ( (i<<3) + k ) * 84 + j ] )
- data |= 0x80;
- }
- // printf("%X ",data);
- WriteByte(data,1);
- }
- // printf("rn");
- }
- }
-
- unsigned char nBitmapDot[] = // 数据表
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xE0,0xE0,
- 0xE0,0xE0,0xE0,0xE0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x06,0x0F,0x0F,0x07,0x0F,0x0E,0x1E,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
- 0x01,0x01,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xFF,0x7F,0x1F,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x3C,0x7E,0x7C,0x70,0x70,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xE0,
- 0xC0,0x30,0x78,0x78,0x78,0x7C,0x7E,0x7F,0x77,0x77,0xF3,0xF1,0xF0,0xF0,0x70,0x70,
- 0x70,0xF0,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0xF0,0xE0,0xE0,0xC0,0x80,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,
- 0xFF,0xFF,0x7F,0x01,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF8,0xFF,0x3F,0x0F,
- 0x03,0x03,0x03,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x01,0x01,0x07,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0xC0,0xE0,
- 0xF8,0x7E,0x3F,0x1F,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x07,0x07,0x03,
- 0x01,0x80,0xC0,0xE0,0xF0,0xF8,0x7C,0x3E,0x0F,0x8F,0xC7,0x80,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,
- 0x03,0x03,0x03,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x01,0x00,0x00,0x00,0x00,0x07,0x0F,0x0F,
- 0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0F,0x07,0x07,0x07,0x07,0x03,0x03,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- };
- void LCD_Refresh2(unsigned char *buff)
- {
- unsigned int i,j;
- WriteByte(0x40,0);
- WriteByte(0x80,0);
- // for(i=0;i<6;i++)
- // {
- // for(j=0;j<84;j++)
- // {
- // WriteByte(buff[i*84 + j],1);
- // }
- // }
- for(i=0;i<6*84;i++)
- {
- WriteByte(buff[i],1);
- }
- }
复制代码
初始化GPIO以及LCD5110代码:
- GPIO_Init();
-
- GPIO_ConfigPin(PC,0,OUT); //CLK
- GPIO_ConfigPin(PC,1,OUT); //DC
- GPIO_ConfigPin(PC,2,OUT); //DIN
- GPIO_ConfigPin(PC,3,OUT); //CE
-
- GPIO_ConfigPin(PA,15,OUT); //LED
-
- while(i--)
- {
- GPIO_SetPin(PA,15,1);
- usleep(50000);
- GPIO_SetPin(PA,15,0);
- usleep(50000);
- }
-
- unsigned int data;
-
- WriteByte(0x21,0);
- WriteByte(0xc8,0);
- WriteByte(0x06,0);
- WriteByte(0x13,0);
- WriteByte(0x20,0);
- WriteByte(0x0c,0);
-
- WriteByte(0x40,0);
- WriteByte(0x80,0);
复制代码
初始化LCD之后调用LCD_Refresh或者LCD_Refresh2即可看到图片显示。
制作的简单的显示gif图片的视频连接:http://v.youku.com/v_show/id_XMTgyMzI5NzI0MA==.html?spm=a2hzp.%208253869%20.0.0.IIMz9D&from=y1.7-2
|