OpenEdv-¿ªÔ´µç×ÓÍø

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á
ÕýµãÔ­×ÓÈ«Ì×STM32/Linux/FPGA¿ª·¢×ÊÁÏ£¬ÉÏǧ½²STM32ÊÓƵ½Ì³ÌÃâ·ÑÏÂÔØ...
²é¿´: 1286|»Ø¸´: 0

ΪʲôSTM32F0 SPIͨѶÎÞ·¨·¢ËÍÊý¾Ý

[¸´ÖÆÁ´½Ó]

1

Ö÷Ìâ

2

Ìû×Ó

0

¾«»ª

ÐÂÊÖÉÏ·

»ý·Ö
29
½ðÇ®
29
×¢²áʱ¼ä
2023-10-25
ÔÚÏßʱ¼ä
5 Сʱ
·¢±íÓÚ 2023-10-30 12:53:52 | ÏÔʾȫ²¿Â¥²ã |ÔĶÁģʽ
3½ðÇ®
°å×ÓÊÇSTM32F030C8T6£¬´úÂëÊǸù¾ÝF103¿ÉÒÔͨѶµÄ´úÂëÒÆÖ²¹ýÀ´µÄ£¬½ÓÏßҲûÓÐÎÊÌ⣬µ«ÊǼĴæÆ÷¾ÍÊÇûÓÐÊý¾Ý£¬Ï£Íû´óÉñ°ïæ¿´¿´ÊÇÄÄÀïÓÐʲôÎÊÌâÂð£¿Ð»Ð»´ó¼Ò£¡
SPI1£¨Ö÷»úģʽ£©³õʼ»¯´úÂ룺
void SPI1_Init(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
        RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);


        // SPI_Cmd(SPI1, DISABLE);
        SPI_InitTypeDef SPI_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;

        GPIO_InitStructure.GPIO_Pin = SPI1_SCK_PIN | SPI1_MOSI_PIN | SPI1_MISO_PIN;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(SPI1_MISO_PORT, &GPIO_InitStructure);

        GPIO_PinAFConfig(SPI1_SCK_PORT, SPI1_SCK_SOURCE, GPIO_AF_0);
        GPIO_PinAFConfig(SPI1_MISO_PORT, SPI1_MISO_SOURCE, GPIO_AF_0);
        GPIO_PinAFConfig(SPI1_MOSI_PORT, SPI1_MOSI_SOURCE, GPIO_AF_0);

        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_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7;
        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

        SPI_Init(SPI1, &SPI_InitStructure);

        SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
        SPI_Cmd(SPI1, ENABLE);
}

Ö÷»ú·¢ËÍÊý¾Ý´úÂ룺
uint8_t SPI1_WriteRead8Byte(uint8_t TxData)
{
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);       
        SPI_SendData8(SPI1, TxData);
        // *(uint8_t*)&SPI1->DR = TxData;
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);               
        return SPI_ReceiveData8(SPI1);
}

SPI2£¨´Ó»úģʽ£©³õʼ»¯´úÂ룺
void SPI2_Init(void)
{
        RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

        GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN | SPI2_MOSI_PIN | SPI2_MISO_PIN;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(SPI2_MISO_PORT, &GPIO_InitStructure);

        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7;

        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
        SPI_Init(SPI2, &SPI_InitStructure);
        SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
        SPI_Cmd(SPI2, ENABLE);

        SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
        SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_ERR, ENABLE);
       
       
        NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}

´Ó»úÖжϺ¯Êý£º
void SPI2_IRQHandler(void)
{
        if (SET == SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE))
        {
                ucSPI2_RxBuf[ucSPI2_RxCount] =  *(uint8_t*)&SPI2->DR;
                while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
                        ;       
                 *(uint8_t*)&SPI2->DR = ucSPI2_RxBuf[ucSPI2_RxCount] + 0x05;
                ucSPI2_RxCount++;
                if (ucSPI2_RxCount > SPI_BUF_LEN - 1)
                {
                        ucSPI2_RxCount = 0;
                }

                SPI_I2S_ClearFlag(SPI2, SPI_I2S_IT_RXNE);
                //SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_RXNE);
        }
}

Ö÷º¯Êý£º
int main(void)
{
        int i = 0;
        System_Initializes();
        while (1)
        {
                for (i = 0; i < SPI_BUF_LEN; i++)
                {
                        ucSPI1_RxBuf[i] = SPI1_WriteRead8Byte(ucSPI1_TxBuf[i]);
                        Delay_ms(10);
                }
        }
}

ÆäÓà´úÂëÒѾ­ÔÚ¸½¼þÉÏÉÏ´«£¬Âé·³´ó¼Ò°ïæ¿´¿´£¬Ð»Ð»

STM32F0_SPI.zip

11.41 MB, ÏÂÔØ´ÎÊý: 5

ÕýµãÔ­×ÓÂß¼­·ÖÎöÒÇDL16¾¢±¬ÉÏÊÐ
»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

ÄúÐèÒªµÇ¼ºó²Å¿ÉÒÔ»ØÌû µÇ¼ | Á¢¼´×¢²á

±¾°æ»ý·Ö¹æÔò



¹Ø±Õ

Ô­×Ӹ缫Á¦ÍƼöÉÏÒ»Ìõ /2 ÏÂÒ»Ìõ

ÕýµãÔ­×Ó¹«ÖÚºÅ

QQ|ÊÖ»ú°æ|OpenEdv-¿ªÔ´µç×ÓÍø ( ÔÁICP±¸12000418ºÅ-1 )

GMT+8, 2024-11-23 19:53

Powered by OpenEdv-¿ªÔ´µç×ÓÍø

© 2001-2030 OpenEdv-¿ªÔ´µç×ÓÍø

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí