📄 chap7.c
字号:
// Chapter 7 6812 C programs// Jonathan W. Valvano// This software accompanies the book,// Embedded Microcomputer Systems: Real Time Interfacing// published by Brooks Cole, 1999// Program 7.4. Three C functions to output a character using the SCI port.// 68HC812A4 SCI routines void init(void) { SC0BD=417; // 1200 baud SC0CR1=0x00; // 8data, 1stop SC0CR2=0x0C;} // enable gadfly#define RDRF 0x20#define TDRE 0x80#define TC 0x40unsigned char insci(void){ while ((SC0SR1 & RDRF) == 0); return(SC0DRL); }void OutChar(unsigned char letter){/* Wait for TDRE then output */ while ((SC0SR1 & TDRE) == 0); SC0DRL=letter; }void outsci2(unsigned char letter){/* Output then wait for TDRE y */ SC0DRL=letter; while ((SC0SR1 & TDRE) == 0); }void outsci3(unsigned char letter){/* Output then wait for TC */ SC0DRL=letter; while ((SC0SR1 & TC) == 0); }// Program 7.9. C language implementation of receiver interrupts.// MC68HC812A4 and MC68HC912B32#pragma interrupt_handler SCIHAN()#define RDRF 0x20// Executed on RDRF (not TDRE)void SCIhandler(void){ if((SC0SR1&0x20)!= RDRF) error(); if(SC0SR1&0x0F) error(); if(PutFifo(SC0DRL)) error(); }void RitualSCI(void){asm(" sei"); SC0BD=104; // 4800 baud SC0CR1=0x00; // 8data, 1stop SC0CR2=0x2C; // Receiver intrpt CLRQ(); // Clear FIFOasm(" cli");}// Program 7.14. C functions for serial output using DTR synchronization.// MC68HC812A4 or MC68HC912B32// PT3/IC3 is DTRvoid OutChar(unsigned char data) { while((PORTT&0x08)||((SC0SR1&0x80)==0)); SC0DRL= data; }// Program 7.15. C language helper function for serial output using DTR synchronization.// MC68HC812A4 or MC68HC912B32// PT3/IC3 is DTRvoid checkIC3(void) { if(PORTT&0x08) // PT3=1 if DTR=-12 SC0CR2=0x0C; // SCI TxD disarmed else SC0CR2=0x8C;} // SCI TxD armed// Program 7.16. C language output function using DTR synchronization.int OutChar(unsigned char data) { unsigned char flag; flag=PutFifo(data^0x80); /* Bit7=1 is the first stop bit */ checkIC3(); /* Arm SCI if DTR=+12 */ return(flag);} /* error if FIFO is full */// Program 7.17. C language ISR using DTR synchronization.// MC68HC812A4 or MC68HC912B32// PT3/IC3 is DTR #pragma interrupt_handler IC3Han()void IC3Han(void) { TFLG1=0x08; // Ack clear C3F checkIC3();} // Arm SCI if DTR=+12#pragma interrupt_handler SCIHAN()void SCIHan(void) { unsigned char data; if(GetFifo(&data);) SC0CR2=0x0C; // disarmed, empty else SC0DRL=data;} // output, ack// Program 7.18. C language initialization using DTR synchronization.// MC68HC812A4 or MC68HC912B32// PT3/IC3 is DTRvoid Ritual(void){ asm(" sei"); InitFifo(); SC0BD=417; // 1200 baud SC0CR1=0x00; // 8 bit, 1 stop SC0CR2=0x0C; TIOS &= 0xF7; // PT3 input capture DDRT &= 0xF7; // PT3 is input TSCR = 0x80; // enable TCNT TMSK2= 0x32; // 500ns clock TCTL4 |= 0xC0; // both rise, fall TMSK1 |= 0x08; // Arm IC3 TFLG1=0x08; // initially clear asm(" cli");}// Program 7.19. C language initialization for a D/A interface using the SPI.// MC68HC812A4 or MC68HC912B32void DACInit(void){ // PS7=LD=1 DDRS=0xE0; // PS6=CLK=SPI clock out PORTS=0x80; // PS5=SRI=SPI master out/* bit SP0CR1 7 SPIE = 0 no interrupts 6 SPE = 1 enable SPI 5 SWOM = 0 regular outputs 4 MSTR = 1 master 3 CPOL = 0 output changes on fall, 2 CPHA = 0 clock normally low 1 SSOE = 0 PS7 regular output, LD 0 LSBF = 0 most sign bit first */ SP0CR1=0x50;/* bit SP0CR2 3 PUPS = 0 no internal pullups 2 RDS = 0 regular drive 0 SPC0 = 0 normal mode */ SP0CR2=0x00; SP0BR=0x00;} // 4Mhz// Program 7.20. C language function for a D/A interface using the SPI.// MC68HC812A4 or MC68HC912B32#define SPIF 0x80void DACout(unsigned int code){ unsigned char dummy; SP0DR=0x00FF &(code>>8); // msbyte while((SP0SR&SPIF)==0); // gadfly wait dummy=SP0DR; // clear SPIF SP0DR=0x00FF& code; // lsbyte while((SP0SR&SPIF)==0); // gadfly wait dummy=SP0DR; // clear SPIF PORTS &= ~0x80; // PS7=LD=0 PORTS |= 0x80; } // PS7=LD=1// Program 7.21. C language initialization for an A/D interface using the SPI.// MC68HC812A4 or MC68HC912B32void ADCInit(void){ // PS7=CS=1 DDRS=0xE0; // PS6=SCLK=SPI clock out PORTS=0x80; // PS5=DIN=SPI master out/* bit SP0CR1 PS4=DOUT=SPI master in 7 SPIE = 0 no interrupts 6 SPE = 1 enable SPI 5 SWOM = 0 regular outputs 4 MSTR = 1 master 3 CPOL = 0 output changes on fall, 2 CPHA = 0 clock normally low 1 SSOE = 0 PS7 regular output, LD 0 LSBF = 0 most sign bit first */ SP0CR1=0x50;/* bit SP0CR2 3 PUPS = 0 no internal pullups 2 RDS = 0 regular drive 0 SPC0 = 0 normal mode */ SP0CR2=0x00; SP0BR=0x01;} // 2Mhz#define CH0 0x9F#define CH1 0xDF#define CH2 0xAF#define CH3 0xEF// Program 7.22. C language function for an A/D interface using the SPI.// MC68HC812A4 or MC68HC912B32unsigned int ADCin(unsigned char code){ unsigned int data; PORTS&= ~0x80; // PS7=CS=0 SP0DR=code; // set channel,mode while((SP0SR&0x80)==0); // gadfly wait data=SP0DR; // clear SPIF SP0DR=0; // start SPI while((SP0SR&0x80)==0); // gadfly wait data=SP0DR<<8; // msbyte of A/D SP0DR=0; // start SPI while((SP0SR&0x80)==0); // gadfly wait data+=SP0DR; // lsbyte of A/D PORTS |= 0x80; // PS7=CS=1 return data>>3;} // right justify// Program 7.23. C language initialization of a temperature sensor interface using the SPI.// Interface between MC68HC812A4/MC68HC912B32 and DS1620 using the SPI// bit status Configuration/Status Register meaning// 7 DONE 1=Conversion done, 0=conversion in progress// 6 THF 1=temperature above TH, 0=temperature below TH// 5 TLF 1=temperature below TL, 0=temperature above TL// 1 CPU 1=CPU control, 0=stand alone operation// 0 1SHOT 1=one conversion and stop, 0=continuous conversions// temperature digital value (binary) digital value (hex)// +125.0 C 011111010 $0FA// +64.0 C 010000000 $080// +1.0 C 000000010 $002// +0.5 C 000000001 $001// 0 C 000000000 $000// -0.5 C 111111111 $1FF// -16.0 C 111100000 $1E0// -55.0 C 110010010 $192void DS1620Init(void){ // PS7=RST=0 DDRS=0xE0; // PS6=CLK=SPI clock out PORTS=0x60; // PS5=DQ=SPI bidirectional data/* bit SP0CR1 7 SPIE = 0 no interrupts 6 SPE = 1 enable SPI 5 SWOM = 0 regular outputs? 4 MSTR = 1 master 3 CPOL = 1 output changes on fall 2 CPHA = 1 and input clocked in on rise 1 SSOE = 0 PS7 regular output DS1620 RST 0 LSBF = 1 least significant bit first */ SP0CR1=0x5D;/* bit SP0CR2 3 PUPS = 0 no internal pullups 2 RDS = 0 regular drive 0 SPC0 = 1 bidirectional mode */ SP0CR2=0x01; SP0BR=0x02;} // 1MHz could be 2Mhz// Program 7.24. C language helper functions for a temperature sensor interface using the SPI.#define SPIF 0x80void out8(char code){ unsigned char dummy;// assumes DDRS bit 5 is 1, output SP0DR=code; while((SP0SR&SPIF)==0); // gadfly wait for SPIF dummy=SP0DR;} // clear SPIFvoid out9(int code){ unsigned char dummy; SP0DR=0x00FF & code; // lsbyte while((SP0SR&SPIF)==0); // gadfly wait for SPIF dummy=SP0DR; // clear SPIF SP0DR=0x00FF&(code>>8); // msbyte while((SP0SR&SPIF)==0); // gadfly wait for SPIF dummy=SP0DR;} // clear SPIFunsigned char in8(void){ int n; unsigned char result; DDRS &= 0xDF; // PS5=DQ input SP0DR=0; // start shift register while((SP0SR&SPIF)==0); // gadfly wait for SPIF result=SP0DR; // get data, clear SPIF DDRS |= 0x20; // PS5=DQ output return result;}unsigned int in9(void){ unsigned int result; DDRS &= 0xDF; // PS5=DQ input SP0DR=0; // start shift register while((SP0SR&SPIF)==0); // gadfly wait for SPIF result=SP0DR; // get LS data, clear SPIF SP0DR=0; // start shift register while((SP0SR&SPIF)==0); // gadfly wait for SPIF if(SP0DR&0x01) // get MS data, clear SPIF result |= 0xFF00; // negative else result &= 0x00FF; // positive DDRS |= 0x20; // PS5=DQ output return result;}// Program 7.25. C language functions for a temperature sensor interface using the SPI.#define RST1 PORTS|=0x80; #define RST0 PORTS= 0x7F;void DS1620Start(void){ RST1 // RST=1 out8(0xEE); RST0} // RST=0void DS1620WriteConfig(char data){ RST1 // RST=1 out8(0x0C); out8(data); RST0} // RST=0void DS1620WriteTH(int data){ RST1 // RST=1 out8(0x01); out9(data); RST0} // RST=0void DS1620WriteTL(int data){ RST1 // RST=1 out8(0x02); out9(data); RST0} // RST=0unsigned char DS1620ReadConfig(void){ unsigned char value; RST1 // RST=1 out8(0xAC); value=in8(); RST0 // RST=0 return value;}unsigned int DS1620ReadT(void){ unsigned int value; RST1 // RST=1 out8(0xAA); value=in9(); RST0 // RST=0 return value;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -