Part Number:MSP430FR6989
Hi,
I want to measure the IV characteristic for an organic device and in order to do that I connect this device wiht an external circuit and I want to generate the voltage from MCU with a fixed time delay between steps, the voltage range is from 0 V to 3 V, the step increment could be any small number ,while the voltage is applied I want to read the output of the external circuit at each point using the ADC and save that output in order to plot the IV at the end, I wrote this code but it does not work properly. I'm still not aware to how to save the outputs , how to apply the delay.
#include <msp430fr6989.h>
#define PERIOD_PWM 4000
unsigned int j;
unsigned int i;
unsigned int adc_value[1] = {0};
float adc_value_volts[1] = {0};
_Bool flag_start = 0;
void init_tim_a(void);
void init_ADC(void);
void init_gpio(void);
void init_dco(void);
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
PM5CTL0 &= ~LOCKLPM5;
init_dco();
init_gpio();
init_tim_a();
init_ADC();
_enable_interrupt();
ADC12CTL0 |= ADC12SC;
while(1)
{
for(i=0;i<=5095;i=i+200)
{
TA0CCR0 = (i*PERIOD_PWM/3.3) - 1;
TA0CTL |= MC_2;
_delay_cycles(100000000);//delay
if(flag_start == 1)
{
ADC12CTL0 |= ADC12SC; // Start ADC
flag_start = 0;
adc_value_volts[0] = adc_value[0]*3.3/4095;
//Y11_out=adc_value_volts[1];
}
TA0CTL &= ~MC_0;
}
//}
}
}
void init_ADC(void)
{
P8SEL0 |= BIT7;//|BIT6|BIT5|BIT4; // P8.7, P8.6,P8.5,P8.4 ADC option select
P8SEL1 |= BIT7;//|BIT6|BIT5|BIT4;
ADC12CTL0 &=~ADC12ENC;
ADC12CTL1 |= ADC12CONSEQ_1;
ADC12CTL0 |= ADC12SHT0_5 |ADC12SHT1_5 | ADC12ON|ADC12MSC;
ADC12CTL1 |= ADC12PDIV__1|ADC12SHP|ADC12DIV_5|ADC12SSEL_0;
ADC12CTL2 |= ADC12RES_2;
ADC12IER0 |= BIT0;//|BIT1|BIT2|BIT3|BIT4|BIT5|BIT6|BIT7|BIT8;; // Enable interrupt
ADC12MCTL0 = ADC12INCH_4|ADC12EOS;
ADC12CTL0 |= ADC12ENC;
} // ADC_init
void init_dco(void)
{
FRCTL0 = FRCTLPW | NWAITS_2;
CSCTL0_H = CSKEY >> 8; // Unlock CS registers
CSCTL1 = DCOFSEL_5|DCORSEL; // DCO = 24 MHz
CSCTL3 = DIVA_0 | DIVS_0 | DIVM_0;
CSCTL0_H = 0; // // Lock CS registers
}
void init_tim_a(void)
{
TA0CCR0 = PERIOD_PWM/2 - 1;
TA0CCR1 = PERIOD_PWM - 1;
TA0CCTL0 = CCIE;
TA0CCTL1 = CCIE;
TA0CTL = TASSEL_2 | ID_0 | MC_2;
}
void init_gpio(void)
{
P1DIR |= BIT5|BIT6;
P1OUT |= BIT5;
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void CCR0(void)
{
P1OUT &= ~BIT5;
}
#pragma vector = TIMER0_A1_VECTOR
__interrupt void CCR1(void)
{
if(TA0CCTL1&CCIFG == CCIFG)
{
TA0CCTL1 &= ~CCIFG;
P1OUT |= BIT5;
TA0R = 0;
}
}
#pragma vector = ADC12_VECTOR
__interrupt void adc_isr(void)
{
switch(ADC12IV)
{
case 0: break;
case 2: break;
case 4: break;
case 6: break;
case 8: break;
case 10: break;
case 12: // Vector 12: ADC12IFG0
{
adc_value[0] = ADC12MEM0;
flag_start = 1;
} break;
default: break;
}
}