Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

ADC always reading constant number

$
0
0

Hello,

I am trying to use the ADC in two modes, one in measuring mode where i just take measurements and record them, while the other is live mode where I just take measurements and send them over uart. I currently have the measuring mode working good, but when I try the live mode it always measures high byte 1 low byte 255, no-matter what the voltage is on the input.

Here is my code, please ignore the comments as some of them are wrong:

#include <msp430.h>
// Macros
#define usb_mode() ((P3IN & BIT7) == BIT7)
#define exit_to_active() __bic_SR_register_on_exit(CPUOFF+OSCOFF+SCG1+SCG0); __bis_SR_register_on_exit(GIE);
// Constants
unsigned int i,j,k;
unsigned long count;
unsigned short send;
unsigned short cleardata;
unsigned short livemode = 0;
unsigned short usb;
unsigned short writing;
unsigned int ADC_data = 0,ADC_S1 = 0,ADC_S2 = 0,ADC_S3 = 0,ADC_S4 = 0,ADC_S5 = 0,ADC_S6 = 0, ADCDone = 0;
unsigned int TimerInt = 0;
unsigned int seconds = 0;
unsigned int temp = 0;
unsigned int min = 0;
unsigned int timerperiod = 1920;
#pragma SET_DATA_SECTION(".fram_vars")
unsigned int x = 0;
unsigned int y = 0;
unsigned int clearrtc = 1;
unsigned int data[867][7];
#pragma SET_DATA_SECTION()
void rtc_init() {
PJSEL0 |= BIT4; // XT1
PJSEL1 &= ~BIT4;
CSCTL0_H = 0xA5;
CSCTL1 = DCORSEL + DCOFSEL0 + DCOFSEL1; // Set max. DCO setting
CSCTL2 = SELA_0 | SELS_3 | SELM_3; // set ACLK = XT1; MCLK = DCO
CSCTL3 = DIVA_0 | DIVS_0 | DIVM_0;
CSCTL4 &= ~XT1OFF;
do {
CSCTL5 &= ~XT1OFFG; // Clear XT1 fault flag
SFRIFG1  &= ~OFIFG;
} while (SFRIFG1 & OFIFG);
CSCTL0_H = 0x01; // Lock Register
RTCCTL01 &= ~RTCHOLD; // Turn on RTC
}
void init() {
WDTCTL = WDTPW + WDTHOLD;
P1DIR = 0;
P1OUT = 0;
P1REN = 0;
P2DIR = 0;
P2OUT = 0;
P2REN = BIT2;
P2IES &= ~BIT2; //low to high interrupts
P2IFG &= ~BIT2; // clear interrupt flag
P2IE |= BIT2; // enable interrupts
P4DIR = 0;
P4OUT = 0;
P4REN = 0;
P3DIR = (0xFF) & (~BIT7);
P3REN = BIT7;
P3OUT = 0;
if(usb = usb_mode()){
PJDIR = 0xFF;
PJOUT = BIT0; // Pump enable
PJREN = 0;
P3IES = BIT7; // P1.3 Falling edge
P3IE |= BIT7; // P1.3 interrupt enabled
P3IFG &= ~BIT7; // P1.3 IFG cleared
} else {
PJDIR = 0xFF;
PJOUT = 0;
PJREN = 0;
P3IES &= ~BIT7; // P1.3 Rising edge
P3IFG &= ~BIT7; // P1.3 IFG cleared
P3IE |= BIT7; // P1.3 interrupt enabled
}
writing = 0;
rtc_init();
}
void wakeup_init() {
PMMCTL0_H = PMMPW_H; // open PMM
PMMCTL0_L &= ~PMMREGOFF;
PMMCTL0_L |= SVSLE;
__bic_SR_register(GIE+CPUOFF+OSCOFF+SCG1+SCG0); // needed?
__no_operation();
init();
PM5CTL0 &= ~LOCKLPM5; // Clear LOCKIO and enable ports
}
void usb_init() {
// Configure Clock
RTCCTL01 |= RTCHOLD; // Turn on RTC
CSCTL0_H = 0xA5;
CSCTL1 = DCORSEL + DCOFSEL0 + DCOFSEL1; // Set max. DCO setting
CSCTL2 = SELA_1 + SELS_3 + SELM_3; // set ACLK - VLO, the rest = MCLK = DCO
CSCTL3 = DIVA_0 + DIVS_0 + DIVM_0; // set all dividers to 0
// Configure UART
P2SEL1 |= (BIT5 + BIT6);
P2SEL0 &= ~(BIT5 + BIT6);
UCA1CTL1 |= UCSWRST;
UCA1CTL1 = UCSSEL_2;
UCA1MCTLW |= 0x0000;
UCA1BR0 = 0xc4; // 9600 baud
UCA1BR1 = 0x09;
UCA1CTL1 &= ~UCSWRST; // release from reset
UCA1IE |= UCRXIE;
}
void uart_disable() {
UCA1CTL1 |= UCSWRST;
}
void adc_init(char number){
P3OUT |= number;
P1SEL1 |= number;
P1SEL0 |= number;
}
void adc_measure(char number){
ADC10CTL0 &= ~ADC10ENC; // Disable ADC
ADC10CTL0 |= ADC10SHT_2 + ADC10ON;        // ADC10ON, S&H=16 ADC clks
ADC10CTL1 |= ADC10SHP;                    // ADCCLK = MODOSC; sampling timer
ADC10CTL1 |= ADC10SSEL_2; // Set 'chan', SMCLK
ADC10CTL2 |= ADC10RES;                    // 10-bit conversion results
ADC10MCTL0 = number;                // A1 ADC input select; Vref=AVCC
ADC10IE |= ADC10IE0;                      // Enable ADC conv complete interrupt
ADC10CTL0 |= ADC10ENC + ADC10SC;        // Sampling and conversion start
while(!ADCDone){}
ADCDone = 0;
}
void adc_exit(char number){
ADC10CTL0 &= ~ADC10ENC; // Disable ADC
P3OUT &= ~number;
P1SEL1 &= ~number;
P1SEL0 &= ~number;
}
void init_timer() {
TA0CTL |= TASSEL_1 | MC_1 | TAIE | TACLR; // ACLK source, Up mode, interrupt enable
TA0CCR0 = 655;
}
void timer_exit(){
TA0CTL &= ~MC1 & ~MC0; // ACLK source, Up mode, interrupt enable
TA0CTL &= ~TAIE;
}
void adc(char store){
   while((RTCCTL01 & RTCRDY) != RTCRDY){}
seconds = RTCSEC + RTCMIN * 60 + RTCHOUR * 3600;
for(i=0;i<5;i++){
ADCDone = 0;
adc_init(BIT0);
adc_init(BIT1);
adc_init(BIT2);
adc_init(BIT3);
adc_init(BIT4);
adc_init(BIT5);
adc_measure(ADC10INCH_0);ADC_S1 = ADC_data;
adc_measure(ADC10INCH_1);ADC_S2 = ADC_data;
adc_measure(ADC10INCH_2);ADC_S3 = ADC_data;
adc_measure(ADC10INCH_3);ADC_S4 = ADC_data;
adc_measure(ADC10INCH_4);ADC_S5 = ADC_data;
adc_measure(ADC10INCH_5);ADC_S6 = ADC_data;
adc_exit(BIT0);
adc_exit(BIT1);
adc_exit(BIT2);
adc_exit(BIT3);
adc_exit(BIT4);
adc_exit(BIT5);
if(store){
if(x<867){
data[x][0] = seconds;
data[x][1] = ADC_S1;
data[x][2] = ADC_S2;
data[x][3] = ADC_S3;
data[x][4] = ADC_S4;
data[x][5] = ADC_S5;
data[x][6] = ADC_S6;
x++;
}else{
y++;
}
}
if(store == 0){
usb_init();
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S1 >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S1;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S2 >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S2;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S3 >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S3;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S4 >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S4;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S5 >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S5;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S6 >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = ADC_S6;
livemode = 0;
return;
}
if(i == 0){
init_timer();
}
TimerInt = 0;
if(i != 4){
while(TimerInt != 1){
__bis_SR_register(LPM3_bits + GIE);
}
}else{
timer_exit();
}
__no_operation();
}
}
int main(void)
{
init();
if(clearrtc){
RTCCTL01 |= RTCHOLD; // Hold RTC for now..
RTCSEC = 0;
RTCMIN = 0;
RTCHOUR = 0;
RTCCTL01 &= ~RTCHOLD; // Turn on RTC
clearrtc = 0;
}
while(1) {
if(usb = usb_mode()){
send = 0;
cleardata = 0;
usb_init();
if(usb = usb_mode()){
__bis_SR_register(GIE);
}
while(usb){
if(send){
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = x >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = x;
for(k = 0; k < x; k++){
for(j = 0; j  <7; j++){
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = data[k][j] >> 8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = data[k][j];
}
}
send = 0;
}
if(cleardata){
for(k = 0; k < x; k++){
for(j = 0; j < 7; j++){
data[k][j] = 0;
}
}
x=0;
y=0;
clearrtc = 1;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = 0x01;
cleardata = 0;
}
if(livemode){
rtc_init();
wakeup_init();
__bic_SR_register(CPUOFF+OSCOFF+SCG1+SCG0);
__bis_SR_register(GIE);
adc(0);
}
}
rtc_init();
} else if(writing ) {
adc(1);
}
writing = 0;
uart_disable();
REFCTL0 |= REFTCOFF;
REFCTL0 &= ~REFON;
// Right sequence for LPM 3.5?
PMMCTL0_H = PMMPW_H;
PMMCTL0_L |= PMMREGOFF;
PMMCTL0_L &= ~SVSLE;
__bis_SR_register(GIE+CPUOFF+OSCOFF+SCG1+SCG0);       // Enter LPM3
}
}
#pragma vector=PORT3_VECTOR
__interrupt void PORT3(void){
if(P3IFG & BIT7) {
wakeup_init();
exit_to_active();
return;
} else {
P3IFG = 0; // Reset flag
return;
}
}
// Read Ready Interrupt
#pragma vector=PORT2_VECTOR
__interrupt void PORT2(void){
if(P2IFG & BIT2) {
wakeup_init();
writing = 1;
exit_to_active();
return;
} else {
P2IFG = 0; // Reset flag
return;
}
}
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
switch(__even_in_range(UCA1IV,0x08)) {
case 0: break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
if(UCA1RXBUF == 0x01){
send = 1;
}
if(UCA1RXBUF == 0x02){
cleardata = 1;
}
if(UCA1RXBUF == 0x03){
livemode = 1;
}
//if(UCA1RXBUF == 0x04){
// livemode = 0;
//}
if(UCA1RXBUF == 0x05){
timerperiod = 1920;
}
if(UCA1RXBUF == 0x06){
timerperiod = 2560;
}
if(UCA1RXBUF == 0x07){
timerperiod = 3200;
}
break;
case 4: break; // Vector 4 - TXIFG
default: break;
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
  switch(__even_in_range(ADC10IV,12))
  {
    case  0: break;                          // No interrupt
    case  2: break;                          // conversion result overflow
    case  4: break;                          // conversion time overflow
    case  6: break;                          // ADC10HI
    case  8: break;                          // ADC10LO
    case 10: break;                          // ADC10IN
    case 12: ADC_data = ADC10MEM0;
    ADCDone = 1;   // Sets flag for main loop.
             break;
    default: break;
  }
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{
  switch(__even_in_range(TA0IV,14))
  {
    case  0: break;                          // No interrupt
    case  2: break;                          // CCR1 not used
    case  4: break;                          // CCR2 not used
    case  6: break;                          // reserved
    case  8: break;                          // reserved
    case 10: break;                          // reserved
    case 12: break;                          // reserved
    case 14: //P3OUT ^= BIT1;
    TimerInt = 1;                  // overflow
    __bic_SR_register_on_exit(LPM3_bits);
    __bis_SR_register_on_exit(GIE);
             break;
    default: break;
  }
}
I hope I can get some help on this,
Thanks,
-Omri



Viewing all articles
Browse latest Browse all 262198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>