测试STM8L050 SPI输出测试
一、端口配置:
使用STM8CubeMX来查看SPI需要的引脚
二、程序部分
2.1、SPI程序
- //spi.c
- #include "stm8l15x.h"
- void SPI_FLASH_Init(void)
- {
- GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_Out_PP_High_Fast); //SPI_MOSI
- GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT); //SPI_MISO
- GPIO_Init(GPIOB, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast); //SPI_SCK
-
- CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
-
- SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_4, SPI_Mode_Master,\
- SPI_CPOL_High, SPI_CPHA_2Edge, \
- SPI_Direction_2Lines_FullDuplex, SPI_NSS_Soft, 0x07);
-
- SPI_Cmd(SPI1, ENABLE); /* 使能SPI */
-
- /* 配置CS管脚 */
- // GPIO_Init(SPI_CS , SPI_Pin_CS, GPIO_Mode_Out_PP_High_Fast);
- // GPIO_WriteBit(SPI_CS, SPI_Pin_CS, SET); /* 拉高不使能外部SPI设备 */
- }
- uint8_t SPI_FLASH_SendByte(u8 byte)
- {
- /* Loop while DR register in not emplty */
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
- /* Send byte through the SPI1 peripheral */
- SPI_SendData(SPI1, byte);
- /* Wait to receive a byte */
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
- /* Return the byte read from the SPI bus */
- return SPI_ReceiveData(SPI1);
- }
- //spi.h
- #ifndef __SPI_H
- #define __SPI_H
- void SPI_FLASH_Init(void);
- uint8_t SPI_FLASH_SendByte(u8 byte);
- #endif
复制代码
2.2、主程序
- void main(void)
- {
- CLK_Config(); //时钟初始化
-
- Delay(0x2fffff); //延时5S
-
- //led_init(); //led初始化
- //swim_init();
- uart_init_halfduplex();
-
- SPI_FLASH_Init();
-
- while (1)
- {
-
- SPI_FLASH_SendByte(0x55);
- Delay(0xffff);
- }
- }
复制代码
三、执行结果
手头上没有SPI通信的器件,使用示波器测试下,SPI_SCK和SPI_MOSI波形:
黄色:SPI_SCK
绿色:SPI_MOSI
SPI发送的数据是0x55
|
STM8CubeMX不能生成代码