📄 chap7.c
字号:
// Chapter 7 6811 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.// 68HC11A8 SCI routines void init(void) { BAUD=0x33; // 1200 baud SCCR1=0x00; // 8data, 1stop SCCR2=0x0C;}// enable gadfly#define RDRF 0x20#define TDRE 0x80#define TC 0x40unsigned char insci(void){ while ((SCSR & RDRF) == 0); return(SCDR); }void OutChar(unsigned char letter){/* Wait for TDRE then output */ while ((SCSR & TDRE) == 0); SCDR=letter; }void outsci2(unsigned char letter){/* Output then wait for TDRE y */ SCDR=letter; while ((SCSR & TDRE) == 0); }void outsci3(unsigned char letter){/* Output then wait for TC */ SCDR=letter; while ((SCSR & TC) == 0); }// Program 7.9. C language implementation of receiver interrupts.// MC68HC11A8#pragma interrupt_handler SCIHAN()#define RDRF 0x20// Executed on RDRF (not TDRE)void SCIhandler(void){ if((SCSR&0x21)!= RDRF) error(); if(SCSR&0x0E) error(); if(PutFifo(SCDR)) error(); }void RitualSCI(void){asm(" sei"); BAUD=0x31; // 4800 bits/sec SCCR1=0; // 8 bit 1 stop SCCR2=0x2C; // Receiver intrpt CLRQ(); // Clear FIFOasm(" cli");}// Program 7.14. C functions for serial output using DTR synchronization.// MC68HC11A8// PA0/IC3 is DTRvoid OutChar(unsigned char data) { while((PORTA&0x01)||((SCSR&0x80)==0)); SCDR= data; }// Program 7.15. C language helper function for serial output using DTR synchronization.// MC68HC11A8// PA0/IC3 is DTRvoid checkIC3(void) { if(PORTA&0x01) // PA0=1 if DTR=-12 SCCR2=0x0C; // SCI TxD disarmed else SCCR2=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.// MC68HC11A8// PA0/IC3 is DTR #pragma interrupt_handler IC3Han()void IC3Han(void) { TFLG1=0x01; // Ack clear IC3F checkIC3();} // Arm SCI if DTR=+12#pragma interrupt_handler SCIHAN()void SCIHan(void) { unsigned char data; if(GetFifo(&data);) SCCR2=0x0C; // disarmed, empty else SCDR=data;} // output, ack interrupt// Program 7.18. C language initialization using DTR synchronization.// MC68HC11A8// PA0/IC3 is DTRvoid Ritual(void){ asm(" sei"); InitFifo(); BAUD=0x33; // 1200 bits/sec SCCR1=0x00; // 8 bit, 1 stop SCCR2=0x0C; /* SCI disarmed because FIFO is empty */ TCTL2=0x03; // both rise or fall TMSK1=0x01; // Arm IC3 TFLG1=0x01; // clear IC3F */ asm(" cli");}// Program 7.19. C language initialization for a D/A interface using the SPI.// MC68HC11A8void DACInit(void){ // PD5=LD=1 DDRD|=0x38; // PD4=CLK=SPI clock out PORTD|=0x20; // PD3=SRI=SPI master out/* bit SPCR 7 SPIE = 0 no interrupts 6 SPE = 1 enable SPI 5 DWOM = 0 regular outputs 4 MSTR = 1 master 3 CPOL = 0 output changes on fall, 2 CPHA = 0 clock normally low 1 SPR1 = 0 1 MHz operation 0 SPR0 = 0 */ SPCR=0x50;}// Program 7.20. C language function for a D/A interface using the SPI.// MC68HC11A8#define SPIF 0x80void DACout(unsigned int code){ unsigned char dummy; SPDR=0x00FF &(code>>8); // msbyte while((SPSR&SPIF)==0); // gadfly wait dummy=SPDR; // clear SPIF SPDR=0x00FF& code; // lsbyte while((SPSR&SPIF)==0); // gadfly wait dummy=SPDR; // clear SPIF PORTD &= ~0x20; // PD5=LD=0 PORTD |= 0x20; } // PD5=LD=1// Program 7.21. C language initialization for an A/D interface using the SPI.// MC68HC11A8void ADCInit(void){ // PD5=CS=1 DDRD|=0x38; // PD4=SCLK=SPI clock out PORTD|=0x20; // PD3=DIN=SPI master out/* bit SPCR PD2=DOUT=SPI master in 7 SPIE = 0 no interrupts 6 SPE = 1 enable SPI 5 DWOM = 0 regular outputs 4 MSTR = 1 master 3 CPOL = 0 output changes on fall, 2 CPHA = 0 clock normally low 1 SPR1 = 0 1 MHz operation 0 SPR0 = 0 */ SPCR=0x50;}#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.// MC68HC11A8unsigned int ADCin(unsigned char code){ unsigned int data; PORTD &= ~0x20; // PD5=CS=0 SPDR=code; // set channel,mode while((SPSR&SPIF)==0); // gadfly wait data=SPDR; // clear SPIF SP0DR=0; // start SPI while((SPSR&SPIF)==0); // gadfly wait data=SPDR<<8; // msbyte of A/D SPDR=0; // start SPI while((SPSR&SPIF)==0); // gadfly wait data+=SPDR; // lsbyte of A/D PORTD |= 0x20; // PD5=CS=1 return data>>3;} // right justify// Program 7.23. C language initialization of a temperature sensor interface using the SPI.// MC68HC812A4/MC68HC912B32 only// Program 7.24. C language helper functions for a temperature sensor interface using the SPI.// MC68HC812A4/MC68HC912B32 only// Program 7.25. C language functions for a temperature sensor interface using the SPI.// MC68HC812A4/MC68HC912B32 only
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -