Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.34 KB | None | 0 0
  1. #include <msp430xG46x.h>
  2. #include <stdio.h>
  3. #include "LCD.h"
  4. #define DAC_V_REF 2500
  5. inline void setupADC();
  6. inline void setupDAC();
  7. inline void setupPorts();
  8. volatile int DAC12_0V;
  9. volatile int g = 1;
  10. volatile int c = 0;
  11. volatile int a = 0;
  12. volatile int m = 50;
  13. volatile int n = 0;
  14. volatile int u = 0;
  15. volatile int y = 0;
  16. volatile int k = 0;
  17. volatile int Vr;
  18. volatile int vout[10];
  19.  
  20.  
  21. /*
  22. * main.c
  23. */
  24.  
  25. int main(void) {
  26.     WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
  27.     a = 0;
  28.     setupADC();
  29.     setupDAC();
  30.     setupPorts();
  31.  
  32.     __enable_interrupt();
  33.  
  34.     initLCD_A();
  35.     clrLCD();
  36.  
  37.     __bis_SR_register(LPM0_bits + GIE);
  38.  
  39. }
  40.  
  41. void setupDAC(void) {
  42.  // dac0 DAC12.0
  43.     DAC12_0CTL = DAC12IR
  44.             | DAC12AMP_5
  45.             | DAC12ENC
  46.             | DAC12IE
  47.             | DAC12LSEL0;
  48.     printf("dac on\n");
  49. }
  50.  
  51. void setupADC(void) {
  52.     ADC12CTL0 = REF2_5V // set reference voltage (for DAC)
  53.             | REFON // enable reference voltage generator (for DAC)
  54.             | ADC12ON // enable adc
  55. //            | MSC // enable multi sample conversion
  56.             | SHT0_12; // 1024 cycles
  57.     ADC12CTL1 |= CONSEQ_0
  58.             | SHP // multichannel
  59.             | CSTARTADD0;
  60.     // A5
  61.     P6SEL |= BIT5;
  62.     //P6DIR = 0x00;
  63.     ADC12MCTL1 |= INCH_5;
  64.     printf("adc on\n");
  65.     ADC12CTL0 |= ENC;
  66.     ADC12IE = 0xFFFF;
  67. }
  68.  
  69. void setupPorts() {
  70.     // init button
  71.     P1SEL &= ~BIT0; // enable GPIO in P1.0
  72.     P1IE |= BIT0; // enable interrupt from P1.0
  73.     P1IES |= BIT0; // select interrupt-edge
  74.     P1DIR &= ~BIT0; // set P1.0 to input
  75.     // init all ports
  76.     P2SEL &= ~BIT0;
  77.     P3SEL &= ~BIT0;
  78.     P4SEL &= ~BIT0;
  79.     P7SEL &= ~BIT0;
  80.     P8SEL &= ~BIT0;
  81.     P9SEL &= ~BIT0;
  82.     P10SEL &= ~BIT0;
  83.     PASEL &= ~BIT0;
  84.     PBSEL &= ~BIT0;
  85. }
  86.  
  87. #pragma vector=DAC12_VECTOR
  88. __interrupt void DAC12ISR(void) {
  89.     //printf("dac interrupt\n");
  90.     DAC12_0CTL &= ~DAC12IFG;
  91.     ADC12CTL0 |= ADC12SC;
  92. }
  93.  
  94. #pragma vector=ADC12_VECTOR
  95. __interrupt void ADC12ISR(void) {
  96.     //printf("adc interrupt, %d\n", n);
  97.     if(n < 9)
  98.     {
  99.         vout[n] = ADC12MEM1;
  100.         //printf("%d\t", vout[c]);
  101.         n++;
  102.         ADC12CTL0 |= ADC12SC;
  103.     }
  104.     else
  105.     {
  106.         vout[9] = ADC12MEM1;
  107.         n = 0;
  108.         //printf("\n");
  109.         for(u = 9; u > 0; u--)
  110.         {
  111.             for(y = 0; y < u; y++)
  112.             {
  113.                 if(vout[y] > vout[y + 1])
  114.                 {
  115.                    k = vout[y];
  116.                    vout[y] = vout[y + 1];
  117.                    vout[y + 1] = k;
  118.                 }
  119.             }
  120.         }
  121.         //Vr = abs(vout[4])*27/16 - abs(DAC12_0V);
  122.             printf("c: %d, Vin: %d, Vout: %d\n", c, DAC12_0V, vout[4]);
  123.                     if(abs(vout[4]) < 4030)
  124.             //if(1)
  125.             {
  126.                         printf("ok\n");
  127.                         if(DAC12_0V < 2450)
  128.                                 {
  129.                                     c++;
  130.                                     DAC12_0V += m;
  131.                                     //printf("%d\n", DAC12_0V * (4096. / DAC_V_REF));
  132.                                     DAC12_0DAT = DAC12_0V * (4096. / DAC_V_REF);
  133.                                 }
  134.                                 else
  135.                                 {
  136.                                     clrLCD();
  137.                                     displayPass();
  138.                                     __bis_SR_register_on_exit(LPM0_bits);
  139.                                 }
  140.                     }
  141.                     else
  142.                     {
  143.                         printf("not ok\n");
  144.                         clrLCD;
  145.                         displayFail();
  146.                         //__bis_SR_register_on_exit(LPM0_bits);
  147.                     }
  148.  
  149.  
  150.  
  151.     }
  152. }
  153.  
  154. #pragma vector = PORT1_VECTOR
  155. __interrupt void PORT1_ISR(void) {
  156.     P1IE &= ~BIT0;
  157.     if (!(P1IN & BIT0)) {
  158.         if(g == 1)
  159.         {
  160.             printf("btn\n");
  161.             // vrefsum = 0;
  162.             // vusum = 0;
  163.             n = 0;
  164.             c = 0;
  165.             displayWait();
  166.             DAC12_0V = 0;
  167.             DAC12_0DAT = DAC12_0V * (4096. / DAC_V_REF);
  168.             g = 0;
  169.         }
  170.         else
  171.         {
  172.             clrLCD();
  173.             g = 1;
  174.             __bis_SR_register_on_exit(LPM0_bits);
  175.         }
  176.     }
  177.     P1IE |= BIT0;
  178.     P1IFG &= ~BIT0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement