老师您好:
程序输入到芯片里后触摸屏程序正常,但是peizhi()子程序的aa2乱码,加不了
减不了。请老师指点一下,看那有错误。
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
uint x;
uint y;
uchar SEG2[16]={0x30,0x31,0x32,0x33,
0x34,0x35,0x36,0x37,0x38,0x39,0x41,
0x42,0x43,0x44,0x45,0x46};
uchar ab1,ab2,ab3,ab4,ab5,ab6;
uchar ac1,ac2,ac3,ac4,ac5,ac6;
uchar ad1,ad2,ad3,ad4;
uchar ae1,ae2,ae3,ae4;
uint aa2,aa4;
uint xl,xh;
uint yl,yh;
//-------------------------
void delay(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
//-------------------------------
void smalldelay(unsigned char tmp)
{
unsigned char i;
while(tmp--)
{
for(i = 0; i < 250; i++)
{
NOP();
}
}
}
//===================================
void chushihua() //LCD
{
DDRA=0XFF;
DDRD|=BIT(4)|BIT(5)|BIT(6);
PORTD&=~BIT(6);
}
//--------------------------
void spiinit(void) //7843
{
DDRB|=BIT(PB4)|BIT(PB5)|BIT(PB7); // MOSI and SCK port out
DDRB&=~BIT(PB6);
PORTB|=BIT(PB6);
PORTB|=BIT(PB4);
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (0 << CPOL)| (0 << CPHA)| (1 << SPR1)| (1 << SPR0); // enable spi,master mode, MCLK/4,spi 0 mode
}
//-----------------------------
/*
void portini(void)
{
//spiinit();
//sendspi();
DDRD &= ~(1 << PD3); // port input
PORTD |= (1 << PD3); // pull-up resistance
//MCUCR |= 1<<ISC11; // down edge enable
GICR |= 1<<INT1; // extern interrupt 1 enable
标志寄存器touch = 0;
}*/
//---------------------------
void chushihua1()
{
write_com(0X38);
delay(1);
write_com(0X01);
delay(1);
write_com(0X0C);
delay(1);
write_com(0X06);
delay(1);
}
//-------------------------
void sendspi(unsigned char data)
{
PORTB&=~BIT(PB4);
SPDR = data; // send data
while( !(SPSR & (1 << SPIF)) ); // wait data transfer end
//PORTB|=BIT(PB4);
}
//--------------------------
unsigned char readspi(void)
{
//PORTB&=~BIT(PB4);
SPSR = 0x00;
while( !(SPSR & (1 << SPIF)) );
return SPDR;
PORTB|=BIT(PB4);
}
//--------------------------
void keydown()
{
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (0 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
sendspi(0x9B);
smalldelay(20);
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (1 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
xl = readspi();
smalldelay(20);
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (0 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
sendspi(0x9B);
smalldelay(20);
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (1 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
xh = readspi();
smalldelay(20);
//x = xl+xh*256;
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (0 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
sendspi(0xDB);
smalldelay(20);
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (1 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
yl = readspi();
smalldelay(20);
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (0 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
sendspi(0xDB);
smalldelay(20);
SPCR = (1 << SPE) | (0 << DORD)| (1 << MSTR) | (1 << CPOL)| (0 << CPHA)| (0 << SPR1)| (1 << SPR0);
yh = readspi();
smalldelay(20);
//y = yl+yh*256;
}
//===========================
void write_com(uchar com)
{
PORTD&=~BIT(4);
PORTD&=~BIT(5);
PORTA=com;
PORTD|=BIT(6);
delay(1);
PORTD&=~BIT(6);
}
//--------------------------
void write_dat(uchar dat)
{
PORTD|=BIT(4);
PORTD&=~BIT(5);
PORTA=dat;
PORTD|=BIT(6);
delay(1);
PORTD&=~BIT(6);
}
//--------------------------
void zhuanhuan()
{ uint aa1,aa2,aa3,aa4;
aa1=xl;
aa2=xh;
ab1=aa1/100;
ab2=(aa1%100)/10;
ab3=aa1%10;
ab4=aa2/100;
ab5=(aa2%100)/10;
ab6=aa2%10;
aa3=yl;
aa4=yh;
ac1=aa3/100;
ac2=(aa3%100)/10;
ac3=aa3%10;
ac4=aa4/100;
ac5=(aa4%100)/10;
ac6=aa4%10;
ad1 = aa2/1000;
ad2 = (aa2%1000)/100;
ad3 = (aa2%100)/10;
ad4 = aa2%10;
ae1 = aa4/1000;
ae2 = (aa4%1000)/100;
ae3 = (aa4%100)/10;
ae4 = aa4%10;
}
//--------------------------
/*
void zhuanhuan()
{
uint aa1,aa2,aa3,aa4;
//aa1 = positionxl;
aa1 = x;
ab1 = aa1/100000;
ab2 = (aa1%100000)/10000;
ab3 = (aa1%10000)/1000;
ab4 = (aa1%1000)/100;
ab5 = (aa1%100)/10;
ab6 = aa1%10;
//aa3 = positionyl;
aa3 = y;
ac1 = aa3/100000;
ac2 = (aa3%100000)/10000;
ac3 = (aa3%10000)/1000;
ac4 = (aa3%1000)/100;
ac5 = (aa3%100)/10;
ac6 = aa3%10;
ad1 = aa2/1000;
ad2 = (aa2%1000)/100;
ad3 = (aa2%100)/10;
ad4 = aa2%10;
ae1 = aa4/1000;
ae2 = (aa4%1000)/100;
ae3 = (aa4%100)/10;
ae4 = aa4%10;
} */
//--------------------------
void xianshi()
{
uchar i;
write_com(0X80);
delay(1);
write_dat(SEG2[ab1]);
delay(1);
write_dat(SEG2[ab2]);
delay(1);
write_dat(SEG2[ab3]);
delay(1);
write_dat(SEG2[ab4]);
delay(1);
write_dat(SEG2[ab5]);
delay(1);
write_dat(SEG2[ab6]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[ad1]);
delay(1);
write_dat(SEG2[ad2]);
delay(1);
write_dat(SEG2[ad3]);
delay(1);
write_dat(SEG2[ad4]);
delay(1);
write_com(0X90);
delay(1);
write_dat(SEG2[ac1]);
delay(1);
write_dat(SEG2[ac2]);
delay(1);
write_dat(SEG2[ac3]);
delay(1);
write_dat(SEG2[ac4]);
delay(1);
write_dat(SEG2[ac5]);
delay(1);
write_dat(SEG2[ac6]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[ae1]);
delay(1);
write_dat(SEG2[ae2]);
delay(1);
write_dat(SEG2[ae3]);
delay(1);
write_dat(SEG2[ae4]);
delay(1);
write_com(0X88);
delay(1);
write_dat(0xd7);
delay(1);
write_dat(0xa2);
delay(1);
write_dat(0xb2);
delay(1);
write_dat(0xe1);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(0x1e);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(0x1f);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_com(0X98);
delay(1);
write_dat(0xc9);
delay(1);
write_dat(0xbe);
delay(1);
write_dat(0xb3);
delay(1);
write_dat(0xfd);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(0x1e);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
write_dat(0x1f);
delay(1);
write_dat(SEG2[0x0e]);
delay(1);
}
//--------------------------
void peizhi()
{
uchar a,b;
if(xl==219) //删除 加
{
if(xl==219)
{
if(aa2<120)
{
aa2++;
}
else
{
aa2=0;
}
}
}
//-------------
if(xl==220) //删除 加
{
if(yl==220)
{
if(aa2>0)
{
aa2--;
}
else
{
aa2=120;
}
}
}
}
//--------------------------
void main()
{
delay(200); // delay wait tranquilization
chushihua();
spiinit();
chushihua();
chushihua1();
sendspi(0x00);
smalldelay(20);
aa2=50;
aa4=60;
while(1)
{
peizhi();
keydown();
zhuanhuan();
xianshi();
}
} |
|