[英]STM32 SPI Transmit
我一直在使用STM32F103C8,試圖獲取SPI接口已有一段時間。 一直在使用Atollic TrueStudio。 我在這方面還很陌生,所以如果這是一個愚蠢的問題,希望您能原諒我。
無論嘗試如何,我似乎都無法在SPI接口上傳輸任何內容。 我翻閱了手冊,以為自己做的一切正確,但顯然做不到。 根據我的閱讀,似乎是:
我嘗試將MOSI引腳環回至MISO引腳並進行寫操作,但一無所獲。 然后,我連接了邏輯分析儀,而SPI時鍾引腳甚至什么也沒做。 下面的相關代碼,如果有人可以幫助的話,那會很棒:
void PrintStrToUART(char str[])
{
char *pointertostr;
for (pointertostr = &str[0]; *pointertostr != '\0'; pointertostr++){
USART1 -> DR = (*pointertostr & USART_DR_DR);
while ((USART1 -> SR & USART_SR_TXE) == 0){
;
}
}
}
void PrintCharArrayToUART(unsigned char str[], int arraysize){
int j;
unsigned char buffer[((5*arraysize)-2)];
unsigned char *positionpointer = &buffer[0];
for(j=0; j <= (arraysize-1); j++){
if(j){
positionpointer += sprintf(positionpointer, ", ");
}
positionpointer += sprintf(positionpointer, "%d", str[j]);
}
int newarrayend;
newarrayend = (positionpointer - &buffer[0]);
PrintStrToUART("[");
for(j = 0; j <= newarrayend; j++){
USART1 -> DR = (buffer[j] & USART_DR_DR);
while ((USART1 -> SR & USART_SR_TXE) == 0){
;
}
}
PrintStrToUART("]\n\r");
}
void SelfSPIInit(void){ //because the Mx one is not working
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; //Enable SPI clock
//Set baud prescaler
SPI1->CR1 = SPI_CR1_BR; //Slowest SPI I can have
//CPHA CPOL
SPI1-> CR1 &= ~(SPI_CR1_CPOL | SPI_CR1_CPHA);
//8 bit data format
SPI1->CR1 &= ~(SPI_CR1_DFF);
//Full duplex mode
SPI1->CR1 &= ~(SPI_CR1_BIDIMODE);
SPI1->CR1 &= ~(SPI_CR1_RXONLY);
//MSB first
SPI1->CR1 &= ~(SPI_CR1_LSBFIRST);
//Master mode
SPI1->CR1 |= SPI_CR1_MSTR;
//Software NSS mode off
SPI1->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI;
//Enable
SPI1->CR1 |= SPI_CR1_SPE;
//Enable the SPI GPIO pins
}
int main(void)
{
int i;
HAL_Init();
/* Configure the system clock */
SystemClock_Config(); //config to run at the correct speed
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_TIM1_Init();
MX_USART1_UART_Init();
MX_TIM2_Init();
MX_CRC_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
MX_TIM3_Init();
//MX_SPI1_Init();
SelfSPIInit();
TIM1 -> CR1 |= TIM_CR1_CEN;
TIM2 -> CR1 |= TIM_CR1_CEN;
I2C1 -> CR1 |= I2C_CR1_PE; //peripheral enable
SPI1 -> CR1 |= SPI_CR1_SPE;
unsigned char newchararray[21] = {0x41};
unsigned char *pnewchararray = &newchararray[0];
unsigned char recarray[7] = {127};
unsigned char *precarray = &recarray[0];
while (1)
{
GPIOC -> BSRR |= GPIO_BSRR_BR13;
GPIOA -> BSRR |= GPIO_BSRR_BR6;
MilliSecondDelay(500);
GPIOC -> BSRR |= GPIO_BSRR_BS13;
GPIOA -> BSRR |= GPIO_BSRR_BS6;
MilliSecondDelay(500);
SPI1->DR = *pnewchararray;
while((SPI1->SR & SPI_SR_TXE) == 0){
//while transmit buffer not empty, wait
;
}
while((SPI1->SR & SPI_SR_RXNE) == 0){
;//while receive buffer not empty
}
*precarray = SPI1->DR;
PrintCharArrayToUART(precarray, 7);
}
}
很抱歉縮進,正如我所說的那樣,我仍然很陌生,但這就是編譯和運行的代碼。 任何幫助表示贊賞,謝謝!
編輯:不確定我是否應該包括USART功能,那只是為了打印到我的筆記本電腦上,以便我可以看到SPI數據寄存器中的內容。 但是我認為,如果包含它,該程序將更有意義。
請訪問http://www.handsonembedded.com/stm32f103-spl-tutorial-5/ 。 stm32f103有完整的spi示例。
還有一個是:您好像忘記了引腳多路復用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.