- #include <MSP430X14X.h>
- #include "init.h"
- void Init_ADC(void)
- {
- //设置P6.0为模拟输入通道
- P6SEL = 0X07;
- //设置ENC为0,从而修改ADC12寄存器的值
- ADC12CTL0 &= ~(ENC);
- //转换的起始地址为:ADCMEM0
- ADC12CTL1 |= CSTARTADD_0;
- //设置参考电压分别为AVSS和AVCC,输入通道为A0
- ADC12MCTL0 = INCH_0;
- //设置参考电压分别为AVSS和AVCC,输入通道为A1
- ADC12MCTL1 = INCH_1;
- //设置参考电压分别为AVSS和AVCC,输入通道为A2
- ADC12MCTL2 = INCH_2;
- //设置参考电压分别为AVSS和AVCC,输入通道为A3
- ADC12MCTL3 = INCH_3;
- //设置参考电压分别为AVSS和AVCC,输入通道为A4
- ADC12MCTL4 = INCH_4;
- //设置参考电压分别为AVSS和AVCC,输入通道为A5
- ADC12MCTL5 = INCH_5;
- //设置参考电压分别为AVSS和AVCC,输入通道为A6
- ADC12MCTL6 = INCH_6;
- //设置参考电压分别为AVSS和AVCC,输入通道为A7
- ADC12MCTL7 = INCH_7 + EOS;
- ADC12CTL0 |= ADC12ON;
- ADC12CTL0 |= MSC;
- //转换模式为:多通道、单次转换
- ADC12CTL1 |= CONSEQ_1;
- //SMCLK,时钟分频为1,采样脉冲由采用定时器产生
- ADC12CTL1 |= ADC12SSEL_1;
- ADC12CTL1 |= ADC12DIV_0;
- ADC12CTL1 |= (SHP);
- //使能ADC转换
- ADC12CTL0 |= ENC;
- return;
- }
- void Init_TimerA(void)
- {
- // 选择SMCLK,清除TAR
- TACTL = TASSEL1 + TACLR;
- // 1/8 SMCLK
- TACTL += ID1;
- TACTL += ID0;
- // CCR0 中断允许
- CCTL0 = CCIE;
- // 时间间隔为 250Hz
- CCR0 = 4000;
- // 增记数模式
- TACTL |= MC0;
- return;
- }
- void Init_Port(void)
- {
- //将P4口和P5口设置为输入方式
- P4DIR = 0;
- P5DIR = 0;
- //将P4口和P5口设置为一般I/O口
- P4SEL = 0;
- P5SEL = 0;
- // 将P4.0设置为输出方向
- P4DIR |= BIT0;
- return;
- }
- void Init_TimerB(void)
- {
- TBCTL = TBSSEL0 + TBCLR; // 选择ACLK,清除TAR
- TBCCTL0 = CCIE; // TBCCR0 中断允许
- TBCCR0 = 32768; // 时间间隔为 1 s
- TBCTL |= MC0; // 增记数模式
- }
- void Delay_ms(unsigned long nValue)//毫秒为单位,8MHz为主时钟
- {
- unsigned long nCount;
- int i;
- unsigned long j;
- nCount = 2667;
- for(i = nValue;i > 0;i--)
- {
- for(j = nCount;j > 0;j--);
- }
- return;
- }
- void Delay_us(unsigned long nValue)//微秒为单位,8MHz为主时钟
- {
- int nCount;
- int i;
- int j;
- nCount = 3;
- for(i = nValue;i > 0;i--)
- {
- for(j = nCount;j > 0;j--);
- }
- return;
- }
- void Init_UART1(void)
- {
- U1CTL = 0X00; //将寄存器的内容清零
- U1CTL += CHAR; //数据位为8bit
- U1TCTL = 0X00; //将寄存器的内容清零
- U1TCTL += SSEL1; //波特率发生器选择SMCLK
- UBR0_1 = 0X45; //波特率为115200
- UBR1_1 = 0X00;
- UMCTL_1 = 0X00;
- ME2 |= UTXE1 + URXE1; //使能UART1的TXD和RXD
- IE2 |= URXIE1; //使能UART1的RX中断
- IE2 |= UTXIE1; //使能UART1的TX中断
- P3SEL |= BIT6; //设置P3.6为UART1的TXD
- P3SEL |= BIT7; //设置P3.7为UART1的RXD
- P3DIR |= BIT6; //P3.6为输出管脚
- return;
- }
- void Init_CLK(void)
- {
- unsigned int i;
- BCSCTL1 = 0X00; //将寄存器的内容清零
- //XT2震荡器开启
- //LFTX1工作在低频模式
- //ACLK的分频因子为1
- do
- {
- IFG1 &= ~OFIFG; // 清除OSCFault标志
- for (i = 0x20; i > 0; i--);
- }
- while ((IFG1 & OFIFG) == OFIFG); // 如果OSCFault =1
- BCSCTL1 &= ~(XT2OFF + XTS);//open XT2, LFTX2 select low frequency
- BCSCTL1 |= RSEL0 + RSEL1 + RSEL2; //DCO Rsel=7(Freq=3200k/25摄氏度)
- BCSCTL1 |= 0x07;
- BCSCTL2 += SELM1; //MCLK的时钟源为TX2CLK,分频因子为1
- BCSCTL2 += SELS; //SMCLK的时钟源为TX2CLK,分频因子为1
- }