stm32 调试 ads7843时读取结果为oxfff
[复制链接]
大家好,这两天在调试tft屏上带的触控功能ic---ADS7843,但是无论用STM32F107的GPIO模拟spi或是SPI3,读出来的数据都是oxfff,请大家帮忙解答一下.....
TPCS --->PB8
TPCLK -->PC10
TPMOSI -->PC12
TPMISO -->PC11
TPIRQ -->PC5
/*******************************GPIO模拟方式*********************************/
/************* H 文件 **************/
#ifndef ADS7843_H #define ADS7843_H
/********************* CS :PB8 SCK :PC10 MOSI :PC12 MISO :PC11 INT :PC5 *********************/ extern u16 TP_x,TP_y;
#define TP_CS() GPIO_ResetBits(GPIOB,GPIO_Pin_8) #define TP_DCS() GPIO_SetBits(GPIOB,GPIO_Pin_8)
#define TP_DCLK2L() GPIO_ResetBits(GPIOC,GPIO_Pin_10) #define TP_DCLK2H() GPIO_SetBits(GPIOC,GPIO_Pin_10)
#define TP_DOUT2L() GPIO_ResetBits(GPIOC,GPIO_Pin_12) #define TP_DOUT2H() GPIO_SetBits(GPIOC,GPIO_Pin_12)
#define TP_RD_DIN() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_11)
#define TP_RD_INT() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)
void ADS7843_GPIO_Config(void) ; void spistar(void); void WriteCharTo7843(unsigned char num); unsigned int ReadFromCharFrom7843(void) ; void AD7843(void) ; //获取横坐标和纵坐标
#endif
/***************************************/
/************* C 文件 **************/
#include "stm32f10x.h" #include "ads7842_gpio.h"
void ADS7843_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;
//SPI3 Periph clock enable // RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE); //SPI2 Periph clock enable // RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE ) ;
//Configure SPI3 pins: SCK, MISO and MOSI GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 SCK MOSI GPIO_Init(GPIOC,&GPIO_InitStructure);
//Configure SPI3 pins: SCK, MISO and MOSI GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 TP__CS GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 MISO GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 TP__PEN IRQ INT GPIO_Init(GPIOC,&GPIO_InitStructure);
}
//********************************************************** void spistar() //SPI开始 { TP_CS(); //CS=1; TP_DCLK2H();//DCLK=1; TP_DOUT2H();//DIN=1; TP_DCLK2H();//DCLK=1; } //********************************************************** void WriteCharTo7843(unsigned char num) //SPI写数据 { unsigned char count=0; unsigned char tp_in=0; TP_DCLK2L();//DCLK=0; for(count=0;count<8;count++) { tp_in=num&0x80; if(tp_in==1) TP_DOUT2H(); //DIN=CY; if(tp_in==0) TP_DOUT2L(); num<<=1; TP_DCLK2L();//DCLK=0; __nop();__nop();__nop(); //上升沿有效 TP_DCLK2H();//DCLK=1; __nop();__nop();__nop();
} } //********************************************************** unsigned int ReadFromCharFrom7843() //SPI 读数据 { unsigned char count=0; unsigned int Num=0; for(count=0;count<16;count++) { Num<<=1; TP_DCLK2H();//DCLK=1; __nop();__nop();__nop(); //下降沿有效 TP_DCLK2L();//DCLK=0; __nop();__nop();__nop(); if(TP_RD_DIN())Num++; //if(DOUT) Num++; } Num=Num>>3; return(Num); }
void AD7843(void) //获取横坐标和纵坐标 { TP_DCS();//CS=0; //delayms(1); //中断后延时以消除抖动,使得采样数据更准确 //while(BUSY); //如果BUSY信号不好使可以删除不用 //delayms(1); WriteCharTo7843(0x90); //送控制字 10010000 即用差分方式读X坐标 详细请见有关资料 //while(BUSY); //如果BUSY信号不好使可以删除不用 //delayms(1); TP_DCLK2H();//DCLK=1; __nop();__nop();__nop();__nop(); TP_DCLK2L();//DCLK=0; __nop();__nop();__nop();__nop(); TP_y=ReadFromCharFrom7843(); WriteCharTo7843(0xD0); //送控制字 11010000 即用差分方式读Y坐标 详细请见有关资料 TP_DCLK2H();//DCLK=1; __nop();__nop();__nop();__nop(); TP_DCLK2L();//DCLK=0; __nop();__nop();__nop();__nop(); TP_x=ReadFromCharFrom7843(); TP_CS();//CS=1; }
/***************************************/
/**********************************************************************************/
/*********************************** SPI3 ***************************************/
/************* H 文件 **************/
#ifndef ADS7843_H #define ADS7843_H
#define TP_CS() GPIO_ResetBits(GPIOA,GPIO_Pin_4) #define TP_DCS() GPIO_SetBits(GPIOA,GPIO_Pin_4) #define TP_RD_INT() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)
// GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE ); void SPI_Config(void) ; unsigned char SPI_WriteByte(unsigned char data); void SpiDelay(unsigned int DelayCnt); u16 TPReadX(void); u16 TPReadY(void);
#endif
/***************************************/
/************* C 文件 **************/
#include "stm32f10x.h" #include "ads7843_wht.h"
void SPI_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure;
//SPI3 Periph clock enable // RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE); //SPI2 Periph clock enable // RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE ) ;
//Configure SPI3 pins: SCK, MISO and MOSI GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 SCK MOSI GPIO_Init(GPIOC,&GPIO_InitStructure);
//Configure SPI3 pins: SCK, MISO and MOSI GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 TP__CS GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 MISO GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 TP__PEN IRQ INT GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE ); //外设端口重映射 开启
// SPI3 Config SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //SPI_NSS_Hard SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI3,&SPI_InitStructure);
// SPI1 enable SPI_Cmd(SPI3,ENABLE); }
unsigned char SPI_WriteByte(unsigned char data) { unsigned char Data = 0;
//Wait until the transmit buffer is empty while(SPI_I2S_GetFlagStatus(SPI3,SPI_I2S_FLAG_TXE)==RESET); // Send the byte SPI_I2S_SendData(SPI3,data);
//Wait until a data is received while(SPI_I2S_GetFlagStatus(SPI3,SPI_I2S_FLAG_RXNE)==RESET); // Get the received data Data = SPI_I2S_ReceiveData(SPI3);
// Return the shifted data return Data; } void SpiDelay(unsigned int DelayCnt) { unsigned int i; for(i=0;i<DelayCnt;i++); }
u16 TPReadX(void) { u16 x=0; TP_CS(); SpiDelay(10); SPI_WriteByte(0x90); SpiDelay(10); x=SPI_WriteByte(0x00); x<<=8; x+=SPI_WriteByte(0x00); SpiDelay(10); TP_DCS(); x = x>>3; return (x); }
u16 TPReadY(void) { u16 y=0; TP_CS(); SpiDelay(10); SPI_WriteByte(0xD0); SpiDelay(10); y=SPI_WriteByte(0x00); y<<=8; y+=SPI_WriteByte(0x00); SpiDelay(10); TP_DCS(); y = y>>3; return (y); }
/***************************************/
/**********************************************************************************/
GPIO和外设的始终在main文件中已经使能。SPI3是重映射后的引脚。
|