|
哪位大哥帮我看看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;
}
|
|