写一段msp430f149单片机测量内部温度并利用数码管显示的代码
时间: 2024-05-11 20:16:08 浏览: 25
好的,以下是msp430f149单片机测量内部温度并利用数码管显示的代码:
```c
#include <msp430.h>
#include <stdlib.h>
#define TEMP_SENSOR_ADCH 0x1e // DTC temperature sensor value
#define DIGIT1 BIT0 // P2.0
#define DIGIT2 BIT1 // P2.1
#define DIGIT3 BIT2 // P2.2
#define DIGIT4 BIT3 // P2.3
#define SEG_A BIT4 // P3.4
#define SEG_B BIT5 // P3.5
#define SEG_C BIT6 // P3.6
#define SEG_D BIT7 // P3.7
volatile int temp_data = 0; // volatile variable to store temperature data
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
// configure temperature sensor and ADC12
ADC12CTL0 = ADC12ON + ADC12SHT02 + ADC12REFON + ADC12REF2_5V; // enable ADC12, set sampling time and reference voltage
ADC12CTL1 = ADC12SHP + ADC12CONSEQ_3 + ADC12TCOFF; // set ADC12 to pulse mode, sequence-of-channels mode and turn off temperature sensor calibration
ADC12MCTL0 = TEMP_SENSOR_ADCH + ADC12INCH_31; // set channel to the temperature sensor with internal reference voltage
ADC12IE = 0x0001; // enable ADC12 interrupt
ADC12CTL0 |= ADC12ENC; // enable ADC12 conversion
// configure 7-segment display
P2DIR |= (DIGIT1 + DIGIT2 + DIGIT3 + DIGIT4); // set P2.0 - P2.3 as output
P2OUT &= ~(DIGIT1 + DIGIT2 + DIGIT3 + DIGIT4); // set P2.0 - P2.3 to low
P3DIR |= (SEG_A + SEG_B + SEG_C + SEG_D); // set P3.4 - P3.7 as output
P3OUT &= ~(SEG_A + SEG_B + SEG_C + SEG_D); // set P3.4 - P3.7 to low
// configure TIMER_A
TA0CTL = TASSEL_2 + MC_1; //set timer clock to SMCLK and set upmode
TA0CCR0 = 4096; // set timer period to 1024*4 = 4096 ticks
TA0CCTL0 |= CCIE; // enable timer compare interrupt
__enable_interrupt(); // enable global interrupt
while(1) {} // stay in infinite loop
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR(void) {
temp_data = ADC12MEM0; // store temperature data
ADC12CTL0 &= ~ADC12IFG; // clear ADC12 interrupt flag
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0_ISR(void) {
static int digit = 1; // variable to store current digit
static int display_data[4] = {0, 0, 0, 0}; // array to store 4 digits of temperature data
int i;
switch(digit) {
case 1: // first digit
// calculate the hundreds digit of temperature and display it
display_data[0] = temp_data / 100;
P2OUT |= DIGIT1;
P2OUT &= ~(DIGIT2 + DIGIT3 + DIGIT4);
break;
case 2: // second digit
// calculate the tens digit of temperature and display it
display_data[1] = (temp_data % 100) / 10;
P2OUT |= DIGIT2;
P2OUT &= ~(DIGIT1 + DIGIT3 + DIGIT4);
break;
case 3: // third digit
// calculate the ones digit of temperature and display it
display_data[2] = (temp_data % 10);
P2OUT |= DIGIT3;
P2OUT &= ~(DIGIT1 + DIGIT2 + DIGIT4);
break;
case 4: // fourth digit
// display the degree symbol
display_data[3] = 10;
P2OUT |= DIGIT4;
P2OUT &= ~(DIGIT1 + DIGIT2 + DIGIT3);
break;
}
// update 7-segment display
for(i = 0; i < 4; i++){
switch(display_data[i]){
case 0:
P3OUT |= (SEG_A + SEG_B + SEG_C);
P3OUT &= ~(SEG_D);
break;
case 1:
P3OUT |= (SEG_B + SEG_C);
P3OUT &= ~(SEG_A + SEG_D);
break;
case 2:
P3OUT |= (SEG_A + SEG_B + SEG_D);
P3OUT &= ~(SEG_C);
break;
case 3:
P3OUT |= (SEG_A + SEG_B + SEG_C);
P3OUT &= ~(SEG_D);
break;
case 4:
P3OUT |= (SEG_B + SEG_C + SEG_D);
P3OUT &= ~(SEG_A);
break;
case 5:
P3OUT |= (SEG_A + SEG_C + SEG_D);
P3OUT &= ~(SEG_B);
break;
case 6:
P3OUT |= (SEG_A + SEG_C + SEG_D);
P3OUT &= ~(SEG_B + SEG_D);
break;
case 7:
P3OUT |= (SEG_A + SEG_B + SEG_C);
P3OUT &= ~(SEG_D);
break;
case 8:
P3OUT |= (SEG_A + SEG_B + SEG_C + SEG_D);
P3OUT &= ~(0x00);
break;
case 9:
P3OUT |= (SEG_A + SEG_B + SEG_C + SEG_D);
P3OUT &= ~(SEG_B);
break;
case 10:
P3OUT |= SEG_D;
P3OUT &= ~(SEG_A + SEG_B + SEG_C);
break;
}
__delay_cycles(500);
P3OUT &= ~(SEG_A + SEG_B + SEG_C + SEG_D);
}
digit = digit % 4 + 1; // update current digit
}
```
这段代码可以实现用msp430f149单片机测量内部温度,并通过4位7段数码管显示出来。在此代码中,使用了ADC12模块来进行温度数据的采集,使用了定时器TA0来控制数码管的刷新,使用了P2口以及P3口来控制数码管的动态显示。语言为C。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)