2274与2013spi通信

fuhognquan   2009-9-2 20:42 楼主
哪位大哥帮我看看2274做主控,2013做ad用
问题是2274有时能接受到2013数据但有时接收到错误的数据
#include "msp430X22X4.h"
unsigned char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int data =4;
unsigned int send_data=0x67;
void Init_Port(void) //初始化端口,设置相关端口为普通I/O口
{P2SEL&=~(BIT0+BIT1+BIT2);
P2DIR=BIT0+BIT1+BIT2;
}
/************产生时钟***************/
void SCLK_H(void)//输出高电平
{P2OUT=BIT1;
}

void SCLK_L(void)//输出高电平
{P2OUT&=~BIT1;
}

void STCLK_H(void)//输出高电平
{P2OUT=BIT2;
}

void STCLK_L(void)//输出高电平
{P2OUT&=~BIT2;
}

/************初始化系统时钟***************/
void Init_clk(void)
{ //unsigned int n;
//WDTCTL=WDTPW+WDTHOLD; //关看门狗
//BCSCTL1 =CALBC1_1MHZ; //设定DCO为1MHZ
//DCOCTL =CALBC1_1MHZ;

WDTCTL=WDTPW+WDTHOLD; //关看门狗
BCSCTL1 =CALBC1_1MHZ; //设定DCO为1MHZ
DCOCTL =CALDCO_1MHZ;

volatile unsigned int n;
do //等待晶振稳定
{
IFG1 &=~OFIFG;
for(n=5000;n>0;n--);
} while(OFIFG&IFG1 != 0);

/*
do{ //等待晶振稳定
IFG1 &=~OFIFG;
for(n=5000;n>0;n--);
}while(OFIFG&IFG1);
*/
}
/************数据输入程序***************/
void data_in(unsigned char value)
{int i,j;
for(i=0;i<8;i++)
{if((value&0x01)==1)
P2OUT=BIT0;
else
P2OUT&=~BIT0;
SCLK_L();
for(j=0;j<10;j++);
SCLK_H();
for(j=0;j<10;j++);
value>>=1;
}
}

/************显示程序***************/
void xianshi(float x)
{unsigned char i,wei[4];
wei[3]=x/1000;
wei[2]=(x-wei[3]*1000)/100;
wei[1]=(x-wei[3]*1000-wei[2]*100)/10;
wei[0]=(x-wei[3]*1000-wei[2]*100-wei[1]*10)/1;
for(i=0;i<4;i++)
{ int j;
j=0x08;
j>>=i;
STCLK_L();
data_in(seg[wei[i]]);
data_in(j);
STCLK_H();

}

}
/************主程序***************/
void main(void)
{ Init_Port();
Init_clk();
P3SEL |=BIT1+BIT2+BIT3;
P3DIR |=BIT1;
UCB0CTL1 |=UCSSEL1+UCSWRST; //时钟源选择32768晶振
UCB0CTL0=0;
UCB0CTL0 |=UCMST+UCSYNC+UCMSB+UCCKPH+UCCKPL; //spi主机模式,spi通讯,时钟极性选为高电平无效,选择最高有效位先发
UCB0BR1=0; //32768/1200=27.3
UCB0BR0=100;
//UCB0MCTL=0;
UCB0CTL1 &=~UCSWRST; //进入SPI工作模式
IE2 |= UCB0RXIE; //使能spi接收中断
_BIS_SR(GIE);
int i,j;
//{for(j=0;j<100;j++)
//xianshi(66);
// }
for(i=0;i<1000;i++);
while(!(UCB0TXIFG&IFG2));
UCB0TXBUF=send_data;
while(1)
xianshi(data);
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void rx_isr(void)
{
data=UCB0RXBUF;
//UCA0TXBUF=rec_data;
int x;
//for(x=0;x<150;x++)
//xianshi(76);
//for(x=0;x<200;x++)
//xianshi(data);
}




#include "msp430x20x3.h"
int send_data=90;
int recdata;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗USICTL0=0;
USICTL0 |=USIPE7+USIPE6+USIPE5+USIOE;

//USICKCTL|=USICKPL;
USICTL1 |= USIIE+USICKPH ;//// Counter interrupt ena××e, flag remains set
USICTL0 &= ~USISWRST;
_EINT(); //使能总中断
//USISRL = 1; // init-load data
USICNT = 8; // init-load counter
while(1);
}
#pragma vector =USI_VECTOR
__interrupt void usi(void)
{int i;
//USICTL1 &= ~USIIE;
recdata=USISRL;
USICNT = 8;
while (!(USIIFG & USICTL1));
USISRL=send_data; //查询发送标志位
//USICNT = 8;
// USICTL1 |= USIIE;
}

回复评论 (6)

斑竹大虾帮帮忙,都愁死了

请教各位大虾
点赞  2009-9-2 20:43
你把程序修改成只有通讯部分。
仔细查看你的时序,应该出在这个问题上面。
点赞  2009-9-2 21:36

谢谢

#include "msp430X22X4.h"
unsigned int data =4;
unsigned int send_data=0x67;


/************初始化系统时钟***************/
void Init_clk(void)
{ //unsigned int n;
//WDTCTL=WDTPW+WDTHOLD; //关看门狗
//BCSCTL1 =CALBC1_1MHZ; //设定DCO为1MHZ
//DCOCTL =CALBC1_1MHZ;

WDTCTL=WDTPW+WDTHOLD; //关看门狗
BCSCTL1 =CALBC1_1MHZ; //设定DCO为1MHZ
DCOCTL =CALDCO_1MHZ;

volatile unsigned int n;
do //等待晶振稳定
{
IFG1 &=~OFIFG;
for(n=5000;n>0;n--);
} while(OFIFG&IFG1 != 0);

/*
do{ //等待晶振稳定
IFG1 &=~OFIFG;
for(n=5000;n>0;n--);
}while(OFIFG&IFG1);
*/
}
/************数据输入程序***************/

/************主程序***************/
void main(void)
{
Init_clk();
P3SEL |=BIT1+BIT2+BIT3;
P3DIR |=BIT1;
UCB0CTL1 |=UCSSEL1+UCSWRST; //时钟源选择32768晶振
UCB0CTL0=0;
UCB0CTL0 |=UCMST+UCSYNC+UCMSB+UCCKPH+UCCKPL; //spi主机模式,spi通讯,时钟极性选为高电平无效,选择最高有效位先发
UCB0BR1=0; //32768/1200=27.3
UCB0BR0=100;
//UCB0MCTL=0;
UCB0CTL1 &=~UCSWRST; //进入SPI工作模式
IE2 |= UCB0RXIE; //使能spi接收中断
_BIS_SR(GIE);

for(i=0;i<1000;i++);
while(!(UCB0TXIFG&IFG2));
UCB0TXBUF=send_data;

#pragma vector=USCIAB0RX_VECTOR
__interrupt void rx_isr(void)
{
data=UCB0RXBUF;
}




#include "msp430x20x3.h"
int send_data=90;
int recdata;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗USICTL0=0;
USICTL0 |=USIPE7+USIPE6+USIPE5+USIOE;

//USICKCTL|=USICKPL;
USICTL1 |= USIIE+USICKPH ;//// Counter interrupt ena××e, flag remains set
USICTL0 &= ~USISWRST;
_EINT(); //使能总中断
//USISRL = 1; // init-load data
USICNT = 8; // init-load counter
while(1);
}
#pragma vector =USI_VECTOR
__interrupt void usi(void)
{int i;
//USICTL1 &= ~USIIE;
recdata=USISRL;
USICNT = 8;
while (!(USIIFG & USICTL1));
USISRL=send_data; //查询发送标志位
//USICNT = 8;
// USICTL1 |= USIIE;
}
点赞  2009-9-2 21:58
我的2274没接外部晶振,2274的spi用的是b
点赞  2009-9-2 22:00

调试成功后的程序

#include "msp430X22X4.h"
unsigned char seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int data =4;
unsigned int send_data=0x67;
void Init_Port(void) //初始化端口,设置相关端口为普通I/O口
{P2SEL&=~(BIT0+BIT1+BIT2);
P2DIR=BIT0+BIT1+BIT2;
}
/************产生时钟***************/
void SCLK_H(void)//输出高电平
{P2OUT=BIT1;
}

void SCLK_L(void)//输出高电平
{P2OUT&=~BIT1;
}

void STCLK_H(void)//输出高电平
{P2OUT=BIT2;
}

void STCLK_L(void)//输出高电平
{P2OUT&=~BIT2;
}

/************初始化系统时钟***************/
void Init_clk(void)
{ //unsigned int n;
//WDTCTL=WDTPW+WDTHOLD; //关看门狗
//BCSCTL1 =CALBC1_1MHZ; //设定DCO为1MHZ
//DCOCTL =CALBC1_1MHZ;

WDTCTL=WDTPW+WDTHOLD; //关看门狗
BCSCTL1 =CALBC1_1MHZ; //设定DCO为1MHZ
DCOCTL =CALDCO_1MHZ;

volatile unsigned int n;
do //等待晶振稳定
{
IFG1 &=~OFIFG;
for(n=5000;n>0;n--);
} while(OFIFG&IFG1 != 0);

/*
do{ //等待晶振稳定
IFG1 &=~OFIFG;
for(n=5000;n>0;n--);
}while(OFIFG&IFG1);
*/
}
/************数据输入程序***************/
void data_in(unsigned char value)
{int i,j;
for(i=0;i<8;i++)
{if((value&0x01)==1)
P2OUT=BIT0;
else
P2OUT&=~BIT0;
SCLK_L();
for(j=0;j<10;j++);
SCLK_H();
for(j=0;j<10;j++);
value>>=1;
}
}

/************显示程序***************/
void xianshi(float x)
{unsigned char i,wei[4];
wei[3]=x/1000;
wei[2]=(x-wei[3]*1000)/100;
wei[1]=(x-wei[3]*1000-wei[2]*100)/10;
wei[0]=(x-wei[3]*1000-wei[2]*100-wei[1]*10)/1;
for(i=0;i<4;i++)
{ int j;
j=0x08;
j>>=i;
STCLK_L();
data_in(seg[wei[i]]);
data_in(j);
STCLK_H();

}

}
/************主程序***************/
void main(void)
{ Init_Port();
Init_clk();
P3SEL |=BIT1+BIT2+BIT3;
P3DIR |=BIT1;
UCB0CTL1 |=UCSSEL_2+UCSWRST;

UCB0CTL0 |=UCMST+UCSYNC+UCMSB;
UCB0BR1=0;
UCB0BR0=100;
//UCB0MCTL=0;
UCB0CTL1 &=~UCSWRST; //进入SPI工作模式
IE2 |= UCB0RXIE; //使能spi接收中断
_BIS_SR(GIE);
int i,j;
//{for(j=0;j<100;j++)
//xianshi(66);
// }
for(i=0;i<1000;i++);
while(!(UCB0TXIFG&IFG2));
UCB0TXBUF=send_data;
while(1)
xianshi(data);
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void rx_isr(void)
{
data=UCB0RXBUF;
//UCA0TXBUF=rec_data;
int x;
//for(x=0;x<150;x++)
//xianshi(76);
//for(x=0;x<200;x++)
//xianshi(data);

}







#include "msp430x20x3.h"
int send_data=99;
int recdata;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗USICTL0=0;
USICTL0 |=USIPE7+USIPE6+USIPE5+USIOE+USIGE;

//USICKCTL|=USICKPL;
USICTL1 |= USIIE;//// Counter interrupt ena××e, flag remains set
USICTL0 &= ~USISWRST;
_EINT(); //使能总中断
//USISRL = 1; // init-load data
//USICNT = 1; // init-load counter
while(1);
}
#pragma vector =USI_VECTOR
__interrupt void usi(void)
{int i;
//USICTL1 &= ~USIIE;
recdata=USISRL;
USICNT = 8;
while (!(USIIFG & USICTL1));
USISRL=send_data; //查询发送标志位
//USICNT = 8;
// USICTL1 |= USIIE;
}
/*#pragma vector = USI_VECTOR
__interrupt void universal_serial_interface(void)
{
threshold = USISRL; // 把接收到的数据赋给threshold
USICNT = 1;
}*/
点赞  2009-9-3 10:28
学习了,谢谢楼主分享!
点赞  2009-9-3 13:08
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复