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

📄 chap13.c

📁 Motorola 6808嵌入式接口设计开发原程序
💻 C
字号:
// 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.// MC68HC708XL36, Hiware compilerinterrupt 8 void TOC3handler(void){   PORTB=Pt->Out; // output for this state    Pt=Pt->Next;   // Move to next state    TSC3&=0x7F;    // acknowledge CH3F   TCH3=TCH3+Speed;} // Executed every stepvoid ritual(void) { asm{ sei};       // make atomic  TSC=0x02;      // 500 ns clock  TSC3=0x54;     // Arm output compare  DDRB=0xFF;     // Make Port B outputs  TSC3&=0x7F;    // Initially clear CH3F  Speed=10000;   // initial speed  Pt=S6;         // initial state  TCH3=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.// MC68HC708XL36, Hiware compilerinterrupt 8 void TOC3handler(void){   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    TSC3&=0x7F;    // acknowledge CH3F   TCH3=TCH3+rate;} // periodic rate// Program 13.6 Incremental position control software.// MC68HC708XL36, Hiware compilerinterrupt 8 void TOC3handler(void){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   TSC3&=0x7F;    // acknowledge CH3F   TCH3=TCH3+rate;} // periodic rate

⌨️ 快捷键说明

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