First, learn from others' programming:
#define MOSI_H GPIO_SetBits(GPIOB, GPIO_Pin_10)
#define MOSI_L GPIO_ResetBits(GPIOB, GPIO_Pin_10)
#define SCLK_H GPIO_SetBits(GPIOB, GPIO_Pin_13)
#define SCLK_L GPIO_ResetBits(GPIOB, GPIO_Pin_13)
#define MISO GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)
unsigned char SPI_SendByte(unsigned char dt)
{
u8 i;
u8 temp=0;
for(i=8;i>0;i--)
{
if(dt&0x80)MOSI_H;
else MOSI_L;
dt<<=1;
SCLK_H;
__nop();
__nop();
__nop();
temp<<=1;
if(MISO)temp++;
__nop();
__nop();
__nop();
SCLK_L;
}
return temp;
}
Finally, I realized a SPI host program that I needed. The protocol stipulates that CPOL=1, CPHA=1, high bit first, low bit last.
#define MOSI_H GPIO_SetBits(GPIOF, GPIO_Pin_2)
#define MOSI_L GPIO_ResetBits(GPIOF, GPIO_Pin_2)
#define SCLK_H GPIO_SetBits(GPIOF, GPIO_Pin_1)
#define SCLK_L GPIO_ResetBits(GPIOF, GPIO_Pin_1)
#define CS_H GPIO_SetBits(GPIOF, GPIO_Pin_0)
#define CS_L GPIO_ResetBits(GPIOF, GPIO_Pin_0)
void SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOF, ENABLE); //Enable PF port clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2; //PF port configuration
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO port speed is 50MHz
GPIO_Init(GPIOF, &GPIO_InitStructure); //Initialize GPIOF according to the set parameters
GPIO_SetBits(GPIOF,GPIO_Pin_0); //PF0 PF1 PF2 output high
GPIO_SetBits(GPIOF,GPIO_Pin_1);
GPIO_SetBits(GPIOF,GPIO_Pin_2);
}
unsigned char SPI_SendByte(unsigned char dt)
{
u8 i;
u8 temp=0;
for(i=8;i>0;i--)
{
CS_L;
if(dt&0x80)
MOSI_H;
else MOSI_L;
dt<<=1;
SCLK_L;
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
SCLK_H;
}
CS_H;
return temp;
}
Previous article:Note on using STM32's SPI
Next article:STM32 SPI communication
Recommended ReadingLatest update time:2024-11-15 15:03
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Which mooncake filling is the most delicious? EEWorld has selected several for you, come and redeem them!
- Xiaobai asks for help with Xilinx programming issues
- DSP CMD file MEMORY PAGE
- Comparison of network isochronous line settings after AD18 and previous versions
- 99iot AFW127PI Wi-Fi Module Specification
- MSP430F5438 interrupt problem
- Lightweight MicroPython GUI library nano-gui
- 【Recruitment】Full-time and part-time RF engineers
- When posting, when inviting others to participate, can you directly write the names of multiple people and submit them together, which can save time
- 2020 Python Developer Survey Results