# SparkFun Forums

Where electronics enthusiasts find answers.

### The 430F5529 Programming Troubles

Discussions on how to get your MSP JTAG programmer up and running.

### The 430F5529 Programming Troubles#194831

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.

### Re: The 430F5529 Programming Troubles#194840

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

### Re: The 430F5529 Programming Troubles#194846

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".
{"ft_lang_label":"__label__en","ft_lang_prob":0.5506717,"math_prob":0.92322874,"size":2484,"snap":"2019-13-2019-22","text_gpt3_token_len":955,"char_repetition_ratio":0.1375,"word_repetition_ratio":0.49567723,"special_character_ratio":0.43921095,"punctuation_ratio":0.2416,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9530858,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-26T04:47:43Z\",\"WARC-Record-ID\":\"<urn:uuid:86914370-5cff-49d7-88ff-d4d776500844>\",\"Content-Length\":\"60949\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2657dd30-0ff8-43e6-b0c6-7e847e9f918d>\",\"WARC-Concurrent-To\":\"<urn:uuid:6c0c5a29-392c-4189-9625-8a0d32c2d0ed>\",\"WARC-IP-Address\":\"66.35.40.172\",\"WARC-Target-URI\":\"https://forum.sparkfun.com/viewtopic.php?f=9&p=194846&sid=5ede7eef77335218fa5f67c12b7b925a\",\"WARC-Payload-Digest\":\"sha1:PQB2T25WAXHKQFBJMWIVQVUD256HQ5PI\",\"WARC-Block-Digest\":\"sha1:AYOB3B6CCDZCHTJ3LBINERCLDTM222VO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912204790.78_warc_CC-MAIN-20190326034712-20190326060712-00285.warc.gz\"}"}