📄 main.c
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
void MCGInit (void) {
//if (ExtClkFlg == 1) { /* If using a 4 MHz crystal */
if (SRS_LOC == 0) { /* If no loss of clock has occurred, enter PEE to generate 16 MHz bus */
/* FEI -> FBE Mode */
MCGC2 = 0x36; /* BDIV by 1, xtal freq RANGE high, High GAIN, EREFS set */
MCGC1 = 0xB8; /* RDIV by 128 down to 31.25 kHz for FLL, Switch to ext. ref */
while (MCGSC_IREFST); /* Check internal ref. no longer on */
MCGC3 = 0xA0; /* LOL interrupt, LOC reset (CME) enabled (ONLY after IREFS = 0) */
while (!MCGSC_OSCINIT); /* Wait until crystal initializes */
while (MCGSC_CLKST != 0b10);/* Check external ref. switched */
/* FBE -> PBE */
MCGC3 = 0xE8; /* Set PLLS bit with VDIV mult by 32 */
MCGC1 = 0x90; /* RDIV by 4 (4 MHz/4 = 1 MHz for PLL) */
while (!MCGSC_PLLST);
while (!MCGSC_LOCK);
/* PBE -> PEE */
MCGC1 = 0x10; /* CLKS changed to [0:0] - PLL output = 32 MHz clock, 16 MHz bus */
while (MCGSC_CLKST != 0b11);
}
}
////AD初始化///////////////////////////////////////
void InitAD(void)
{
ADCCFG=0x00;//8位AD采集,使用系统时钟
ADCSC2=0x00;
APCTL1=0x00;//AD0-7作为模拟信号输入
}
int ad2;
void main(void)
{
//关中断
DisableInterrupts;
SRS = 0x55; /* Clear WatchDog counter */
SRS = 0xAA;
SOPT1=0x00;
SPMSC2 = 0x00; /* LVD lvl=4.0V , LVW lvl=4.3V */
SPMSC1 = 0x00; /* LVD resets, LVD enabled in STOP, BG buffer enabled */
MCGInit(); /* Initialize MCG 时钟频率为16M*/
InitAD();
PTBDD=0xff;
EnableInterrupts;
while(1){
ADCSC1=0x22; //AD中断禁止,从通道2采集,连续采集
while(ADCSC1_COCO==0){;}//查询AD转换标志位,没有则等待
ad2=ADCRL;//将AD采来的值赋给ad2
if(ad2>100)
PTBD_PTBD7=1;
else PTBD_PTBD7=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -