Hi everyone i am a beginner in SPI. i am using STM32F051 discovery board. DAC081S151 is used as the slave in this commmunication.I view the output on CRO. The MOSI out is in correct. Please help me to get the DAC output.Tne code i used is given below.
#include <stdio.h> #include "stm32f0xx.h" #include "stm32f0xx_gpio.h" #include "stm32f0xx_it.h" #include "stm32f0xx_rcc.h" #include "stm32f0xx_usart.h" #include "stm32f0xx_conf.h" #include "stm32f0xx_adc.h" #include "stm32f0xx_i2c.h" #include "math.h" #include "string.h" #include "main.h" #include "main.h" void delay_ns(unsigned int d){ /* Not exact value, for 168 MHz @ Level3 Opt */ unsigned int i; i=d/25;while(i)i--; } void delay_us(unsigned int d){ /* for 168 MHz @ Level3 Opt */ unsigned int i,j; i=d;while(i){i--;j=55;while(j)j--;} } void delay_ms(unsigned int d){ /* for 168 MHz @ Level3 Opt */ unsigned int i; i=d;while(i){i--;delay_us(250);} } void spiInit(void); void spiInit() { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB ,ENABLE); RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE); 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;//32 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); ///////////////////////////////////////////////////////////////////////////// // SPI1 Master initialization, SCK -> PA5, MOSI -> PA7 GPIO_StructInit (&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; //PuPd_UP GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0); GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0); // MISO -> PA6 GPIO_StructInit (&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0); // CS or SS chip select -> PA4 GPIO_StructInit (&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); // SPI1 SPI_Init(SPI1, &SPI_InitStructure); SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF); SPI_SSOutputCmd(SPI1,ENABLE); SPI_Cmd(SPI1, ENABLE); } void ChipSelectLow() { GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET); } void ChipSelectHigh () { GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET); } uint8_t SPI1_send(uint8_t data){ SPI1->DR = data; // write data to be transmitted to the SPI data register while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore return SPI1->DR; // return received data from SPI data register } int main() { while (1) { spiInit(); ChipSelectHigh (); //********************************* ChipSelectLow (); SPI_SendData8(SPI1 , 0x055); // SPI_SendData8(SPI1 , 0x55); while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore ChipSelectHigh (); } }
.