我用STM32F4的SPI操作 unsigned char XPT2046_SendByte(unsigned char byte) { /* Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET); /* Send Half Word through the SPI3 peripheral */ SPI_I2S_SendData(SPI3, byte); /* Wait to receive a Half Word */ while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET); /* Return the Half Word read from the SPI bus */ return SPI_I2S_ReceiveData(SPI3); } 发送一个字节的时候,一直卡在红色的那句,不知怎么回事,求教~ |
RE:STM32 SPI 问题
1:当一字节写进发送缓冲器时,发送过程开始。
在发送第一个数据位时,数据字被并行地( 通过内部总线) 传入移位寄存器,而后串行地移出到
MOSI脚上;MSB在先还是LSB 在先,取决于SPI_CR1寄存器中的LSBFIRST 位。数据从发送缓冲器传输到移位寄存器时TXE 标志将被置位,如果设置SPI_CR1寄存器中的TXEIE 位,将产生
中断。
回复:STM32 SPI 问题
#include "stdio.h"
#include "SPI3.h"
#define TP_CS GPIO_ResetBits(GPIOG, GPIO_Pin_4)
#define TP_DCS GPIO_SetBits(GPIOG, GPIO_Pin_4)
#define SD_CS GPIO_ResetBits(GPIOG, GPIO_Pin_5)
#define SD_DCS GPIO_SetBits(GPIOG, GPIO_Pin_5)
#define FLASH_CS GPIO_ResetBits(GPIOG, GPIO_Pin_3)
#define FLASH_DCS GPIO_SetBits(GPIOG, GPIO_Pin_3)
void SPI3_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
//CS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
TP_DCS;
SD_DCS;
FLASH_DCS;
//Enable peripheral clock
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
//Enable SCK, MOSI, MISO and NSS GPIO clocks
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_SPI3);
//Set as Output AF push-pull - SCK and MOSI PC10 PC12//SPI3_MISO PC11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//Peripherals alternate function
//SPI3 设置
SPI_I2S_DeInit(SPI3);
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_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;//由于说明书最高速率2M //72/256=
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
/* Enable SPI3 */
SPI_Cmd(SPI3, ENABLE);
}
//TSC2046
unsigned int X_Addata=0,Y_Addata=0;
static void XPT2046_EXIT_Config(void)//中断设置下降沿
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOA clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//极其重要,缺少不可!!!
//Configure the I/O in input mode
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line2);
//Select the input source pin for the EXTI line
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOG,EXTI_PinSource2);
/* Configure Button EXTI line */
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the EXTI2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void XPT2046_Config(void)
{
SPI3_Config();
XPT2046_EXIT_Config();
}
unsigned char XPT2046_SendByte(unsigned char byte)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
/* Send Half Word through the SPI3 peripheral */
SPI_I2S_SendData(SPI3, byte);
/* Wait to receive a Half Word */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the Half Word read from the SPI bus */
return (unsigned char)SPI_I2S_ReceiveData(SPI3);
}
unsigned char XPT2046_ReadByte(void)
{
return (XPT2046_SendByte(0x00));
}
void XPT2046_Rd_Addata(void)
{
unsigned int temp;
TP_CS;
XPT2046_SendByte(CHX);
temp = 0;//相当于简短的延时啦
temp = 0;
temp = 0;
temp = XPT2046_ReadByte();
Y_Addata = temp= 3;
Y_Addata &= 0x0FFF;
printf("\nY_Addata = %d ",Y_Addata);
XPT2046_SendByte(CHY);
temp = 0;
temp = 0;
temp = 0;
temp = XPT2046_ReadByte();
X_Addata = temp= 3;
X_Addata &= 0x0FFF;
printf("\nX_Addata = %d \n",Y_Addata);
TP_DCS;
}
//SD
//FLASH
第一次用STM32,大家帮忙看下,是不是我SPI没有配置好啊~谢谢~
RE:STM32 SPI 问题
回复:STM32 SPI 问题 【悬赏问答】
请问一下,开启SPI3的时钟了吗?
开了
//Enable peripheral clock
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
是这句么?
回复:STM32 SPI 问题 【悬赏问答】
回复第 4 楼 于2012-11-19 16:29:41发表:
请问一下,开启SPI3的时钟了吗?
开了
//Enable peripheral clock
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
是这句么?
多谢,应该是ClockCmd看来即使是官方库,也不能随便抄……
RE:STM32 SPI 问题 【悬赏问答】
RE:STM32 SPI 问题 【悬赏问答】
RE:STM32 SPI 问题 【悬赏问答】
回复:STM32 SPI 问题 【悬赏问答】