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

📄 main.c

📁 this application program demonstrate programming and simulation of the on-chip CAN (controller area
💻 C
字号:
/*
 * Module uses CAN Driver functions
 */

#include <string.h>
#include <stdio.h>
#include <CAN1.H>

#define PERIOD     -2500                    /* 1 msec interrupt period      */

#include <REG167.h>			    // register include file for C167
#include <stdio.h>

// definition of LED outputs on Port2
struct port2pins  {
  unsigned int       :4;                    // unused
  unsigned int canLED:2;                    // LED D4 & D5
};

union port2  {
  unsigned int     i;
  struct port2pins v;
};

union port2 p2out;



char buffer[8];                         // buffer for receiving objects
char old_buffer[8] = {0x55} ;           // buffer for receiving objects
unsigned short analog [4];              // voltage on analog Pins AN0 .. AN3

static bit SendInfo;
static unsigned char IOinfo = 0xFF;


/*
 * Check UART for available char and get ASCII char from Serial Port
 *   return:   ASCII character read via serial UART
 *             0 if no char available is available
 */
unsigned char getkey (void) {
  unsigned char c;

  if (!S0RIR) return 0;
  c = S0RBUF;
  S0RIR = 0;
  return (c);
}

/*
 * Timer Interrupt Service Routine - used for time triggered function calls
 */
static void timer0(void) interrupt 0x20 using INTREGS {
  unsigned char i;

  for (i = 0; i != 4; i++)  {              /* loop for 4 A/D inputs       */
    ADCON = i;                             /* select A/D input,single conv.*/
    ADST = 1;                              /* start conversion            */
    while (ADBSY) { ; }                    /* wait for A/D result         */
    analog[i] = ADDAT & 0x03FF;            /* result of A/D process       */ 
  }
  CAN1SetRemote(6,(void *) analog[0]);          // update remote message with new A/D values 

}

/*
 * Print remote requested data
 */
static void PrintAnalogValues (unsigned short *v)  {
  unsigned char i;
  unsigned int  sv;

  for (i = 0; i < 4; i++)  {
    sv = ((*v) >> 8) | ((*v) << 8);
    printf ("AIN%bd=%4.2fV ", i, (float)(sv * 5.0) / 1024);
        v++;
  }
  printf ("\n");
}


/*
 * Send a string via the Can Interface
 */
static void SendString (char *s)  {  
  unsigned char len, i; 

  len = strlen (s) + 1;                      // length of the string to send
  while (len)  {
    if (len > 8)  i = sizeof (buffer);       // split the message into CAN size-compatible strings (8 Bytes) if bigger
    else          i = len;              
    memcpy (buffer, s, i);                   // copy to send buffer
    len -= i;
    s   += i;
    if (CAN1SendIsr (3, buffer) < 0) return; // send message
  }
}


/*
 * Read a string from the Can Interface 
 */
static void ReadString (void)  {
  unsigned char i;

  if (CAN1ReadIsr (4, buffer) < 0)  return; // no new message received by interrupt
  for (i = 0; i < 8; i++)  {
    if (!buffer[i])  return;                // no more info in receive buffer
    putchar (buffer[i]);                    // buffer out on serial port
  }
}


/*
 * main function
 */
void main(void)  {
  bit CanMessageReq = 0;                        // Remote Frame Requested
  unsigned char c;
 
#ifndef MCB167          // do not initialize if you use Monitor-166
  P3  |= 0x0400;        // SET PORT 3.10 OUTPUT LATCH (TXD)
  DP3 |= 0x0400;        // SET PORT 3.10 DIRECTION CONTROL (TXD OUTPUT)
  DP3 &= 0xF7FF;        // RESET PORT 3.11 DIRECTION CONTROL (RXD INPUT)
  S0TIC = 0x80;         // SET TRANSMIT INTERRUPT FLAG
  S0RIC = 0x00;         // DELETE RECEIVE INTERRUPT FLAG
  S0BG  = 0x40;         // SET BAUDRATE TO 9600 BAUD
  S0CON = 0x8011;       // SET SERIAL MODE
#endif

  // setup the timer 0 interrupt
  T0REL  = PERIOD;                              // set reload value 
  T0     = PERIOD;
  T0IC   = 0x44;                                // set T0IE and ILVL = 1
  IEN    = 1;                                   // set global interrupt enable flag
  T01CON = 0x40;                                // start timer 0
  
  DP2    = 0x01FF;                              // open P2 for LED output

  CAN1Init();                                   // initilize CAN module
  IEN  =  1;                                    // global interrupt enable

  while(1) {

    ReadString();                               // read CAN message received in interrupt

    c = getkey ();                              // read ASCII input from UART

    switch (c)  {
      case '1':                                 // ASCII input '1'
        SendString("This is a string send via CAN interrupt\n");
        break;

      case '2':                                 // ASCII input '2'
        if (CanMessageReq)  break;              // CAN remote frame already requested
        if (CAN1ReqRemote (5) == 0) {           // request CAN remote frame
          CanMessageReq = 1;                    // set marker
        }
        break;
    }

    if (c >= '0' && c <= '3')  {                // ASCII input '0' .. '3'
      IOinfo = (c - '0');                       // send new status LED message
      SendInfo = 1;                             // set flag to mark new LED status
    }

    if (SendInfo)  {
      if (CAN1Send (1, &IOinfo) == 0)  {        // transfer LED status
        SendInfo = 0;
      }
    }

    if (CAN1Read (2, buffer) == 0)  {           // output new info to LED
      p2out.v.canLED = buffer[0];
      P2 = p2out.i;
    }

    if (CanMessageReq)  {                       // CAN remote frame requested before?
      if (CAN1Read (5, buffer) == 0) {          // read requested remote frame
        CanMessageReq = 0;                      // remote frame is there
        PrintAnalogValues ((unsigned short *) buffer);
      }
    }
  }
} 

⌨️ 快捷键说明

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