1.目標(biāo):
? ? ? ? a.數(shù)碼管顯示相同的值 0000 1111 ......9999;
? ? ? ? b.數(shù)碼管顯示不同的值 1234;
2.分析m74hc595芯片內(nèi)部框圖;
?真值表:
3.代碼;
---spi.h頭文件---
#ifndef __SPI_H__
#define __SPI_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
// MOSI對(duì)應(yīng)的引腳輸出高低電平的信號(hào)
#define MOSI_OUTPUT_H() do{GPIOE->ODR |= (0x1 << 14);}while(0)
#define MOSI_OUTPUT_L() do{GPIOE->ODR &= (~(0x1 << 14));}while(0)
// 對(duì)應(yīng)595芯片的鎖存引腳輸出高低電平
#define NSS_OUTPUT_H() do{GPIOE->ODR |= (0x1 << 11);}while(0)
#define NSS_OUTPUT_L() do{GPIOE->ODR &= (~(0x1 << 11));}while(0)
// 時(shí)鐘信號(hào)對(duì)應(yīng)的引腳輸出高低電平
#define SCK_OUTPUT_H() do{GPIOE->ODR |= (0x1 << 12);}while(0)
#define SCK_OUTPUT_L() do{GPIOE->ODR &= (~(0x1 << 12));}while(0)
/*
* 函數(shù)功能: SPI初始化函數(shù),推挽輸出,高速,禁止上拉和下拉
* 函數(shù)參數(shù):無
* 函數(shù)返回值:無
*/
void SPI_init(void);
/*
* 函數(shù)功能:SPI發(fā)送數(shù)據(jù)的函數(shù)
* 函數(shù)參數(shù):dat : 要發(fā)送的數(shù)據(jù)
* 函數(shù)返回值:無
*
*/
void SPI_write(unsigned char dat);
#endif // __SPI_H__
---spi.c函數(shù)文件---
#include "spi.h"
/* SPI4_NSS ----> PE11
* SPI4_SCK ----> PE12
* SPI4_MOSI ----> PE14
* SPI4_MISO ----> PE13
* */
/* 數(shù)碼管的編碼, 先發(fā)送低位,在發(fā)送高位
* A B C D E F G DP
* 1 1 1 1 1 1 0 0 0xFC 0
* 0 1 1 0 0 0 0 0 0x60 1
* 1 1 0 1 1 0 1 0 0xDA 2
* 1 1 1 1 0 0 1 0 0xF2 3
* 0 1 1 0 0 1 1 0 0x66 4
* 1 0 1 1 0 1 1 0 0xB6 5
* 1 0 1 1 1 1 1 0 0xBE 6
* 1 1 1 0 0 0 0 0 0xE0 7
* 1 1 1 1 1 1 1 0 0xFE 8
* 1 1 1 1 0 1 1 0 0xF6 9
* */
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的時(shí)鐘線拉低
}
void SPI_write(unsigned char dat)
{
//1.for循環(huán)
unsigned char i;
for(i=0; i<8; i++)
{
if(dat & 0x01) //先發(fā)低位,再發(fā)高位
MOSI_OUTPUT_H(); //發(fā)1
else
MOSI_OUTPUT_L(); //發(fā)0
dat >>= 1;
//移位寄存器時(shí)鐘,上升沿
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++);
}
int num[10] = {0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6};
int main()
{
SPI_init();
//實(shí)驗(yàn)1:0000 1111 .... 9999
unsigned char i;
while(1)
{
/* for(i=0; i<10; i++)
{
//循環(huán)條
SPI_write(0xF0); //傳位
SPI_write(num[i]); //傳段
//鎖存時(shí)鐘上升沿,鎖存器數(shù)據(jù)才會(huì)變化
NSS_OUTPUT_L();
delay_ms(10);
NSS_OUTPUT_H();
delay_ms(10);
}
*/
/ /實(shí)驗(yàn)2:1234
for(i=0; i<4; i++)
{
SPI_write(0x80 >> i);
SPI_write(num[i+1]);
//鎖存時(shí)鐘上升沿,鎖存器數(shù)據(jù)才會(huì)變化
NSS_OUTPUT_L();
delay_ms(1);
NSS_OUTPUT_H();
}
}
return 0;
}
4.運(yùn)行結(jié)果;
a.數(shù)碼管顯示相同的值 0000 1111 ......9999;
文章來源:http://www.zghlxwxcb.cn/news/detail-669839.html
b.數(shù)碼管顯示不同的值 1234;
文章來源地址http://www.zghlxwxcb.cn/news/detail-669839.html
到了這里,關(guān)于ARM開發(fā),stm32mp157a-A7核SPI總線實(shí)驗(yàn)(實(shí)現(xiàn)數(shù)碼管的顯示)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!