Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Stability problem with a MSP430 design

Status
Not open for further replies.

thisistausif

Member level 2
Joined
Aug 3, 2005
Messages
49
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
india
Activity points
1,662
__delay_cycles

Hello experts,

i`m having stability problem with my msp430 ADC12 while using a
ref3033 refrence generator too, its not working well, the averaged
value of binary from ADC12MEM0 is quite stable but not the double
value which i get after some calculation on that binary value.

here is the code for that written in IAR, please suggest if this can
be solved by the some change in code,

thanks for the help.
Tausif.

#include <MSP430x13x.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>


void MSP430_Init_HW( void);//intialise hardware
void init_ADC(void);//intialise ADC
void wr_lcd_ir(int inst);// write lcd instruction register
void wr_lcd_dr(char data);// write lcd data register
int rd_lcd_sr(void);// read lcd status register
void init_lcd(void);// initialise lcd
void display_string( char * str, int cnt);// display string
void writeValue(double value,int unit);//write value to lcd
void itoa(unsigned int n, char s[], int * cnt); // number to string
void reverse(char s[]); // reverse string

//
// Main routine
//
void main(void)
{
MSP430_Init_HW( ); //initialize MCU
init_lcd(); //initialize lcd

display_string( "my test ",8);
//__delay_cycles( 2000000);

wr_lcd_ir(0xc0);//seting to line 2
//__delay_cycles( 20000);

display_string( "program",7);
__delay_cycles( 2000000);

wr_lcd_ir(0x01); //clear display
__delay_cycles( 20000);

double b=0.0,d=0.0,befz = 0.0 ;// b=>unit 1 d=> unit 2 befz=>value before zero adjustment
int unit=1,tmp=0xff;// unit selecteo
unsigned long avrv;
unsigned short inva=0,inv[8] =
{3180,3180,3180,3180,3180,3180,3180,3180};//invafz;
double zero_ram = 0.0;

unsigned short Adcinb[64];
for(int i=0;i<=63;i++)
Adcinb = 3180;
double *Flash_ptrA;
init_ADC();//initialise ADC12


checkinput:// check input at ADC12MEM0

ADC12IFG=0;
__delay_cycles( 200);
ADC12IFG=0;
__delay_cycles( 200);


if(ADC12IFG==0)
goto checkinput;


Adcinb[63]=Adcinb[62];
Adcinb[62]=Adcinb[61];
Adcinb[61]=Adcinb[60];
Adcinb[60]=Adcinb[59];
Adcinb[59]=Adcinb[58];
Adcinb[58]=Adcinb[57];
Adcinb[57]=Adcinb[56];
Adcinb[56]=Adcinb[55];
Adcinb[55]=Adcinb[54];
Adcinb[54]=Adcinb[53];
Adcinb[53]=Adcinb[52];
Adcinb[52]=Adcinb[51];
Adcinb[51]=Adcinb[50];
Adcinb[50]=Adcinb[49];
Adcinb[49]=Adcinb[48];
Adcinb[48]=Adcinb[47];
Adcinb[47]=Adcinb[46];
Adcinb[46]=Adcinb[45];
Adcinb[45]=Adcinb[44];
Adcinb[44]=Adcinb[43];
Adcinb[43]=Adcinb[42];
Adcinb[42]=Adcinb[41];
Adcinb[41]=Adcinb[40];
Adcinb[40]=Adcinb[39];
Adcinb[39]=Adcinb[38];
Adcinb[38]=Adcinb[37];
Adcinb[37]=Adcinb[36];
Adcinb[36]=Adcinb[35];
Adcinb[35]=Adcinb[34];
Adcinb[34]=Adcinb[33];
Adcinb[33]=Adcinb[32];
Adcinb[32]=Adcinb[31];
Adcinb[31]=Adcinb[30];
Adcinb[30]=Adcinb[29];
Adcinb[29]=Adcinb[28];
Adcinb[28]=Adcinb[27];
Adcinb[27]=Adcinb[26];
Adcinb[26]=Adcinb[25];
Adcinb[25]=Adcinb[24];
Adcinb[24]=Adcinb[23];
Adcinb[23]=Adcinb[22];
Adcinb[22]=Adcinb[21];
Adcinb[21]=Adcinb[20];
Adcinb[20]=Adcinb[19];
Adcinb[19]=Adcinb[18];
Adcinb[18]=Adcinb[17];
Adcinb[17]=Adcinb[16];
Adcinb[16]=Adcinb[15];
Adcinb[15]=Adcinb[14];
Adcinb[14]=Adcinb[13];
Adcinb[13]=Adcinb[12];
Adcinb[12]=Adcinb[11];
Adcinb[11]=Adcinb[10];
Adcinb[10]=Adcinb[9];
Adcinb[9]=Adcinb[8];
Adcinb[8]=Adcinb[7];
Adcinb[7]=Adcinb[6];
Adcinb[6]=Adcinb[5];
Adcinb[5]=Adcinb[4];
Adcinb[4]=Adcinb[3];
Adcinb[3]=Adcinb[2];
Adcinb[2]=Adcinb[1];
Adcinb[1]=Adcinb[0];

Adcinb[0]=ADC12MEM0;

avrv=0;

for(int i=0;i<=63;i++)
avrv = avrv + Adcinb;//adding for average

//average of binary values

//taking avrage of averaged binary
inv[7]=inv[6];
inv[6]=inv[5];
inv[5]=inv[4];
inv[4]=inv[3];
inv[3]=inv[2];
inv[2]=inv[1];
inv[1]=inv[0];
inv[0]=avrv/64;//average of binary values

inva = 0;
for(int i=0;i<=7;i++)
inva = inva + inv;

inva= inva/8; // average of averaged value

befz = 0.08217577087*inva-254.9316528; //converting binary value
to value of unit 1

//retriving value from flash
Flash_ptrA = (double *) 0x1080; //initialsie pointer
zero_ram = *Flash_ptrA; // copy from flash to ram
b = befz - zero_ram; //corrected value

// check unit key pressed
//tmp1 = P5IN;
tmp = P5IN | 0xf8;
if( tmp == 0xfe )
unit =1; // mm
else if ( tmp == 0xfd)
unit =2; // inch
else if ( tmp == 0xfb )
unit =0; // pascal
else if ( tmp == 0xf9 )
{
zero_ram = befz;

// Flash pointer

Flash_ptrA = (double *) 0x1080; // Initialize
Flash pointer
FCTL1 = FWKEY + ERASE; // Set Erase bit
FCTL3 = FWKEY; // Clear Lock bit
*Flash_ptrA = 0; // Dummy write to
erase Flash segment

FCTL1 = FWKEY + WRT; // Set WRT bit for
write operation

{ *Flash_ptrA = zero_ram;
// Write value to flash
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Set LOCK bit
}

wr_lcd_ir(0x01); //clear display
__delay_cycles( 2000);

/*if( b >= 50.0)//( d >= 490.3325)
{
display_string("OVERFLOW",8); //overflow seting
__delay_cycles( 20000);
goto checkinput;
}else if( b <= -25.0)//( d <= -245.16625)
{
display_string("OVERFLOW",8); //overflow seting
__delay_cycles( 20000);
goto checkinput;
}
*/
//display sign // check for sign


if(unit ==1) // display in mm
{
if(b<0)
{ wr_lcd_ir(0x80);//seting to line 1#2
wr_lcd_dr('-');
}
else
{ wr_lcd_ir(0x80);//seting to line 1#2
wr_lcd_dr('+');
}
wr_lcd_ir(0xc1);//seting to line 2#1
display_string("MM WC",5);
wr_lcd_ir(0x81);//seting to line 1#3
writeValue(b,unit);
}

else if( unit == 2 ) // display in inch
{
wr_lcd_ir(0xc1);//seting to line 2#1
display_string("ADCM ",5);
wr_lcd_ir(0x81);//setting to line 1#3
writeValue(inva,unit);
}

else if(unit == 0) // display in pascal
{
d=b*9.8;
if(d<0)
{ wr_lcd_ir(0x80);//seting to line 1#2
wr_lcd_dr('-');
}
else
{ wr_lcd_ir(0x80);//seting to line 1#2
wr_lcd_dr('+');
}
wr_lcd_ir(0xc3);//seting to line 2#3
display_string("Pa",2);
wr_lcd_ir(0x81);// srtting to line 1#3
writeValue(d,unit);
}

__delay_cycles( 60000);

goto checkinput;
}

void MSP430_Init_HW( void)
{
//
// [Watchdog Timer]
//
WDTCTL=0x5A00 | 0x80;
//
// [Port 1/2]
//
P1OUT=P1IN;
P1DIR=0xFF;
P2OUT=P2IN & ~0x01; //P2.0 is LCD_E
P2DIR=0xFF;
//
// [Port 3/4]
//
// P3DIR=0x00; //P3.x are LCD_DBx
P4OUT=P4IN & ~0x18; //P4.4 is LCD_R/W, P4.3 is LCD_RS
P4DIR=0xFF;
//
// [Port 5/6]
//
P5OUT=P5IN;
P5DIR=0xF8; //P5.2, P5.1, P5.0 are push buttons
}


// initialise ADC

void init_ADC()
{
P6SEL |= 0x01; // Enable A/D channel A0
ADC12CTL0 = !REFON; //off internal reference
ADC12CTL0 |= ADC12ON+SHT0_15+MSC; // Turn on ADC12,
set sampling time
ADC12CTL1 = SHP+CONSEQ_2+ADC12DIV_7; // Use
sampling timer, set mode
ADC12IE = 0x01; // Enable ADC12IFG.0
ADC12MCTL0 = SREF_0; // external reference
ADC12MCTL0 |= INCH_0; //SELECT A0

ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start conversion
//_BIS_SR( GIE); // Enter LPM0,Enable interrupts
}

//
// write lcd instruction register
//
void wr_lcd_ir(int inst)
{
P4OUT&=~0x18; //R/W=Wr, RS=Ir
P2OUT|=0x01; //set E
P3OUT=inst; //load DB
P3DIR=0xFF; //drive DB
P2OUT&=~0x01; //clear E
P3DIR=0x00; //float DB
}

//
// write lcd data register
//
void wr_lcd_dr(char data)
{
P4OUT&=~0x10; //R/W=Wr
P4OUT|=0x08; //RS=Dr
P2OUT|=0x01; //set E
P3OUT=data; //load DB
P3DIR=0xFF; //drive DB
P2OUT&=~0x01; //clear E
P3DIR=0x00; //float DB
P4OUT&=~0x18; //R/W=Wr, RS=Ir
}

//
// read lcd status register
//
int rd_lcd_sr(void)
{
P4OUT|=0x10; //R/W=Rd
P4OUT&=~0x08; //RS=Sr
P2OUT|=0x01; //set E
int status=P3IN; //read status
P2OUT&=~0x01; //clear E
P4OUT&=~0x18; //R/W=Wr, RS=Ir
return (status);
}

//
// initialise lcd
//
void init_lcd(void)
{
__delay_cycles( 20000); // ~20000us @ ~1MHz
wr_lcd_ir(0x30) ;
__delay_cycles( 5500); // ~5500us @ ~1MHz
wr_lcd_ir(0x30) ;
__delay_cycles( 150); // ~150us @ ~1MHz
wr_lcd_ir(0x30) ;
while ((rd_lcd_sr( )&0x80)!= 0) {};
wr_lcd_ir(0x3c) ; //8-bit, 2-line, 5x10
while ((rd_lcd_sr( )&0x80)!= 0) {};
wr_lcd_ir(0x01) ; //clear-display
while ((rd_lcd_sr( )&0x80)!= 0) {};
wr_lcd_ir(0x0C) ; //display-on, cursor-off, blink-off
while ((rd_lcd_sr( )&0x80)!= 0) {};
wr_lcd_ir(0x06) ; //increment, shift-off
}


//
// display string
//
void display_string( char * str, int cnt)
{
for (int i=0; i<cnt; i++)
wr_lcd_dr(str[ i]);
}




// write numeric value on lcd
void writeValue(double value,int unit)
{
char res[7];
unsigned int d1=0,d2=0;
int cntd=0, cntreal=0; //real;
// number to out put.
d1 = (unsigned int) fabs(value);
res[0]='\0';
//d1=fabs(d1);
if(d1==0)
wr_lcd_dr('0');
else
{
itoa(d1,res,&cntd);
//num2str ( d1, res, &cntd ); // convert interger
display_string(res, cntd); // display integer part
}
if( unit == 1|unit == 2) // 1- mm 2 inch 3 pascal
{
if( cntd <=7)
//wr_lcd_dr('0');
wr_lcd_dr('.');

// convert real and print

if( cntd <=2 && unit == 1)
{
d2 = (unsigned int) abs(( value - d1 ) * 1000000); // get
6 digits of real part
itoa( d2, res, &cntreal );
//num2str ( d2, res, &cntreal ); // convert real part (6
digits ) to string
wr_lcd_dr(res[0]);
/*if( cntd <=5)
wr_lcd_dr( res[1]);}
if( cntd <= 4)
wr_lcd_dr(res[2]);
if( cntd <= 3)
wr_lcd_dr(res[3]);
if( cntd <= 2)
wr_lcd_dr(res[4]);
if( cntd <= 1)
wr_lcd_dr(res[5]);*/
}
/*{
d2 = ( value - d1 ) * 100000; // get 6 digits of real part
itoa ( d2, res, &cntreal ); // convert real part (6 digits )
to string
wr_lcd_dr(res[0]);
if(unit == 2)
{
if( cntd <=5)
wr_lcd_dr( res[1]);
}
if(unit == 2){
if( cntd <=4)
wr_lcd_dr( res[2]);
}*/

}
///// end of printing values

}

/* itoa: convert n to characters in s */
void itoa(unsigned int n, char s[], int *cnt)
{
int i, sign;

if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s = '\0';
*cnt=i;
reverse(s);
}

/* reverse: reverse string s in place */
void reverse(char s[])
{
int c, i, j;

for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s;
s = s[j];
s[j] = c;
}
}
 

msp430 __delay_cycles

Hi

What is the meaning of this code?

All the best

Bobi
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top