📄 chap3.c
字号:
// Chapter 3 6805 C programs// Jonathan W. Valvano// This software accompanies the book,// Embedded Microcomputer Systems: Real Time Interfacing// published by Brooks Cole, 1999// Program 3.1. A software function that outputs to a simple printer.void Output(unsigned char LETTER) { unsigned short cnt; PORT=LETTER; /* sets Port outputs */ Pulse(); /* pulses GO */ for(cnt=0,cnt<10000,cnt++); /* Wait for 100 ms */} // Program 3.2. A software function that inputs from an A/D.unsigned char Input(void); { int dummy; Pulse(); /* pulses GO */ dummy=1000; /* Wait for 5us */ return(PORT); /* Read A/D result */} // Program 3.4. Assembly language routines to initialize and output to a printer.// MC68HC705J1Avoid Init(void){ DDRA=0xFF; // outputs DDRB=0x01; PORTB=1;} // GO=1void Out(unsigned char value){unsigned int n; PORTA=value; PORTB=0; // GO=0 PORTB=1; // GO=1 for(n=0;n<40000;n++);}// Program 3.9. C language routines to initialize and read from an A/D.// MC68HC705J1Avoid Init(void){ DDRA=0x00; // PortA DATA DDRB=0x01; // PB0 GO PORTB=0;} // GO=0unsigned char In(void){ int n; PORTB=1; // GO=1 PORTB=0; // GO=0 for(n=0;n<1;n++); return(PORTA);}// Program 3.11. C language routines to initialize and read from a keyboard.// MC68HC705J1Avoid Init(void){ // PA7=STROBE DDRA=0x00;} // PA6-0 DATAunsigned char In(void){ while(PORTA&0x80); // wait for PA7=0 while((PORTA&0x80)==0); // wait for PA7=1 return(PORTA&0x7F);}// Program 3.13. C language routines to initialize and read from an A/D.// MC68HC705J1Avoid Init(void){ // PB1=DONE in DDRB=0x01; // PB0=GO out PORTB=0; // GO=0 DDRA=0x00;} // PA=DATA inunsigned char In(void){ PORTB=1; // GO pulse PORTB=0; while((PORTB&0x02)==0); // wait for PB1=1 return(PORTA);}// Program 3.16. Handshaking C language routines to initialize and read from a sensor./ MC68HC705J1Avoid Init(void){ // PB1=READY in DDRB=0x01; // PB0=ACK out PORTB=0x01; // ACK=1 DDRA=0x00;} // PA=DATA inunsigned char In(void){ unsigned char data; while((PORTB&0x02)==0); PORTB=0; // ACK=0 data=PORTA; // read data PORTB=1; // ACK=1 while(PORTB&0x02); return(data);}// Program 3.18. Handshaking C language routines to initialize and write to a printer.// MC68HC705J1Avoid Init(void){ // PB1=READY in DDRB=0x01; // PB0=START out PORTB=0x01; // ACK=1 DDRA=0xFF;} // PA=DATA outvoid Out(unsigned char data){ PORTB=0; // START=0 PORTA=data; // write data PORTB=1; // START=1 while(PORTB&0x02); while((PORTB&0x02)==0);}// Program 3.20. C language initialization of the DS1620// MC68HC705J1Avoid Init(void){ // PB2=RST=0 DDRB=0x07; // PB1=CLK=1 PORTB=0x03;} // PB0=DQ=1// Program 3.22. C language helper functions for the DS1620// MC68HC705J1Avoid out8(char code){ int n; for(n=0;n<8;n++){ PORTB &= 0xFD; // PB1=CLK=0 if(code&0x01) PORTB |= 0x01; // PB0=DQ=1 else PORTB &= 0xFE; // PB0=DQ=0 PORTB |= 0x02; // PB1=CLK=1 code = code>>1;}}void start(void){ PORTB |= 0x04; // PB2=RST=1 out8(0xEE); PORTD &= 0xFB;} // PB2=RST=0void stop(void){ PORTB |= 0x04; // PB2=RST=1 out8(0x22); PORTB &= 0xFB;} // PB2=RST=0// Program 3.24. C language functions to set the configuration register on the DS1620// MC68HC705J1Avoid config(char data){ PORTB |= 0x04; // PB2=RST=1 out8(0x0C); out8(data); PORTD &= 0xFB;} // PB2=RST=0// Program 3.27. C language functions to set the threshold registers on the DS1620// MC68HC705J1Avoid out9(int code){ int n; for(n=0;n<9;n++){ PORTB &= 0xFD; // PB1=CLK=0 if(code&0x01) PORTB |= 0x01; // PB0=DQ=1 else PORTB &= 0xFE; // PB0=DQ=0 PORTB |= 0x02; // PB1=CLK=1 code = code>>1;}}void WriteTH(int data){ PORTB |= 0x04; // PB2=RST=1 out8(0x01); out9(data); PORTD &= 0xFB;} // PB2=RST=0void WriteTL(int data){ PORTB |= 0x04; // PB2=RST=1 out8(0x02); out9(data); PORTB &= 0xFB;} // PB2=RST=0// Program 3.29. C language functions to read the configuration register on the DS1620// MC68HC705J1Aunsigned char in8(void){ int n; unsigned char result; DDRB &= 0xFE; // PB0=DQ input for(n=0;n<8;n++){ PORTB &= 0xFD; // PB1=CLK=0 result = result>>1; if(PORTB&0x01) result |= 0x80; // PB0=DQ=1 PORTB |= 0x02;} // PB1=CLK=1 DDRB |= 0x01; // PB0=DQ=output return result;}unsigned char ReadConfig(void){unsigned char value; PORTB |= 0x04; // PB2=RST=1 out8(0xAC); value=in8(); PORTD &= 0xFB; // PB2=RST=0 return value;}// Program 3.31. C language 9-bit read helper function for the DS1620// MC68HC705J1Aunsigned int in9(void){ int n; unsigned int result=0; DDRB &= 0xFE; // PB0=DQ input for(n=0;n<9;n++){ PORTB &= 0xFD; // PB1=CLK=0 result = result>>1; if(PORTB&0x01) result |= 0x0100; // PB0=DQ=1 PORTB |= 0x02;} // PB1=CLK=1 DDRB |= 0x01; // PB0=DQ=output return result;}// Program 3.32. C language functions to read the temperatures from the DS1620// MC68HC705J1Aunsigned int ReadTH(void){unsigned int value; PORTB |= 0x04; // PB2=RST=1 out8(0xA1); value=in9(); PORTD &= 0xFB; // PB2=RST=0 return value;}unsigned int ReadTL(void){unsigned int value; PORTB |= 0x04; // PB2=RST=1 out8(0xA2); value=in9(); PORTD &= 0xFB; // PB2=RST=0 return value;}unsigned int ReadT(void){unsigned int value; PORTB |= 0x04; // PB2=RST=1 out8(0xAA); value=in9(); PORTD &= 0xFB; // PB2=RST=0 return value;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -