STC15頻率產生器(粗調+微調+數碼管顯示)完整程式碼
阿新 • • 發佈:2018-11-25
#include "stc15.h" #include "intrins.h" #include "gpio.h" #include "595hc.h" #define MAIN_Fosc 11059200L //定義主時鐘 u16 counter = 0; u8 countdown = 5; u16 i,j,k; bit start_flag = 0; bit next_flag = 0; bit confirm_flag = 0; bit up = 0; bit down = 0; u16 Fre_input = 0; u16 Fre_counter = 0; u8 code start[8] = {1,22,24,25,26,17,17,17}; // Y P04 P05 P06 P07 // | | | | // X | | | | // P00 ---- K00 ---- K01 ---- K02 ---- K03 ---- // | | | | // P01 ---- K04 ---- K05 ---- K06 ---- K07 ---- // | | | | // P02 ---- K08 ---- K09 ---- K10 ---- K11 ---- // | | | | // P03 ---- K12 ---- K13 ---- K14 ---- K15 ---- // | | | | u8 KeyCode = 0; //給使用者使用的鍵碼, 1~16有效 u8 IO_KeyState = 0, IO_KeyState1 = 0, IO_KeyHoldCnt = 0; //行列鍵盤變數 static u8 get_flag = 1; void IO_KeyDelay(void) { i = 60; while (--i); } void IO_KeyScan(void) //50ms call { j = IO_KeyState1; //儲存上一次狀態 P0 = 0xf0; //X低,讀Y IO_KeyDelay(); IO_KeyState1 = P0 & 0xf0; P0 = 0x0f; //Y低,讀X IO_KeyDelay(); IO_KeyState1 |= (P0 & 0x0f); IO_KeyState1 ^= 0xff; //取反 if (j == IO_KeyState1) //連續兩次讀相等 { j = IO_KeyState; IO_KeyState = IO_KeyState1; if (IO_KeyState != 0) //有鍵按下 { F0 = 0; if (j == 0) F0 = 1; //第一次按下 else if (j == IO_KeyState) { if (++IO_KeyHoldCnt >= 20) //1秒後重鍵 { IO_KeyHoldCnt = 18; F0 = 1; } } if (F0) { next_flag = 1; switch(IO_KeyState) { case 0x11: get_flag = 0 ;break;//0 case 0x12: get_flag = 1 ;break;//1 case 0x14: get_flag = 2 ;break;//2 case 0x18: get_flag = 3 ;break;//3 case 0x21: get_flag = 4 ;break;//4 case 0x22: get_flag = 5 ;break;//5 case 0x24: get_flag = 6 ;break;//6 case 0x28: get_flag = 7 ;break;//7 case 0x41: get_flag = 8 ;break;//8 case 0x42: get_flag = 9 ;break;//9 case 0x44: get_flag = 10 ;break;//a case 0x48: get_flag = 11 ;break;//b case 0x81: get_flag = 12 ;break;//c case 0x82: up = 1 ;break;//d case 0x84: down = 1 ;break;//e case 0x88: IAP_CONTR = 0x20;break;//復位 default: get_flag = 16 ;break; } } } else IO_KeyHoldCnt = 0; } P0 = 0xff; } /********************延時函式******************************/ void Delay1ms()//@11.0592MHz { _nop_(); _nop_(); _nop_(); i = 11; j = 190; do { while (--j); } while (--i); } void Delay_ms(u16 xms) { for (k=0;k<xms;k++) { Delay1ms(); } } /********************** Timer0初始化 ************************/ void Timer0Init(void)//1毫秒 @11.0592MHz { AUXR = 0x80;//定時器時鐘1T模式 TMOD = 0x00;//設定定時器模式 TL0 = 0xCD;//設定定時初值 TH0 = 0xD4;//設定定時初值 EA = 1; //開啟總中斷 ET0 = 1;//允許定時器0中斷 TF0 = 0;//清除TF0標誌 TR0 = 1;//定時器0開始計時 } /********************** Timer0中斷函式 ************************/ void timer0 (void) interrupt 1 { counter++; Fre_counter++; DisplayScan(); } /********************** 頻率產生 ************************/ void Fre_output(u16 Fre)//T=Fre(ms);f=1000/Fre { LED8[0] = 15; //F LED8[1] = 29; //r LED8[2] = 14; //E LED8[3] = 17; //- LED8[4] = 17; //- Fre = 500 / Fre; if(Fre_counter >= Fre) { P16 = !P16; Fre_counter = 0; } } /********************** 步進加_減 ************************/ //返回1:加 //返回0:減 int up_down(bit up,bit down) { if(up == 1)//步進 - 加1 { LED8[7] = LED8[7] + 1; if(LED8[7] == 10) { if(LED8[6] == 9) { if(LED8[5] == 9) { LED8[7] = 9; LED8[6] = 9; LED8[5] = 9; } else { LED8[7] = 0; LED8[6] = 0; LED8[5] ++; } } else { LED8[7] = 0; LED8[6] ++; } } return 1; } else if (down == 1)//步進 - 減1 { LED8[7] = LED8[7] - 1; if(LED8[7] < 0) { if(LED8[6] == 0 ) { if(LED8[5] == 0) { LED8[7] = 0; LED8[6] = 0; LED8[5] = 0; } else { LED8[7] = 9; LED8[6] = 9; LED8[5] --; } } else { LED8[7] = 9; LED8[6] --; } } return 0; } } /********************** 主函式 ************************/ void main(void) { GPIO(); Timer0Init(); P16 = 1; P17 = 1; for (k = 0; k < 8; k++) LED8[k] = 17; Fre_counter = 0; counter = 0; while(1) { if(counter >= 50) //50ms掃描鍵盤 { IO_KeyScan(); counter = 0; } if(next_flag==0) //提示資訊:input { for(i=0; i<5; i++) LED8[i] = start[i]; } if(next_flag==1) //有按鍵按下 { if(get_flag != 12) //輸入目標頻率:10~999 { LED8[5] = LED8[6];//移位 LED8[6] = LED8[7]; LED8[7] = get_flag; next_flag = 0; } else //確認按鍵 { Fre_input = LED8[5] * 100 + LED8[6] * 10 + LED8[7]; switch(up_down(up,down)) //步進微調 { case 0: Fre_input --; //減1 down = 0; break; case 1: Fre_input ++; //加1 up = 0; break; default:break; } Fre_output(Fre_input); P17 = !P17; } } } }