|
單片機(jī)源程序如下:
- #include "delay.h"
- #include "key.h"
- #include "hc595.h"
- #include "intrins.h "
- ///////////////////////////引腳定義///////////////////////////////
- sbit Hc595_Sck_P11 = P3^2; //輸入串行移位時(shí)鐘脈沖 sh_cp_595
- sbit Hc595_Rck_P12 = P3^3; //輸出鎖存器控制脈沖 st_cp_595
- //sbit Hc595_Oe_P13 = P1^5; //8 位并行輸出使能禁能(高阻態(tài))
- sbit Hc595_Ser_P14 = P3^4; //串行數(shù)據(jù)輸入線
- unsigned char Hc595_Datas[6] = {0x00U}; //存放6個(gè)數(shù)碼管顯示的數(shù)據(jù)
- unsigned long Hc595_Count = 0U;//顯示定時(shí)計(jì)數(shù),
- unsigned long Hc595_Time_Count = 0U;//定時(shí)器超時(shí)計(jì)數(shù)
- unsigned long hc595_time_count_temp = 0U;//臨時(shí)存儲(chǔ)
- unsigned long hc595_time_count_temp_1 = 0U;//臨時(shí)存儲(chǔ)
- unsigned long hc595_time_count_temp_2 = 0U;//臨時(shí)存儲(chǔ)
- unsigned char Hc595_Led_Code[]={
- 0x3FU, // 0
- 0x06U, // 1
- 0x5BU, // 2
- 0x4FU, // 3
- 0x66U, // 4
- 0x6DU, // 5
- 0x7DU, // 6
- 0x07U, // 7
- 0x7FU, // 8
- 0x6FU, // 9
- 0x77U, // A
- 0x7CU, // B
- 0x39U, // C
- 0x5EU, // D
- 0x79U, // E
- 0x71U, // F
- 0x00U //0x10:表示不顯示
- };//共陽極數(shù)碼管
- /////////////////////////////////////////////////外部調(diào)用函數(shù)/////////////////////////////////////////////
- /***********************************************************************************************
- //功能:單個(gè)數(shù)據(jù)往hc595發(fā)送,串行輸入數(shù)據(jù)(SER)移入寄存器
- //輸入:unsigned char SendVal:要發(fā)送的數(shù)據(jù)
- //輸出:無
- //備注:無
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595SendData(unsigned char SendVal)//往hc595發(fā)送數(shù)據(jù),串行輸入數(shù)據(jù)(SER)移入寄存器
- {
- unsigned char i;
- for(i = 0U; i < 8U; i++)
- {
- if((SendVal <<i ) & 0x80U)
- {
- Hc595_Ser_P14 = HC595_HIGH;
- }
- else
- {
- Hc595_Ser_P14 = HC595_LOW;
- }
- //由低電平跳為高電平,產(chǎn)生上升沿,串行輸入數(shù)據(jù)(SER)移入寄存器
- Hc595_Sck_P11 = HC595_HIGH;
- _nop_();
- _nop_();
- Hc595_Sck_P11 = HC595_LOW;
- }
- }
- /***********************************************************************************************
- //功能:數(shù)據(jù)顯示.RCK_595 產(chǎn)生上升沿,(輸出數(shù)據(jù)用于引腳)
- //輸入:無
- //輸出:無
- //備注:無
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595ShowData(void)//RCK_595 產(chǎn)生上升沿,(輸出數(shù)據(jù)用于引腳)
- {
- Hc595_Rck_P12 = HC595_LOW;
- _nop_();
- _nop_();
- Hc595_Rck_P12 = HC595_HIGH;
- _nop_();
- _nop_();
- Hc595_Rck_P12 = HC595_LOW;
- _nop_();
- _nop_();
- }
-
- /***********************************************************************************************
- //功能:6個(gè)數(shù)據(jù)級(jí)聯(lián)顯示
- //輸入:無
- //輸出:無
- //備注:無
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595LedDisplayN(void)
- {
- unsigned char i;//,j;
- unsigned char movebit[6];
- //Hc595_Oe_P13 = HC595_LOW; //選中數(shù)碼管
- for(i = 0U; i < 6U; i++)
- {
- movebit[i] = Hc595_Led_Code[Hc595_Datas[i]];
- }
- // P1 = 0U;
- Delay(1U);
- for(i = 0U; i< 6U; i++) //數(shù)據(jù)移位
- {
- if(i == 1U)//第二個(gè)字段小數(shù)點(diǎn)常亮處理
- {
- Hc595SendData(movebit[i] & 0x7FU );//共陽極低電平點(diǎn)亮對應(yīng)的二極管
- }
- else
- {
- Hc595SendData(movebit[i]);
- }
-
- /*test = movebit[i];
- for(j = 0U;j < 8U; j++)
- {
- testb = (test & 0x80U);
- test = (test << 1U);
- if(testb)
- {
- Hc595_Ser_P14 = HC595_HIGH;
- }
- else
- {
- Hc595_Ser_P14 = HC595_LOW;
- }
- Hc595_Sck_P11 = HC595_HIGH;
- _nop_();
- _nop_();
- _nop_();
- Hc595_Sck_P11 = HC595_LOW;
- } //數(shù)據(jù)移位*/
- }
-
- //顯示數(shù)據(jù),
- Hc595ShowData();
- /*Hc595_Rck_P12 = HC595_LOW;
- _nop_();
- _nop_();
- _nop_();
- Hc595_Rck_P12 = HC595_HIGH;
- _nop_();
- _nop_();
- _nop_();
- Hc595_Rck_P12 = HC595_LOW;
- _nop_();
- _nop_();
- _nop_();*/
- }
- /***********************************************************************************************
- //功能:定時(shí)器計(jì)數(shù)處理函數(shù),并將對應(yīng)的數(shù)據(jù)轉(zhuǎn)化為十進(jìn)制數(shù)處理
- //輸入:無
- //輸出:無
- //備注:應(yīng)用于中斷中
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595TimerCountHandle(void)
- {
- unsigned char hc595_key_status = 0U;//按鍵狀態(tài)
- unsigned char hc595_key_count_modle = 0U;//檔位
- unsigned char hc595_stop_display_flag = 0U;
-
- hc595_key_count_modle = KeyCheckCount();//取按鍵設(shè)置的檔位顯示
- hc595_key_status = KeyCheckStatus();//獲取按鍵狀態(tài)
- switch(hc595_key_status)
- {
- case KeySwitchModeTimeOutInit://上電初始化狀態(tài),可用于切換檔位
- Hc595_Datas[0] = hc595_key_count_modle;//取按鍵設(shè)置的檔位顯示
- Hc595_Datas[1] = 0x10U;//不顯示,全關(guān)處理
- Hc595_Datas[2] = 0x10U;//不顯示,全關(guān)處理
- Hc595_Datas[3] = 0x10U;//不顯示,全關(guān)處理
- Hc595_Datas[4] = 0x10U;//不顯示,全關(guān)處理
- Hc595_Datas[5] = 0x10U;//不顯示,全關(guān)處理
- Hc595_Count = 0U;
- break;
- case KeyTimeCountInit://檔位設(shè)置完成后初始態(tài),開啟計(jì)數(shù)
- case KeyTimeCountStopClear://按鍵計(jì)時(shí)清零狀態(tài)
- case KeyTimeCountOver://計(jì)時(shí)結(jié)束
- case KeyTimeCountOverStop://超出后停止?fàn)顟B(tài)
- case KeyTimeCountOverInit_1://超出后停止?fàn)顟B(tài)
- case KeyTimeCountOverInit://超出后初始化狀態(tài)
- Hc595_Count = 0U;
- Hc595_Datas[0] = 0x00U;//全險(xiǎn)0
- Hc595_Datas[1] = 0x00U;//全險(xiǎn)0
- Hc595_Datas[2] = 0x00U;//全險(xiǎn)0
- Hc595_Datas[3] = 0x00U;//全險(xiǎn)0
- Hc595_Datas[4] = 0x00U;//全險(xiǎn)0
- Hc595_Datas[5] = 0x00U;//全險(xiǎn)0
- break;
- case KeyTimeCountStart://開啟計(jì)數(shù)
- //根據(jù)檔位處理時(shí)間
- Hc595_Count ++;
- hc595_time_count_temp_2 = Hc595_Count;
- //顯示
- if(hc595_key_count_modle == 5U)//第5檔只顯示時(shí)間
- {
- if(Hc595_Count >= 600000U)
- {
- Hc595_Count = 0U;
- Key.Key_Status = KeyTimeCountOver;//計(jì)時(shí)結(jié)束
- }
- //分鐘
- Hc595_Datas[0] = (Hc595_Count / 6000U) / 10U;
- Hc595_Datas[1] = (Hc595_Count / 6000U) % 10U;
- //秒
- Hc595_Datas[2] = (Hc595_Count % 6000U) / 1000U;
- Hc595_Datas[3] = (Hc595_Count % 1000U) / 100U;
- //毫秒
- Hc595_Datas[4] = (Hc595_Count % 100U)/10U;
- Hc595_Datas[5] = (Hc595_Count % 10U)/1U;
- }
- else
- {
- hc595_time_count_temp = Hc595_Count;
- if(hc595_key_count_modle == 0U)
- {
- hc595_time_count_temp = (Hc595_Count*5 + Hc595_Count%5);//400us
- }
- else if(hc595_key_count_modle == 1U)
- {
- hc595_time_count_temp = (Hc595_Count*4 + Hc595_Count%4);
- }
- else if(hc595_key_count_modle == 2U)
- {
- hc595_time_count_temp = (Hc595_Count*3 + Hc595_Count%3);
- }
- else if(hc595_key_count_modle == 3U)
- {
- hc595_time_count_temp = (Hc595_Count*2 + Hc595_Count%2);
- }
- else if(hc595_key_count_modle == 4U)
- {
- hc595_time_count_temp = (Hc595_Count + Hc595_Count);
- }
-
- //if((hc595_key_count_modle == 0U) || (hc595_key_count_modle == 1U) || (hc595_key_count_modle == 4U))
- {
- if(hc595_time_count_temp >= 10000U)
- {
- Hc595_Count = 0U;
- hc595_time_count_temp = 0U;
- Key.Key_Status = KeyTimeCountOver;//計(jì)時(shí)結(jié)束
- }
- Hc595_Datas[0] = (Hc595_Count % 10000U)/1000U;
- Hc595_Datas[1] = (Hc595_Count % 1000U)/100U;
- Hc595_Datas[2] = (hc595_time_count_temp % 100U)/10U;
- Hc595_Datas[3] = (hc595_time_count_temp % 10U)/1U;
- Hc595_Datas[4] = (hc595_time_count_temp % 100U)/10U;
- Hc595_Datas[5] = (hc595_time_count_temp % 10U)/1U;
- }
- hc595_time_count_temp_1 = ((unsigned long)Hc595_Datas[0])*100000U + ((unsigned long)Hc595_Datas[1])*10000U + ((unsigned long)Hc595_Datas[2])*1000U + ((unsigned long)Hc595_Datas[3])*100U + ((unsigned long)Hc595_Datas[4])*10U + (unsigned long)Hc595_Datas[4];
-
- }
- break;
- case KeyTimeCountStop://停止計(jì)數(shù)狀態(tài)
- if(hc595_key_count_modle != 5U)
- {
- switch(hc595_key_count_modle)
- {
- case 0U:
- if((hc595_time_count_temp_1 >= 99990U) && (hc595_time_count_temp_1 <= 100000U))
- {
- hc595_time_count_temp = 100000U;
- hc595_stop_display_flag = 1U;
- }
- break;
- case 1U:
- if((hc595_time_count_temp_1 >= 99970U) && (hc595_time_count_temp_1 <= 100000U))
- {
- hc595_time_count_temp = 100000U;
- hc595_stop_display_flag = 1U;
- }
- break;
- case 2U:
- if((hc595_time_count_temp_1 >= 99950U) && (hc595_time_count_temp_1 <= 100000U))
- {
- hc595_time_count_temp = 100000U;
- hc595_stop_display_flag = 1U;
- }
- break;
- case 3U:
- if((hc595_time_count_temp_1 >= 99910U) && (hc595_time_count_temp_1 <= 100000U))
- {
- hc595_time_count_temp = 100000U;
- hc595_stop_display_flag = 1U;
- }
- break;
- case 4U:
- if((hc595_time_count_temp_1 >= 98000U) && ((hc595_time_count_temp_1 <= 100000U)))
- {
- hc595_time_count_temp = 100000U;
- hc595_stop_display_flag = 1U;
- }
- break;
- default:
- break;
- }
- if(hc595_stop_display_flag == 1U)
- {
- Hc595_Datas[0] = (hc595_time_count_temp % 1000000U)/100000U;
- Hc595_Datas[1] = (hc595_time_count_temp % 100000U)/10000U;
- Hc595_Datas[2] = (hc595_time_count_temp % 10000U)/1000U;
- Hc595_Datas[3] = (hc595_time_count_temp % 1000U)/100U;
- Hc595_Datas[4] = (hc595_time_count_temp % 100U)/10U;
- Hc595_Datas[5] = (hc595_time_count_temp % 10U)/1U;
- }
- }
-
- Hc595_Count = 0U;
-
- break;
-
- default:
- break;
- }
-
- Hc595LedDisplayN();//數(shù)據(jù)顯示處理
- }
復(fù)制代碼
- #include "stc15w408as.h"
- #include "hc595.h"
- #include "key.h"
- #include "sys.h"
- #include "timer0.h"
- int main(void)
- {
- Timer0Init();
- KeyInit();
-
- SYS_INTERUPT_EN();
- while(1)
- {
- //定時(shí)器0處理
- if(Timer0CheckIntFlag() == 1U)
- {
- Timer0ClearIntFlag();
- //KeyInitTimeOutHandle();
- //Hc595TimerCountHandle();
- }
- //按鍵處理
- if(KeyCheckUpDownStatus() == 1U)
- {
- KeyClearUpDownFlag();//清除按鍵標(biāo)志
- KeyHandle();
- }
-
- //數(shù)碼管顯示處理
-
- }
- return 0U;
- }
復(fù)制代碼
所有資料51hei提供下載:
STC15W408AS_V1.3.zip
(124.7 KB, 下載次數(shù): 157)
2018-6-14 20:39 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|