您的位置 首页 发布

ATMEGA16用IO模仿SPI驱动ADS7843

最近在搞AVR单片机的ILI9325的驱动、简单GUI的移植,成功之后就搞ADS7843的驱动,用avr-gcc(WinAVR20100110)4.3.3编译。其效果如下图…

最近在搞AVR单片机的ILI9325的驱动、简略GUI的移植,成功之后就搞ADS7843的驱动,用avr-gcc (WinAVR 20100110) 4.3.3编译。

其作用如下图:

以下是其驱动程序:
typedef unsigned char BYTE; // 8-bit
typedef unsigned int WORD; // 16-bit
typedef unsigned long DWORD; // 32-bit
#define _nop_() asm(“NOP”)
#define touch_PORT PORTB
#define PEN_Q 7
#define DOUT 6
#define DIN 5 //引脚界说
#define CS 4 //mega16
#define DCLK 3
#define SPI_CS_Assert() touch_PORT &= ~_BV(CS)
#define SPI_CS_Deassert() touch_PORT |= _BV(CS)
WORD TP_X=1,TP_Y=1; //当时触控坐标
//**********************************************************
void SPI_Init(void) //SPI开端
{
DDRB|=_BV(CS)|_BV(DCLK)|_BV(DIN);
touch_PORT|=_BV(CS)|_BV(DCLK)|_BV(DIN);
}
//**********************************************************
void WriteCharTo7843(unsigned char num) //SPI写数据
{
unsigned char count=0,temp;
touch_PORT&=~_BV(DCLK);
for(count=0;count<8;count++)
{
temp=num;
if(temp&0x80)
touch_PORT|=_BV(DIN);
else
touch_PORT&=~_BV(DIN);
num<<=1;
touch_PORT&=~_BV(DCLK);
_nop_();_nop_();_nop_(); //上升沿有用
touch_PORT|=_BV(DCLK);
_nop_();_nop_();_nop_();
}
}
//**********************************************************
unsigned int ReadFromCharFrom7843(void) //SPI 读数据
{
unsigned char count=0;
unsigned int Num=0;
for(count=0;count<12;count++)
{
Num<<=1;
touch_PORT|=_BV(DCLK);
_nop_();_nop_();_nop_(); //下降沿有用
touch_PORT&=~_BV(DCLK);
_nop_();_nop_();_nop_();
if(PINB&_BV(DOUT))
Num++;
}
return(Num);
}
void AD7843(void) //接纳11次后求10次的平均值
{
static WORD X_TEMP[11],Y_TEMP[11];
static BYTE count=0;
WORD lx=0,ly=0;
if (!(PINB&_BV(PEN_Q)))
{
_delay_us(500);
while (!(PINB&_BV(PEN_Q)))
{
SPI_CS_Assert();
WriteCharTo7843(0x90); //送操控字 10010000 即用差分方法读X坐标 具体请见有关材料
touch_PORT|=_BV(DCLK);
_nop_();_nop_();_nop_();
touch_PORT&=~_BV(DCLK);
_nop_();_nop_();_nop_();
Y_TEMP[count]=ReadFromCharFrom7843();
WriteCharTo7843(0xD0); //送操控字 11010000 即用差分方法读Y坐标 具体请见有关材料
touch_PORT|=_BV(DCLK);
_nop_();_nop_();_nop_();
touch_PORT&=~_BV(DCLK);
_nop_();_nop_();_nop_();
X_TEMP[count]=ReadFromCharFrom7843();
SPI_CS_Deassert();
if(count++==10)
{
for(count=0;count<10;count++)
{
X_TEMP[10]+=X_TEMP[count];
Y_TEMP[10]+=Y_TEMP[count];
}
TP_X=X_TEMP[10]/10;
TP_Y=Y_TEMP[10]/10;
count=0;
// GUI_wrul(100,20,TP_X,RGB(120,255,0),color[2]);//(uchar x, uint y, unsigned long num, uint color,uint b_color);
// GUI_wrul(200,20,TP_Y,RGB(120,255,0),color[2]);
lx=240-((TP_X-240)/16); //将AD值转换位显示屏坐标
ly=320-((TP_Y-380)/12); //将AD值转换位显示屏坐标
// GUI_wrul(100,40,X_TEMP[10],RGB(120,255,0),color[2]);//(uchar x, uint y, unsigned long num, uint color,uint b_color);
// GUI_wrul(200,40,Y_TEMP[10],RGB(120,255,0),color[2]);
GUI_Point(lx,ly,RGB(250,0,250));
Y_TEMP[10]=0;
X_TEMP[10]=0;
}
}
count=0;
}
}
整个程序比较简略,自己剖析吧。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/fabu/268494.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部