现在的问题是:想用SPI读取X的坐标值(触摸屏AD值),为什么发上来的数据一直是0
#include "include.h"
#define CMD_RDX 0x90
#define CMD_RDY 0xD0
void SPI_Touch_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
SPI_TOUCH_CS_HIGH();
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_High; //设置为SPI工作模式4,即CPOL = 1,
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //CPOH = 1
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
u8 Touch_WR_Byte(u8 num)
{
SPI_TOUCH_CS_LOW();
SPI_Delay(10);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, num);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
SPI_TOUCH_CS_HIGH();
}
int main(void)
{
/* --------程序里面的SYSTEM 初始化操作---------- */
SystemInit(); //SYSTEM_STM32F10X.C里面的文件
delay_init(); //延时函数初始化
uart_init(9600); //串口初始化为9600
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
/* --------HARDWARE初始化操作------------------- */
LCD_Init();
SPI_Touch_Init();
/* --------HARDWARE初始化结束------------------- */
while(1)
{
printf("\r\n CMD now is :%d\r\n", Touch_WR_Byte(CMD_RDX));
printf("hello world");
printf("\r\n x now is :%d\r\n", Touch_WR_Byte(0));
delay_ms(200);
}
} |