#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高
for( n=0;n<200;n++);
P3OUT |= 0x02; //SCLK高
P3OUT &= ~0x01; //SYNC低 片选使能
for( n=0;n<200;n++);
for(j=0;j<4;j++)
{
for(n=0;n<100;n++);
P3OUT |= 0x02; //SCLK高
for(n=0;n<100;n++);
if((jj>>(3-j)) & 0x01)
P3OUT |= 0x04;//数据高电平
else
P3OUT &=~0x04;//数据低电平
for(n=0;n<100;n++);
P3OUT &= ~0x02; //SCLK低
}
for(j=0;j<12;j++)
{
for(n=0;n<100;n++);
P3OUT |= 0x02; //SCLK高
for(n=0;n<100;n++);
if((data12>>(11-j)) & 0x01)
P3OUT |= 0x04;//数据高电平
else
P3OUT &=~0x04;//数据低电平
for(n=0;n<100;n++);
P3OUT &= ~0x02; //SCLK低
}
for(n=0;n<100;n++);
P3OUT |= 0x02; //SCLK高
for(n=0;n<100;n++);
P3OUT |= 0x01; //SYNC高
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; //设置p1.0为输出
P1REN|=BIT1;
P1OUT|=BIT1;
P3OUT |= 0x07;//P3.0 - P3.2模拟SPI接口
P3DIR |= 0x07;//P3.0->SYNC,P3.1->SCLK,P3.2->SDIN
//int code=0x28; //衰减20db,100倍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);
}
}
|