第一范文网 - 专业文章范例文档资料分享平台

实验4(串行通行实验报告)

来源:用户分享 时间:2025/6/2 12:12:54 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

}

delay(45); //delay 500 us presence=~DQ; } DQ=1; //set DQ }

/************write byte program for 18b20***********/ void write_byte(uchar val) {

uchar i;

for(i=8;i>0;i--) {

DQ=1;_nop_();_nop_(); DQ=0;_nop_();_nop_();_nop_();_nop_(); DQ=val&0x01; //shift lowest bit delay(6);

val=val/2; //shift a bit to right }

DQ=1; delay(1); }

/************read byte program for 18b20***********/ uchar read_byte(void) {

uchar i;

uchar value=0; for(i=8;i>0;i--) {

DQ=1;_nop_();_nop_(); value>>=1;

DQ=0;_nop_();_nop_();_nop_();_nop_(); DQ=1;_nop_();_nop_();_nop_();_nop_(); if(DQ)value|=0x80;

delay(6); }

DQ=1;

return(value); }

/************read temperature program**************/ read_temp() {

ow_reset(); //reset delay(200);

write_byte(0xcc); //send command

write_byte(0x44); //send converse command ow_reset(); delay(1);

write_byte(0xcc); //send command write_byte(0xbe);

temp_data[0]=read_byte(); //read the lower byte temp_data[1]=read_byte(); //read the higher byte temp=temp_data[1];

temp<<=8; temp=temp|temp_data[0]; //combine

return temp; //return temperature value }

/****************deal temperature data program**********/ work_temp(uint tem) {

uchar n=0;

if(tem>6348) //judge positive or negative {tem=65536-tem;n=1;} // bu ma,flag display[4]=tem&0x0f; // decimal display[0]=ditab[display[4]]; // save decimal display[4]=tem>>4; // get the int //display[3]=display[4]/100; // save hundred display[1]=display[4]0; // save last two display[2]=display[1]/10; // save ten display[1]=display[1]; // save sig // if(!display[3])

// display[3]=0x0a; //not display when highest 0 if(!display[2])

display[2]=0x0a; //not display when shighest 0 // }

// if(n){display[3]=0x0b;} //display \}

/****************display scan and sent program***************************/ scan() {

char k,temp;

for(k=3;k>-1;k--) //4 seg { while(SS1); SPISendByte(display[k]); //sent a byte to slave write_cyte(k,display[k]); //write into 24c04 Disdata=dis_7[read_random(k)]; //display f595(Disdata); if (k==1) {f595(Disdata-0x80);} //display point;

discan=scan_con[3-k]; //choose seg s595(discan); SBUF=pcplay[read_random(k)]; //read from 24c04 if (k==3) SBUF=' '; while(TI==0); TI=0; if (k==1){SBUF='.';while(TI==0);TI=0;} if (k==0){SBUF=0x0a;while(TI==0);TI=0;SS1=1;} } }

从机:

/*-Two mcu communicate by SPI program for slave- Get temperature from master by SPI and display it. Powered by naoshi on 4.3*/ #include #include

sbit SCK=P2^0; // P2.0 simulate clock out sbit MOSI=P2^2; // P2.1 simulate master out sbit MISO=P2^1; // P2.2 simulate master in sbit SS1=P2^3; // P2.3 simulate choice sbit DS1=P1^5; //Serial datain 1 sbit ST1=P1^4; //Output flip-latch impulse 1 sbit SH1=P1^3; //Shift clock impulse 1 sbit DS2=P1^2; //Serial datain 2 sbit ST2=P1^1; //Output flip-latch impulse 2 sbit SH2=P1^0; //Shift clock impulse 2 #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();}; #define uchar unsigned char #define uint unsigned int

unsigned data table[4]={0,0,0,0}; /*delay ms program*/ void delayms(uint ms) {

uchar t; while(ms--) {

for(t = 0; t < 120; t++); } }

/*Program for the 1st 74hc595*/ void f595(uchar cod1)

{ uchar j; uchar fdata=cod1; ST1=0; //Ready for the rise impulse for(j=0;j<8;j++) //Send data to 595 by this cycle { DS1=fdata&0x80; fdata<<=1; SH1=0; //One impulse one byte _nop_; _nop_; SH1=1; _nop_; _nop_; } ST1=1; //Impulse comes and send out data }

/*Program for the 2nd 74hc595*/ void s595(uchar cod2) { uchar i; uchar sdata=cod2; ST2=0; for(i=0;i<8;i++) { DS2=sdata&0x80; sdata<<=1; SH2=0; _nop_; _nop_; SH2=1; _nop_; _nop_; } ST2=1; }

/*LCD write command program*/ void write_com(uchar com) { s595(0x00); //RS=0; f595(com); //Transmit data by the 1st 595 delayms(5); s595(0x40); //EN=1; delayms(5);

搜索更多关于: 实验4(串行通行实验报告) 的文档
实验4(串行通行实验报告).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2wb2h12uj52nsft0jg45_5.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top