SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By ling1995
#194831
Hello,
I am making a programmable amplifier, the MCU I used is MSP430F5529 (This is its PDF: http://www.kynix.com/uploadfiles/pdf879 ... 29IPNR.pdf )and the software is ccs7.0 . Now it has realized to send a fixed digital quantity to dac7811 by using spi communication. Please look the following program:
Code: Select all
#include <msp430.h>
#include <msp430f5529.h>
/*
* main.c
*/
#define uint unsigned int
int codeArray[]={0x199,0xCC,0x88,0x66,0x51,0x51,0x44,0x3A,0x33,0x2D,0x28};
int i=0;
void spiwrite(int data12,int cmd)
{
    int n,j;
    int jj=cmd;
    P3OUT |= 0x01; //SYNC high
    for( n=0;n<200;n++);
    P3OUT |= 0x02; //SCLK high
    P3OUT &= ~0x01; //SYNC low CS enabled
    for( n=0;n<200;n++);
    for(j=0;j<4;j++)
    {
        for(n=0;n<100;n++);
        P3OUT |= 0x02; //SCLK high
        for(n=0;n<100;n++);
        if((jj>>(3-j)) & 0x01)
            P3OUT |= 0x04;//data high level
        else
            P3OUT &=~0x04;//data low level
        for(n=0;n<100;n++);
        P3OUT &= ~0x02; //SCLK low
    }
    for(j=0;j<12;j++)
    {
        for(n=0;n<100;n++);
        P3OUT |= 0x02; //SCLK high
        for(n=0;n<100;n++);
        if((data12>>(11-j)) & 0x01)
            P3OUT |= 0x04;//data high level
        else
            P3OUT &=~0x04;//data low level
        for(n=0;n<100;n++);
        P3OUT &= ~0x02; //SCLK low
    }
    for(n=0;n<100;n++);
    P3OUT |= 0x02; //SCLK high
    for(n=0;n<100;n++);
    P3OUT |= 0x01; //SYNC high
    for(n=0;n<200;n++);
    }

    void delay(uint z)
    {
        uint x,y;
        for(x=z;x>0;x--)
            for(y=110;y>0;y--);
    }
    void main(void)

    {
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR|=BIT0;   //set p1.0 to be output
    P1REN|=BIT1;
    P1OUT|=BIT1;


          P3OUT |= 0x07;//P3.0 - P3.2 analog SPI port
          P3DIR |= 0x07;//P3.0->SYNC,P3.1->SCLK,P3.2->SDIN
          //int code=0x28;      //deline 20db,100 times 0x28.
          //int code=0x800;
          int code=codeArray[i];
          int code1;
          spiwrite(code,1);
          while(1)
          {

              if(!(P1IN&0x02)){
                  delay(1);
                  if(!(P1IN&0x02)){
                      while(!(P1IN&0x02)){
                    P1OUT^=BIT0;
                   if(i<9)
                    i++;
                   code=codeArray[i];
                      }
                  }
              }
            code1 = code;
          spiwrite(code1,1);

         }

}
Now I want to ask a question:
Touching the key-pay p1.1 off,it can just change digital quantity one time and then it jumped to the ninth array directly. Why?
Thanks.
User avatar
By DanV
#194840
Clearly, the section below is the issue:
Code: Select all
              if(!(P1IN&0x02)){             // not P1IN&0x02
                  delay(1);
                  if(!(P1IN&0x02)){         // again not P1IN&0x02
                      while(!(P1IN&0x02)){  // while not P1IN&0x02
                    P1OUT^=BIT0;            // do something
                   if(i<9)                  // if i < 9
                    i++;                    // increment i
                   code=codeArray[i];
                      }
                  }
that all happens pretty quickly so you are done in an instant
By UhClem
#194846
I am not sure what the problem is but some comments:

Why are you bit banging SPI when that chip has multiple SPI hardware ports?

Your delay routine will get optimized to nothing. I don't know about your specific compiler but look for something like delay_cycles() for reliable spin delays. Or use a timer. Or declare the variables in delay() as volatile.

I have no idea what you are trying to do with P1IN. Well commented code would help but no one seems to remember the book "Elements of Programming Style".