//ICC-MSP application builder [F149] : 11/24/2010 3:45:20 PM //Target 16x device #include #include #include #include unsigned int M=32; volatile unsigned long int adcvalue[32] = {0}; volatile unsigned long int data[32]; int i,j; volatile unsigned int ready = 0; void clock(void) { volatile unsigned int ic; WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog Timer BCSCTL1 &= ~XT2OFF; do { IFG1 &= ~OFIFG; for(ic=0xFF;ic>0;ic--); } while((IFG1 & OFIFG)); BCSCTL2 |= SELM_2+SELS; } void port_init(void) { P1OUT=0x00; P1DIR=0x00; P1IES=0x00; P1IE=0x00; P1SEL=0x00; P2OUT=0x00; P2DIR=0x00; P2IES=0x00; P2IE=0x00; P2SEL=0x00; P3OUT=0x00; P3DIR=0x00; P3SEL=0x00; P4OUT=0x00; P4DIR=0x00; P4SEL=0x00; P5OUT=0x10; P5DIR=0x10; P5SEL=0x10; P6OUT=0x00; P6DIR=0x00; P6SEL=0x00+BIT0; } void init_adc12 (void) { ADC12CTL0=SHT0_7+ADC12ON+REFON+REF2_5V+MSC; // switch on reference voltage , ref=2.5 ADC12CTL1=CONSEQ_3+SHS_0+ADC12SSEL_2+SHP; //repeated sequence of channels ADC12MCTL0=SREF_1+INCH_0+EOS; //Vr+=Vref+ ADC12IE=0x01; ADC12CTL0|=ENC } void init_timera(void) { TACCTL0 = OUTMOD_3; // timer mode is set/reset TACCR0=0xD2; // timer ticks at 210 TACTL = TASSEL_2 + MC_1; // SMCLK and Up mode } void usart0_init(void) //Clock source=SMCLK //Desired baud=19200 //Actual baud=19196 { UCTL0=0x10;//CHAR0 UTCTL0=0x20;//SSEL0.1 URCTL0=0x08;//URXEIE0 U0BR1=0x00; U0BR0=0xD0; UMCTL0=0xA2; } //interrupt handlers //call this routine to initialise all peripherals void init_devices(void) { //stop errant interrupts until set up DINT(); //disable all interrupts ME1=0X00; //disable sfr peripherals ME2=0X00; IE1=0x00; //disable sfr interrupts IE2=0x00; //watchdog initialisation including nmi function //WDTCTL=0x5A00 | 0x00; WDTCTL=0x5A00 | 0x10;//TMSEL //initialise other peripherals port_init(); usart0_init(); init_adc12(); init_timera(); clock(); ME1=0x00; ME2=0x00; IE1=0x00; IE2=0x00; EINT(); //re-enable interrupts } //interrupt handlers #pragma interrupt_handler adc12_isr:ADC_VECTOR void adc12_isr(void) { static unsigned int x = 0; ADC12CTL0|=ADC12SC+ENC; data[x++]=ADC12MEM0; if (x==32) { x= 0; ready = 1; } } void main(void) { init_devices(); sinewave(); while(1) { while(!ready); for(j=0;j<32;j++) adcvalue[j]+=data[j];// samples in M*32 ready=0; } /*============================================================================*/ /* */ /* ----------------------------- Signal generation ------------------------ */ /* */ /*============================================================================*/ static int Sin_tab[32] = { 1843, 2202, 2548, 2867, 3145, 3375, 3545, 3650, 3686, 3650, 3545, 3375, 3146, 2867, 2548, 2202, 1843, 1483, 1138, 819, 540, 311, 140, 35, 0, 35, 140, 311, 540, 819, 1138, 1483 }; void sinewave(void) { WDTCTL = WDTPW + WDTHOLD; ADC12CTL0 = REFON+REF2_5V; //Use 2.5V ADC reference DMA2SA = (int) Sin_tab; //Initialize the DMA source address with the sine lookup table DMA2DA = (unsigned int)&DAC12_1DAT; //Initialize the DMA destination address with the DAC1 data register DMA2SZ = 0x20; //Specify the DMA trasfer size (32) DMACTL0 = DMA2TSEL_2; //Trigger for the DMA transfers (TimerB CCIFG flag) DMA2CTL = DMADT_4 + DMASRCINCR_3 + DMAEN; //Repeated single transfer,increment the source address & enable DMA DAC12_1CTL = DAC12LSEL_3 + DAC12IR + DAC12AMP_5 + DAC12IFG + DAC12ENC; //DAC trigger with timerB rising edge & enable DAC TBCCTL2 = OUTMOD_3; //TimerB output-mode (set/reset) TBCCR2 =1; TBCCR0 =0x1D; TBCTL = TBSSEL_2 + MC_1; //Source SMCLK ,UP mode }