Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP Write a program in C for MSP430: At the beginning the switch (P2.3) is turn

ID: 3866247 • Letter: H

Question

HELP

Write a program in C for MSP430:

At the beginning the switch (P2.3) is turned off, and in low poer mode (LMP3); when the switchis turned on you leave low power mode (using Port I/O interrupt)

Using a timer every second the program does an A/D conversion (temperature). every 5 seconds the average temperature is calculated in F, and the temperature is displayed on the 7 segment display.

int main(void)

{

       //Port Initializations

//Port Interrupt for switch at P1.X

       //Set edge transition pattern

       //Reset port interrupt flags

       //Initial setting for the Timer

       //Enable interrupt system

//Enter low power mode

}

void ConfigureAdc(void)
{
   ADC10CTL1 = CONSEQ_0 + INCH_0;
       ADC10CTL0 = ADC10SHT_3 + MSC + ADC10ON;

       while (ADC10CTL1 & BUSY);
       ADC10DTC1 = 5;
       ADC10AE0 |= BIT0;
}

Port I/O ISR (assume that a pin in port 1 is used):

#pragma vector = PORT1_VECTOR

__interrupt void PORT1_ISR(void)

{

       //Leave low power mode

       //Enable WDT for 7-seg display

       //Timer_A for timing of A/D conversions; 1 second intervals

       //A/D Conversion setup

       //reset Port I/O interrupt flags

}

In Timer A interrupt:

#pragma vector = TIMER0_A1_VECTOR

__interrupt void Timer_A(void)

{

       switch(TAIV)

       {

       case 0x02: break;

       case 0x04: break;

       case 0x0A:

              //if the input switch is on, do

              {

                     //Get an A/D sample,

                     //If 5 samples are collected, take an average and display it.                          //Else keep looping for more samples

              }

       break;

       }

}

void getanalogvalues()
{
for (i=0;i<5;i++)
{
    ADC10CTL0 &= ~ENC;
    while (ADC10CTL1 & BUSY);                         //Wait while ADC is busy
    ADC10SA = (unsigned)&ADCReading;               //RAM Address of ADC Data, must be reset every conversion
    ADC10CTL0 |= (ENC | ADC10SC);                     //Start ADC Conversion
    while (ADC10CTL1 & BUSY);
    temp += ADCReading;
    __delay_cycles(00010);
}
temp= temp/5;
// Average the 5 reading for the variable
}

Explanation / Answer

// Global variables

int adc[10] = {0}; //Sets up an array of 10 integers and zero's the values

int avg_adc = 0;

// Function prototypes

void adc_Setup();

void adc_Sam10();

void main()

{

  WDTCTL = WDTPW + WDTHOLD; // Stop WDT

  adc_Setup(); // Fucntion call for adc_setup

  while(1)

  {

  adc_Sam10(); // Function call for adc_samp

  // Add all the sampled data and divide by 10 to find average

  avg_adc = ((adc[0]+adc[1]+adc[2]+adc[3]+adc[4]+adc[5]+adc[6]+adc[7]+adc[8]+adc[9]) / 10);

  }

}

// ADC10 interrupt service routine

#pragma vector=ADC10_VECTOR

__interrupt void ADC10_ISR(void)

{

  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)

}

// ADC set-up function

void adc_Setup()

{

ADC10CTL1 = CONSEQ_2 + INCH_0; // Repeat single channel, A0

ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; // Sample & Hold Time + ADC10 ON + Interrupt Enable

ADC10DTC1 = 0x0A; // 10 conversions

ADC10AE0 |= 0x01; // P1.0 ADC option select

}

// ADC sample conversion function

void adc_Sam10()

{

    ADC10CTL0 &= ~ENC; // Disable Conversion

    while (ADC10CTL1 & BUSY); // Wait if ADC10 busy

    ADC10SA = (int)adc; // Transfers data to next array (DTC auto increments address)

    ADC10CTL0 |= ENC + ADC10SC; // Enable Conversion and conversion start

    __bis_SR_register(CPUOFF + GIE);// Low Power Mode 0, ADC10_ISR

}

int main(void)

{

  Setup_HW();

  while (1)

  {

Read_Acc(); // This function reads the ADC and stores the x, y and z values

  }

}

// ADC10 interrupt service routine

#pragma vector=ADC10_VECTOR

__interrupt void ADC10_ISR(void)

{

  __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)

}

void Setup_HW(void)

{

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  ADC10CTL1 = INCH_2 + CONSEQ_1;            // A2/A1/A0, single sequence

  ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE;

  ADC10DTC1 = 0x03;                         // 3 conversions

  ADC10AE0 |= 0x03;                         // Disable digital I/O on P1.0 to P1.2

}

void Read_Acc(void)

{

    ADC10CTL0 &= ~ENC;

    while (ADC10CTL1 & BUSY);               // Wait if ADC10 core is active

    ADC10SA = (unsigned int)adc; // Copies data in ADC10SA to unsigned int adc array

    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start

    X_Axis = adc[0]; // adc array 0 copied to the variable X_Axis

    Y_Axis = adc[1]; // adc array 1 copied to the variable Y_Axis

    Z_Axis = adc[2]; // adc array 2 copied to the variable Z_Axis

    __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit

}

main()
{
adc_init();
read_adc();
}
void adc_init()
{
// WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL1 = INCH_10 + CONSEQ_1; // A2/A1/A0, single sequence
ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON;
ADC10DTC1 = 0x02; // 2 conversions
ADC10AE0 |= 0x03; // Disable digital I/O on P1.0 to P1.
}

void read_ADC()
{

//ADC10CTL1 |=INCH_10;
ADC10SA =adc; // Copies data in ADC10SA to unsigned int adc array
//ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
ADC10CTL0 |= ENC + ADC10SC; // Start Conversion
while(ADC10CTL1 & ADC10BUSY);

t = adc[0]; // adc array 0 copied to the variable X_Axis
t1 =adc[1];
// Y_Axis = adc[1]; // adc array 1 copied to the variable Y_Axis

//t = ((((unsigned int)ADC10MEM-673)*423)/1024);//for tempreture
t = ((((unsigned int)t-673)*423)/1024);//for tempreture
temp = ((float)((float)(ADC10MEM*3.3f)/1024));//for potentiometer
send_string(“temp=”);
temp2=t;
sprintf(c,”%d”,temp2);
send_string(c);
send_string(“vol=”);
temp1=temp;
sprintf(a,”%d”,temp1);
send_string(a);
send_data(‘.’);
temp3=(temp – temp1)*100;
sprintf(b,”%d”,temp3);
send_string(b);

//return (float)t1;
//return(int) ((t * 27069L – 18169625L) >> 16);
}