ARM1.2作业

实现数码管不同位显示不同的数字

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发送数据的函数
void SPI_write(unsigned char dat);
void delay_us1(unsigned int us);
#endif

spi.c

#include "spi.h"

void delay_us1(unsigned int us)
{
    int i,j;
    for(i = 0;iMP_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));
    //MOSI   PE13
    GPIOE->MODER &= (~(0x3 <<26));
    GPIOE->OSPEEDR &= (~(0x3 << 26));
    GPIOE->PUPDR &= (~(0x3 <<26));
    //MOSI   PE12
    GPIOE->MODER &= (~(0x3 <<24));
    GPIOE->MODER |= (0x1 <<24);
    GPIOE->OTYPER &= (~(0x1 << 12));
    GPIOE->OSPEEDR &= (~(0X3 << 24));
    GPIOE->PUPDR &= (~(0X3 <<24));
    //MOSI   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();
        delay_us1(10);
        SCK_OUTPUT_H();
        delay_us1(10);
    }
}

main.c

#include "spi.h"

extern void printf(const char *fmt,...);

void delay_ms(int ms)

{

	int i,j;

	for(i=0;i

将温湿度数据显示到数码

main.c

#include "spi.h"

#include "si7006.h"

extern void printf(const char *fmt,...);

void delay_ms(int ms)

{

	int i,j;

	for(i=0;i

你可能感兴趣的:(ar)