📄 chap8.c
字号:
TOC5=TOC5+period; TFLG1=0x20;} // ack OC5F// Program 8.15. C software interface of a scanned LED display.// MC68HC812A4// PB7-PB0 output, 7 bit pattern // PC2-PC0 output, selects LED digit unsigned char code[3]; // binary codesstatic unsigned char select[3]={4,2,1};unsigned int index; // 0,1,2#define C5F 0x20#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=C5F; // Acknowledge TC5=TOC5+10000; // every 5 ms PORTC=select[index]; // which LED? PORTB=code[index]; // enable if(++index==3) index=0;}void ritual(void) { asm(" sei"); // make atomic index=0; DDRC=0xFF; // outputs 7 segment code DDRB=0xFF; // outputs select LED TMSK1|=C5F; // Arm OC5 TIOS|=C5F; // enable OC5 TSCR|=0x80; // enable TMSK2=0x32; // 500 ns clock TC5=TCNT+10000; asm(" cli"); }// Program 8.16. C software interface of a multiplexed LED display.// MC68HC812A4unsigned int global; // 12 bit packed BCDconst struct LED{ unsigned char enable; // select unsigned char shift; // bits to shift const struct LED *Next; }; // Link#typedef const struct LED LEDType;#typedef LEDType * LEDPtr;LEDType LEDTab[3]={{ 0x04, 8, &LEDTab[1] }, // Most sig{ 0x02, 4, &LEDTab[2] },{ 0x01, 0, &LEDTab[0] }}; // least sigLEDPtr Pt; // Points to current digit#define C5F 0x20#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=C5F; // Acknowledge TC5=TC5+10000; // every 5 ms PORTB=(Pt->enable) +(global>>(pt->shift))<<4); Pt=Pt->Next; }void ritual(void) { asm(" sei"); // make atomic DDRB=0xFF; // outputs to LED's global=0; Pt=&LEDTab[0]; TMSK1|=C5F; // Arm OC5 TIOS|=C5F; // enable OC5 TSCR|=0x80; // enable TMSK2=0x32; // 500 ns clock TC5=TCNT+10000; asm(" cli"); }// Program 8.17. C software interface of an integrated LED display.// MC68HC812A4// PS5/MOSI = MC14489 DATA IN// PS6/SCLK = MC14489 CLOCK IN// PS7 (simple output) = MC14489 ENABLEvoid ritual(void) { DDRS |= 0xE0; // outputs to MC14489 SP0CR1=0x50; // bit meaning// 7 SPIE=0 no interrupts// 6 SPE=1 SPI enable// 5 SWOM=0 regular outputs// 4 MSTR=1 master// 3 CPOL=0 match timing with MC14489// 2 CPHA=0// 1 SSOE=0 PS7 is simple output// 0 LSBF=0 MSB first SP0CR2=0x00; // no pull-up, regular drive SP0BR=0x02; // 1Mhz SCLK PORTS|= 0x80; // ENABLE=1 PORTS&= 0x7F; // ENABLE=0 SP0DR= 0x01; // hex format while(SP0SR&0x80)==0){}; PORTS|=0x80;} // ENABLE=1void LEDout(unsigned char data[3]){ //packed PORTS &= 0x7F; // ENABLE=0 SP0DR = data[2]; // send MSbyte while(SP0SR&0x80)==0){}; SP0DR = data[1]; // send middle byte while(SP0SR&0x80)==0){}; SP0DR = data[0]; // send LSbyte while(SP0SR&0x80)==0){}; PORTS |= 0x80;} // ENABLE=1// Program 8.18. Helper function for a simple LCD display.void LCDOutDigit(unsigned char position, unsigned char data) { // position is 0x80, 0x40, 0x20, or 0x10 and data is the BCD digit PORTB=0x0F&data; // set BCD digit on the A-D inputs of the MC14543B PORTB|=position; // toggle one of the LD inputs high PORTB=0x0F&data;} // LD=0, latch digit into MC14543B// Program 8.19. C software interface of a simple LCD display.void LCDOutNum(unsigned int data){ unsigned int digit,num,i; unsigned char pos; num=min(data,9999); // data should be unsigned from 0 to 9999 pos=0x10; // position of first digit (ones) for(i=0;i<4;i++){ digit=num%10; num=num/10; // next BCD digit 0 to 9 LCDOutDigit(pos,digit); pos=pos<<1;}}// Program 8.20. Bit-banged interface to a scanned LCD display.void LCDOut (unsigned char *pt) {unsigned int i; unsigned char mask; for(i=0;i<6;i++){ for(mask=0x80;mask;mask=mask>>1){ // look at bits 7,6,5,4,3,2,1,0 if((*pt)&mask) PORTB=1; else PORTB=0; // Serial data of the MC145000 PORTB|=2; // toggle the serial clock first high PORTB&=0xFD;} // then low pt++; }}// Program 8.21. SPI interface to a scanned LCD display using a MC145000.// MC68HC812A4// PS5/MOSI = MC145000 DATA IN// PS6/SCLK = MC145000 CLOCK INvoid ritual(void) { DDRS |= 0x60; // outputs to MC145000 SP0CR1=0x50; // bit meaning// 7 SPIE=0 no interrupts// 6 SPE=1 SPI enable// 5 SWOM=0 regular outputs// 4 MSTR=1 master// 3 CPOL=0 match timing with MC14489// 2 CPHA=0// 1 SSOE=0 PS7 is simple output// 0 LSBF=0 MSB first SP0CR2=0x00; // no pull-up, regular drive SP0BR=0x02;} // 1Mhz SCLKvoid LCDout(unsigned char data[6]){ unsigned int j; for(j=5; j>=0 ; j--){ SP0DR = data[j]; // Msbyte first while(SP0SR&0x80)==0){};}}// Program 8.22. Private functions for an HD44780 controlled LCD display.// MC68HC812A4// 1 by 16 char LCD Display (HD44780)// ground = pin 1 Vss// power = pin 2 Vdd +5v// 10Kpot = pin 3 Vlc contrast adjust// PJ2 = pin 6 E enable// PJ1 = pin 5 R/W 1=read, 0=write// PJ0 = pin 4 RS 1=data, 0=control// PH0-7 = pins7-14 DB0-7 8 bit data#define LCDdata 1 // PJ0=RS=1#define LCDcsr 0 // PJ0=RS=0#define LCDread 2 // PJ1=R/W=1#define LCDwrite 0 // PJ1=R/W=0#define LCDenable 4 // PJ2=E=1#define LCDdisable 0 // PJ2=E=0void LCDcycwait(unsigned short cycles){ TC5=TCNT+cycles; // 500ns cycles to wait TFLG1 = 0x20; // clear C5F while((TFLG1&0x20)==0){};}// Program 8.23. Public functions for an HD44780 controlled LCD display.// MC68HC812A4void LCDputchar(unsigned short letter){ // letter is ASCII code PORTH=letter; PORTJ=LCDdisable+LCDwrite+LCDdata; PORTJ=LCDenable+LCDwrite+LCDdata; // E=1 PORTJ=LCDdisable+LCDwrite+LCDdata; // E=0 LCDcycwait(80);} // 40 us waitvoid LCDputcsr(unsigned short command){ PORTH=command; PORTJ=LCDdisable+LCDwrite+LCDcsr; PORTJ=LCDenable+LCDwrite+LCDcsr; // E=1 PORTJ=LCDdisable+LCDwrite+LCDcsr; // E=0 LCDcycwait(80);} // 40 us waitvoid LCDclear(void){ LCDputcsr(0x01); // Clear Display LCDcycwait(3280); // 1.64ms wait LCDputcsr(0x02); // Cursor to home LCDcycwait(3280);} // 1.64ms waitvoid LCDinit(void){ DDRH=0xFF; DDRJ=0xFF; TIOS |= 0x20; // enable OC5 TSCR |= 0x80; // enable, no fast clear TMSK2=0xA2; // 500 ns clock LCDputcsr(0x06); // I/D=1 Increment, S=0 nodisplayshift LCDputcsr(0x0C); // D=1 displayon, // C=0 cursoroff, B=0 blinkoff LCDputcsr(0x14); // S/C=0 cursormove, R/L=0 shiftright LCDputcsr(0x30); // DL=1 8bit, N=0 1 line, F=0 5by7dots LCDclear(); } // clear display// Program 8.27. C linked list and helper functions used to control the stepper motor.const struct State{ unsigned char Out; /* Output for this state */ const struct State *Next[2]; /* Next state CW or CCW motion */};#typedef struct State StateType;#typedef StateType * StatePtr;unsigned char POS; /* between 0 and 199 representing shaft angle */#define clockwise 0 /* Next index*/#define counterclockwise 1 /* Next index*/StateType fsm[4]={ {10,{&fsm[1],&fsm[3]}}, { 9,{&fsm[2],&fsm[0]}}, { 5,{&fsm[3],&fsm[1]}}, { 6,{&fsm[0],&fsm[2]}}};StatePtr Pt; /* Current State */void CW(void){ Pt=Pt->Next[clockwise]; /* circulates around linked list */ PORTB=Pt->Out; /* step motor */ if(POS++==200) POS=0;} /* maintain shaft angle */ void CCW(void){ Pt=Pt->Next[counterclockwise]; /* circulates around linked list*/ PORTB=Pt->Out; /* step motor */ if(POS==0)POS=199; else POS--; } /* maintain shaft angle */void Init(void){ DDRB=0xFF; // 6812 only POS=0; Pt=&fsm[0];}// Program 8.28. High-level C function to control the stepper motor.void SEEK(unsigned char New){ int CWsteps,i; if((CWsteps=New-POS)<0) CWsteps+=200; if(CWsteps>100) for(i=CWsteps;i<200;i++) CCW(); else for(i=0;i<CWsteps;i++) CW(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -