?? mcs51-項(xiàng)目開(kāi)發(fā)經(jīng)典.c
字號(hào):
//=================================================================================================
// 項(xiàng)目名:MCS51-項(xiàng)目開(kāi)發(fā)經(jīng)典
// 硬件文件名:51Studay.DDB
// 程序組目錄名:MCS51-項(xiàng)目開(kāi)發(fā)經(jīng)典
// 開(kāi)始時(shí)間:2007年5月21日
// 完成時(shí)間:2007年6月10日
//-------------------------------------------------------------------------------------------------
// 程序中用到哪些C51標(biāo)準(zhǔn)函數(shù),就需要把該函數(shù)對(duì)應(yīng)的頭文件包含進(jìn)來(lái)
//-------------------------------------------------------------------------------------------------
#include <intrins.h>
#include <string.h>
#include <absacc.h>
#include <reg52.h>
//-------------------------------------------------------------------------------------------------
// 為提高書(shū)寫(xiě)效率做以下宏定義
//-------------------------------------------------------------------------------------------------
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define CLOSE 1
#define OPEN 0
//=================================================================================================
#define TEST_74HC595 1
#define TEST_24C02 2
#define TEST_93C46 3
#define SET_SD2000 4
#define READ_SD2000 5
#define TEST_18B20 6
#define TEST_HD7279 7
#define TEST_BIT_IO 8
#define TEST_TLC2543 9
#define TEST_TLC5617 10
#define TEST_PWM_DA 11
#define TEST_L297 12
#define TEST_DC_MOTOR 13
#define TEST_LCM 14
//-------------------------------------------------------------------------------------------------
// 串并轉(zhuǎn)換芯片74HC595操控位宏定義
//-------------------------------------------------------------------------------------------------
sbit SER_DATA_74HC595=P2^7; //74HC595串行數(shù)據(jù)輸入端
sbit CP_LA_74HC595 =P2^6; //74HC595寄存器數(shù)據(jù)移入鎖存器的時(shí)鐘
sbit CP_SR_74HC595 =P2^5; //74HC595串行數(shù)據(jù)移入寄存器的時(shí)鐘
//-------------------------------------------------------------------------------------------------
uchar t1_delay_time;//50毫秒計(jì)數(shù)器累加變量,在T1中斷里累加
//-------------------------------------------------------------------------------------------------
// 函數(shù)聲明
//-------------------------------------------------------------------------------------------------
void init_cpu(void);
void ctrl_74hc595(uchar led_display_data);
void ctrl_74hc595(uchar led_display_data);
//=================================================================================================
//===================== ================================================
//===================== 主程序 ================================================
//===================== ================================================
//=================================================================================================
void main (void)
{
init_cpu();
wqyloop:
ctrl_74hc595(0x00);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x01);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x02);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x04);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x08);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x10);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x20);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x40);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(~0x80);
for(t1_delay_time=0;t1_delay_time<4;);
ctrl_74hc595(0xff);
for(t1_delay_time=0;t1_delay_time<4;);
goto wqyloop;
}//The end of main()
//########################## 主程序結(jié)束 #########################################################
//########################## 子程序開(kāi)始 #########################################################
//=================================================================================================
// 函數(shù)功能:CPU初始化函數(shù)
// 串口波特率可以由T1或T2產(chǎn)生,本例中用T1產(chǎn)生波特率
// T2用于50毫秒精確定時(shí)
// T0用于PWM控制中頻率的時(shí)基
//=================================================================================================
void init_cpu(void)
{
TMOD=0x12;//T1為16位計(jì)數(shù)器,T0為8位自動(dòng)重裝載計(jì)數(shù)器
ET1=1;
TH1=0x4c;//11.0592M晶振時(shí)T1定時(shí)時(shí)間長(zhǎng)度為50毫秒
TL1=0x00;
TR1=1;//T1開(kāi)始定時(shí)
EA=1;//CPU中斷開(kāi)放
//以下是程序中軟件邏輯需要的變量初值設(shè)置和硬件初始狀態(tài)設(shè)置
ctrl_74hc595(0xff);
}
//=================================================================================================
// 74HC595實(shí)現(xiàn)串并轉(zhuǎn)換
// 其輸出允許控制端(13腳-EN)接地,表示永遠(yuǎn)允許輸出
// 數(shù)據(jù)應(yīng)從高位(MSB)到低位(LSB)串行送如74HC595
//=================================================================================================
void ctrl_74hc595(uchar led_display_data)//595的輸出,控制8個(gè)LED
{ uchar i;
for(i=0;i<8;i++)
{if(led_display_data&0x80) SER_DATA_74HC595 =1;//判斷數(shù)據(jù)最高位是0或1,并準(zhǔn)備到595數(shù)據(jù)端
else SER_DATA_74HC595 =0;
CP_SR_74HC595=0;//時(shí)鐘端上升沿將數(shù)據(jù)送入寄存器
CP_SR_74HC595=1;
led_display_data<<=1;//數(shù)據(jù)左移1位,為判斷次高位做準(zhǔn)備
}
CP_LA_74HC595 =0;//時(shí)鐘端上升沿將寄存器中的數(shù)據(jù)送入鎖存器
CP_LA_74HC595 =1;
}
//=================================================================================================
// T1中斷服務(wù)程序
//=================================================================================================
void T1_interrupt(void) interrupt 3
{
TH1=0x4c;//11.0592M晶振時(shí)T1定時(shí)時(shí)間長(zhǎng)度為50毫秒
TL1=0x00;
t1_delay_time++;//在需要延時(shí)的地方清空并判斷該變量
}
//=================================================================================================
// end of the file
//=================================================================================================
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -