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

📄 app01.c

📁 基于p87c591的can通讯驱动程序源码
💻 C
字号:
/*======================================================================*/
/* NAME:    app01.c                                                     */
/* INFO:    Application 01 using the r591lib files and the              */
/*          PHYTEC training board equipped with                         */
/*          PHYTEC phyCORE 591 and Philips P8x591 Microcontroller       */
/*          DIP switches and potentiometer may be changed while running */
/*          the application                                             */
/* RIGHTS:  Embedded Systems Academy   www.esacademy.com                */
/* DETAILS:  o Set CAN bus for 125kBit, receive all messages with       */
/*             priority < 0x100                                         */
/*           o Toggle LED with each incoming CAN message of             */
/*             priority < 0x100                                         */
/*           o Use lower 4 bits of dip switches to set a receive filter */
/*             on all messages with ID = 0x400 + (4-bit awitch value)   */
/*           o Use higher 4 bits of dip switches to set the transmit ID */
/*             of messages send: ID = 0x400 + (4-bit awitch value)      */
/*           o Transmit poti value in byte 1 of CAN transmit message    */
/*           o Display received analog value on LED D1                  */
/*----------------------------------------------------------------------*/
/* HISTORY: V1.0     Pf    21-FEB-2000                                  */
/*          V1.1     Pf    16-JUN-2000                                  */
/*          V1.2     AA    17-JUL-2000                                  */
/*======================================================================*/

#include "reg591.h"
#include "r591io.h"
#include "r591can.h"

BYTE led_status; // needed to implement a toggle LEDs function
extern WORD timer1tick;

/*======================================================================*/
/* FUNCTION:   toggle_led                                               */
/* DESCRIPTION:Toggles both LEDs onn the Phytec board, using the        */
/*             function switch_leds from "591io". If LED D1 is used     */
/*             in dimmed mode, that will NOT be toggled                 */
/* INPUT:      none                                                     */
/* OUTPUT:     none                                                     */
/*======================================================================*/
void toggle_led (void)
{
   if (led_status == 0)
      led_status = 0x03;
   else
      led_status = 0x00;
   switch_leds(led_status);
}


/*======================================================================*/
/* FUNCTION:   main                                                     */
/* DESCRIPTION:See application description above                        */
/* INPUT:      none                                                     */
/* OUTPUT:     none                                                     */
/*======================================================================*/
void main (void)
{
   BYTE dip;               // value of dip switches
   WORD bright;            // desired LED D1 brightness
   WORD id_rx, id_tx;      // CAN IDs of received and trasmitted message
   MSG_BUF can;            // Message buffer for received or transmitted msg

   switch_leds(0);                              // switch LEDs off

   init_timer1(45535);                          // reload for 10ms timer ticks
/* To get specific reload values calculated (for example to achieve a   */
/* 5ms interrupt), use the Microcontroller Peripheral Timing Calculator */
/* provided by the Embedded Systems Academy at                          */
/* www.esacademy.com/faq/calc/philips.htm                               */

   init_timer0_dim_d1();                        // init LEDs
   set_brightness_d1(0);                        // set brightness of dimmed LED to 0

   init_can_125_12();                           // init CAN interface
/* To get BTRx timing values for other speeds, consult the              */
/* Microcontroller Peripheral Timing Calculators provided by the        */
/* Embedded Systems Academy at www.esacademy.com/faq/calc/philips.htm   */

   // Screener 1: Receive everything with priority < 0x100
   set_screener_std(1,0x00FF,0x00FF,0xFF,0xFF,0xFF,0xFF);

   EA = 1; // End of initialization, Enable all interrupts

   dip = read_dip_switches();             // read dip switches
   id_rx = (dip >> 4) + 0x400;            // high nibble for CAN receive ID
   id_tx = (dip & 0x0F) + 0x400;          // low nibble for CAN transmit ID

   // Screener 2: Receive message id_rx only
   set_screener_std(2,0x0000,id_rx,0xFF,0xFF,0xFF,0xFF);

   while(1)
   {
      if(check_busoff_state())               // CAN module still running?
         error_state(3); // No -> Error state 3

      if (receive_message(&can))             // message received?
      {
         if (can.ID <= 0x00FF)               // ID < 0x100?
            toggle_led();                    // toggle LED D2
         else
            if (can.ID == id_rx)             // matching ID?
            { // receive msg and use contents to dim LED D1
               bright = can.BUF[0];
               bright = bright * 4 / 10;     // convert 0-255 to a value 0-102 
               set_brightness_d1((BYTE)bright);
            }
            else
               error_state(4);               // unknown msg received
      } // end if


      if (timer1tick >= 10) // after 100ms has passed, do
      {
         timer1tick = 0;
         // Transmit message with id_tx and poti value
         can.ID = id_tx;                           // set CAN identifier
         can.LEN = 1;                              // set data length (1 byte)
         can.BUF[0] = read_poti();                 // get value for potentiometer
         transmit_message(&can);                   // transmit the message
      } // end while timer1tick < 10
   } // end of while(1)
}

⌨️ 快捷键说明

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