|
by starli 李明星
来源:ouravr
原理图:
Clock-System.pdf
(49.69 KB, 下载次数: 25)
关键代码
-
-
- #include "includes.h"
- typedef enum
- {
- SYS_CLOCK_RUN,
- SYS_CLOCK_SET,
- }SYSTEM_STATE;
- static SYSTEM_STATE SystemFlag;
- static void SysTem_ClockRun(void);
- static void System_ClockSet(void);
- static void (*pFunc_Sys[])(void)=
- {
- SysTem_ClockRun,
- System_ClockSet,
- };
- typedef struct
- {
- unsigned char buff[DISP_NUM>>1];
- unsigned char Flash;
- unsigned char dp;
- }CLOCK_SET_STRUCT;
- static __flash unsigned char const FlashTbl[]={DISP_1|DISP_2, DISP_3|DISP_4, DISP_5|DISP_6};
- static CLOCK_SET_STRUCT ClockSet;
- //static void Download(void);
- static void SystemInitialize(void);
- static void TimerToDisplayBuff(unsigned char *pbuff, TIMER_STRUCT *ptimer);
- static unsigned char disp_buff[DISP_NUM];
- /*
- //////////////////////////////////////////////////////////////////////////
- static void Download(void)
- {
- unsigned char i;
- unsigned char cnt;
- unsigned int j;
- PORTB &= 0xc7;
- DDRB &= 0xc7;
- PORTD &= 0xbf;
- DDRD &= 0xbf;
- cnt = 0;
- for(i=0; i<200; i++)
- {
- for(j=0; j<1000; j++)
- {
- if(GETB(PIND, PD6))
- {
- cnt++;
- break;
- }
- }
- if(cnt>10)
- {
- cnt = 100;
- break;
- }
- }
- // 要进行下载
- if(cnt==100)
- {
- cnt = 1;
- while(cnt)
- {
- i = PINB&(1<
- __delay_cycles(3000);
- }
- }
- }
- */
- //////////////////////////////////////////////////////////////////////////
- static void SystemInitialize(void)
- {
- unsigned char i;
- __disable_interrupt();
- #if CPU_TYPE==0x48
- CLKPR = (1<
- CLKPR = (1<
- #endif
- //Download();
- SPI_Init();
- ExFlash_Init();
- Timer_Init();
- Key_Init();
- Display_Init();
- __enable_interrupt();
- for(i=0; i
- {
- disp_buff[i] = 0;
- }
- SystemFlag = SYS_CLOCK_RUN;
- }
- static void TimerToDisplayBuff(unsigned char *pbuff, TIMER_STRUCT *ptimer)
- {
- *pbuff++ = ptimer->sec%10;
- *pbuff++ = ptimer->sec/10;
- *pbuff++ = ptimer->min%10;
- *pbuff++ = ptimer->min/10;
- *pbuff++ = ptimer->hour%10;
- *pbuff = ptimer->hour/10;
- }
- void main(void)
- {
- SystemInitialize();
- while(TRUE)
- {
- pFunc_Sys[SystemFlag]();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- static void SysTem_ClockRun(void)
- {
- TIMER_STRUCT *ptimer;
- unsigned long time;
- unsigned int addr;
- unsigned char mask;
- unsigned char dbyte;
- CRITICAL_DEF;
- ptimer = Timer_GetTimer();
- if(Timer_IsUpdate())
- {
- time = ptimer->hour;
- time *= 3600;
- addr = ptimer->min;
- addr *= 60;
- time += addr+ptimer->sec;
- addr = time>>3;
- mask = 7-(time&0x07);
- ENTER_CRITICAL();
- dbyte = ExFlash_ReadByte(addr);
- EXIT_CRITICAL();
- if(GETB(dbyte, mask))
- {
- TimerToDisplayBuff(disp_buff, Timer_GetTimer());
- Display_Update(disp_buff);
- }
- /*
- ENTER_CRITICAL();
- dbyte = ExFlash_ReadByte(ad);
- EXIT_CRITICAL();
- ad++;
- if(ad>=10800)
- {
- ad = 0;
- }
- addr = ad;
- for(mask=0; mask<4; mask++)
- {
- disp_buff[mask+2] = addr%10;
- addr /= 10;
- }
- disp_buff[0] = dbyte&0x0f;
- disp_buff[1] = dbyte>>4;
- Display_Update(disp_buff);
- */
- }
- if(Key_GetCode()==KEY_CODE_ENTER)
- {
- SystemFlag = SYS_CLOCK_SET;
- ClockSet.Flash = 0;
- ClockSet.dp = 0;
- ClockSet.buff[0] = ptimer->sec;
- ClockSet.buff[1] = ptimer->min;
- ClockSet.buff[2] = ptimer->hour;
- Display_SetFlash(FlashTbl[ClockSet.Flash]);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- static __flash unsigned char const TimerTbl[]={59, 59, 23};
- static void System_ClockSet(void)
- {
- unsigned char key;
- key = Key_GetCode();
- if(key^KEY_CODE_NULL)
- {
- switch(key)
- {
- case KEY_CODE_ENTER:
- SystemFlag = SYS_CLOCK_RUN;
- Timer_SetTimer((TIMER_STRUCT*)ClockSet.buff);
- Display_SetFlash(DISP_NULL);
- break;
- case KEY_CODE_SUB:
- if(--ClockSet.buff[ClockSet.Flash]>TimerTbl[ClockSet.Flash])
- {
- ClockSet.buff[ClockSet.Flash] = TimerTbl[ClockSet.Flash];
- }
- disp_buff[ClockSet.dp] = ClockSet.buff[ClockSet.Flash]%10;
- disp_buff[ClockSet.dp+1] = ClockSet.buff[ClockSet.Flash]/10;
- break;
- case KEY_CODE_ADD:
- if(++ClockSet.buff[ClockSet.Flash]>TimerTbl[ClockSet.Flash])
- {
- ClockSet.buff[ClockSet.Flash] = 0;
- }
- disp_buff[ClockSet.dp] = ClockSet.buff[ClockSet.Flash]%10;
- disp_buff[ClockSet.dp+1] = ClockSet.buff[ClockSet.Flash]/10;
- break;
- case KEY_CODE_SWITCH:
- if(++ClockSet.Flash>=3)
- {
- ClockSet.Flash = 0;
- }
- ClockSet.dp = ClockSet.Flash<<1;
- Display_SetFlash(FlashTbl[ClockSet.Flash]);
- break;
- default:
- ;
- }
- Display_Update(disp_buff);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- // END
复制代码 |
|