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

📄 chap2.c

📁 Motorola 6812芯片开发的接口程序。
💻 C
字号:
// Chapter 2 6812 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. 6811 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. 6812 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 */  DDRB=0xFF;           /* Make Port B outputs  */  DDRC=0x00;           /* Make Port C inputs */  TSCR=0x80;           /* Enable TCNT, default rate 8 MHz */  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.14: C implementation of SCI basic input/output.// 68HC812A4 SCI routines void init(void) {         SC0BD=417;    // 1200 baud     SC0CR1=0x00;  // 8data, 1stop     SC0CR2=0x0C;} // enable gadfly#define RDRF 0x20#define TDRE 0x80#define TC 0x40unsigned char insci(void){    while ((SC0SR1 & RDRF) == 0);       return(SC0DRL); }/* letter is character to print, */void OutChar(unsigned char letter){/* Wait for TDRE then output */    while ((SC0SR1 & TDRE) == 0);       SC0DRL=letter; }//Program 2.18. Main program with three modules.#include "HC12.H"#include "LCD12.H"#include "COP12.H"#include "Timer.H"void main(void){ char letter; int n=0;    COPinit(); // Enable TOF interrupt to make COP happy   LCDinit();   TimerInit()   LCDString("Adapt812 LCD");   TimerMsWait(1000);   LCDclear();   letter='a'-1;   while(1){      if (letter=='z')         letter='a';      else         letter++;      LCDputchar(letter);      TimerMsWait(250);      if(++n==16){         n=0;         LCDclear();}}}#include "LCD12.C"#include "COP12.C"#include "Timer.C"#include "VECTORS.C"// Program 2.19: Timer.H header file has public functionsvoid TimerInit(void);void TimerMsWait(unsigned int time);// Program 2.20: Timer.C implementation file defines all functionsunsigned int TimerClock; // private globalvoid TimerInit(void){ // public function  TSCR |=0x80; // TEN(enable)  TMSK2=0xA2;  // TOI arm, TPU(pullup) timer/4 (500ns)  TimerClock=2000; // 2000 counts per ms}void TimerWait(unsigned int time){ // private function  TC5=TCNT+TimerClock;  // 1.00ms wait  TFLG1 = 0x20;         // clear C5F  while((TFLG1&0x20)==0){};}void TimerMsWait(unsigned int time){ // public function  for(;time>0;time--)    TimerWait(TimerClock); // 1.00ms wait}// Program 2.21. Enhanced C implementation of a Mealy Finite State Machine./* 6812 PortC bits 1,0 are input, Port B bits 1,0 are output */#define	OutPort (*(unsigned char  volatile *)(0x0001))#define	OutDDR  (*(unsigned char  volatile *)(0x0003))#define	InPort  (*(unsigned char  volatile *)(0x0004))#define	InDDR   (*(unsigned char  volatile *)(0x0006))/* rate is the number of cycles/100usec */#define rate 800const struct State{	unsigned char Out;             /* Output values */	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,5*rate,{SB,SA,SB,SC}},  /* SA out=1, wait= 500usec, next states */  {0x02,10*rate,{SC,SA,SB,SC}}, /* SB out=2, wait=1000usec, next states */  {0x03,20*rate,{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){ StateType *Pt;  unsigned char Input;  Pt=SA;                /* Initial State */  OutDDR=0xFF;          /* Make Output port outputs  */  InDDR=0x00;           /* Make Input port inputs  */  TSCR=0x80;            /* Enable TCNT, default rate 8 MHz */  while(1){    OutPort=Pt->Out;    Wait(Pt->Time);     /* Time to wait in this state  */    Input=InPort&0x03;  /* Input=0,1,2,or 3 */    Pt=Pt->Next[Input];  }};// Program 2.22. C++ implementation of a Mealy Finite State Machine.// a DDRAddress of 1 means fixed output port with no DDR// a DDRAddress of 0 means fixed input port with no DDRtemplate <class T> class port{  protected :       unsigned char *PortAddress;   // pointer to data      unsigned char *DDRAddress;    // pointer to data direction register  public : port(unsigned short ThePortAddress, unsigned short TheDDRAddress){       PortAddress = (unsigned char *)ThePortAddress; // initialize pointer to I/O port       DDRAddress  = (unsigned char *)TheDDRAddress;} // initialize pointer to DDRvirtual short Initialize(unsigned char  data){       if((int)DDRAddress==1)   // fixed output port           return(data==0xFF);  // OK if initializing all bits to output       if(DDRAddress==0)        // fixed input port           return(data==0);     // OK if initializing all bits to input       (*DDRAddress) = data;    // configure direction register       return 1;}   // successfulvirtual void put(unsigned char  data){       if((int)DDRAddress==0) return;  // fixed input       if((*DDRAddress)==0) return;    // all input       (*PortAddress) = data;}         // output data to portvirtual unsigned char get(void){       return (*PortAddress);} // input data from port}// 6812 PortC bits 1,0 are input, Port B bits 1,0 are output  port<unsigned char> OutPort(0x0001,0x0003);port<unsigned char> InPort(0x0004,0x0006);void main(void){ StateType *Pt;  unsigned char Input;  Pt=SA;                // Initial State   OutPort.Initialize(0xFF);          // Make Output port outputs   InPort.Initialize(0x00);           // Make Input port inputs   TSCR=0x80;            // Enable TCNT, default rate 8 MHz   while(1){    OutPort.put(Pt->Out);    Wait(Pt->Time);     // Time to wait in this state     Input=InPort.get()&0x03;  // Input=0,1,2,or 3     Pt=Pt->Next[Input];  }// Program 2.23. Additional member functions for the I/O port class.T operator = (T data){       put(data);              // output to port       return data;}           // returns data itselfoperator T () (T data){       return get();}          // returns port datavirtual T operator |= (T data){       put(data |= get());     // read modify write port access       return data;}           // returns new datavirtual T operator &= (T data){       put(data &= get());     // read modify write port access       return data;}           // returns new datavirtual T operator ^= (T data){       put(data ^= get());     // read modify write port access       return data;}           // returns new data// Program 2.24: Recursion is when a function calls itself.//-----------------------Start of OutUDec-------------------------------------// Output a 16 bit number in unsigned decimal format// Variable format 1 to 5 digits with no space before or after// This function uses recursion to convert a decimal number of //   unspecified length as an ASCII stringvoid OutUDec(unsigned int number){     if (number>=10){         OutUDec(number/10);         OutUDec(number%10);    }    else         OutChar(number+'0');}// 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){   DDRB=0xFF;  // 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 + -