1.2 作业

2024-01-02 20:37:16

spi.h

#ifndef __SPI_H__
#define __SPI_H__

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
// MOSI对应的引脚输出高低电平的信号PE14
#define  MOSI_OUTPUT_H()    do{GPIOE->ODR |= (0x1 << 14);}while(0)
#define  MOSI_OUTPUT_L()    do{GPIOE->ODR &= (~(0x1 << 14));}while(0)

// 对应595芯片的锁存引脚输出高低电平  PE11
#define  NSS_OUTPUT_H()     do{GPIOE->ODR |= (0x1 << 11);}while(0)
#define  NSS_OUTPUT_L()     do{GPIOE->ODR &= (~(0x1 << 11));}while(0)
    
// 时钟信号对应的引脚输出高低电平  PE12
#define  SCK_OUTPUT_H()     do{GPIOE->ODR |= (0x1 << 12);}while(0)
#define  SCK_OUTPUT_L()     do{GPIOE->ODR &= (~(0x1 << 12));}while(0)

/*
 * 函数功能: SPI初始化函数,推挽输出,高速,禁止上拉和下拉
 * 函数参数:无
 * 函数返回值:无
*/
void SPI_init(void);
/*
 * 函数功能:SPI发送数据的函数
 * 函数参数:dat : 要发送的数据
 * 函数返回值:无
 *
*/
void SPI_write(unsigned char dat);

void delay_us1(unsigned int us);

#endif  // __SPI_H__

spi.c

#include "spi.h"

void delay_us1(unsigned int us)
{
    int i,j;
    for(i = 0; i < us;i++)
        for (j = 0; j < 1;j++);
}

void SPI_init(void)
{
    RCC->MP_AHB4ENSETR |= (0x1 << 4);
    // MOSI    PE14 
    GPIOE->MODER &= (~(0x3 << 28));
    GPIOE->MODER |= (0x1 << 28);
    GPIOE->OTYPER &= (~(0x1 << 14));
    GPIOE->OSPEEDR &= (~(0x3 << 28));
    GPIOE->PUPDR &= (~(0x3 << 28));
    // MISO    PE13
    GPIOE->MODER &= (~(0x3 << 26));
    GPIOE->OSPEEDR &= (~(0x3 << 26));
    GPIOE->PUPDR &= (~(0x3 << 26));
    // SCK     PE12 
    GPIOE->MODER &= (~(0x3 << 24));
    GPIOE->MODER |= (0x1 << 24);
    GPIOE->OTYPER &= (~(0x1 << 12));
    GPIOE->OSPEEDR &= (~(0x3 << 24));
    GPIOE->PUPDR &= (~(0x3 << 24));
    // NSS     PE11
    GPIOE->MODER &= (~(0x3 << 22));
    GPIOE->MODER |= (0x1 << 22);
    GPIOE->OTYPER &= (~(0x1 << 11));
    GPIOE->OSPEEDR &= (~(0x3 << 22));
    GPIOE->PUPDR &= (~(0x3 << 22));
    NSS_OUTPUT_L();    // 595芯片的锁存引脚拉低
    SCK_OUTPUT_L();    // SPI的时钟线拉低
}

void SPI_write(unsigned char dat)
{
    unsigned char i;
    for(i = 0; i < 8; i++)
    {

        if(dat & 0x01)
        {
            MOSI_OUTPUT_H();  // MOSI线写高
        } else {
            MOSI_OUTPUT_L();  // MOSI线写低
        }
        dat >>= 1;
        // 时钟线从低电平到高电平的变化时,MOSI数据线上的数据
        // 被写到595芯片的移位寄存器中
        SCK_OUTPUT_L();   // SCK拉低
        delay_us1(1);
        SCK_OUTPUT_H();   // SCK拉高
        delay_us1(10);
    }


}




mian.c

#include "spi.h"

#include "si7006.h"

#include "key_it.h"



void delay_mas(unsigned int ms){

  for(int i=0;i<ms;i++){

      for(int j=0;j<1800;j++);

  }

}

int math[10]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0x3E,0xE0,0xFE,0xF6};

unsigned short hum,tem;

int main()

{

  key1_it_config();

  SPI_init();

  si7006_init();

  while(1){

                        hum=si7006_read_hum_data(0x40,0xE5);

                        hum=125*hum/65536-6;

                        int hum_m=hum/10;

                        int hum_l=hum%10;



                        tem=si7006_read_temp_data(0x40,0xE3);

                        tem=175.72*tem/65536-46.85;

                        int tem_m=tem/10;

                        int tem_l=tem%10;

                             for(int i=0;i<10000;i++)

                                  {

                                    SPI_write(0x80);

                                    SPI_write(math[hum_m]);

                                    NSS_OUTPUT_L();

                                    delay_us1(10);

                                    NSS_OUTPUT_H();

                                    delay_us1(10);



                                    SPI_write(0x40);

                                    SPI_write(math[hum_l]);

                                    NSS_OUTPUT_L();

                                    delay_us1(10);

                                    NSS_OUTPUT_H();

                                    delay_us1(10);



                                    SPI_write(0x20);

                                    SPI_write(math[tem_m]);

                                    NSS_OUTPUT_L();

                                    delay_us1(10);

                                    NSS_OUTPUT_H();

                                    delay_us1(10);



                                    SPI_write(0x10);

                                    SPI_write(math[tem_l]);

                                    NSS_OUTPUT_L();

                                    delay_us1(10);

                                    NSS_OUTPUT_H();

                                    delay_us1(10);

                                }

    }

    

    return 0;

}

?j结果:

文章来源:https://blog.csdn.net/weixin_71279393/article/details/135348740
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。