?? main.c
字號(hào):
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: main.c
** Last modified Date: 2004-09-16
** Last Version: 1.0
** Descriptions: The main() function example template
**
**------------------------------------------------------------------------------------------------------
** Created by: Chenmingji
** Created date: 2004-09-16
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by: Chenixbing
** Modified date: 2005-01-18
** Version:
** Descriptions: SSP作SPI主機(jī)控制74HC595。
**
********************************************************************************************************
********************************************************************************************************/
#include "config.h"
// SSP作主機(jī),當(dāng)只和1個(gè)從機(jī)通信的時(shí)候,可以不用另外的IO口作從機(jī)選擇線,直接用SSEL1即可。
//#define SLAVE_CS (1 << 29) // P0.29口為SSP為從機(jī)的選擇控制引腳
/*
*********************************************************************************************************
** 函數(shù)名稱:DelayNS()
** 函數(shù)功能:長軟件延時(shí)
** 入口參數(shù):dly 延時(shí)參數(shù),值越大,延時(shí)越久
** 出口參數(shù):無
*********************************************************************************************************
*/
void DelayNS(uint32 dly)
{
uint32 i;
for(; dly>0; dly--)
{
for(i=0; i<50000; i++);
}
}
/*
*********************************************************************************************************
** 函數(shù)名稱:SSP_Init()
** 函數(shù)功能:將SSP控制器設(shè)置為主機(jī)SPI。
** 入口參數(shù):無
** 出口參數(shù):無
*********************************************************************************************************
*/
void SSP_Init(void)
{
SSPCR0 = (0x01 << 8) | // SCR 設(shè)置SPI時(shí)鐘分頻
(0x00 << 7) | // CPHA 時(shí)鐘輸出相位,僅SPI模式有效
(0x01 << 6) | // CPOL 時(shí)鐘輸出極性,僅SPI模式有效
(0x00 << 4) | // FRF 幀格式 00=SPI,01=SSI,10=Microwire,11=保留
(0x07 << 0); // DSS 數(shù)據(jù)長度,0000-0010=保留,0011=4位,0111=8位,1111=16位
SSPCR1 = (0x00 << 3) | // SOD 從機(jī)輸出禁能,1=禁止,0=允許
(0x00 << 2) | // MS 主從選擇,0=主機(jī),1=從機(jī)
(0x01 << 1) | // SSE SSP使能,1=允許SSP與其它設(shè)備通信
(0x00 << 0); // LBM 回寫模式
SSPCPSR = 0x52; // PCLK分頻值
// SSPIMSC = 0x07; // 中斷屏蔽寄存器
SSPICR = 0x03; // 中斷清除寄存器
}
/*
*********************************************************************************************************
* 函數(shù)名稱:SSP_SendData()
* 函數(shù)功能:SSP接口向SPI總線發(fā)送數(shù)據(jù)。
* 入口參數(shù):data 待發(fā)送的數(shù)據(jù)
* 出口參數(shù):返回值為讀取的數(shù)據(jù)
*********************************************************************************************************
*/
uint8 SSP_SendData(uint8 data)
{
// IOCLR = SLAVE_CS; // 選擇從機(jī)
SSPDR = data;
while( (SSPSR & 0x01) == 0 ); // 等待TFE置位,即發(fā)送FIFO空
// IOSET = SLAVE_CS;
return(SSPDR);
}
/* 此表為LED0~F以及L、P的字模 */
uint8 const DISP_TAB[16] = {
// 0 1 2 3 4 5 6 7 8 9
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,
// A b C d E F
0x88, 0x83, 0xC6, 0xA1,0x86, 0x8E};
/*
*********************************************************************************************************
* 函數(shù)名稱:main
* 函數(shù)功能:SSP作SPI 主機(jī),控制74HC595驅(qū)動(dòng)LED顯示。
*********************************************************************************************************
*/
volatile uint8 rcv;
int main(void)
{
uint8 i;
PCONP |= 1<<10;
// PINSEL1 = 0x000002A8; // 設(shè)置SSP管腳連接
// PINSEL1 = 0xAA << 2;
PINSEL1 = (PINSEL1 & (~(0xFF << 2))) | (0xAA << 2);
// IO0DIR = SLAVE_CS;
// IO0SET = SLAVE_CS;
SSP_Init(); // 初始化SSP接口
while(1)
{
for(i=0; i<16; i++)
{
rcv = SSP_SendData(DISP_TAB[i]); // 發(fā)送顯示數(shù)據(jù)
DelayNS(80); // 延時(shí)
}
}
return(0);
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -