Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <msp430xG46x.h>
- #include <stdio.h>
- #include "LCD.h"
- #define DAC_V_REF 2500
- inline void setupADC();
- inline void setupDAC();
- inline void setupPorts();
- volatile int DAC12_0V;
- volatile int g = 1;
- volatile int c = 0;
- volatile int a = 0;
- volatile int m = 50;
- volatile int n = 0;
- volatile int u = 0;
- volatile int y = 0;
- volatile int k = 0;
- volatile int Vr;
- volatile int vout[10];
- /*
- * main.c
- */
- int main(void) {
- WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
- a = 0;
- setupADC();
- setupDAC();
- setupPorts();
- __enable_interrupt();
- initLCD_A();
- clrLCD();
- __bis_SR_register(LPM0_bits + GIE);
- }
- void setupDAC(void) {
- // dac0 DAC12.0
- DAC12_0CTL = DAC12IR
- | DAC12AMP_5
- | DAC12ENC
- | DAC12IE
- | DAC12LSEL0;
- printf("dac on\n");
- }
- void setupADC(void) {
- ADC12CTL0 = REF2_5V // set reference voltage (for DAC)
- | REFON // enable reference voltage generator (for DAC)
- | ADC12ON // enable adc
- // | MSC // enable multi sample conversion
- | SHT0_12; // 1024 cycles
- ADC12CTL1 |= CONSEQ_0
- | SHP // multichannel
- | CSTARTADD0;
- // A5
- P6SEL |= BIT5;
- //P6DIR = 0x00;
- ADC12MCTL1 |= INCH_5;
- printf("adc on\n");
- ADC12CTL0 |= ENC;
- ADC12IE = 0xFFFF;
- }
- void setupPorts() {
- // init button
- P1SEL &= ~BIT0; // enable GPIO in P1.0
- P1IE |= BIT0; // enable interrupt from P1.0
- P1IES |= BIT0; // select interrupt-edge
- P1DIR &= ~BIT0; // set P1.0 to input
- // init all ports
- P2SEL &= ~BIT0;
- P3SEL &= ~BIT0;
- P4SEL &= ~BIT0;
- P7SEL &= ~BIT0;
- P8SEL &= ~BIT0;
- P9SEL &= ~BIT0;
- P10SEL &= ~BIT0;
- PASEL &= ~BIT0;
- PBSEL &= ~BIT0;
- }
- #pragma vector=DAC12_VECTOR
- __interrupt void DAC12ISR(void) {
- //printf("dac interrupt\n");
- DAC12_0CTL &= ~DAC12IFG;
- ADC12CTL0 |= ADC12SC;
- }
- #pragma vector=ADC12_VECTOR
- __interrupt void ADC12ISR(void) {
- //printf("adc interrupt, %d\n", n);
- if(n < 9)
- {
- vout[n] = ADC12MEM1;
- //printf("%d\t", vout[c]);
- n++;
- ADC12CTL0 |= ADC12SC;
- }
- else
- {
- vout[9] = ADC12MEM1;
- n = 0;
- //printf("\n");
- for(u = 9; u > 0; u--)
- {
- for(y = 0; y < u; y++)
- {
- if(vout[y] > vout[y + 1])
- {
- k = vout[y];
- vout[y] = vout[y + 1];
- vout[y + 1] = k;
- }
- }
- }
- //Vr = abs(vout[4])*27/16 - abs(DAC12_0V);
- printf("c: %d, Vin: %d, Vout: %d\n", c, DAC12_0V, vout[4]);
- if(abs(vout[4]) < 4030)
- //if(1)
- {
- printf("ok\n");
- if(DAC12_0V < 2450)
- {
- c++;
- DAC12_0V += m;
- //printf("%d\n", DAC12_0V * (4096. / DAC_V_REF));
- DAC12_0DAT = DAC12_0V * (4096. / DAC_V_REF);
- }
- else
- {
- clrLCD();
- displayPass();
- __bis_SR_register_on_exit(LPM0_bits);
- }
- }
- else
- {
- printf("not ok\n");
- clrLCD;
- displayFail();
- //__bis_SR_register_on_exit(LPM0_bits);
- }
- }
- }
- #pragma vector = PORT1_VECTOR
- __interrupt void PORT1_ISR(void) {
- P1IE &= ~BIT0;
- if (!(P1IN & BIT0)) {
- if(g == 1)
- {
- printf("btn\n");
- // vrefsum = 0;
- // vusum = 0;
- n = 0;
- c = 0;
- displayWait();
- DAC12_0V = 0;
- DAC12_0DAT = DAC12_0V * (4096. / DAC_V_REF);
- g = 0;
- }
- else
- {
- clrLCD();
- g = 1;
- __bis_SR_register_on_exit(LPM0_bits);
- }
- }
- P1IE |= BIT0;
- P1IFG &= ~BIT0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement