其主程序代码如下:
times New Roman">#include <msp430g2231.h> #include "LCD12864.h"int DegC; //待显示的温度值unsigned int number=1000; //计数变量long int AD_Result; //Ad的采样值char m=0;int n=0;int i=0;long int ave;char Flag=0;unsigned char const tab[]={ "温度 'C" " " " " " " };unsigned char const tab1[]={ "1 2 'C" "5 6 'C" "3 4 'C" "7 8 'C" };void ConfigTimerA2(void) { CCTL0 = CCIE; // CCR0 interrupt enabled This bit enables the interrupt request of the corresponding CCIFG flag. CCR0 = number; // 时间间隔设置部分 TACTL = TASSEL_2 + MC_2+ID_0; // SMCLK, continuous mode,无分频 }void AD_Init(void){ ADC10CTL0 |= SREF_1+REFON+ADC10IE;//将AD10基准设置为1.5V 开启AD允许中断 ADC10CTL0 |= ADC10SHT_3+MSC;//打开AD转换,过采样率设置为64个采样周期 ADC10CTL1 |= ADC10SSEL_3+SHS_0;//选择250K的采样时钟,用ADC10SC触发采集 ADC10CTL1 |= CONSEQ_0+INCH_0;//连续采样模式, ADC10CTL0 |= ADC10ON;}void clk_init(void) //配置时钟{ BCSCTL1 |= CALBC1_1MHZ; DCOCTL |= CALDCO_1MHZ;//上面两句将内部DCO校准至1MHz //while(IFG1&OFIFG) { IFG1 &= ~OFIFG; delay_ms(100); } BCSCTL2 |= SELM_0;//MCLK采用1M的内部DCO BCSCTL2 |= DIVS_0;//SMCLK采用内部的时钟}void IO_init(void){ P1DIR&=~(BIT0+BIT3+BIT4+BIT5); //按键P1.3,p1.4,p1.5 P1IES |=BIT3+BIT4+BIT5; //灯p1.6,P1.7,P2.7 P1IE |=BIT3+BIT4+BIT5; //允许中断 P1SEL &= ~(BIT1+BIT2); P1DIR |= BIT2+BIT1+BIT6+BIT7; //设置输出方向 P2DIR |=BIT7; P2OUT &=~BIT7; P1OUT &=~(BIT6+BIT7);}void main(void) //主函数部分{ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer clk_init(); IO_init(); lcd_init(); ConfigTimerA2(); AD_Init(); for(n=0;n<500;n++); chn_disp1(tab); _EINT(); while(1) { for(i=0;i<5000;i++); ADC10CTL0 |= ADC10SC+ENC; }}#pragma vector=ADC10_VECTOR //AD中断及其服务子程序__interrupt void ADC10_ISR (void){ ADC10CTL0 &= ~ENC; // Disable ADC conversion ADC10CTL0 &= ~ADC10SC; m++; AD_Result+=ADC10MEM; if(m>=8) { m=0; ave=AD_Result>>3; AD_Result=0; DegC=((long int )ave*2300)/264-100; }}#pragma vector=TIMERA0_VECTOR //主计数器独占一个中断源__interrupt void Timer_A (void){ Flag++; switch(Flag) { case 1:Write_Num(0x83,DegC,2); break; case 2:P1OUT|=BIT6; break; case 3: P1OUT&=~BIT6;Flag=0; break; } }#pragma vector= PORT1_VECTOR //IO口中断__interrupt void PORT1_ISR (void){ unsigned char PushKey; PushKey=P1IFG&(BIT4+BIT5+BIT3); __delay_cycles(5000); if(P1IN&PushKey==PushKey) { P1IFG=0; return; } if(PushKey&BIT3) { P1OUT=BIT7; number=100+number; if ( number>=2000) { number=1000; } } if(PushKey&BIT4) { P1OUT&=~BIT7; }P1IFG=0; }
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>