新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > SPI主機實驗---7段數(shù)碼管顯示

SPI主機實驗---7段數(shù)碼管顯示

作者: 時間:2016-11-10 來源:網(wǎng)絡 收藏
1、功能:在7段數(shù)碼管上顯示一些字符

2、原理圖:

本文引用地址:http://butianyuan.cn/article/201611/317313.htm

3、程序清單:

/****************************************Copyright (c)**************************************************
**--------------File Info-------------------------------------------------------------------------------
** File name:main.c
** Last modified Date: 2011-04-24
** Last Version:1.0
** Descriptions:The main() function example template
**------------------------------------------------------------------------------------------------------
** Created by:lxliu
** Created date:2011-04-24
** Version:1.0
** Descriptions:The original version
**
********************************************************************************************************/
#include "config.h"

#define HC595_CS (1<<29) //P0.29為74HC595的片選

/*************************************************************************
** 函數(shù)名稱:DelayNS()
** 函數(shù)功能:長軟件延時
** 入口參數(shù):dly延時控制值,值越大,延時越長
** 出口參數(shù):無
*************************************************************************/

void DelayNS(uint32 dly)
{
uint32 i;
for(;dly>0;dly--)
for(i=0;i<50000;i++);
}

/*************************************************************************
** 函數(shù)名稱:MSPI_Init()
** 函數(shù)功能:初始化SPI接口,設置為主機
** 入口參數(shù):無
** 出口參數(shù):無
*************************************************************************/
void MSPI_Init(void)
{
PINSEL0 = (PINSEL0 & (~(0xFF<<8)))|(0x55<<8);
S0PCCR= 0x52;
S0PCR= (0<<3)|
(1<<4)|
(1<<5)|
(0<<6)|
(0<<7);

}


/*************************************************************************
** 函數(shù)名稱:MSPI_SendData()
** 函數(shù)功能:向SPI總線發(fā)送數(shù)據(jù)
** 入口參數(shù):data 待發(fā)送的數(shù)據(jù)
** 出口參數(shù):返回值為讀取的數(shù)據(jù)
*************************************************************************/
uint8 MSPI_SendData(uint8 data)
{
IO0CLR = HC595_CS; // 片選74HC595

S0PDR = data;
while(0==(S0PSR & 0x80));// 等待SPIF置位,即等待數(shù)據(jù)發(fā)送完畢

IO0SET = HC595_CS;
return(SPI_SPDR);
}


/* 此表為LED0~~F及LPC的字模 */
uint8 const DISP_TAB[19] = {
// 0 1 2 3 4 5 6 7 8 9
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,
// AB CD E F
0x88, 0x83, 0xC6, 0xA1,0x86, 0x8E,
// L P C
0xC7, 0x8C,0xC6 };

/* 此表為LPC2131字模 */
// L P C 2 1 3 1
uint8 const LPC2131[7] = { 0xC7, 0x8C, 0xC6, 0xA4, 0xF9, 0xB0, 0xF9};
/********************************************************************************************************
** 函數(shù)名稱:main()
** 函數(shù)功能:使用硬件SPI,74HC595驅動控制7段數(shù)碼管顯示。
** 先顯示0~F的字模,然后顯示LPC2131字樣。
********************************************************************************************************/
uint8 rcv_data;
int main (void)
{
uint8 i;

PINSEL0 = 0x00005500; // 設置SPI管腳連接
PINSEL1 = 0x00000000;
IO0DIR = HC595_CS;

MSPI_Init(); // 初始化SPI接口
while(1)
{
/* 顯示0~F字模 */
for(i=0; i<16; i++)
{
rcv_data = MSPI_SendData(DISP_TAB[i]);// 發(fā)送顯示數(shù)據(jù)
DelayNS(80); // 延時
}
/* 顯示LPC2131字樣 */
for(i=0; i<7; i++)
{
rcv_data = MSPI_SendData(LPC2131[i]);// 發(fā)送顯示數(shù)據(jù)
DelayNS(80); // 延時
}

}

return 0;
}

/*********************************************************************************************************
** End Of File
********************************************************************************************************/

4、Debug

(1)寄存器程序中使用的時候應該加上編號:SPCCR,SPCR應該寫為:S0PCCR,S0PCR

(2) S0PCCR,S0PCR這兩個寄存器不要寫錯了。

在調試的時候,程序一直停在while(0==(S0PSR & 0x80));這一行,仔細檢查這一行代碼,并沒有發(fā)現(xiàn)錯誤,最后發(fā)現(xiàn)是S0PCCR,S0PCR這兩個寄存器寫反了



評論


技術專區(qū)

關閉