#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; // 記數(shù)器0 計數(shù)器1 都以 16 位 記數(shù) TCON=0x00; IE=0x83; //1000 0001 EA=1中斷允許, IP=0x01; while(1); } //========================================== void ZhongDuan() interrupt 0 //外部中斷0 用來接受鍵盤發(fā)來的數(shù)據(jù) { unsigned int X=0,Y=0; delay(10000); //中斷后延時以消除抖動,使得采樣數(shù)據(jù)更準確 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寫數(shù)據(jù) { 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 讀數(shù)據(jù) { 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); } |