?? chap2.c
字號:
// Chapter 2 6805 C programs// Jonathan W. Valvano// This software accompanies the book,// Embedded Microcomputer Systems: Real Time Interfacing// published by Brooks Cole, 1999// Program 2.2. An inappropriate use of #define.#define size 10short data[size];void initialize(void){ short j for(j=0;j<10;j++) data[j]=0;};// Program 2.3. An appropriate use of #define.#define size 10short data[size];void initialize(void){ short j for(j=0;j<size;j++) data[j]=0;};// Program 2.4. 6805 C implementation of a Mealy Finite State Machine.const struct State{ unsigned char Time; /* Time to wait in each state */ unsigned char Out[2]; /* Output if input=0,1 */ const struct State *Next[2]; /* Next state if input=0,1 */};typedef const struct State StateType;#define SA &fsm[0]#define SB &fsm[1]#define SC &fsm[2]#define SD &fsm[3]StateType fsm[4]={ {100,{0,0},{SB,SD}}, {100,{0,8},{SC,SA}}, { 15,{0,0},{SB,SD}}, { 15,{8,8},{SC,SD}}};void Wait(unsigned int delay){ int Endt; Endt=TCNT+delay; /* Time (125ns cycles) to wait */ while((Endt-(int)TCNT)>0); /* wait */};void main(void){ StatePtr *Pt; /* Current State */ unsigned char Input; Pt=SA; /* Initial State */ DDRC=0x08; /* PortC bit3 is output */ while(1){ Wait(Pt->Time); /* Time to wait in this state */ Input=PORTC<<7; /* Input=0 or 1 */ PORTC=Pt->Out[Input]; /* Perform output for this state */ Pt=Pt->Next[Input]; /* Move to the next state */}};// Program 2.6. 6805 C implementation of a Moore Finite State Machine./* PortC bits 1,0 are input, Port B bits 1,0 are output */const struct State { unsigned char Out; /* Output to Port B */ unsigned int Time; /* Time in 100 祍ec to wait in this state */ const struct State *Next[4]; /* Next state if input=0,1,2,3 */};typedef const struct State StateType;#define SA &fsm[0]#define SB &fsm[1]#define SC &fsm[2]StateType fsm[3]={ {0x01, 4000,{SB,SA,SB,SC}}, /* SA out=1, wait= 500usec, next states */ {0x02, 8000,{SC,SA,SB,SC}}, /* SB out=2, wait=1000usec, next states */ {0x03,16000,{SA,SA,SB,SA}} /* SC out=3, wait=2000usec, next states */};void Wait(unsigned int delay){ int Endt; Endt=TCNT+delay; /* Time (125ns cycles) to wait */ while((Endt-(int)TCNT)>0); /* wait */};void main(void){ StatePtr *Pt; /* Current State */ unsigned char Input; Pt=SA; /* Initial State */ DDRC=0x00; /* Make Port C inputs */ DDRB=0xFF; while(1){ PORTB=Pt->Out; /* Perform output for this state */ Wait(Pt->Time); /* Time to wait in this state */ Input=PORTC&0x03; /* Input=0,1,2,or 3 */ Pt=Pt->Next[Input]; /* Move to next state */ }};// Program 2.7: A median function that is not very portable.unsigned int Median(unsigned int u1,unsigned int u2,unsigned int u3){ unsigned int result; printf("The inputs are %d, %d, %d.\n",u1,u2,u3); if(u1>u2) if(u2>u3) result=u2; // u1>u2,u2>u3 u1>u2>u3 else if(u1>u3) result=u3; // u1>u2,u3>u2,u1>u3 u1>u3>u2 else result=u1; // u1>u2,u3>u2,u3>u1 u3>u1>u2 else if(u3>u2) result=u2; // u2>u1,u3>u2 u3>u2>u1 else if(u1>u3) result=u3; // u2>u1,u2>u3,u1>u3 u2>u1>u3 else result=u1; // u2>u1,u2>u3,u3>u1 u2>u3>u1 printf("The median is %d.\n",result); return(result):}// Program 2.27: Empirical measurement of dynamic efficiency in C.unsigned short before,elasped;void main(void){ ss=100; before=TCNT; tt=sqrt(ss); elasped=TCNT-before;}// Program 2.29. Another empirical measurement of dynamic efficiency in C.void main(void){// PB7 is connected to a scope ss=100; while(1){ PORTB |= 0x80; // set PB7 high tt=sqrt(ss); PORTB &= ~0x80; // clear PB7 low }}// Program 2.30: A time/position profile dumping into a data array.unsigned short time[100];unsigned short place[100];unsigned short n;void profile(unsigned short p){ time[n]=TCNT; // record current time place[n]=p; n++;}unsigned short sqrt(unsigned short s){ unsigned short t,oldt; profile(0); t=0; // based on the secant method if(s>0) {profile(1); t=32; // initial guess 2.0 do{profile(2); oldt=t; // calculation from the last iteration t=((t*t+16*s)/t)/2;} // t is closer to the answer while(t!=oldt);} // converges in 4 or 5 iterations profile(3); return t;} // Program 2.31: A time/position profile using two output bits.unsigned int sqrt(unsigned int s){ unsigned int t,oldt; PORTB=0; t=0; // based on the secant method if(s>0) {PORTB=1; t=32; // initial guess 2.0 do{PORTB=2; oldt=t; // calculation from the last iteration t=((t*t+16*s)/t)/2;} // t is closer to the answer while(t!=oldt);} // converges in 4 or 5 iterations PORTB=3; return t;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -