xpt2046芯片
#include<reg52.h>
#include<intrins.h>
#define AD_CH0 0x94
#define AD_CH1 0xd4
#define AD_CH2 0xa4
#define AD_CH3 0xe4
typedef unsigned char uchar;
typedef unsigned int uint;
#define MAIN_Fosc 11059200UL
sbit DIN=P2^0;
sbit DCLK=P2^1;
sbit DOUT=P2^5;
sbit CS=P3^7;
sbit WE=P2^7;
sbit DU=P2^6;
uchar code SMGduan[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};
uchar code SMGwei[] = {0xfe, 0xfd, 0xfb, 0xf7};
void delay(uint ms)
{
uint i;
do
{
i=MAIN_Fosc/96000;
while(--i);
}while(--ms);
}
void display(uint i)
{
uchar q, b, s, g;
static uchar wei;
q = i / 1000;
b = i % 1000 / 100;
s = i % 100 / 10;
g = i % 10;
P0 = 0XFF;
WE = 1;
P0 = SMGwei[wei];
WE = 0;
P0 = 0XFF;
switch(wei)
{
case 0: DU = 1; P0 = SMGduan[q] | 0x80; DU = 0; break;
case 1: DU = 1; P0 = SMGduan[b]; DU = 0; break;
case 2: DU = 1; P0 = SMGduan[s]; DU = 0; break;
case 3: DU = 1; P0 = SMGduan[g]; DU = 0; break;
}
wei++;
if(wei == 4)
wei = 0;
}
void SPI_Write(uchar DAT)
{
uchar i;
for(i=0;i<8;i++)
{
DCLK=0;
if(DAT&0x80)
DIN=1;
else
DIN=0;
DCLK=1;
DAT<<=1;
}
}
uint SPI_Read()
{
uint i,DAT;
for(i=0;i<12;i++)
{
DAT<<=1;
DCLK=1;
DCLK=0;
if(DOUT)
DAT|=0x01;
}
return DAT;
}
uint Read_AD(uchar cmd)
{
uint DAT;
CS=0;
SPI_Write(cmd);
DCLK=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
DAT=SPI_Read();
CS=1;
return DAT;
}
void main()
{
uint i,date;
while(1)
{
if(i>=100)
{
i=0;
date=Read_AD(AD_CH1);
date=date*1.2207;
}
display(date);
delay(5);
i++;
}
}