单片机源程序如下:
- #include "delay.h"
- #include "key.h"
- #include "hc595.h"
- #include "intrins.h "
- ///////////////////////////引脚定义///////////////////////////////
- sbit Hc595_Sck_P11 = P3^2; //输入串行移位时钟脉冲 sh_cp_595
- sbit Hc595_Rck_P12 = P3^3; //输出锁存器控制脉冲 st_cp_595
- //sbit Hc595_Oe_P13 = P1^5; //8 位并行输出使能禁能(高阻态)
- sbit Hc595_Ser_P14 = P3^4; //串行数据输入线
- unsigned char Hc595_Datas[6] = {0x00U}; //存放6个数码管显示的数据
- unsigned long Hc595_Count = 0U;//显示定时计数,
- unsigned long Hc595_Time_Count = 0U;//定时器超时计数
- unsigned long hc595_time_count_temp = 0U;//临时存储
- unsigned long hc595_time_count_temp_1 = 0U;//临时存储
- unsigned long hc595_time_count_temp_2 = 0U;//临时存储
- 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:表示不显示
- };//共阳极数码管
- /////////////////////////////////////////////////外部调用函数/////////////////////////////////////////////
- /***********************************************************************************************
- //功能:单个数据往hc595发送,串行输入数据(SER)移入寄存器
- //输入:unsigned char SendVal:要发送的数据
- //输出:无
- //备注:无
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595SendData(unsigned char SendVal)//往hc595发送数据,串行输入数据(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;
- }
- //由低电平跳为高电平,产生上升沿,串行输入数据(SER)移入寄存器
- Hc595_Sck_P11 = HC595_HIGH;
- _nop_();
- _nop_();
- Hc595_Sck_P11 = HC595_LOW;
- }
- }
- /***********************************************************************************************
- //功能:数据显示.RCK_595 产生上升沿,(输出数据用于引脚)
- //输入:无
- //输出:无
- //备注:无
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595ShowData(void)//RCK_595 产生上升沿,(输出数据用于引脚)
- {
- Hc595_Rck_P12 = HC595_LOW;
- _nop_();
- _nop_();
- Hc595_Rck_P12 = HC595_HIGH;
- _nop_();
- _nop_();
- Hc595_Rck_P12 = HC595_LOW;
- _nop_();
- _nop_();
- }
-
- /***********************************************************************************************
- //功能:6个数据级联显示
- //输入:无
- //输出:无
- //备注:无
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595LedDisplayN(void)
- {
- unsigned char i;//,j;
- unsigned char movebit[6];
- //Hc595_Oe_P13 = HC595_LOW; //选中数码管
- for(i = 0U; i < 6U; i++)
- {
- movebit[i] = Hc595_Led_Code[Hc595_Datas[i]];
- }
- // P1 = 0U;
- Delay(1U);
- for(i = 0U; i< 6U; i++) //数据移位
- {
- if(i == 1U)//第二个字段小数点常亮处理
- {
- Hc595SendData(movebit[i] & 0x7FU );//共阳极低电平点亮对应的二极管
- }
- 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;
- } //数据移位*/
- }
-
- //显示数据,
- 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_();*/
- }
- /***********************************************************************************************
- //功能:定时器计数处理函数,并将对应的数据转化为十进制数处理
- //输入:无
- //输出:无
- //备注:应用于中断中
- //日期:2018.06.05
- *************************************************************************************************/
- void Hc595TimerCountHandle(void)
- {
- unsigned char hc595_key_status = 0U;//按键状态
- unsigned char hc595_key_count_modle = 0U;//档位
- unsigned char hc595_stop_display_flag = 0U;
-
- hc595_key_count_modle = KeyCheckCount();//取按键设置的档位显示
- hc595_key_status = KeyCheckStatus();//获取按键状态
- switch(hc595_key_status)
- {
- case KeySwitchModeTimeOutInit://上电初始化状态,可用于切换档位
- Hc595_Datas[0] = hc595_key_count_modle;//取按键设置的档位显示
- Hc595_Datas[1] = 0x10U;//不显示,全关处理
- Hc595_Datas[2] = 0x10U;//不显示,全关处理
- Hc595_Datas[3] = 0x10U;//不显示,全关处理
- Hc595_Datas[4] = 0x10U;//不显示,全关处理
- Hc595_Datas[5] = 0x10U;//不显示,全关处理
- Hc595_Count = 0U;
- break;
- case KeyTimeCountInit://档位设置完成后初始态,开启计数
- case KeyTimeCountStopClear://按键计时清零状态
- case KeyTimeCountOver://计时结束
- case KeyTimeCountOverStop://超出后停止状态
- case KeyTimeCountOverInit_1://超出后停止状态
- case KeyTimeCountOverInit://超出后初始化状态
- Hc595_Count = 0U;
- Hc595_Datas[0] = 0x00U;//全险0
- Hc595_Datas[1] = 0x00U;//全险0
- Hc595_Datas[2] = 0x00U;//全险0
- Hc595_Datas[3] = 0x00U;//全险0
- Hc595_Datas[4] = 0x00U;//全险0
- Hc595_Datas[5] = 0x00U;//全险0
- break;
- case KeyTimeCountStart://开启计数
- //根据档位处理时间
- Hc595_Count ++;
- hc595_time_count_temp_2 = Hc595_Count;
- //显示
- if(hc595_key_count_modle == 5U)//第5档只显示时间
- {
- if(Hc595_Count >= 600000U)
- {
- Hc595_Count = 0U;
- Key.Key_Status = KeyTimeCountOver;//计时结束
- }
- //分钟
- 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;//计时结束
- }
- 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://停止计数状态
- 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();//数据显示处理
- }
复制代码
- #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)
- {
- //定时器0处理
- if(Timer0CheckIntFlag() == 1U)
- {
- Timer0ClearIntFlag();
- //KeyInitTimeOutHandle();
- //Hc595TimerCountHandle();
- }
- //按键处理
- if(KeyCheckUpDownStatus() == 1U)
- {
- KeyClearUpDownFlag();//清除按键标志
- KeyHandle();
- }
-
- //数码管显示处理
-
- }
- return 0U;
- }
复制代码
所有资料51hei提供下载:
STC15W408AS_V1.3.zip
(124.7 KB, 下载次数: 157)
|