stm32 io simulates spi communication

Publisher:EnchantedMelodyLatest update time:2019-04-11 Source: eefocusKeywords:stm32  communication Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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;

}

Keywords:stm32  communication Reference address:stm32 io simulates spi communication

Previous article:Note on using STM32's SPI
Next article:STM32 SPI communication

Recommended ReadingLatest update time:2024-11-15 15:03

stm32 CAN LoopBack self-test mode successful
The code in the stm32 peripheral library was ported. //CAN_Polling successful DS1 is on //CAN_Interrupt success DS2 lights up When it fails, DS3 and DS4 will light up respectively. The key code will be displayed below.  main.c =========================== int main(void) { /* Setup STM32 system (clock, PLL
[Microcontroller]
How CPU programs the physical address of peripheral IO ports
Almost every peripheral is operated by reading and writing registers on the device. Peripheral registers are also called "I/O ports", which usually include three categories: control registers, status registers and data registers, and the registers of a peripheral are usually addressed continuously. There are two ways
[Microcontroller]
STM32 conversion mode
1. Main features of ADC: 12-bit resolution Modes: single and continuous conversion modes, scan mode, discontinuous mode Channel: rule group, injection group Power supply requirements: 2.4V~3.6V Input range: Vref- = Vin = Vref+ 16 analog input channels (0..15)   The temperature sensor is connected to channe
[Microcontroller]
STM32 conversion mode
Use of stm32-afio
1. Features of AFIO     To optimize the number of peripherals in a 64-pin or 100-pin package, you can remap some alternate functions to other pins. Set the alternate remapping and debug I/O configuration register (AFIO_MAPR) to achieve pin remapping. In this case, the alternate functions are no longer mapped to their
[Microcontroller]
Use of stm32-afio
STM32 control GPIO explanation
1. GPIO Introduction The STM32 IO port has 8 configuration modes: enter Floating input GPIO_IN_FLOATING ——Floating input, can be used for KEY recognition, RX1 With pull-up input GPIO_IPU——IO internal pull-up resistor input With pull-down input GPIO_IPD——IO internal pull-down resistor input Analog input GPIO_AIN - Us
[Microcontroller]
Understanding of program memory in stm32 and solutions to some problems
A small project, after the test time and product volume were slightly larger, some inexplicable non-logical error bugs appeared (the most troublesome thing is that they cannot be reproduced every time). After modification, no bugs appeared in the tests in the past month. I have received a lot of help from Brother Atom
[Microcontroller]
STM32--CAN simple receiving and sending
The principle of CAN has been explained above. Here I will use a simple example to illustrate the use of CAN. The STM32 chip I use is STM32F103ZE, and the basic configurations are: 1. Configure CAN clock:  /* CAN Periph clock enable */  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE); 2. CAN pin configuration, the
[Microcontroller]
Application of STM32 in variable frequency pulsator washing machine
With the guidance of the national energy conservation and emission reduction policy and the improvement of people's awareness of energy conservation and environmental protection, frequency conversion technology has been widely used in white household appliances such as air conditioners, refrigerators, and washing mach
[Microcontroller]
Application of STM32 in variable frequency pulsator washing machine
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号