本帖最后由 分立元件 于 2020-9-3 13:31 编辑
搞了好多天都不行,不知道哪里错了,现在X轴正常,Y轴定位不对,写上,就显示下,反过来了,还有屏幕最下面不能显示
单片机源程序如下:
#include<reg51.h>
#include<absacc.h>
#include<intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
//液晶屏引脚
sbit cs =P1^0;//片选
sbit reset =P1^1;//复位
sbit rs =P1^2;//地址/数据切换
sbit sda =P1^3;// SDI MOSI
sbit scl =P1^4;//时钟
//SDO引脚可以不连接
//触摸屏引脚
sbit clk=P0^4;
sbit cs1=P0^3;
sbit din=P0^2;
sbit out=P0^1;
sbit a=P0^0;
uchar bdata bitdata;
sbit bit7=bitdata^7;
sbit bit6=bitdata^6;
sbit bit5=bitdata^5;
sbit bit4=bitdata^4;
sbit bit3=bitdata^3;
sbit bit2=bitdata^2;
sbit bit1=bitdata^1;
sbit bit0=bitdata^0;
uint shuju;
void delay(uint time)
{
uint i;
for(i=0;i<time;i++);
}
void write_command(uchar c)
{
cs=0;
rs=0;
bitdata=c;
sda=bit7;scl=0;scl=1;
sda=bit6;scl=0;scl=1;
sda=bit5;scl=0;scl=1;
sda=bit4;scl=0;scl=1;
sda=bit3;scl=0;scl=1;
sda=bit2;scl=0;scl=1;
sda=bit1;scl=0;scl=1;
sda=bit0;scl=0;scl=1;
cs=1;
}
void write_data(uchar d)
{
cs=0;
rs=1;
bitdata=d;
sda=bit7;scl=0;scl=1;
sda=bit6;scl=0;scl=1;
sda=bit5;scl=0;scl=1;
sda=bit4;scl=0;scl=1;
sda=bit3;scl=0;scl=1;
sda=bit2;scl=0;scl=1;
sda=bit1;scl=0;scl=1;
sda=bit0;scl=0;scl=1;
cs=1;
}
void wr_com16(unsigned char i,unsigned char j)
{
cs=0;
rs=1;
bitdata=i;
sda=bit7;scl=0;scl=1;
sda=bit6;scl=0;scl=1;
sda=bit5;scl=0;scl=1;
sda=bit4;scl=0;scl=1;
sda=bit3;scl=0;scl=1;
sda=bit2;scl=0;scl=1;
sda=bit1;scl=0;scl=1;
sda=bit0;scl=0;scl=1;
cs=1;
cs=0;
rs=1;
bitdata=j;
sda=bit7;scl=0;scl=1;
sda=bit6;scl=0;scl=1;
sda=bit5;scl=0;scl=1;
sda=bit4;scl=0;scl=1;
sda=bit3;scl=0;scl=1;
sda=bit2;scl=0;scl=1;
sda=bit1;scl=0;scl=1;
sda=bit0;scl=0;scl=1;
cs=1;
}
//////////////////////////////////////////////////////////////////////////////////////////////
void lcd_initial()
{
reset=0;
delay(100);
reset=1;
delay(100);
write_command(0x11);
write_data(0x00);
write_command(0xCF);
write_data(0X00);
write_data(0XC1);
write_data(0X30);
write_command(0xED);
write_data(0X64);
write_data(0X03);
write_data(0X12);
write_data(0X81);
write_command(0xE8);
write_data(0X85);
write_data(0X11);
write_data(0X78);
write_command(0xF6);
write_data(0X01);
write_data(0X30);
write_data(0X00);
write_command(0xCB);
write_data(0X39);
write_data(0X2C);
write_data(0X00);
write_data(0X34);
write_data(0X05);
write_command(0xF7);
write_data(0X20);
write_command(0xEA);
write_data(0X00);
write_data(0X00);
write_command(0xC0);
write_data(0X20);
write_command(0xC1);
write_data(0X11);
write_command(0xC5);
write_data(0X31);
write_data(0X3C);
write_command(0xC7);
write_data(0XA9);
write_command(0x3A);
write_data(0X55);
write_command(0x36);
write_data(0x48);//竖屏参数
write_command(0xB1);
write_data(0X00);
write_data(0X18);
write_command(0xB4);
write_data(0X00);
write_data(0X00);
write_command(0xF2);
write_data(0X00);
write_command(0x26);
write_data(0X01);
write_command(0xE0);
write_data(0X0F);
write_data(0X17);
write_data(0X14);
write_data(0X09);
write_data(0X0C);
write_data(0X06);
write_data(0X43);
write_data(0X75);
write_data(0X36);
write_data(0X08);
write_data(0X13);
write_data(0X05);
write_data(0X10);
write_data(0X0B);
write_data(0X08);
write_command(0xE1);
write_data(0X00);
write_data(0X1F);
write_data(0X23);
write_data(0X03);
write_data(0X0E);
write_data(0X04);
write_data(0X39);
write_data(0X25);
write_data(0X4D);
write_data(0X06);
write_data(0X0D);
write_data(0X0B);
write_data(0X33);
write_data(0X37);
write_data(0X0F);
write_command(0x29);
}
void addset(unsigned int x,unsigned int y)
{
write_command(0x2a);
wr_com16(0,x);
write_command(0x2b);
wr_com16(0,y);
write_command(0x2c);
}
void dsp_single_colour(DH,DL)
{
unsigned int i;
for (i=0;i<1;i++)
{
wr_com16(DH,DL);
}
}
//触摸屏程序
void write(uchar DAT) //写XY坐标
{
uchar i;
for(i=0;i<8;i++)
{
if(DAT&0x80)
din=1;
else din=0;
DAT<<=1;
clk=0;
clk=1;
}
}
uint read() //读XY坐标数据
{
uint date=0;
uchar i;
clk=0;
for(i=0;i<12;i++)
{
date<<=1;
clk=1;
clk=0;
date|=out;
}
return date;
}
uint ad_read(uchar cmd)
{
char i;
clk=0;
cs1=0;
write(cmd);
for(i=6;i>0;i--);
clk=1;
clk=0;
shuju=read();
cs1=1;
return shuju;
}
main()
{ uint aa,bb,x,y;
lcd_initial(); //液晶屏初始化
aa=0;
bb=0;
while(1)
{
if(a==0) //有触摸低电平
{
// delay(1);
ad_read(0xd0); //读x坐标指令
aa=shuju; //x坐标数据给aa
ad_read(0x90); //读y坐标指令
bb=shuju; //y坐标数据给bb
x=aa/(4095/240);//触摸和液晶坐标转换
y=bb/(4095/320);
addset(x,y); //液晶屏坐标
dsp_single_colour(0xFF,0xFF); //显示白色定位点
}
}
} |