我编的一段两路的AD采集电压的程序,可是一直显示不对,求教是哪里的问题
- #include
- #include
- #include "msp430.h"
- #include "HAL_Dogs102x6.h"
- void lcd_int(void);
- void time_1_int(void);
- void display_time(void);
- void display_style(void);
- void button_init(void);
- void key_scan(void);
- void clock_check(void);
- void display_ADC(void);
- void ADC_init(void);
- char hour=0,min=0,sec=0;
- unsigned int time_counter=0;
- volatile long temp;
- uint32_t sum1=0,com1,sum2=0,com2,flag=0;
- uint32_t tempData1[10];
- uint32_t tempData2[10];
- uint32_t cap,newdata,olddata,data;
- uint32_t num;
- uint8_t time_hour_style=0;
- uint8_t time_min_style=0;
- char time[] ="time: 00:00:00";
- char time_hour[]="00";
- char time_min[]="00";
- char time_sec[]="00";
- void main( void )
- {
- WDTCTL = WDTPW + WDTHOLD;
- time_1_int();
- lcd_int();
- button_init();
- Dogs102x6_stringDraw(1,0,"==Battery power==", DOGS102x6_DRAW_NORMAL);
- Dogs102x6_stringDraw(3,8,time,DOGS102x6_DRAW_NORMAL); // write data to LCD
- //Dogs102x6_stringDraw(3,0,clock,DOGS102x6_DRAW_NORMAL); // write data to LCD
- ADC_init();
- P1DIR |= (0x01<<0);
- P8DIR |= (0x01<<2);
- while(1)
- {
- ADC12CTL0 |= ADC12SC;
- display_ADC();
- display_time();
-
- }
- }
- void display_ADC(void)
- {
- if(flag==1)
- {
- char string1[20];
- string1[0]=com1/1000+'0';
- string1[1]=46;
- string1[2]=com1/100%10+'0';
- string1[3]=com1/10%10+'0';
- string1[4]=com1%10+'0';
- string1[5]=' ';
- char string2[20];
- string2[0]=com2/1000+'0';
- string2[1]=46;
- string2[2]=com2/100%10+'0';
- string2[3]=com2/10%10+'0';
- string2[4]=com2%10+'0';
- string2[5]=' ';
- Dogs102x6_stringDraw(5, 8, "value1=", DOGS102x6_DRAW_NORMAL);
- Dogs102x6_stringDraw(5, 48, string1, DOGS102x6_DRAW_NORMAL);
- Dogs102x6_stringDraw(5, 78, "(V)", DOGS102x6_DRAW_NORMAL);
- Dogs102x6_stringDraw(6, 8, "value2=", DOGS102x6_DRAW_NORMAL);
- Dogs102x6_stringDraw(6, 48, string2, DOGS102x6_DRAW_NORMAL);
- Dogs102x6_stringDraw(6, 78, "(V)", DOGS102x6_DRAW_NORMAL);
- }
-
- }
- void ADC_init(void)
- {
-
- ADC12CTL0 = ADC12ON+ADC12SHT0_8+ADC12MSC;
-
- ADC12CTL1 = ADC12SHP+ADC12CONSEQ_3; // Use sampling timer, sequence
- ADC12MCTL0 = ADC12SREF_0+ADC12INCH_6; // ref+=AVcc, channel = A6
- ADC12MCTL1 = ADC12SREF_0+ADC12INCH_7+ADC12EOS; // ref+=AVcc, channel = A7
- // Enable A/D channel inputs
- P6SEL |=BIT6;
- P6SEL |=BIT7;
- ADC12IE = 0x02; // Enable ADC12IFG.1
- //__delay_cycles(75); // 75us delay to allow Ref to settle
- ADC12CTL0 |= ADC12ENC; // Enable conversions
- }
- void display_time(void)
- {
- time_hour[0]=hour/10+'0';
- time_hour[1]=hour%10+'0';
- time_min[0]=min/10+'0';
- time_min[1]=min%10+'0';
- time_sec[0]=sec/10+'0';
- time_sec[1]=sec%10+'0';
-
- Dogs102x6_stringDraw(3,44,time_hour,time_hour_style);
- Dogs102x6_stringDraw(3,62,time_min,time_min_style);
- Dogs102x6_stringDraw(3,80,time_sec,DOGS102x6_DRAW_NORMAL);
-
- }
- void lcd_int(void)
- {
- Board_init(); // GPIO initialization
- Dogs102x6_init(); // initialize LCD
- Dogs102x6_clearScreen(); // clear screen of LCD
- Dogs102x6_backlightInit(); // initialize the backlight of LCD
- Dogs102x6_setBacklight(20); // set up the backlight of LCD
- __enable_interrupt();//enable globle interrupt
- }
- void time_1_int(void)
- {
- TA1CCTL0 = CCIE; // CCR0 interrupt enabled
- TA1CCR0 = 1045; //周期为1ms
- TA1CTL = TASSEL_2 + MC_2 + TACLR; // SMCLK ~= 1.045MHz, contmode, clear TAR
- __enable_interrupt();//enable globle interrupt
- }
- void button_init(void)
- {
- P1DIR &= ~(0x01<<7); //set P1.7 as input
- P1REN |= (0x01<<7); //enable resistor
- P1OUT |= (0x01<<7); //pull up
- P2DIR &= ~(0x01<<2); //set P2.2 as input
- P2REN |= (0x01<<2); //enable resistor
- P2OUT |= (0x01<<2); //pull up
- }
- #pragma vector=TIMER1_A0_VECTOR
- __interrupt void TIMER1_A0_ISR(void)
- {
- ++time_counter;
- TA1CCR0 += 1045;
- if (time_counter>=1000)
- {
- time_counter = 0;
- sec++;
- //P1OUT ^= BIT0;
- if(sec==60)
- {
- sec=0;
- min++;
- if(min==60)
- {
- min=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- }
- }
- #pragma vector=ADC12_VECTOR
- __interrupt void ADC12ISR (void)
- {
- static unsigned int count = 0;
- float factor=0.80566;//3300/4095
- switch(__even_in_range(ADC12IV,34))
- {
- case 0: break; // Vector 0: No interrupt
- case 2: break; // Vector 2: ADC overflow
- case 4: break; // Vector 4: ADC timing overflow
- case 6: break; // Vector 6: ADC12IFG0
- case 8: // Vector 8: ADC12IFG1
- tempData1[count] = ADC12MEM0;
- tempData2[count] = ADC12MEM1;// Move results, IFG is cleared
- // Move results, IFG is cleared
- count++;
- com1=tempData1[count];
- //com1=com1*factor;
- com2=tempData2[count];
- //com2=com2*factor;
- if (count == 12)
- {
- count = 0;
- flag=1;
- ADC12IE = 0x00;
- }
- // Move results, IFG is cleared
- // __bic_SR_register_on_exit(LPM4_bits); // Exit active CPU
- break;
- case 10: break; // Vector 10: ADC12IFG2
- case 12:break; // Vector 12: ADC12IFG3
- case 14: break; // Vector 14: ADC12IFG4
- case 16: break; // Vector 16: ADC12IFG5
- case 18: break; // Vector 18: ADC12IFG6
- case 20: break; // Vector 20: ADC12IFG7
- case 22: break; // Vector 22: ADC12IFG8
- case 24: break; // Vector 24: ADC12IFG9
- case 26: break; // Vector 26: ADC12IFG10
- case 28: break; // Vector 28: ADC12IFG11
- case 30: break; // Vector 30: ADC12IFG12
- case 32: break; // Vector 32: ADC12IFG13
- case 34: break; // Vector 34: ADC12IFG14
- default: break;
- }
- }
- #pragma vector=TIMER1_A1_VECTOR
- __interrupt void TIMER1_A1_ISR(void)
- {
- switch(__even_in_range(TA1IV,14))
- {
- case 0: break; // No interrupt
- case 2:
- if(TA1CCTL1&CM0)
- {
- newdata=TA1R;
- if(newdata
- {
- data=65536+newdata-olddata;
- }
- else
- {
- data=newdata-olddata;
- }
- olddata=newdata;
- num=data;
- }
- P1OUT ^= BIT3;
- break; // CCR1 not used
- case 4: break; // CCR2 not used
- case 6: break; // reserved
- case 8: break; // reserved
- case 10: break; // reserved
- case 12: break; // reserved
- case 14: break; // overflow
- default: break;
- }
- }
复制代码
此帖出自
小平头技术问答
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include "msp430.h"
- #include "HAL_UCS.h"
- #include "HAL_Board.h"
- #include "CTS_Layer.h"
- #include "HAL_AppUart.h"
- #include "ADC.h"
- unsigned int flag=0;
- volatile unsigned int A0results;
- volatile unsigned int A1results;
- volatile unsigned int A2results;
- volatile unsigned int A3results;
- void ADCInit()
- {
- P6SEL |= BIT5 |BIT6| BIT7| BIT1; //使能ADC通道7
- REFCTL0 &= ~REFMSTR; //重置REFMSTR,交出控制权
- ADC12CTL0 = ADC12ON+ADC12MSC+ADC12REFON+ADC12REF2_5V; //打开ADC12采样时间,设置参考电压为2.5v
- ADC12CTL1 = ADC12SHP|ADC12CONSEQ_3; //使用采样时钟
- ADC12MCTL0 = ADC12SREF_1+ADC12INCH_1 ; //A1,V(R+) = VREF+ and V(R-) = AVSS, ADC12MCTL0转换结果存储寄存器
- ADC12MCTL1= ADC12SREF_1+ADC12INCH_5 ; //A5,V(R+) = VREF+ and V(R-) = AVSS
- ADC12MCTL2 = ADC12SREF_1+ADC12INCH_6 ; //A6,V(R+) = VREF+ and V(R-) = AVSS
- ADC12MCTL3= ADC12SREF_1+ADC12INCH_7+ADC12EOS ; //A7,V(R+) = VREF+ and V(R-) = AVSS,结束码
- for ( int i=0; i<0x30; i++); //延时,确保参考电压设置完成
- }
- void ADCStart()
- {
- ADC12IE = 0x08; //使能中断ADC12IFG.3
- ADC12CTL0 |= ADC12ENC; //使能转换
- ADC12CTL0 |= ADC12SC; //开始转换
- }
- unsigned int GetPMValue()
- {
- while(flag==0);
- flag=1;
- return A2results;
- }
- unsigned int GetVBATValue()
- {
- while(flag==0);
- flag=1;
- return A3results;
- }
- #pragma vector = ADC12_VECTOR
- __interrupt void ADC12_ISR(void)
- {
- switch(__even_in_range(ADC12IV,34))
- {
- case 0: break; // Vector 0: No interrupt
- case 2: break; // Vector 2: ADC overflow
- case 4: break; // Vector 4: ADC timing overflow
- case 6: break; // Vector 6: ADC12IFG0
- case 8: break; // Vector 8: ADC12IFG1
- case 10: break; // Vector 10: ADC12IFG2
- case 12: // Vector 12: ADC12IFG3
- A0results = ADC12MEM0; // Move A0 results, IFG is cleared
- A1results= ADC12MEM1; // Move A1 results, IFG is cleared
- A2results= ADC12MEM2; // Move A2 results, IFG is cleared
- A3results = ADC12MEM3; // Move A3 results, IFG is cleared
- #ifdef DEBUG
- if(A0results>500)
- printf("PM2.5 VALUE is %d
",A0results);
- #endif
- case 14: break; // Vector 14: ADC12IFG4
- case 16: break; // Vector 16: ADC12IFG5
- case 18: break; // Vector 18: ADC12IFG6
- case 20: break; // Vector 20: ADC12IFG7
- case 22: break; // Vector 22: ADC12IFG8
- case 24: break; // Vector 24: ADC12IFG9
- case 26: break; // Vector 26: ADC12IFG10
- case 28: break; // Vector 28: ADC12IFG11
- case 30: break; // Vector 30: ADC12IFG12
- case 32: break; // Vector 32: ADC12IFG13
- case 34: break; // Vector 34: ADC12IFG14
- default: break;
- }
-
- ADC12IE = 0x00; // Enable ADC12IFG.3
- ADC12CTL0 &= ~(ADC12ENC); // Enable conversions
- ADC12CTL0 &= ~(ADC12SC);
- flag=1;
- }
复制代码参考下上面代码吧
一周热门 更多>