spi.h
#ifndef __SPI_H__
#define __SPI_H__#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"#include"uart4.h"
#include"key_it.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_ms(int ms);
void bian();
#endif
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)
{//1.使能GPIOE时钟RCC->MP_AHB4LPENSETR |=(0X1<<4);//2.MOSI管脚PE14 设置输出的属性GPIOE->MODER &=(~(0X3<<28));GPIOE->MODER |=(0X1<<28);GPIOE->OTYPER &=(~(0X1<<14));GPIOE->OSPEEDR &=(~(0X3<<28));GPIOE->PUPDR &=(~(0X3<<28));//3.MISO管脚PE13 设置输入的属性GPIOE->MODER &=(~(0X3<<26)); GPIOE->OSPEEDR &=(~(0X3<<26));GPIOE->PUPDR &=(~(0X3<<26));//4.SCK管脚PE12 设置输出的属性GPIOE->MODER &=(~(0x3<<24));GPIOE->MODER |=(0x1<<24);GPIOE->OTYPER &=(~(0X1<<12));GPIOE->OSPEEDR &=(~(0X3<<24));GPIOE->PUPDR &=(~(0X3<<24));//5.RCK(NSS)管脚PE11 设置输出的属性GPIOE->MODER &=(~(0X3<<22));GPIOE->MODER |=(0X1<<22);GPIOE->OTYPER &=(~(0X1<<11));GPIOE->OSPEEDR &=(~(0X3<<22));GPIOE->PUPDR &=(~(0X3<<22));//默认PE12和PE11输出低电平,方便产生上升沿NSS_OUTPUT_L();SCK_OUTPUT_L();}void SPI_write(unsigned char dat)
{int i;for(i=0;i<8;i++){if(dat&0X01){//输出高电平MOSI_OUTPUT_H();}else{//输出低电平MOSI_OUTPUT_L();}dat >>= 1;//产生上升沿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 < ms;i++)for (j = 0; j < 1800; j++);} void bian(){NSS_OUTPUT_L();delay_ms(1);NSS_OUTPUT_H();delay_ms(1000);}//保存0-9编码的数组int main(void){//unsigned char i;SPI_init();while(1){// 发送数码管的位 SPI_write(0X80);SPI_write(0XFC);bian();SPI_write(0X40);SPI_write(0XF2);bian();SPI_write(0X20);SPI_write(0X3e);bian();SPI_write(0X10);SPI_write(0XF6);bian();}return 0;}
spi.h(加了key_it.h)
#ifndef __SPI_H__
#define __SPI_H__#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"#include"uart4.h"
#include"key_it.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_ms(int ms);
void bian();
#endif
main.c
#include "spi.h"#include"key_it.h"extern void printf(const char *fmt, ...);void delay_ms(int ms){int i,j;for(i = 0; i < ms;i++)for (j = 0; j < 1800; j++);} //保存0-9编码的数组int main(void){SPI_init();key1_it_config();key2_it_config();key3_it_config();//unsigned char i;while(1){// 发送数码管的位 printf("do_main.....\n");delay_ms(1000);}return 0;}
spi.c
同上
do_irq.c
#include"led.h"
#include"spi.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void bian()
{NSS_OUTPUT_L();delay_ms(1);NSS_OUTPUT_H();delay_ms(1000);
}
void do_irq(void)
{int irqno=(GICC->IAR &0X3FF);switch(irqno){case 99:{printf("im key1\n");SPI_write(0X80);SPI_write(0XFC);bian();SPI_write(0X20);SPI_write(0X3e);bian();GICD->ICPENDR[3] |=(0X1<<3);EXTI->FPR1 |=(0X1<<9);led1_off();}break;case 98:{printf("im key3\n");SPI_write(0X40);SPI_write(0XF2);bian();GICD->ICPENDR[3] |=(0X1<<2);EXTI->FPR1 |=(0X1<<8);}break;case 97: {printf("im key2\n");SPI_write(0X10);SPI_write(0XF6);bian();GICD->ICPENDR[3] |=(0X1<<1);EXTI->FPR1 |=(0X1<<7);}break;}GICC->EOIR=irqno;
}