📄 mcdemo.c
字号:
/*
** Copyright (C)1999 KVASER AB, http://www.kvaser.com
** This code may be freely distrubuted and used if the source is indicated.
**
*/
#ifdef __WIN32__
#include <vcl.h>
#pragma hdrstop
#include <canlib.h>
#include "mainUnit.h"
#include "mcThread.h"
#include "..\inc\canUnit.h"
#include "util_w32.h"
#else
#include <string.h>
#include "..\inc\util_mc.h"
#include <pic.h>
#include <stdio.h>
#endif
#include "..\inc\mcdemo.h"
#include "..\inc\ck.h"
//---------------------------------------------------------------------------
#ifdef __WIN32__
#pragma package(smart_init)
#endif
/*
Available functions:
There are three transmit buffes in MCP2510, but only the first one is used.
If it is in use, a message may be dropped. This could be improved.
The two receive buffers are used as a short receive queue. The are polled,
should they both be full, received messages are dropped.
Document list. Document number and Folder number are identical and fixed
Receive documents
0 Kings Document DLC = 8
Page 0. Selects Action and Communication mode.
Page 1. Provides the Base numbers, asks for a Mayor's response.
Page 2. Assigns an Envelope to a Folder.
Page 9. Change the city address.
Page 16. Partial support, enable/disable a folder by number.
2 Parameter settings DLC = 8
Page 0
line 0: 0 (page number)
line 1..2: Period to transmit the Inputs document, 1..65536, 0 no transmissions
line 3..7: 0
3 LED control DLC = 1
line 0: Bit 0..1 controls the two LEDs (RXBF0, RXBF1)
4 Request Document Inputs DLC = 0
Transmit documents
1 Mayor's Document DLC = 8
Page 0
line 0: 0
line 1: 0 (page no)
line 2..6: EAN code (LSB..MSB)
line 7: 0
Page 1
line 0: 0
line 1: 1 (page no)
line 2..6: Serial number (LSB..MSB)
line 7: 0
5 Inputs DLC = 2 RTR always enabled
line 0: Bit 0..1 Current LED status
Bit 2..4 Buttons 0..2, current value
Bit 5..7 Buttons 0..2, sticky (cleared at Tx).
line 1: Analog value, 0..255
Optional receive documents (not implemented):
KP3. Assign a City to Groups
KP4. Removing a City from Groups
KP8. CAN bus parameter setting
KP9. Change of the City physical adress
KP16. Placing a Document in a folder
KP20. Setup filters
Optional transmit documents (not implemented):
MP128
line 0: 0
line 1: 128
line 2: No of dropped transmit documents
line 3: No of lost recieve documents (if possible to detect)
The Main program
Startup:
Turn off LEDs. Initiate analogue input, button inputs etc.
Initialize the CAN communication (Baud rate etc, set up one receive
and one transmit buffer, go on line).
Check for received CAN message.
If one, handle it. May result in a message being transmitted.
Poll the buttons.
Check if a periodic transmission should be done.
*/
// --- Global variables
BANK1 canIdT folder_env[FOLDER_COUNT]; // CAN id's. Bit 31 marks Extended CAN.
// We want to be able to disable a folder but keep the envelope, so we must use this
BANK1 uchar folder_enabledMask;
BANK1 canIdT baseNo = envInvalid; // Assigned envInvalid for an invalid base number.
BANK1 uchar nodeNo = 10;
BANK1 actModesT actMode = ckFreeze;
BANK1 commModesT commMode = ckSilent;
BANK1 unsigned int txPeriod = 0;
//int canWriteFailures = 0;
BANK1 unsigned int txLastTime; // Used for periodic transmissions
BANK1 uchar LEDsave = 0; // Saves which LEDs are active
BANK1 uchar buttonsSticky = 0;
// --- Protottypes
void ckInit(void);
void checkMsg(void);
void rxKP(uchar *buf);
void txMP(uchar pageNo);
void txInputs(void);
void rxParam(uchar *buf);
BANK1 uchar mpPending = 255;
void setFolderEnable(uchar fNo, uchar f) {
if (f)
folder_enabledMask |= (1 << fNo);
else
folder_enabledMask &= ~(1 << fNo);
}
uchar checkFolderEnable(uchar fNo) {
return folder_enabledMask & (1 << fNo);
}
/* Reset the module. Will never return.
*/
void doReset(void) {
setLEDs(0);
#ifdef __WIN32__
for(;;)
Sleep(100);
#else
#endif
}
/* Perform any action that should be done when Freeze mode is entered such
* as turning off all outputs etc.
* may be called also when the mode _is_ Freeze.
*/
void doFreeze(void) {
setLEDs(0);
}
/* Perform any action that should be done when Run mode is entered.
* may be called also when the mode _is_ Run.
*/
void doRun(void) {
setLEDs(LEDsave);
}
#ifdef __WIN32__
// The "main" function will be called by a thread
void mcMain(void)
#else
// The actual main function
void main(void)
#endif
{
uchar b;
setupModule();
//Test S4, S5, or S6, if pressed, run Test Procedure, else run Demo
// b = getButtons();
// if (b)
// {
// GIE = 0;
// TPmain(); //if button pressed, run Test Procedure
// GIE = 1;
// }
setLEDs(0);
canSetup();
ckInit();
#ifndef __WIN32__
mcp_read_all();
#endif
for (;;) {
checkMsg();
b = getButtons();
buttonsSticky |= b;
if (b == 0x04)
mcp_read_all();
#ifdef __WIN32__
// Give up the current time slice
Sleep(1);
#endif
if (txPeriod) {
unsigned int t = timerValue();
if ((t - txLastTime) > txPeriod) {
txInputs();
// We add txPeriod to txLastTime to get less jitter.
txLastTime += txPeriod;
// If we missed it too much, catch up. Otherwise, we would get a burst.
if ((t - txLastTime) > txPeriod)
txLastTime = t;
}
}
}
}
void fixMPEnv(void) {
if (baseNo == envInvalid)
folder_env[doc_MayorsDocument] = envInvalid;
else
folder_env[doc_MayorsDocument] = baseNo+nodeNo;
}
void ckInit(void) {
int i;
for (i = 0; i < FOLDER_COUNT; i++) {
folder_env[i] = envInvalid;
setFolderEnable(i, 0);
}
// Set up for the King's Page and the Mayor's Page.
setFolderEnable(doc_KingsDocument, 1);
folder_env[doc_KingsDocument] = 0;
setFolderEnable(doc_MayorsDocument, 1);
fixMPEnv();
#ifdef __WIN32__
mcThread->showCKStatus();
mcThread->showFolders();
mcThread->showModuleStatus();
#endif
}
/*
* Check if a message is received; if so, act on it.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -