📄 chap3.c
字号:
// Chapter 3 6812 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.// MC68HC812A4void Init(void){ DDRJ=0xFF; // outputs DDRH=0x01; PORTH=1;} // GO=1void Out(unsigned char value){unsigned int n; PORTJ=value; PORTH=0; // GO=0 PORTH=1; // GO=1 for(n=0;n<40000;n++);}// Program 3.7. 6811 or 6812 C language routine to create an accurate time delay.// 6811 or 6812, numCycles can range from 25 to 32767 void Wait(short numCycles){ short EndT; // TCNT at the end of the delay EndT=TCNT+numCycles; while(EndT-(short)TCNT>0);} // wait until TCNT passes EndT// Program 3.9. C language routines to initialize and read from an A/D.// MC68HC812A4void Init(void){ DDRJ=0x00; // PortJ DATA DDRH=0x01; // PH0 GO PORTH=0;} // GO=0unsigned char In(void){int n; PORTH=1; // GO=1 PORTH=0; // GO=0 for(n=0;n<8;n++); return(PORTJ);}// Program 3.11. C language routines to initialize and read from a keyboard.// MC68HC812A4void Init(void){ // PJ7=STROBE DDRJ=0x00; // PJ6-0 DATA KPOLJ=0x80; // rise on PJ7 KWIFJ=0x80;} // clear flagunsigned char In(void){ while((KWIFJ&0x80)==0); // wait KWIFJ=0x80; // clear flag return(PORTJ&0x7F);}// Program 3.13. C language routines to initialize and read from an A/D.// MC68HC812A4void Init(void){ // PJ1=DONE in DDRJ=0x01; // PJ0=GO out KPOLJ=0x02; // rise on PJ1 DDRH=0x00; // PH DATA in PORTJ=0;} // GO=0unsigned char In(void){ KWIFJ=0x02; // clear flag PORTJ=1; // GO pulse PORTJ=0; while((KWIFJ&0x02)==0); return(PORTH);}// Program 3.16. Handshaking C language routines to initialize and read from a sensor.// MC68HC812A4void Init(void){ // PJ1=READY in DDRJ=0x01; // PJ0=ACK out KPOLJ=0x02; // rise on PJ1 DDRH=0x00; // PH DATA in KWIFJ=0x02; // clear flag1 PORTJ=0X01;} // ACK=1unsigned char In(void){ unsigned char data; while((KWIFJ&0x02)==0); PORTJ=0; // ACK=0 data=PORTH; // read data KWIFJ=0x02; // clear flag PORTJ=0x01; // ACK=1 return(data);}// Program 3.18. Handshaking C language routines to initialize and write to a printer.// MC68HC812A4void Init(void){ // PJ1=READY in DDRJ=0x01; // PJ0=START out KPOLJ=0x02; // rise on PJ1 DDRH=0xFF; // PH DATA out PORTJ=0X01;} // START=1void Out(unsigned char data){ KWIFJ=0x02; // clear flag PORTJ=0; // START=0 PORTH=data; // write data PORTJ=0x01; // START=1 while((KWIFJ&0x02)==0);}// Program 3.20. C language initialization of the DS1620// MC68HC812A4/MC68HC912B32void Init(void){ // PS7=RST=0 DDRS=0xE0; // PS6=CLK=1 PORTS=0x60;} // PS5=DQ=1// Program 3.22. C language helper functions for the DS1620// MC68HC812A4/MC68HC912B32void out8(char code){ int n; for(n=0;n<8;n++){ PORTS &= 0xBF; // PS6=CLK=0 if(code&0x01) PORTS |= 0x20; // PS5=DQ=1 else PORTS &= 0xDF; // PS5=DQ=0 PORTS |= 0x40; // PS6=CLK=1 code = code>>1;}}void start(void){ PORTS |= 0x80; // PS7=RST=1 out8(0xEE); PORTS &= 0x7F;} // PS7=RST=0void stop(void){ PORTS |= 0x80; // PS7=RST=1 out8(0x22); PORTS &= 0x7F;} // PS7=RST=0// Program 3.24. C language functions to set the configuration register on the DS1620// MC68HC812A4/MC68HC912B32void config(char data){ PORTS |= 0x80; // PS7=RST=1 out8(0x0C); out8(data); PORTS &= 0x7F;} // PS7=RST=0// Program 3.27. C language functions to set the threshold registers on the DS1620// MC68HC812A4/MC68HC912B32void out9(int code){ int n; for(n=0;n<9;n++){ PORTS &= 0xBF; // PS6=CLK=0 if(code&0x01) PORTS |= 0x20; // PS5=DQ=1 else PORTS &= 0xDF; // PS5=DQ=0 PORTS |= 0x40; // PS6=CLK=1 code = code>>1;}}void WriteTH(int data){ PORTS |= 0x80; // PS7=RST=1 out8(0x01); out9(data); PORTS &= 0x7F;} // PS7=RST=0void WriteTL(int data){ PORTS |= 0x80; // PS7=RST=1 out8(0x02); out9(data); PORTS &= 0x7F;} // PS7=RST=0// Program 3.29. C language functions to read the configuration register on the DS1620// MC68HC812A4/MC68HC912B32unsigned char in8(void){ int n; unsigned char result; DDRS &= 0xDF; // PS5=DQ input for(n=0;n<8;n++){ PORTS &= 0xBF; // PS6=CLK=0 result = result>>1; if(PORTS&0x20) result |= 0x80; // PS5=DQ=1 PORTS |= 0x40;} // PS6=CLK=1 DDRS |= 0x20; // PS5=DQ output return result;}unsigned char ReadConfig(void){unsigned char value; PORTS |= 0x80; // PS7=RST=1 out8(0xAC); value=in8(); PORTS &= 0x7F; // PS7=RST=0 return value;}// Program 3.31. C language 9-bit read helper function for the DS1620// MC68HC812A4/MC68HC912B32unsigned int in9(void){ int n; unsigned int result=0; DDRS &= 0xDF; // PS5=DQ input for(n=0;n<9;n++){ PORTS &= 0xBF; // PS6=CLK=0 result = result>>1; if(PORTS&0x20) result |= 0x0100; // PS5=DQ=1 PORTS |= 0x40;} // PS6=CLK=1 DDRS |= 0x20; // PS5=DQ output return result;}// Program 3.32. C language functions to read the temperatures from the DS1620// MC68HC812A4/MC68HC912B32unsigned int ReadTH(void){unsigned int value; PORTS |= 0x80; // PS7=RST=1 out8(0xA1); value=in9(); PORTS &= 0x7F; // PS7=RST=0 return value;}unsigned int ReadTL(void){unsigned int value; PORTS |= 0x80; // PS7=RST=1 out8(0xA2); value=in9(); PORTS &= 0x7F; // PS7=RST=0 return value;}unsigned int ReadT(void){unsigned int value; PORTS |= 0x80; // PS7=RST=1 out8(0xAA); value=in9(); PORTS &= 0x7F; // PS7=RST=0 return value;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -