利用SCI做的一个足球答题系统

  SCI,异步串行通信接口,内置独立的波特率产生电路和SCI收发器,可以选择发送8或9个数据位(其中一位可以指定为奇或偶校验位)。

  SCI是全双工异步串行通信接口,主要用于MCU与其他计算机或设备之间的通信,几个独立的MCU也能通过SCI实现串行通信,形成网络。

  MC12里有两个SCI(SCI0和SCI1)。设计SCI串口通信程序,主要是掌握八个寄存器,设置好初始化。

  利用SCI做的一个足球答题系统   ,代码如下:

  1 /*******************************************************/
  2 /*                     利用SCI做的一个足球答题系统                         */
  3 /*******************************************************/
  4 #include <hidef.h> /* common defines and macros */
  5 #include <mc9s12dp256.h> /* derivative information */
  6 
  7 #pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
  8 byte sci_data;
  9 
 10 //中断初始化
 11 void SCI_Init(void)
 12 {
 13   SCI0BDL=0x34;             //  波特率控制寄存器  0011 0100
 14   SCI0CR2=0X2C;             //  控制寄存器2       0010 1100
 15 }
 16 
 17 //发送子函数
 18 void SCI_Transmit(byte data)
 19 {
 20   while(!SCI0SR1_TDRE) ;
 21   SCI0DRL=data;              //数据寄存器
 22 }
 23 
 24 //接收子函数
 25 void SCI_Receive(byte *data)
 26 {
 27   *data=SCI0DRL;
 28 }
 29 
 30 //特定输出子函数
 31 void printf(char *str)
 32 {
 33   while(*str!='
')
 34   {
 35     SCI_Transmit(*str);
 36     *str++;
 37   }
 38 }
 39 
 40 N0Choose(byte data)
 41 {
 42   switch(data)
 43   {
 44     case '1':
 45     NO1();
 46     break;
 47     case '2':
 48     NO2();
 49     break ;
 50     case '3':
 51     NO3();
 52     break;
 53     case '4':
 54     NO4();
 55     break;
 56     case '5':
 57     NO5();
 58     break;
 59     default:
 60     break;
 61   }
 62 }
 63 
 64 /********************************************************/
 65 /*                                                 主函数                            */
 66 /********************************************************/
 67 void main(void)
 68 {
 69   SCI_Init();
 70   printf("welcome to lipu's football-quiz system!

");
 71   printf("choose the problem number(1to5)

");          //选择正确的问题号码1~5
 72   while(1)
 73   {
 74     while(!SCI0SR1_RDRF);
 75     SCI_Receive(&sci_data);
 76     SCI_Transmit(sci_data);
 77     N0Choose(sci_data);
 78   }
 79 }
 80 
 81 
 82 void Right(void)
 83 {
 84   printf("
you are RIGHT.
choose the next question

");}
 85   void Wrong(void){
 86   printf("
you are WRONG.
choose the next question

");}
 87   int NO1(void)
 88   {
 89     printf(".which country is the champion of World Cup at 2006?

") ;
 90     printf("A:Brazil B:Italy

");
 91     
 92     while(!SCI0SR1_RDRF);
 93       SCI_Receive(&sci_data);
 94       SCI_Transmit(sci_data);
 95     switch(sci_data)
 96     {
 97       case 'A':
 98       Wrong();
 99       break;
100       case 'B':
101       Right();
102       break;
103       default:
104       break;
105     }
106   }
107   
108 int NO2(void)
109 {
110   printf(".which country have the most champions of World Cup?

") ;
111   printf("A:Brazil B:Italy

");
112   while(!SCI0SR1_RDRF);
113   SCI_Receive(&sci_data);
114   SCI_Transmit(sci_data);
115   switch(sci_data)
116   {
117     case 'B':
118     Wrong();
119     break;
120     case 'A':
121     Right();
122     break;
123     default:
124     break;
125   }
126 }
127 
128 int NO3(void)
129 {
130   printf(".which club is the champion of Spanish Prinera Divison at 06-07

") ;
131   printf("A:Barcelona B:Real Madrid

");
132   while(!SCI0SR1_RDRF);
133   SCI_Receive(&sci_data);
134   SCI_Transmit(sci_data);
135   switch(sci_data)
136   {
137     case 'A':
138     Wrong();
139     break;
140     case 'B':
141     Right();
142     break;
143     default:
144     break;
145   }
146 }
147 
148 int NO4(void)
149 {
150   printf(".which club is the champion of Italian Serie A at 06-07

") ;
151   printf("A:Inter Milan B:AC.Milan

");
152   while(!SCI0SR1_RDRF);
153   SCI_Receive(&sci_data);
154   SCI_Transmit(sci_data);
155   switch(sci_data)
156   {
157     case 'B':
158     Wrong();
159     break;
160     case 'A':
161     Right();
162     break;
163     default:
164     break;
165   }
166 }
167 
168 int NO5(void)
169 {
170   printf(".who is the FIFA World Player at 2006

") ;
171   printf("A:Henry B:Ronaldiaho

");
172   while(!SCI0SR1_RDRF);
173   SCI_Receive(&sci_data);
174   SCI_Transmit(sci_data);
175   switch(sci_data)
176   {
177     case 'B':
178     Wrong();
179     break;
180     case 'A':
181     Right();
182     break;
183     default:
184     break;
185   }
186 }
zuqiu_dati.c
原文地址:https://www.cnblogs.com/lihuidashen/p/3486092.html