#include "reg51.h" #include "intrins.h"
sbit DCLK=P1^6; sbit CS=P2^2; sbit DIN=P2^3; sbit DOUT=P2^4; sbit BUSY=P2^5;
void delay(unsigned char i); void start(); void ads7843_wr(unsigned char num); unsigned int ads7843_rd();
//=============================== main() { TMOD=0x11; // 记数器0 计数器1 都以 16 位 记数 TCON=0x00; IE=0x83; //1000 0001 EA=1中断允许, IP=0x01; while(1); } //========================================== void ZhongDuan() interrupt 0 //外部中断0 用来接受键盘发来的数据 { unsigned int X=0,Y=0; delay(10000); //中断后延时以消除抖动,使得采样数据更准确 start(); //启动SPI //while(BUSY); //如果BUSY信号不好使可以删除不用 delay(2); ads7843_wr(0x90); //送控制字 10010000 即用差分方式读X坐标 详细请见有关资料 //while(BUSY); //如果BUSY信号不好使可以删除不用 delay(2); DCLK=1; _nop_();_nop_();_nop_();_nop_(); DCLK=0; _nop_();_nop_();_nop_();_nop_(); X=ads7843_rd(); ads7843_wr(0xD0); //送控制字 11010000 即用差分方式读Y坐标 详细请见有关资料 DCLK=1; _nop_();_nop_();_nop_();_nop_(); DCLK=0; _nop_();_nop_();_nop_();_nop_(); Y=ads7843_rd(); CS=1; } //=================================== void delay(unsigned char i) { while(i--); }
//================================== void start() //SPI开始 { DCLK=0; CS=1; DIN=1; DCLK=1; CS=0; } //======================================= void ads7843_wr(unsigned char num) //SPI写数据 { unsigned char i=0; DCLK=0; for(i=0;i<8;i++) { num<<=1; DIN=CY; DCLK=0; _nop_();_nop_();_nop_(); //上升沿有效 DCLK=1; _nop_();_nop_();_nop_(); } } //======================================== unsigned int ads7843_rd() //SPI 读数据 { unsigned char i=0; unsigned int Num=0; for(i=0;i<12;i++) { Num<<=1; DCLK=1; _nop_();_nop_();_nop_(); //下降沿有效 DCLK=0; _nop_();_nop_();_nop_(); if(DOUT) Num++; } return(Num); }
|