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

📄 main.c

📁 CAN Example(MC9S12XDP512)
💻 C
字号:
//////////////////////////////////////////////////////////////////////////////
//
// Sample for SofTec Microsystems SK-S12XDP512-A Starter Kit
// (Freescale code: EVB9S12XDP512)
//
// This program sends the potentiometer value from MSCAN0 to MSCAN1 and
// displays it on the two dot-matrix displays, in a graphic way.
//
// A CAN cable must connect the CAN0 node to CAN1 node.
//
// Before to run this example, verify that all jumpers are in their default
// position.
//
// See the "Summary of Jumper and Connector Settings" chapter in
// the user's manual.
//
// NOTE: This example serves the only purpose of setting the CAN peripheral
// correctly, and is not intended to be a starting point for a real-world 
// CAN application.
//
// ---------------------------------------------------------------------------
//
// Copyright (c) 2005 SofTec Microsystems
// http://www.softecmicro.com/
//
//////////////////////////////////////////////////////////////////////////////

#include <hidef.h>
#include "mc9s12xdp512.h"
#include "mscan.h"
#pragma LINK_INFO DERIVATIVE "mc9s12xdp512"

//////////////////////////////////////////////////////////////////////////////
// Defines and variables
//////////////////////////////////////////////////////////////////////////////

#define NUM_COLS            10
#define CAN_MSG_ID          1

unsigned char LED_display_col;
unsigned char LED_matrix_data[NUM_COLS];
unsigned char potentiometer_value;
unsigned char can_delay = 10;

const unsigned char light_table[][5] = {
    0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x08,
    0x00, 0x00, 0x00, 0x08, 0x1C,
    0x00, 0x00, 0x08, 0x1C, 0x3E,
    0x00, 0x08, 0x1C, 0x3E, 0x7F,
    0x08, 0x1C, 0x3E, 0x7F, 0x7F,
    0x1C, 0x3E, 0x7F, 0x7F, 0x7F,
    0x3E, 0x7F, 0x7F, 0x7F, 0x7F,
    0x7F, 0x7F, 0x7F, 0x7F, 0x7F
};

//////////////////////////////////////////////////////////////////////////////
// Peripheral Initialization
//////////////////////////////////////////////////////////////////////////////

void PeriphInit(void)
{
    // Configures PA[6..0] port as output
    PORTA = 0x7F;
    DDRA = 0x7F;

    // Configures PB[3..0] as output and PB[7..4] as input
    PORTB = 0x00;
    DDRB = 0x0F;

    // Enables pull-ups on PB port
    PUCR |= 0x02;

    // Configures PC[4..0] port as output
    PORTC = 0x00;
    DDRC = 0x1F;

    // Configures PD[4..0] port as output
    PORTD = 0x00;
    DDRD = 0x1F;

    // Configures the ATD peripheral
    // (16 conversions per sequence, 8 bit resolution, wrap around channel, continuous conversion)
    ATD1CTL3 = 0x38;
    ATD1CTL4 = 0x80;
    ATD1CTL0 = 0x05;
    ATD1CTL2 = 0x80;
    ATD1CTL5 = 0x32;

    // Configures the PIT (Periodic Interrupt Timer) to generate a periodic interrupt of 500us
    // (Interrupt on channel 0)
    PITCE = 0x01;
    PITINTE = 0x01;
    PITLD0 = 1000;
    PITCFLMT = 0xA0;

    MSCANInit(MSCAN_0);
    MSCANInit(MSCAN_1);

    EnableInterrupts;
}

//////////////////////////////////////////////////////////////////////////////
// Outputs a graphical pattern on the displays
//////////////////////////////////////////////////////////////////////////////

void disp_light_pattern(unsigned char value)
{
    unsigned char i;

    for(i=0; i<5; i++)
    {
        LED_matrix_data[i] = light_table[value][i];
        LED_matrix_data[5 + i] = light_table[value][5 - i - 1];
    }
}

//////////////////////////////////////////////////////////////////////////////
// Entry point
//////////////////////////////////////////////////////////////////////////////

void main(void)
{
    struct can_msg msg_send, msg_get;
    
    PeriphInit();
    
    for(;;)
    {
        // Reads the ADC channels
        while(!(ATD1STAT0 & 0x80))
             ;

      	potentiometer_value = ATD1DR0H;

        // Resets SCF flag
        ATD1STAT0 = 0x80;

        if(!can_delay)
        {
            msg_send.id = CAN_MSG_ID;
            msg_send.data[0] = (unsigned char)(potentiometer_value);
            msg_send.len = 1;
            msg_send.RTR = FALSE;
            msg_send.prty = 0;
            (void)MSCANSendMsg(MSCAN_0, msg_send);
            can_delay = 10;
        }

        // Checks if a message is received from MSCAN1
        if(MSCANCheckRcvdMsg(MSCAN_1))
        {
            if(MSCANGetMsg(MSCAN_1, &msg_get))
            {
                if(msg_get.id == CAN_MSG_ID && msg_get.RTR == FALSE)
                    disp_light_pattern((unsigned char)(msg_get.data[0] * 9 / 256));
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////////
// PIT0 Interrupt Service Routine
////////////////////////////////////////////////////////////////////////////

#pragma CODE_SEG __NEAR_SEG NON_BANKED

interrupt void PIT0_ISR(void)
{
    PORTC = 0xFF;
    PORTD = 0xFF;
    PORTA = LED_matrix_data[NUM_COLS - LED_display_col - 1];

    if(LED_display_col < 5)
        PORTC = ~(0x01 << LED_display_col);
    else
        PORTD = ~(0x01 << (LED_display_col - 5));

    if(++LED_display_col >= NUM_COLS)
        LED_display_col = 0;

    --can_delay;

    PITTF = 0x01;
}

#pragma CODE_SEG DEFAULT

⌨️ 快捷键说明

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