1 #include "msp430g2553.h" 2 3 void sendChar(unsigned char c) 4 { 5 while(!(IFG2&UCA0TXIFG)); 6 UCA0TXBUF=c; 7 } 8 9 void sendStr(unsigned char *s) 10 { 11 while(*s!='\0') 12 { 13 sendChar(*s); 14 s++; 15 } 16 } 17 18 void main(void) 19 20 { 21 22 unsigned char i; 23 unsigned char temp; 24 unsigned long tempAverage; 25 unsigned long tempMeasured[8]; 26 27 WDTCTL = WDTPW + WDTHOLD; // Stop WDT 28 29 BCSCTL1 = CALBC1_1MHZ; // Set DCO 30 DCOCTL = CALDCO_1MHZ; 31 BCSCTL2 &= ~(DIVS_3); 32 33 34 P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD 35 P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD 36 37 UCA0CTL1 |= UCSSEL_2; // SMCLK 38 UCA0BR0 = 104; // 1MHz 9600 39 UCA0BR1 = 0; // 1MHz 9600 40 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 41 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** 42 43 //IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt 44 45 ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4 选择内部温度传感器通道,4分频 46 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON ; 47 __delay_cycles(1000); // Wait for ADC Ref to settle 48 ADC10CTL0 |= ENC + ADC10SC; 49 50 _EINT(); 51 52 sendStr("\n======== LaunchPad 温度计=======\n"); 53 54 //__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled 55 56 57 while(1) 58 { 59 if(i>7) 60 { 61 tempAverage=0; 62 for(;i>0;i--) 63 tempAverage+=tempMeasured[i-1]; 64 tempAverage >>= 3; 65 temp=(unsigned char)( ((tempAverage - 630) * 761) / 1024 ); 66 sendStr("\n 华氏温度:"); 67 sendChar(temp%100/10+48); 68 sendChar(temp%10+48); 69 sendStr(" | 摄氏温度:"); 70 temp=(temp-32)*5/9; 71 sendChar(temp%100/10+48); 72 sendChar(temp%10+48); 73 sendChar('\n'); 74 75 } 76 else 77 { 78 ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start 抽样及转换 79 tempMeasured[i]=ADC10MEM; 80 i++; 81 } 82 83 __delay_cycles(100000); 84 85 86 } 87 } 88 89 // Echo back RXed character, confirm TX buffer is ready first 90 /* 91 #pragma vector=USCIAB0RX_VECTOR 92 __interrupt void USCI0RX_ISR(void) 93 { 94 while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready? 95 UCA0TXBUF = UCA0RXBUF; // TX -> RXed character 96 } 97 98 */
和官方模拟串口例程不同,这是使用硬件,需要将板子上的串口跳线帽竖接,即选择HW(Hardware)模式,打开串口助手,可以看到不断更新的温度信息