📄 chap8.c
字号:
// Chapter 8 6805 C programs// Jonathan W. Valvano// This software accompanies the book,// Embedded Microcomputer Systems: Real Time Interfacing// published by Brooks Cole, 1999// Program 8.1. The MC68HC812A4 Port J initialization.// MC68HC812A4 only// 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.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){ POS=0; DDRB=0xFF; 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 + -