?? chap13.c
字號(hào):
// Chapter 13 6808 C programs// Jonathan W. Valvano// This software accompanies the book,// Embedded Microcomputer Systems: Real Time Interfacing// published by Brooks Cole, 1999// Program 13.1. C implementation of a Traffic Light Controller./* Port B bits 5-0 outputs that control the traffic signal */const struct State { unsigned char Out; /* Output to Port B */ unsigned short Time; /* Time in sec to wait */ const struct State *Next; /* Next state */};typedef const struct State StateType;#define NorthRed_EastGreen &fsm[0]#define NorthRed_EastYellow &fsm[1]#define NorthGreen_EastRed &fsm[2]#define NorthYellow_EastRed &fsm[3]StateType fsm[4]={/* NorthRed_EastGreen, wait= 3 min, next state */ {0x21, 180, NorthRed_EastYellow}, /* NorthRed_EastYellow, wait= 15 sec, next state */ {0x22, 15, NorthGreen_EastRed}, /* NorthGreen_EastRed, wait= 3 min, next state */ {0x0C, 180, NorthYellow_EastRed}, /* NorthYellow_EastRed, wait= 15 sec, next state */ {0x14, 15, NorthRed_EastGreen}};void main(void){ StatePtr *Pt; /* Current State */ Pt=NorthRed_EastGreen; /* Initial State */ DDRB=0xFF; /* Make Port B outputs */ while(1){ PORTB=Pt->Out; /* Perform output for this state */ Wait(Pt->Time); /* Time to wait in this state */ Pt=Pt->Next; /* Move to next state */ }};// Program 13.2 Circular list used to spin a stepper motor./* Port B bits 3-0 outputs that control the stepper motor */const struct State { unsigned char Out; /* Output to Port B */ const struct State *Next; /* Next state */};typedef const struct State StateType;#define S6 &fsm[0]#define S5 &fsm[1]#define S9 &fsm[2]#define S10 &fsm[3]StateType fsm[4]={ {0x06, S5}, {0x05, S9}, {0x09, S10}, {0x0A, S6};StatePtr *Pt; /* Current State */unsigned short Speed;// Program 13.3 C software to spin a stepper motor at a constant speed.// MC68HC05C8, Hiware compilerinterrupt 3 void TOChan(void){ char dummy; PORTB=Pt->Out; // output for this state Pt=Pt->Next; // Move to next state dummy=TSR; // part of acknowledge OCR=OCR+Speed;} // Executed every stepvoid ritual(void) { char dummy;asm{ sei}; // make atomic TCR=0x40 ; // Arm output compare DDRB=0xFF; // Make Port B outputs Speed=10000; // initial speed Pt=S6; // initial state dummy=TSR; // part of clear OCR=TCNT+2000; // First one in 1 msasm{ cli}; } // Program 13.4 Port B is used to turn on the heater.unsigned char Tlow,Thigh,T; int E; // units in degrees C void Actuator(unsigned char relay){ PORTB=relay; // turns power on/off}// Program 13.5 Bang-bang temperature control software.// MC68HC05C8, Hiware compilerinterrupt 3 void TOChan(void){ char dummy; T=SE(); // estimated Temperature E=Tstar-T; // error if(T<Tlow) Actuator(0); // too cold->off else if (T>Thigh) Actuator(1); // too hot->on// leave as is if Tlow<T<Thigh dummy=TSR; // part of acknowledge OCR=OCR+rate;} // periodic rate// Program 13.6 Incremental position control software.// MC68HC05C8, Hiware compilerinterrupt 3 void TOChan(void){ char dummy; int new; X=SE(); // estimated position (mm) E=Xstar-X; // error (mm) new=PORTB; // promote to 16 bits if(E< -1) new--; // decrease else if (E>1) new++; // increase// leave as is if -1<E<1 if(new<0) new=0; // underflow if(new>255) new=255; // overflow PORTB=new; // output to actuator dummy=TSR; // part of acknowledge OCR=OCR+rate;} // periodic rate
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -