基于msp430的数据传输程序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_shuzi1(unsigned char add,char date)
{
char shi,ge;
shi=date/10;
write_com(0x38);//1602液晶初始化
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)//写入液晶固定部分显示
{
write_date(table[num]);
{
unsigned char i;
for(i=0;i<8;i++)
{
FLAG=2;
while((!(P1IN & BIT7))&&FLAG++); //1bit是否结束
delay_10us();
temp=0;// 26us~28us表示为0
if((P1IN & BIT7))
temp=1; //超过28us依然为高电平表示为1
T_data_H=T_data_H_temp;
T_data_L=T_data_L_temp;
checkdata=checkdata_temp;
}
}
}
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;//关看门狗
int_clk(); //初始化时钟
init_1602();
P1DIR |= BIT2;
TACCR0 = 26; //pwm频率为10k.8000k/800=10k
TACCR1 = 13; //占空比1:1
TACCTL1 = OUTMOD0+OUTMOD1+OUTMOD2;//输出模式选择
TACTL |= TASSEL1+MC0;
}
void COM(void) //数据0 1确认
P6OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = com; //P0=com
delay(15);
P6OUT |= BIT1;//lcden=1
delay(15);
P6OUT &= ~BIT1; //lcden=0
}
void write_date(unsigned char date)
FLAG=2;
while((P1IN & BIT7)&&FLAG++);//判断从机是否发出80us的高电平,如发出则进入数据接收状态
COM();//数据接收状态
RH_data_H_temp=comdata; //湿度整数8位
COM();
RH_data_L_temp=comdata; //湿度小数8位
{//写液晶命令函数
P3DIR = 0x03;
P3OUT &= ~BIT0; //rs=0
P3OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = com; //P0=com
delay(10);
P3OUT |= BIT1;//lcden=1
delay(10);
P3OUT &= ~BIT1; //lcden=0
{//写液晶数据函数
P6DIR = 0x03;
P6OUT |= BIT0; //rs=1
P6OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = date; //P0=date
delay(15);
P6OUT |= BIT1;//lcden=1
delay(15);
P6OUT &= ~BIT1;//lcden=0
int_usart();
init_DHT11();
int_pwm();
P4DIR &= ~BIT0;
while(1)
{
RH();
delay(1500);
write_shuzi(2,RH_data_H);
delay(50);
write_shuzi(11,T_data_H);
delay(50);
if((P4IN&BIT0)==0)
unsigned char T_data_H,T_data_L,RH_data_H,RH_data_L,checkdata;
unsigned char T_data_H_temp,T_data_L_temp,RH_data_H_temp,RH_data_L_temp,checkdata_temp;
unsigned char comdata;
FLAG=2;
while((P1IN & BIT7)&&FLAG++);
if(FLAG==1)
break;//FLAG溢出超时则跳出for循环
//判断数据位是0还是1
//如果高电平高过预定0高电平值则数据位为1
comdata<<=1;
comdata|=temp;
}
}
void init_DHT11()
{a=0;}
if((P4IN&BIT0)==1)
{a=1;}
write_shuzi1(6,a);
RH_DATA = ((RH_data_H<<2)|0);//湿度
TH_DATA = ((T_data_H<<2)|1);//温度
ADD = ((a<<2)|2);//地址
sent_byte( RH_DATA );
do
{
IFG1 &= ~OFIFG; //清除震荡错误标志
for(i = 0;i < 100;i++)
_NOP(); //延时
}
while((IFG1 & OFIFG) != 0);//如果标志为1,则继续循环等待
IFG1 &= ~OFIFG;
}
void int_usart()
{
U0CTL |= SWRST;//复位串口
#include <math.h>
unsigned int m1,m2,data,data1,data2,data3,add;
unsigned char table[]={"H: % T: c "};
unsigned char table1[]={"Light: Add: "};
/***********dht11***************/
temp=(T_data_H_temp+T_data_L_temp+RH_data_H_temp+RH_data_L_temp);
if(temp==checkdata_temp)//数据校验正确执行
{
RH_data_H=RH_data_H_temp;
RH_data_L=RH_data_L_temp;
/*************************/
//unsigned char a,time;
unsigned char RH_DATA,TH_DATA,ADD,a,data;
unsigned char table[]={"H: % T: c "};
unsigned char table1[]={"Light: add:01 "};
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void int_clk()
{
unsigned char i;
BCSCTL1 &= ~XT2OFF; //打开xt振荡器
BCSCTL2 |= SELM1+SELS; //MCLK为8MHZ,SMCLK为1MHZ
}
void init_1602()
{//初始化函数
delay(1000);
sent_byte( TH_DATA );
delay(1000);
sent_byte( ADD );
delay(1000);
//data = U0RXBUF;
//write_shuzi1(8,data);
}
}
接收:
#include <msp430x14x.h>
#include <stdio.h>
void delay(int xms)//i=xms即延时约xms毫秒
{
int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void delay_10us()
{
unsigned char i;
for(i=0;i<40;i++);
}
void write_com(unsigned char com)
}
void write_date(unsigned char date)
{//写液晶数据函数
P3DIR = 0x03;
P3OUT |= BIT0; //rs=1
P3OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = date; //P0=date
delay(10);
P3SEL |= BIT5;//使用扩展功能
P3DIR |= BIT4;
}
sent_byte(unsigned char data)
{
while((IFG1 & UTXIFG0)==0);//判断发送缓存区是否结束
U0TXBUF = data;
}
void int_pwm()
{
P1SEL |= BIT2;//P1.2作为pwm输出
delay_10us();
P1DIR &= ~BIT7;//主机设为输入判断从机响应信号
if(!(P1IN & BIT7))//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行
{
FLAG=2;
while((!(P1IN & BIT7))&&FLAG++);//判断从机是否发出80us的低电平响应信号是否结束
U0CTL |= CHAR;//8位数据
U0TCTL |= SSEL1; //时钟设置
U0BR0 = 0x00;
U0BR1 = 0x0d;
UMCTL0 = 0x00;//波特率2400
ME1 |= UTXE0+URXE0;//使能接收和发送
U0CTL &= ~SWRST;//清除串口复位信号
P3SEL |= BIT4;
delay(10);
}
write_com(0x80+0x40);
for(num=0;num<15;num++)
{
write_date(table1[num]);
delay(10);
}
}
void write_shuzi(unsigned char add,char date)
{
char shi,ge;
/*************************/
void delay(int xms)//i=xms即延时约xms毫秒
{
int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void delay1ms()
{
unsigned int k;
for(k=0;k<1333;k++)
P3OUT |= BIT1;//lcden=1
delay(10);
P3OUT &= ~BIT1;//lcden=0
}
void init_1602()
{//初始化函数
unsigned char num;
P3DIR = 0x03;
P3OUT &= ~BIT1; //lcden=0
P3OUT &= ~BIT0; //rs=0
COM();
T_data_H_temp=comdata; //温度整数8位
COM();
T_data_L_temp=comdata; //温度小数8位
COM();
checkdata_temp=comdata; //校验位
P1DIR |= BIT7;
P1OUT |= BIT7;
P1DIR &= ~BIT7;
{
P1DIR |= BIT7;
P1OUT |= BIT7;
delay(400);
}
void RH(void) //-----湿温度读取子程序------------
{
P1DIR |= BIT7;
P1OUT &= ~BIT7;
delay(300பைடு நூலகம்; //主机拉低高于18ms
P1OUT |= BIT7;//总线由上拉电阻拉高主机延时20us
基于msp430的数据传输程序
发送
#include <msp430x14x.h>
#include <stdio.h>
#include <math.h>
/***********dht11***************/
unsigned char FLAG;
unsigned char temp,TEMP;
_NOP();
}
void delayms(unsigned int z)
{
unsigned int h;
for(h=z;h>0;h--)
delay1ms();
}
void write_com(unsigned char com)
{//写液晶命令函数
P6DIR = 0x03;
P6OUT &= ~BIT0; //rs=0
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_shuzi1(unsigned char add,char date)
{
char shi,ge;
shi=date/10;
write_com(0x38);//1602液晶初始化
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)//写入液晶固定部分显示
{
write_date(table[num]);
{
unsigned char i;
for(i=0;i<8;i++)
{
FLAG=2;
while((!(P1IN & BIT7))&&FLAG++); //1bit是否结束
delay_10us();
temp=0;// 26us~28us表示为0
if((P1IN & BIT7))
temp=1; //超过28us依然为高电平表示为1
T_data_H=T_data_H_temp;
T_data_L=T_data_L_temp;
checkdata=checkdata_temp;
}
}
}
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;//关看门狗
int_clk(); //初始化时钟
init_1602();
P1DIR |= BIT2;
TACCR0 = 26; //pwm频率为10k.8000k/800=10k
TACCR1 = 13; //占空比1:1
TACCTL1 = OUTMOD0+OUTMOD1+OUTMOD2;//输出模式选择
TACTL |= TASSEL1+MC0;
}
void COM(void) //数据0 1确认
P6OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = com; //P0=com
delay(15);
P6OUT |= BIT1;//lcden=1
delay(15);
P6OUT &= ~BIT1; //lcden=0
}
void write_date(unsigned char date)
FLAG=2;
while((P1IN & BIT7)&&FLAG++);//判断从机是否发出80us的高电平,如发出则进入数据接收状态
COM();//数据接收状态
RH_data_H_temp=comdata; //湿度整数8位
COM();
RH_data_L_temp=comdata; //湿度小数8位
{//写液晶命令函数
P3DIR = 0x03;
P3OUT &= ~BIT0; //rs=0
P3OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = com; //P0=com
delay(10);
P3OUT |= BIT1;//lcden=1
delay(10);
P3OUT &= ~BIT1; //lcden=0
{//写液晶数据函数
P6DIR = 0x03;
P6OUT |= BIT0; //rs=1
P6OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = date; //P0=date
delay(15);
P6OUT |= BIT1;//lcden=1
delay(15);
P6OUT &= ~BIT1;//lcden=0
int_usart();
init_DHT11();
int_pwm();
P4DIR &= ~BIT0;
while(1)
{
RH();
delay(1500);
write_shuzi(2,RH_data_H);
delay(50);
write_shuzi(11,T_data_H);
delay(50);
if((P4IN&BIT0)==0)
unsigned char T_data_H,T_data_L,RH_data_H,RH_data_L,checkdata;
unsigned char T_data_H_temp,T_data_L_temp,RH_data_H_temp,RH_data_L_temp,checkdata_temp;
unsigned char comdata;
FLAG=2;
while((P1IN & BIT7)&&FLAG++);
if(FLAG==1)
break;//FLAG溢出超时则跳出for循环
//判断数据位是0还是1
//如果高电平高过预定0高电平值则数据位为1
comdata<<=1;
comdata|=temp;
}
}
void init_DHT11()
{a=0;}
if((P4IN&BIT0)==1)
{a=1;}
write_shuzi1(6,a);
RH_DATA = ((RH_data_H<<2)|0);//湿度
TH_DATA = ((T_data_H<<2)|1);//温度
ADD = ((a<<2)|2);//地址
sent_byte( RH_DATA );
do
{
IFG1 &= ~OFIFG; //清除震荡错误标志
for(i = 0;i < 100;i++)
_NOP(); //延时
}
while((IFG1 & OFIFG) != 0);//如果标志为1,则继续循环等待
IFG1 &= ~OFIFG;
}
void int_usart()
{
U0CTL |= SWRST;//复位串口
#include <math.h>
unsigned int m1,m2,data,data1,data2,data3,add;
unsigned char table[]={"H: % T: c "};
unsigned char table1[]={"Light: Add: "};
/***********dht11***************/
temp=(T_data_H_temp+T_data_L_temp+RH_data_H_temp+RH_data_L_temp);
if(temp==checkdata_temp)//数据校验正确执行
{
RH_data_H=RH_data_H_temp;
RH_data_L=RH_data_L_temp;
/*************************/
//unsigned char a,time;
unsigned char RH_DATA,TH_DATA,ADD,a,data;
unsigned char table[]={"H: % T: c "};
unsigned char table1[]={"Light: add:01 "};
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void int_clk()
{
unsigned char i;
BCSCTL1 &= ~XT2OFF; //打开xt振荡器
BCSCTL2 |= SELM1+SELS; //MCLK为8MHZ,SMCLK为1MHZ
}
void init_1602()
{//初始化函数
delay(1000);
sent_byte( TH_DATA );
delay(1000);
sent_byte( ADD );
delay(1000);
//data = U0RXBUF;
//write_shuzi1(8,data);
}
}
接收:
#include <msp430x14x.h>
#include <stdio.h>
void delay(int xms)//i=xms即延时约xms毫秒
{
int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void delay_10us()
{
unsigned char i;
for(i=0;i<40;i++);
}
void write_com(unsigned char com)
}
void write_date(unsigned char date)
{//写液晶数据函数
P3DIR = 0x03;
P3OUT |= BIT0; //rs=1
P3OUT &= ~BIT1; //lcden=0
P2DIR = 0xff;
P2OUT = date; //P0=date
delay(10);
P3SEL |= BIT5;//使用扩展功能
P3DIR |= BIT4;
}
sent_byte(unsigned char data)
{
while((IFG1 & UTXIFG0)==0);//判断发送缓存区是否结束
U0TXBUF = data;
}
void int_pwm()
{
P1SEL |= BIT2;//P1.2作为pwm输出
delay_10us();
P1DIR &= ~BIT7;//主机设为输入判断从机响应信号
if(!(P1IN & BIT7))//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行
{
FLAG=2;
while((!(P1IN & BIT7))&&FLAG++);//判断从机是否发出80us的低电平响应信号是否结束
U0CTL |= CHAR;//8位数据
U0TCTL |= SSEL1; //时钟设置
U0BR0 = 0x00;
U0BR1 = 0x0d;
UMCTL0 = 0x00;//波特率2400
ME1 |= UTXE0+URXE0;//使能接收和发送
U0CTL &= ~SWRST;//清除串口复位信号
P3SEL |= BIT4;
delay(10);
}
write_com(0x80+0x40);
for(num=0;num<15;num++)
{
write_date(table1[num]);
delay(10);
}
}
void write_shuzi(unsigned char add,char date)
{
char shi,ge;
/*************************/
void delay(int xms)//i=xms即延时约xms毫秒
{
int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void delay1ms()
{
unsigned int k;
for(k=0;k<1333;k++)
P3OUT |= BIT1;//lcden=1
delay(10);
P3OUT &= ~BIT1;//lcden=0
}
void init_1602()
{//初始化函数
unsigned char num;
P3DIR = 0x03;
P3OUT &= ~BIT1; //lcden=0
P3OUT &= ~BIT0; //rs=0
COM();
T_data_H_temp=comdata; //温度整数8位
COM();
T_data_L_temp=comdata; //温度小数8位
COM();
checkdata_temp=comdata; //校验位
P1DIR |= BIT7;
P1OUT |= BIT7;
P1DIR &= ~BIT7;
{
P1DIR |= BIT7;
P1OUT |= BIT7;
delay(400);
}
void RH(void) //-----湿温度读取子程序------------
{
P1DIR |= BIT7;
P1OUT &= ~BIT7;
delay(300பைடு நூலகம்; //主机拉低高于18ms
P1OUT |= BIT7;//总线由上拉电阻拉高主机延时20us
基于msp430的数据传输程序
发送
#include <msp430x14x.h>
#include <stdio.h>
#include <math.h>
/***********dht11***************/
unsigned char FLAG;
unsigned char temp,TEMP;
_NOP();
}
void delayms(unsigned int z)
{
unsigned int h;
for(h=z;h>0;h--)
delay1ms();
}
void write_com(unsigned char com)
{//写液晶命令函数
P6DIR = 0x03;
P6OUT &= ~BIT0; //rs=0