CH554可以改变系统时钟,CLOCK_CFG寄存器的低三位可以控制系统内部时钟分频系数,从而改变系统工作频率。为了安全,改变CLOCK_CFG寄存器前,需要往安全模式控制寄存器中一次写入0x55和0xAA。
下面程序演示了改变系统时钟频率的方法,将P1.0接地一段时间,就会改变时钟,可以通过LED1的变化看出时钟的变化。
- #include <rtx51tny.h>
- #include <SST89X5XXRD2.H>
- #include "stdint.h"
- sbit LED1 = P1^6;
- sbit LED2 = P1^7;
- sbit BTN = P1^0;
- sfr SAFE_MOD = 0xA1;
- sfr CLOCK_CFG = 0xB9;
- #define FREQ_BASE 6000000UL
- #define SYSCLK_187K 0
- #define SYSCLK_750K 1
- #define SYSCLK_3M 2
- #define SYSCLK_6M 3
- #define SYSCLK_12M 4
- #define SYSCLK_16M 5
- #define SYSCLK_24M 6
- #define FREQ_187K 187500UL
- #define FREQ_750K 750000UL
- #define FREQ_3M 3000000UL
- #define FREQ_6M 6000000UL
- #define FREQ_12M 12000000UL
- #define FREQ_16M 16000000UL
- #define FREQ_24M 24000000UL
- code uint32_t SYSCLK_FREQ[] = (FREQ_187K, FREQ_750K, FREQ_3M, FREQ_6M, FREQ_12M, FREQ_16M, FREQ_24M);
- uint32_t F_CPU = FREQ_BASE;
- /* only suppot internal clock, support frequence
- FREQ_187K
- FREQ_750K
- FREQ_3M
- FREQ_6M (default)
- FREQ_12M
- FREQ_16M
- FREQ_24M
- */
- void sysclk(uint8_t freq)
- {
- if(freq > 6)
- return;
-
- F_CPU = SYSCLK_FREQ[freq];
-
- SAFE_MOD = 0x55;
- SAFE_MOD = 0xAA;
- CLOCK_CFG &= 0xF8;
- CLOCK_CFG |= freq;
- SAFE_MOD = 0;
- }
- uint8_t freq=3;
- uint8_t btncnt = 0;
- job0 () _task_ 0
- {
-
- while(1)
- {
- BTN = 1;
- if(BTN == 0)
- {
- btncnt++;
- if(btncnt > 10)
- {
- btncnt = 0;
- freq = (freq+1)%5 + 2;
- sysclk(freq);
- LED2 = !LED2;
- }
- }
- else
- btncnt = 0;
-
- // 0.5s
- LED1 = !LED1;
- os_wait(K_TMO, 5, 0);
- }
- }
此内容由EEWORLD论坛网友dcexpert原创,如需转载或用于商业用途需征得作者同意并注明出处