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

📄 main_lpc2.c

📁 MicoCANOpen很好的学习源代码
💻 C
字号:
/**************************************************************************
MODULE:    MAIN
CONTAINS:  Example application using MicroCANopen
           NXP LPC2000 derivatives with CAN interface.
           Compiled and Tested with Keil Tools www.keil.com
COPYRIGHT: Embedded Systems Academy, Inc. 2002-2007.
           All rights reserved. www.microcanopen.com
           This software was written in accordance to the guidelines at
           www.esacademy.com/software/softwarestyleguide.pdf
DISCLAIM:  Read and understand our disclaimer before using this code!
           www.esacademy.com/disclaim.htm
LICENSE:   THIS IS THE EDUCATIONAL VERSION OF MICROCANOPEN
           See file license_educational.txt or
           www.microcanopen.com/license_educational.txt
           A commercial MicroCANopen license is available at
           www.CANopenStore.com
VERSION:   3.30, ESA 30-JAN-07
           $LastChangedDate: 2007-01-30 10:38:13 -0800 (Tue, 30 Jan 2007) $
           $LastChangedRevision: 232 $
***************************************************************************/ 

#include "mco.h"
#include <LPC21XX.H> // LPC21XX Peripheral Registers

// external declaration for the process image array
extern UNSIGNED8 MEM_NEAR gProcImg[];

/**************************************************************************
DOES:    Execute an A to D conversion
RETURNS: Converted value
**************************************************************************/
UNSIGNED32 read_poti(
  void
  ) 
{    
  UNSIGNED32 val;

  ADCR |= 0x01000000; // Start A/D Conversion

  do 
  {
    val = ADDR; // Read A/D Data Register
  } 
  while ((val & 0x80000000) == 0); // Wait for end of A/D Conversion

  ADCR &= ~0x01000000; // Stop A/D Conversion
  val = (val >> 6) & 0x03FF; // Extract AIN0 Value

  return val;
}


/**************************************************************************
DOES:    The main function
RETURNS: nothing
**************************************************************************/
int main(
  void
  )
{
UNSIGNED32 pot;

  // No divider: peripheral clock = processor clock
  VPBDIV = 1; 
    
  // Init Vector Interrupt Controller
  VICIntEnClr = 0xFFFFFFFF; // Disable all Ints
  VICIntSelect = 0x00000000;

  IODIR1 = 0x00FF0000; // P1.16..23 defined as Outputs
  ADCR   = 0x00270401; // Setup A/D: 10-bit AIN0 @ 3MHz 

  // Reset/Initialize CANopen communication
  MCOUSER_ResetCommunication();

  // foreground loop
  while(1)
  {
    // Update process data
    // Echo all data
    gProcImg[P600001_DIGINPUT8_] = gProcImg[P620001_DIGOUTPUT8_];
    gProcImg[P600001_DIGINPUT8_] = gProcImg[P620002_DIGOUTPUT8_];
    gProcImg[P600001_DIGINPUT8_] = gProcImg[P620003_DIGOUTPUT8_];
    gProcImg[P600001_DIGINPUT8_] = gProcImg[P620004_DIGOUTPUT8_];

    // first analog input is real I/O
    pot = read_poti();
    gProcImg[P640101_ANALOGINPUT16_]   = pot & 0x000000FF; // lo byte
    gProcImg[P640101_ANALOGINPUT16_+1] = (pot >> 8) & 0x000000FF; // hi byte

    // echo all other I/O values from input to output
    // analog
    gProcImg[P640102_ANALOGINPUT16_]   = gProcImg[P641102_ANALOGOUTPUT16_];
    gProcImg[P640102_ANALOGINPUT16_+1] = gProcImg[P641102_ANALOGOUTPUT16_+1];

    // Operate on CANopen protocol stack
    MCO_ProcessStack();
  } // end of while(1)
} // end of main

⌨️ 快捷键说明

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