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

📄 main.lst

📁 基于c8051f040一个多机通讯程序,开发环境keil
💻 LST
📖 第 1 页 / 共 2 页
字号:
 188   1          SFRPAGE = SFRPAGE_SAVE;          // Restore SFR page
 189   1      }
 190          
 191          void strcopy(unsigned char *strx,unsigned char *stry)
 192          {
 193   1              while(*stry)
 194   1              {
 195   2                      *strx++=*stry++;
 196   2              }
 197   1              *strx='\0';
 198   1      } 
 199          
 200          /************************************************************************************
 201          //  主循环程序
 202          *************************************************************************************/
 203          
 204          void main(void)
 205          { 
 206   1              unsigned char data i;
 207   1              char command[3];
 208   1          WDTCN=0xDE;                    //禁止看门狗定时器
 209   1          WDTCN=0xAD;
 210   1      
 211   1              PORT_Init();
 212   1              SYSCLK_Init();
 213   1              BDR=115200;                                        //初始默认的波特率
 214   1              LED=~LED;
 215   1      
 216   1              for(i=1;i<5;i++){
 217   2                      LED=0;
 218   2                      Delay_ms(50);
 219   2                      LED=1;
 220   2                      Delay_ms(50);   }
 221   1      
 222   1              Uart1_Init(BDR);
 223   1          clear_msg_objects();
 224   1      
 225   1      //这是主机节点的配置
 226   1        init_msg_object_TX (0x0A,0);          //10号信箱用于发送广播消息,ID=0   最高优先级
 227   1        init_msg_object_RX (0x01,1);          //1号信箱负责接收 1号节点的消息   优先级1
 228   1        init_msg_object_RX (0x02,2);          //1号信箱负责接收 2号节点的消息   优先级2
 229   1        init_msg_object_RX (0x03,3);          //1号信箱负责接收 3号节点的消息   优先级3
 230   1        init_msg_object_RX (0x04,4);          //1号信箱负责接收 4号节点的消息   优先级4
 231   1        init_msg_object_RX (0x05,5);          //1号信箱负责接收 5号节点的消息   优先级5
 232   1        
 233   1        EIE2 |= 0x20;                                 //允许CAN0中断
 234   1        EIP2 |= 0x20;                                 //设置CAN0中断优先级"高",在UART1_Init()中已设置UART1中断低优先级
 235   1        start_CAN();
 236   1      
 237   1        ET2=1;                //TIMER2 INTERRUPT ENABLE
 238   1        EA = 1;
 239   1              command[0] = 0;
 240   1              command[1] = 0;
 241   1              command[2] = 0;
C51 COMPILER V7.50   MAIN                                                                  04/25/2008 10:51:19 PAGE 5   

 242   1      
 243   1              while(1)
 244   1              {
 245   2                      pp=0;
 246   2                      Uart1_ReceiveLong(command);
 247   2      Uart1_ReceiveLong(command);
 248   2                      jjj=1;//判断确实来了命令
 249   2                               
 250   2                      UART_Data[puart++]='*';//'*'表示来的是命令不是数据
 251   2                              while (pp<3)
 252   2                      {
 253   3                                      UART_Data[puart++]=command[pp];         //往下甩24位命令
 254   3                                      pp++;   
 255   3                              }
 256   2      
 257   2                      if((command[1] & 0x07) == 0x07)//判断目前需要8个通道一起测量,即需要开辟8字节空间数组CAN_Datachannel[64]
 258   2                           channelable = 1;
 259   2                              
 260   2              if(command[0]==0xf2)//主节点复位
 261   2              {    
 262   3                  //强制复位
 263   3                               char SFRPAGE_SAVE = SFRPAGE;        // Save SFRPAGE
 264   3                       
 265   3                               SFRPAGE  =  RST_PAGE;
 266   3                               
 267   3                           RSTSRC |= 0x01;//将硬件上电复位位设为1
 268   3                               SFRPAGE = SFRPAGE_SAVE;
 269   3                      }
 270   2                      
 271   2                      while(able1)//收到8字节数据才允许转发;或者是需要8通道一起传送时收到64字节数据
 272   2                      {
 273   3                                           
 274   3                              Uart1_SendByte(CAN_Data[pcan++]);       
 275   3                          //canlength--;
 276   3                              if(pcan== 8) 
 277   3                                      {
 278   4                                              able1=0;//保护,防止溢出
 279   4                                              CAN_Data[0]=0;
 280   4                                              CAN_Data[1]=0;
 281   4                                              CAN_Data[2]=0;
 282   4                                              CAN_Data[3]=0;
 283   4                                              CAN_Data[4]=0;
 284   4                                              CAN_Data[5]=0;
 285   4                                              CAN_Data[6]=0;
 286   4                                              CAN_Data[7]=0;
 287   4                                      }
 288   3                              if(pcan >= 8) pcan=0;//保护,防止溢出
 289   3                                                              
 290   3              }
 291   2                 if(able2)//需要8通道一起传送时收到64字节数据
 292   2                      {
 293   3                  //if(pchanl%8 ==0 )
 294   3                      //Delay_s(5);   
 295   3                  {
 296   4                                      for(i=1;i<=64;i++)
 297   4                                      {
 298   5                                              Uart1_SendByte(CAN_Datachannel[pchanl++]);
 299   5                                      }
 300   4      
 301   4                              }
 302   3                  //Uart1_SendByte(CAN_Datachannel[pchanl++]);
 303   3                          //canlength--;
C51 COMPILER V7.50   MAIN                                                                  04/25/2008 10:51:19 PAGE 6   

 304   3                              if(pchanl == 64) 
 305   3                                      {
 306   4                                              able2=0;//保护,防止溢出
 307   4                                      }
 308   3                              if(pchanl >= 64) pchanl=0;//保护,防止溢出
 309   3                                                              
 310   3              }
 311   2      
 312   2                      if(puart==4)    
 313   2                      {
 314   3                              if((nexten==1) && (jjj==1))     
 315   3                              {
 316   4                                      CAN_trans(0x0A);
 317   4                              }       
 318   3                              
 319   3                      }
 320   2      
 321   2                      
 322   2              }
 323   1      }
 324          
 325          /***********************************************************************************/
 326          // 文件结束
 327          /***********************************************************************************/
 328          
 329          
 330          ////////////////////////////////////////////////////////////////////////////////
 331          //CAN Functions
 332          ////////////////////////////////////////////////////////////////////////////////
 333          
 334          //Clear Message Objects
 335          void clear_msg_objects (void)
 336          {
 337   1        SFRPAGE  = CAN0_PAGE;
 338   1        CAN0ADR  = IF1CMDMSK;    // Point to Command Mask Register 1
 339   1        CAN0DATL = 0xFF;         // Set direction to WRITE all IF registers to Msg Obj
 340   1        for (ii=1;ii<33;ii++)
 341   1          {
 342   2            CAN0ADR = IF1CMDRQST; // Write blank (reset) IF registers to each msg obj
 343   2            CAN0DATL = ii;
 344   2          }
 345   1      }
 346          
 347          //Initialize Message Object for RX
 348          //***********   MsgNum代表消息对象号码,ID代表该消息对象的优先级   **********
 349          void init_msg_object_RX (char MsgNum,ID)
 350          {
 351   1        SFRPAGE  = CAN0_PAGE;
 352   1        CAN0ADR  = IF1CMDMSK;  // Point to Command Mask 1
 353   1        CAN0DAT  = 0x00B8;     // Set to WRITE, and alter all Msg Obj except ID MASK
 354   1                               // and data bits
 355   1        CAN0ADR  = IF1ARB1;    // Point to arbitration1 register
 356   1        CAN0DAT  = 0x0000;     // Set arbitration1 ID to "0"    因为no extended ID 故仲裁位只有ID28~18有效
 357   1                                                              // 即高字的XXX0 0000 0000 00xx中的'0'为11位有效仲裁位,
 358   1                                                              // 我们将主机广播的仲裁的消息作为 0 (优先级最高),其余从1开始
 359   1        CAN0DAT  = 0x8000+ID*4;     // Arb2 high byte:Set MsgVal bit, no extended ID,
 360   1                               // Dir = RECEIVE
 361   1                                                              // 因为我们是从机,故接受来自主机的消息处 仲裁应为 0
 362   1        CAN0DAT  = 0x0480;     // Msg Cntrl: set RXIE, remote frame function disabled
 363   1        CAN0ADR  = IF1CMDRQST; // Point to Command Request reg.
 364   1        CAN0DATL = MsgNum;     // Select Msg Obj passed into function parameter list
 365   1                               // --initiates write to Msg Obj
C51 COMPILER V7.50   MAIN                                                                  04/25/2008 10:51:19 PAGE 7   

 366   1        // 3-6 CAN clock cycles to move IF register contents to the Msg Obj in CAN RAM
 367   1      }
 368          
 369          //Initialize Message Object for TX
 370          //***********   MsgNum代表消息对象号码,ID代表该消息对象的优先级   **********
 371          void init_msg_object_TX (char MsgNum,ID)
 372          {
 373   1        SFRPAGE = CAN0_PAGE;
 374   1        CAN0ADR = IF1CMDMSK;  // Point to Command Mask 1
 375   1        CAN0DAT = 0x00B2;     // Set to WRITE, & alter all Msg Obj except ID MASK bits
 376   1        CAN0ADR = IF1ARB1;    // Point to arbitration1 register
 377   1        CAN0DAT = 0x0000;     // Set arbitration1 ID to highest priority
 378   1        CAN0DAT = 0xA000+ID*4;     // Autoincrement to Arb2 high byte:
 379   1                              // Set MsgVal bit, no extended ID, Dir = WRITE
 380   1                                                              // 因为我们是主机,故发送到从机的广播消息处 仲裁设为 0
 381   1      
 382   1        CAN0DAT = 0x0088;     // Msg Cntrl: DLC = 8, remote frame function not enabled****************每帧1字节

⌨️ 快捷键说明

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