⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap13.c

📁 Motorola 6812芯片开发的接口程序。
💻 C
字号:
// Chapter 13 6812 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.// MC68HC812A4, ICC12 compiler#define OC5  0x20#pragma interrupt_handler TC5handler()void TC5handler(void){   PORTB=Pt->Out; // output for this state    Pt=Pt->Next;   // Move to next state    TFLG1=OC5;       // ack C5F    TC5=TC5+Speed;}  // Executed every stepvoid ritual(void) { asm(" sei");     // make atomic  TIOS|=OC5;     // enable OC5  TSCR|=0x80;    // enable  TMSK2=0x32;    // 500 ns clock  TMSK1|=OC5;    // Arm output compare 5  DDRB=0xFF;     // Make Port B outputs  TFLG1=OC5;     // Initially clear C5F  Speed=10000;   // initial speed  Pt=S6;         // initial state  TC5=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.// MC68HC812A4, ICC12 compiler#pragma interrupt_handler TC5handler()void TC5handler(void){     T=SE(A2D(channel)); // estimated T      E=Tstar-T;          // error     if(T<Tlow)        Actuator(0);      // too cold so off     else if (T>Thigh)       Actuator(1);      // too hot so on// leave as is if Tlow<T<Thigh     TC5=TC5+rate;      // periodic rate     TFLG1=0x20; }      // ack C5F// Program 13.6 Incremental position control software.// MC68HC812A4, ICC12 compiler#pragma interrupt_handler TC5handler()void TC5handler(void){ int new;   X=SE(A2D(channel));  // estimated (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   TC5=TC5+rate;      // periodic rate   TFLG1=0x20; }      // ack C5F// Program 13.7 Integral position control software.// MC68HC812A4, ICC12 compilerunsigned int Time;  // Time in msecunsigned int X;     // Est position in cmunsigned int Xstar; // Desired pos in cm unsigned int U;     // Actuator duty cycleunsigned int Cnt;   // once a secunsigned int Told;  // used to meas period#pragma interrupt_handler TC5handler()void TC5handler(void){ int NewU;  TFLG1=0x20;        // ack C5F  TC5=TC5+2000;      // every 1 ms  Time++;     // used to measure period  if((Cnt++)==1000){  // every 1 sec      Cnt=0;// 0<X<100, 0<Xstar<100, 100<U<19900, so// Minimum occurs when U=100,Xstar=0,X=100//    Minimum NewU = -900// Max occurs when U=19900,Xstar=100,X=0//    Maximum NewU = 20900// so NewU will be a valid signed int      NewU=U+10*(Xstar-X);     if(NewU<100) NewU=100;  // Constrain     if(NewU>19900) NewU=19900;     U=NewU;  } }// Program 13.8 PWM actuator control software.// MC68HC812A4, ICC12 compiler#pragma interrupt_handler TC4handler()void TC4handler(void){  TFLG1=0x10;        // ack C4F  if (TCTL1&0x01)   /* OL4 bit *//* OL4=1, High for the next U cycles */     TC4=TC4+U;   else/* OL4=0, Low for the next 20000-U cyc */     TC4=TC4+20000-U;       TCTL1^=0x01; }     /* Toggle OL4 */ // Program 13.9 Sensor measurement software.// MC68HC812A4, ICC12 compiler// Time is incremented every 1 ms, by OC5// This handler is executed on rise#pragma interrupt_handler TC1handler()void TC1Handler(void){unsigned int p;  TFLG1 = 0x02;  // Ack C5F  p = Time-Told; // period in msec  X = p-10;    // estimated position (cm)  Told=Time;} // Program 13.10 Initialization software.// MC68HC812A4, ICC12 compilervoid ritual(void) { // Input capture IC1  asm(" sei");      /* atomic */  TIOS=0x30;  // output compare OC5, OC4  TSCR|=0x80; // enable TCNT  TMSK2=0x32; // 500 ns clock  TCTL1=0x02; // Clear OC4   TCTL4=0x08; // capture on rise of IC1   TMSK1=0x32; // Arm OC5F+OC4F+IC1F    U=10000;      // Initial U , half power  Time = 0; Told=0;  Cnt=0;  TFLG1=0x32; // clear flags   TC5=TCNT+2000; // First OC5 in 1 ms   TC4=TCNT+100;  // First OC4 in 50us   asm(" cli");}Program 13.11. Subtraction with overflow/underflow checking.char Subtract(unsigned char N, unsigned char M){   /* returns N-M */unsigned int N16,M16; int Result16;     N16=N;        /* Promote N,M */     M16=M;     Result16=N16-M16;   /* -255睷esult16

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -