|
最近學(xué)習(xí)輝芒微單片機(jī),自己弄了一個(gè)驅(qū)動(dòng)74HC595 四位共陰數(shù)碼管,還在不斷學(xué)習(xí)新知識。
- #include "SYSCFG.h";
- #include "FT62F21X.h";
- #define uchar unsigned char
- #define uint unsigned int
- uchar LedChar[]={
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
- 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
- }; //共陰數(shù)碼管0-F真值表
- #define Key1 PA2 //按鍵輸入
- #define smg_wei1 PA0 //數(shù)碼管位選1
- #define smg_wei2 PA1 //數(shù)碼管位選2
- #define SER PA3 //串行數(shù)據(jù)輸入
- #define RCLK PA4 //存儲寄存器時(shí)鐘
- #define SRCLK PA5 //串行輸入時(shí)鐘
- uchar mode=0;
- //bit flags =0;
- uchar kk;
- void Hc595SendByte(uchar dat);
- //uint i;
- /*-------------------------------------------------
- * 函數(shù)名:DelayUs
- * 功能: 短延時(shí)函數(shù) --16M-4T--大概快1%左右.
- * 輸入: Time延時(shí)時(shí)間長度 延時(shí)時(shí)長Time*2Us
- * 輸出: 無
- -------------------------------------------------*/
- void DelayUs(unsigned char Time)
- {
- unsigned char a;
- for(a=0;a<Time;a++)
- {
- NOP();
- }
- }
- /*-------------------------------------------------
- * 函數(shù)名:DelayMs
- * 功能: 短延時(shí)函數(shù) 快1%
- * 輸入: Time延時(shí)時(shí)間長度 延時(shí)時(shí)長Time ms
- * 輸出: 無
- -------------------------------------------------*/
- void DelayMs(unsigned char Time)
- {
- unsigned char a,b;
- for(a=0;a<Time;a++)
- {
- for(b=0;b<5;b++)
- {
- DelayUs(98);
- }
- }
- }
- /*-------------------------------------------------
- * 函數(shù)名:DelayS
- * 功能: 短延時(shí)函數(shù)
- * 輸入: Time 延時(shí)時(shí)間長度 延時(shí)時(shí)長Time S
- * 輸出: 無
- -------------------------------------------------*/
- void DelayS(unsigned char Time)
- {
- unsigned char a,b;
- for(a=0;a<Time;a++)
- {
- for(b=0;b<10;b++)
- {
- DelayMs(100);
- }
- }
- }
- /*******************************************************************************
- * 函數(shù)名 : Hc595SendByte(u8 dat)
- * 函數(shù)功能 : 向74HC595發(fā)送一個(gè)字節(jié)的數(shù)據(jù)
- * 輸入 : 無
- * 輸出 : 無
- *******************************************************************************/
- void Hc595SendByte(uchar dat)
- {
- char a;
- SRCLK=0; //串行輸入時(shí)鐘
- RCLK =0; //存儲寄存器時(shí)鐘
- for(a=0;a<8;a++)
- {
- SER=dat>>7;
- dat<<=1;
- SRCLK=1;
- NOP();
- NOP();
- SRCLK=0;
- }
- RCLK=1;
- NOP();
- NOP();
- RCLK=0;
- }
- void POWER_INITIAL (void)
- {
- OSCCON = 0B01110000; //IRCF=111=16MHz/4T=4MHz,0.25us
- INTCON = 0; //暫禁止所有中斷
- OPTION = 0; // /PAPU INTEDG T0CS T0SE PSA PS2 PS1 PS0
- //PSRCA = 0; //00:4mA 01/10:8mA 11:28mA bit[3:2]控制PA5源電流 bit[1:0]控制PA4源電流
- //PSINKA = 0; //bit[1:0] 控制PA5和PA4 0:灌電流最小 1:灌電流最大
- TRISA = 0B00000100; //1:輸入 0:輸出 PA2為輸入模式
- PORTA = 0B00000100; //1:PAx輸出高電平 0:PAx輸出低電平 PA2輸出低電平 ,PA3輸出高電平
- WPUA = 0B00000100; //1:使能PA口上拉 0:關(guān)閉PA口上拉 PA3 上拉
- }
- /*-------------------------------------------------
- * 函數(shù)名:main
- * 功能: 主函數(shù)
- * 輸入: 無
- * 輸出: 無
- --------------------------------------------------*/
- void main()
- {
- uchar i=0;
- POWER_INITIAL(); //系統(tǒng)初始化
- Hc595SendByte(LedChar[0]);
- while(1)
- {
- if(Key1==0)
- {
- DelayMs(10);
- if(Key1==0)
- {
- DelayMs(10);
- Hc595SendByte(LedChar[i]);
- i++;
- if(i>=15)
- {
- i=0;
- }
- }
- while(!Key1);
- }
- }
- }
復(fù)制代碼
|
|