当前位置:首页 > 实验4(串行通行实验报告)
}
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
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);
共分享92篇相关文档