单片机原理及应用第十讲 MSP430单片机的ADC实验报告报告人:学号:同组人员:实验内容实验1 AD采集输入电压并比较实验2 AD内部温度采集实验实验3 验收实验:温度采集与显示把实验2中的实测温度值以摄氏度数值显示在段码LCD上。
实验步骤步骤:(1) 将PC 和板载仿真器通过USB 线相连;(2) 打开CCS 集成开发工具,选择样例工程或自己新建一个工程,修改代码;(3) 选择对该工程进行编译链接,生成.out 文件。
然后选择,将程序下载到实验板中。
程序下载完毕之后,可以选择全速运行程序,也可以选择单步调试程序,选择F3 查看具体函数。
也可以程序下载之后,按下,软件界面恢复到原编辑程序的画面。
再按下实验板的复位键,运行程序。
(调试方式下的全速运行和直接上电运行程序在时序有少许差别,建议上电运行程序)。
关键代码:实验1 AD采集输入电压并比较#include<msp430.h>int main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDTADC12CTL0 = ADC12SHT02 + ADC12ON; // Sampling time, ADC12 onADC12CTL1 = ADC12SHP; // Use sampling timerADC12IE = 0x01; // Enable interruptADC12CTL0 |= ADC12ENC;P6SEL |= 0x01; // P6.0 ADC option selectP4DIR |= BIT1; // P4.1 outputwhile (1){ADC12CTL0 |= ADC12SC; // Start sampling/conversion__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit __no_operation(); // For debugger}}#pragma vector = ADC12_VECTOR__interrupt void ADC12_ISR(void){switch(__even_in_range(ADC12IV,34)){case 0: break; // Vector 0: No interruptcase 2: break; // Vector 2: ADC overflowcase 4: break; // Vector 4: ADC timing overflowcase 6: // Vector 6: ADC12IFG0if (ADC12MEM0 >= 0x7ff) // ADC12MEM = A0 > 0.5AVcc?P4OUT |= BIT1; // P4.1 = 1elseP4OUT &= ~BIT1; // P4.1 = 0__bic_SR_register_on_exit(LPM0_bits); // Exit active CPUcase 8: break; // Vector 8: ADC12IFG1case 10: break; // Vector 10: ADC12IFG2case 12: break; // Vector 12: ADC12IFG3case 14: break; // Vector 14: ADC12IFG4case 16: break; // Vector 16: ADC12IFG5case 18: break; // Vector 18: ADC12IFG6case 20: break; // Vector 20: ADC12IFG7case 22: break; // Vector 22: ADC12IFG8case 24: break; // Vector 24: ADC12IFG9case 26: break; // Vector 26: ADC12IFG10case 28: break; // Vector 28: ADC12IFG11case 30: break; // Vector 30: ADC12IFG12case 32: break; // Vector 32: ADC12IFG13case 34: break; // Vector 34: ADC12IFG14default: break;}}实验2 AD内部温度采集实验#include<msp430.h>#define CALADC12_15V_30C *((unsigned int *)0x1A1A) // Temperature Sensor Calibration-30 C//See device datasheet for TLV table memory mapping#define CALADC12_15V_85C *((unsigned int *)0x1A1C) // Temperature Sensor Calibration-85 Cunsigned int temp;volatile float temperatureDegC;volatile float temperatureDegF;int main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDTREFCTL0 &= ~REFMSTR; // Reset REFMSTR to hand over control to// ADC12_A ref control registersADC12CTL0 = ADC12SHT0_8 + ADC12REFON + ADC12ON;// Internal ref = 1.5VADC12CTL1 = ADC12SHP; // enable sample timerADC12MCTL0 = ADC12SREF_1 + ADC12INCH_10; // ADC i/p ch A10 = temp sense i/p ADC12IE = 0x001; // ADC_IFG upon conv result-ADCMEMO __delay_cycles(100); // Allow ~100us (at default UCS settings)// for REF to settleADC12CTL0 |= ADC12ENC;while(1){ADC12CTL0 &= ~ADC12SC;ADC12CTL0 |= ADC12SC; // Sampling and conversion start__bis_SR_register(LPM4_bits + GIE); // LPM0 with interrupts enabled__no_operation();// Temperature in Celsius. See the Device Descriptor Table section in the // System Resets, Interrupts, and Operating Modes, System Control Module // chapter in the device user's guide for background information on the // used formula.temperatureDegC = (float)(((long)temp - CALADC12_15V_30C) * (85 - 30)) / (CALADC12_15V_85C - CALADC12_15V_30C) + 30.0f;// Temperature in Fahrenheit Tf = (9/5)*Tc + 32temperatureDegF = temperatureDegC * 9.0f / 5.0f + 32.0f;__no_operation(); // SET BREAKPOINT HERE}}#pragma vector=ADC12_VECTOR__interrupt void ADC12ISR (void){switch(__even_in_range(ADC12IV,34)){case 0: break; // Vector 0: No interruptcase 2: break; // Vector 2: ADC overflowcase 4: break; // Vector 4: ADC timing overflow case 6: // Vector 6: ADC12IFG0temp = ADC12MEM0; // Move results, IFG is cleared__bic_SR_register_on_exit(LPM4_bits); // Exit active CPUcase 8: break; // Vector 8: ADC12IFG1case 10: break; // Vector 10: ADC12IFG2case 12: break; // Vector 12: ADC12IFG3case 14: break; // Vector 14: ADC12IFG4case 16: break; // Vector 16: ADC12IFG5case 18: break; // Vector 18: ADC12IFG6case 20: break; // Vector 20: ADC12IFG7case 22: break; // Vector 22: ADC12IFG8case 24: break; // Vector 24: ADC12IFG9case 26: break; // Vector 26: ADC12IFG10case 28: break; // Vector 28: ADC12IFG11case 30: break; // Vector 30: ADC12IFG12case 32: break; // Vector 32: ADC12IFG13case 34: break; // Vector 34: ADC12IFG14default: break;}}实验3 验收实验:温度采集与显示int main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDTInit_TS3A5017DR(); // Configure TS3A5017DR IN1 and IN2Init_lcd(); // LCD初始化Backlight_Enable(); // 打开背光LcdGo(1); // 打开液晶模块LCD_Clear(); // 清屏REFCTL0 &= ~REFMSTR; // Reset REFMSTR to hand over control to// ADC12_A ref control registersADC12CTL0 = ADC12SHT0_8 + ADC12REFON + ADC12ON;// Internal ref = 1.5VADC12CTL1 = ADC12SHP; // enable sample timerADC12MCTL0 = ADC12SREF_1 + ADC12INCH_10; // ADC i/p ch A10 = temp sense i/p ADC12IE = 0x001; // ADC_IFG upon conv result-ADCMEMO __delay_cycles(100); // Allow ~100us (at default UCS settings)// for REF to settleADC12CTL0 |= ADC12ENC;while(1){ADC12CTL0 &= ~ADC12SC;ADC12CTL0 |= ADC12SC; // Sampling and conversion start__bis_SR_register(LPM4_bits + GIE); // LPM0 with interrupts enabled__no_operation();// Temperature in Celsius. See the Device Descriptor Table section in the // System Resets, Interrupts, and Operating Modes, System Control Module // chapter in the device user's guide for background information on the // used formula.temperatureDegC = (float)(((long)temp - CALADC12_15V_30C) * (85 - 30)) / (CALADC12_15V_85C - CALADC12_15V_30C) + 30.0f;i = (int)temperatureDegC/100;j = (int)(temperatureDegC-i*100)/10;k = (int)(temperatureDegC-i*100-j*10);LCDMEM[0] = char_gen[i];LCDMEM[1] = char_gen[j];LCDMEM[2] = char_gen[k];__delay_cycles(1000000); // Allow ~100us (at default UCS settings)// Temperature in Fahrenheit Tf = (9/5)*Tc + 32temperatureDegF = temperatureDegC * 9.0f / 5.0f + 32.0f;__no_operation(); // SET BREAKPOINT HERE}}实验现象分析:实验1 AD采集输入电压并比较旋转电位器RP1,改变AD6输入端的电压:当输入电压大于VCC/2时,P4.1的LED亮;当输入电压小于VCC/2时,P4.1的LED灭。