现在在尝试做一个四线电阻屏的控制器,计划用tiny44+ADS7843。
用m16的硬件SPI能成功与ADS7843通信,但由于tiny44的硬件SPI只有3或2线模式,ADS7843要用4线模式,所以想用IO口模拟SPI。然后就尝试用m16模拟SPI,但试了很久也不明白为什么自己写的程序不能与ADS7843通信,请求各位大侠帮助
我的程序
M16模拟SPI失败的程序
#include
#include
#include
/* for _delay_ms() */
/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
#define NOP() asm("nop")
#define KOU PORTB
#define SB(bitn)
(KOU |=(1<<(bitn)))
#define CB(bitn)
(KOU&=~(1<<(bitn)))
#define GB(bitn)
(KOU &(1<<(bitn)) )
#define SCK7//DCLK
#define SS 4//CS
#define MOSI 5//DIN
//#define BUSY 0//
#define MISO 6//DOUT
#define IQR1//
unsigned char duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x76,0x6e};
//unsigned char test1,test2,test3;
/*------函数定义------*/
void Display(unsigned int TX,unsigned int TY)
{
unsigned char i;
unsigned char num[9];
DDRA=0xff;
DDRC=0xff;
num[1]=TX/1000;
num[2]=(TX%1000)/100;
num[3]=(TX%100)/10;
num[4]=TX%10;
num[5]=TY/1000;
num[6]=(TY%1000)/100;
num[7]=(TY%100)/10;
num[8]=TY%10;
for(i=0;i<8;i++)
{
PORTA=0;
PORTC=i;
PORTA=duanma[num[i+1]];
_delay_ms(1);
}
}
//------------------------------------------------------------------------
void SPI_MasterInit(void)
{
DDRB=0b10110000;//SCK、MOSI、SS置输出,MISO置输入
SB(MISO);//MISO使用上拉电阻
SB(SS);
//SB(MOSI);
}
void SPI_MasterSent(uchar cData)
{
uchar spicount=0;
CB(SCK);
_delay_us(10);
for(spicount=0;spicount<8;spicount++)
{
if(cData&0x80)
SB(MOSI);
else
CB(MOSI);
cData<<=1;
CB(SCK);_delay_us(10);
SB(SCK);_delay_us(10);
}
}
uint SPI_MasterRead(void)//功能:使用SPI接收AD转换的值
{
uchar spicount=0;
uint temp=0;
for(spicount=0;spicount<12;spicount++)
{
temp<<=1;
SB(SCK);_delay_us(10);
CB(SCK);_delay_us(10);
if(GB(MISO))
temp++;
}
return temp;
}
void SPI_Start(void)
{
SB(SCK);_delay_us(10);
CB(SS);_delay_us(10);
}
void SPI_Stop(void)
{
SB(SCK);
SB(SS);
//SB(MOSI);
}
//----------------------------------------------------------
//主函数
int main(void)
{
uint X=0,Y=0,zux[7],zuy[7];
SPI_MasterInit();
while(1)
{
if(!(PINB &(1<<(1))))//有点击时
{
SPI_Start();
SPI_MasterSent(0X90);
//CB(SCK);_delay_us(5);
SB(SCK);_delay_us(10);
CB(SCK);_delay_us(10);
X=SPI_MasterRead();
SPI_Stop();
SPI_Start();
SPI_MasterSent(0XD0);
Y=SPI_MasterRead();
SPI_Stop();
}
Display(X,Y);
}
}
|