// Chapter 3 9S12C32 C programs // Jonathan W. Valvano, 2/07/07 // This software accompanies the book, // Embedded Microcomputer Systems: Real Time Interfacing, Second Edition // published by Thomson Engineering, 2006 void Output (unsigned char LETTER) { PORT = LETTER; /* sets Port outputs */ Pulse(); /* pulses GO */ Timer_MsWait(100); /* Wait for 100 ms */ } // Program 3.1 A software function that outputs to a simple printer. unsigned char Input(void); { Pulse(); /* pulses GO */ Timer_Wait(5); /* Wait for 5us */ return(PORT); /* Read ADC result */ } // Program 3.2 A software function that inputs from an ADC. // MC9S12C32 void Init(void){ DDRT = 0xFF; // outputs DDRM |= 0x01; PTM |= 1; // GO=1 Timer_Init();} // Program 2.6 void Out(unsigned char value){ PTT = value; PTM &= ~0x01; // GO=0 PTM |= 0x01; // GO=1 Timer_Wait(10000);} // 10ms //Program 3.4. C language routines to initialize and output to a printer. // MC9S12C32 void Init(void){ DDRT = 0x00; // input DATA DDRM |= 0x01; // PM0 GO PTM &= ~0x01; // GO=0 Timer_Init(); // Program 2.6 } unsigned char In(void){ PTM |= 0x01; // GO=1 PTM &=~0x01; // GO=0 Timer_Wait(10); // 10us return(PTT); } //Program 3.6. C language routines to initialize and read from an ADC. // MC9S12C32 void Init(void){ // PJ7=STROBE DDRJ = 0x00; // PT6-0 DATA DDRT = 0x80; // PT7 unused output PPSJ = 0x80; // rise on PJ7 PIFJ = 0x80;} // clear flag7 unsigned char In(void){ while((PIFJ&0x80)==0); // wait PIFJ = 0x80; // clear flag7 return(PTT); } //Program 3.8. C language routines to initialize and read from a keyboard. // MC9S12C32 void Init(void){ // PJ7=DONE in DDRJ = 0x40; // PJ6=GO out PPSJ = 0x80; // rise on PJ7 DDRT = 0x00; // PT7-0 DATA in PTJ &= ~0x40;} // GO=0 unsigned char In(void){ PIFJ = 0x80; // clear flag7 PTJ |= 0x40; // GO=1 PTJ &= ~0x40; // GO=0 while((PIFJ&0x80)==0); return(PTT); } //Program 3.10. C language routines to initialize and read from an ADC. // MC9S12C32 void Init(void){// PJ7=READY in DDRJ = 0x40; // PJ6=ACK out PPSJ = 0x80; // rise on PJ7 DDRT = 0x00; // PT7-0 DATA in PIFJ = 0x80; // clear flag7 PTJ |= 0x40; // ACK=1 } unsigned char In(void){ unsigned char data; while((PIFJ&0x80)==0); PTJ &= ~0x40; // ACK=0 data = PTT; // read data PIFJ = 0x80; // clear flag7 PTJ |= 0x40; // ACK=1 return(data); } //Program 3.13. Handshaking C language routines to initialize and read from a sensor. // MC9S12C32 void Init(void){// PJ7=READY in DDRJ = 0x40; // PJ6=START out PPSJ = 0x80; // rise on PJ7 DDRT = 0xFF; // PT7-0 DATA out PTJ |= 0x40; // START=1 } void Out(unsigned char data){ PIFJ = 0x80; // clear flag PTJ &= ~0x40; // START=0 PTT = data; // write data PTJ |= 0x40; // START=1 while((PIFJ&0x80)==0); } //Program 3.15. Handshaking C language routines to initialize and write to a printer. // 6812 void DS_Init(void){ // PT7=RST=0 DDRT = 0xE0; // PT6=CLK=1 PTT = 0x60; // PT5=DQ=1 } //Program 3.17. C language initialization of the DS1620 // 6812 void out8(char code){ int n; for(n=0;n<8;n++){ PTT &= 0xBF; // PT6=CLK=0 if(code&0x01) PTT |= 0x20; // PT5=DQ=1 else PTT &= 0xDF; // PT5=DQ=0 PTT |= 0x40; // PT6=CLK=1 code = code>>1;}} void DS_Start(void){ PTT |= 0x80; // PT7=RST=1 out8(0xEE); PTT &= 0x7F;} // PT7=RST=0 void DS_Stop(void){ PTT |= 0x80; // PT7=RST=1 out8(0x22); PTT &= 0x7F;} // PT7=RST=0 //Program 3.19. C language helper functions for the DS1620 // 6812 void DS_Config(char data){ PTT |= 0x80; // PT7=RST=1 out8(0x0C); out8(data); PTT &= 0x7F; // PT7=RST=0 } //Program 3.20. C language functions to set the configuration register on the DS1620 // 6812 void out9(short code){ short n; for(n=0;n<9;n++){ PTT &= 0xBF; // PT6=CLK=0 if(code&0x01) PTT |= 0x20; // PT5=DQ=1 else PTT &= 0xDF; // PT5=DQ=0 PTT |= 0x40; // PT6=CLK=1 code = code>>1; } } void DS_WriteTH(short data){ PTT |= 0x80; // PT7=RST=1 out8(0x01); out9(data); PTT &= 0x7F;} // PT7=RST=0 void DS_WriteTL(short data){ PTT |= 0x80; // PT7=RST=1 out8(0x02); out9(data); PTT &= 0x7F;} // PT7=RST=0 //Program 3.24. C language functions to set the threshold registers on the DS1620 // 6812 unsigned char in8(void){ short n; unsigned char result; DDRT &= 0xDF; // PT5=DQ input for(n=0;n<8;n++){ PTT &= 0xBF; // PT6=CLK=0 result = result>>1; if(PTT&0x20) result |= 0x80; // PT5=DQ=1 PTT |= 0x40;} // PT6=CLK=1 DDRT |= 0x20; // PT5=DQ output return result; } unsigned char DS_ReadConfig(void){ unsigned char value; PTT |= 0x80; // PT7=RST=1 out8(0xAC); value = in8(); PTT &= 0x7F; // PT7=RST=0 return value; } //Program 3.26. C language functions to read the configuration register on the DS1620 // 6812 unsigned short in9(void){ short n; unsigned short result=0; DDRT &= 0xDF; // PT5=DQ input for(n=0;n<9;n++){ PTT &= 0xBF; // PT6=CLK=0 result = result>>1; if(PTT&0x20) result |= 0x0100; // PT5=DQ=1 PTT |= 0x40;} // PT6=CLK=1 DDRT |= 0x20; // PT5=DQ output return result; } //Program 3.29. C language 9-bit read helper function for the DS1620 // 6812 unsigned short DS_ReadTH(void){ unsigned short value; PTT |= 0x80; // PT7=RST=1 out8(0xA1); value = in9(); PTT &= 0x7F; // PT7=RST=0 return value;} unsigned short DS_ReadTL(void){ unsigned short value; PTT |= 0x80; // PT7=RST=1 out8(0xA2); value = in9(); PTT &= 0x7F; // PT7=RST=0 return value;} unsigned short DS_ReadT(void){ unsigned short value; PTT |= 0x80; // PT7=RST=1 out8(0xAA); value = in9(); PTT &= 0x7F; // PT7=RST=0 return value;} //Program 3.30. C language functions to read the temperatures from the DS1620 // 6812 initialize SCI void SCI_Init(void){ SCIBD = 13; // 19200 bits/sec SCICR2 = 0x0C; // enable } #define RDRF 0x20 // Wait for new input, // then return ASCII code char SCI_InChar(void){ while((SCISR1&RDRF) == 0){}; return(SCIDRL); } #define TDRE 0x80 // Wait for buffer to be empty, // then output void SCI_OutChar(char data){ while((SCISR1&TDRE) == 0){}; SCIDRL = data; } //Program 3.32. C functions that implement serial I/O. // programs from first edition // 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. // MC68HC812A4 void Init(void){ DDRJ=0xFF; // outputs DDRH=0x01; PORTH=1;} // GO=1 void 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. // MC68HC812A4 void Init(void){ DDRJ=0x00; // PortJ DATA DDRH=0x01; // PH0 GO PORTH=0;} // GO=0 unsigned 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. // MC68HC812A4 void Init(void){ // PJ7=STROBE DDRJ=0x00; // PJ6-0 DATA KPOLJ=0x80; // rise on PJ7 KWIFJ=0x80;} // clear flag unsigned 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. // MC68HC812A4 void Init(void){ // PJ1=DONE in DDRJ=0x01; // PJ0=GO out KPOLJ=0x02; // rise on PJ1 DDRH=0x00; // PH DATA in PORTJ=0;} // GO=0 unsigned 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. // MC68HC812A4 void 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=1 unsigned 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. // MC68HC812A4 void Init(void){ // PJ1=READY in DDRJ=0x01; // PJ0=START out KPOLJ=0x02; // rise on PJ1 DDRH=0xFF; // PH DATA out PORTJ=0X01;} // START=1 void 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/MC68HC912B32 void 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/MC68HC912B32 void 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=0 void 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/MC68HC912B32 void 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/MC68HC912B32 void 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=0 void 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/MC68HC912B32 unsigned 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/MC68HC912B32 unsigned 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/MC68HC912B32 unsigned 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;}