初级会员
- 积分
- 90
- 金钱
- 90
- 注册时间
- 2013-1-27
- 在线时间
- 12 小时
|
发表于 2013-5-10 20:04:08
|
显示全部楼层
回复【8楼】艾科:
---------------------------------
哥,能否帮看看?
发送:
#include<msp430x14x.h>
#include"LCD5110.h"
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define delay_s(x) __delay_cycles((long)(CPU_F*(double)x/1.0))
void USART_Init(void)
{
WDTCTL=WDTPW+WDTHOLD;
 3SEL |= BIT4+BIT5; // 3.4,5 =用作USART0 TXD/RX
 3DIR|=BIT4;
ME1|=UTXE0+URXE0;
UCTL0|=CHAR;
UTCTL0|=SSEL0;
UBR00 = 0x03; //整数部分低位,读波特率=32768/3.41=9609.38 BRCLK/(URB+(M7+M6+ +M0)/8)
UBR10 = 0x00; //整数部分高位
UMCTL0 = 0x4A; //小数部分 M7,,M0=0100 1010 波特率调整 no modulation一共得波特率分频因子为3.41
UCTL0&=~SWRST;
IE1|=URXIE0+UTXIE0;
IFG1&=~UTXIFG0;
}
void send(int c)
{
int i;
char d[4];
d[0]=c/1000; //分离每位的数字
d[1]=(c-d[0]*1000)/100;
d[2]=(c-d[0]*1000-d[1]*100)/10;
d[3]=(c-d[0]*1000-d[1]*100-d[2]*10);
for(i=0;i<4;i++)
{
d+=0x30;
TXBUF0=d;
delay_us(50);
}
while (!UTXIFG0);
TXBUF0='\n';
}
void Clock_Init()
{
uchar i;
BCSCTL1&=~XT2OFF; //打开XT振荡器
BCSCTL2|=SELM1+SELS; //MCLK为8MHZ,SMCLK为8MHZ
do{
IFG1&=~OFIFG; //清除震荡标志
for(i=0;i<100;i++)
_NOP(); //延时等待
}
while((IFG1&OFIFG)!=0); //如果标志为1,则继续循环等待
IFG1&=~OFIFG;
}
void main()
{
WDTCTL=WDTPW+WDTHOLD;
int a=1234;
Clock_Init();
LCD_5110_Init();
USART_Init();
_EINT();
while(1)
{
//delay_ms(50);
LCD_5110_Clear();
LCD_5110_Set_XY(0,0);
LCD_5110_sendnum(a);
LCD_5110_Set_XY(0,1);
LCD_5110_sendstr("fasong:");
delay_ms(50);
send(a);
}
}
接收:#include <msp430x14x.h>
#include"LCD5110.h"
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define delay_s(x) __delay_cycles((long)(CPU_F*(double)x/1.0))
char a[4];
int num;
void USART_Init(void)
{
//WDTCTL=WDTPW+WDTHOLD;
 3SEL |= BIT4+BIT5; // 3.4,5 =用作USART0 TXD/RXD
ME1|=UTXE0+URXE0;
UCTL0|=CHAR;
UTCTL0|=SSEL0;
UBR00 = 0x03; //整数部分低位,读波特率=32768/3.41=9609.38 BRCLK/(URB+(M7+M6+ +M0)/8)
UBR10 = 0x00; //整数部分高位
UMCTL0 = 0x4A; //小数部分 M7,,M0=0100 1010 波特率调整 no modulation一共得波特率分频因子为3.41
UCTL0&=~SWRST;
IE1|=URXIE0;
IFG1&=~UTXIFG0;
}
void clk()
{
BCSCTL1 &= ~XT2OFF; // 打开XT2高频晶体振荡器
do
{
IFG1 &= ~OFIFG; //清除晶振失败标志
for (int i = 0xFF; i > 0; i--); // 等待8MHz晶体起振
}
while ((IFG1 & OFIFG)); // 晶振失效标志仍然存在?
BCSCTL2 |= SELM_2; //主时钟选择高频晶振
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
USART_Init();
clk();
LCD_5110_Init();
_EINT();
while(1)
{
delay_ms(50);
LCD_5110_Clear();
LCD_5110_Set_XY(0,0);
LCD_5110_sendstr("jieshou:");
}
}
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
a[num++]=RXBUF0;
LCD_5110_Set_XY(0,1);
LCD_5110_sendstr(a);
/* if(RXBUF0=='\n')
num=0;
if(num>3)
{
LCD_5110_Set_XY(0,1);
LCD_5110_sendchar(a[0]);
LCD_5110_Set_XY(6,1);
LCD_5110_sendchar(a[1]);
LCD_5110_Set_XY(12,1);
LCD_5110_sendchar(a[2]);
LCD_5110_Set_XY(18,1);
LCD_5110_sendchar(a[3]);
// LCD_5110_Set_XY(0,1);
//LCD_5110_sendstr(a);
num=0;
}*/
delay_ms(500);
} |
|