📄 util_mc.c.bak
字号:
/*
** Copyright (C)1999 KVASER AB, http://www.kvaser.com
** This code may be freely distrubuted and used if the source is indicated.
**
*/
#include "inc\std.h"
#include <pic.h>
#include "inc\async.h"
#include "inc\spi.h"
#include "inc\spi_mcp.h"
#include "inc\mcp2510.h"
#include <stdio.h>
#include "inc\util_mc.h"
#include "inc\ad.h"
#include "inc\main.h"
void timerSetup(void);
void analogueSetup(void);
void DelayF(unsigned int cnt)
{
int i;
while(cnt--)
for(i=0;i<100;i++);
}
void mcp_init_ports(void)
{
// Set RTS pins as inputs (used for the buttons)
SPI_mcp_write_bits(TXRTSCTRL, 0 , 0xFF);
// The two pins RX0BF and RX1BF are used to control two LEDs; set them as outputs and
// set them as 00.
SPI_mcp_write_bits(BFPCTRL, 0x3C, 0xFF );
}
void setupModule(void) {
TRISB = 0x00; // all bits output
TRISA = 0xFF; // all bits input
TRISC = 0x00;
PORTC = 0xFF;
PORTB = 0x00; // turn all on
async_Init();
printf("Module started\n\r");
SPI_init_hw();
mcpRST = 1;
DelayF(100);
mcpRST = 0;
DelayF(100);
mcpRST = 1;
DelayF(200);
mcp_reset();
//xjz modi
DelayF(200);
mcp_init();
mcp_init_ports();
timerSetup();
analogueSetup();
}
// --------------------------------------------------------------------
void analogueSetup(void) {
TRISA0 = 1; // Set as input
// The ADCON1 register
// - - - - - [00000]
// PCFG2-0 A/D Port Configuration Control bits, [000] for A/A/A/A/A/A/A/A/Vdd
ADCON1 = 0x00;
// The ADCON0 register
// ADCS10 A/D Conversion Clock Select, [00] for FOSC/2
// CHS2-0 Analog Channel Select bits, [000] for channel 0, (RA0/AN0)
// GO/DONE*, - [00]
// ADON A/D On bit, [1] for A/D converter module is operating
ADCON0 = 0x41; // 00 000 00 1
}
// Read the analogue value.
// We assume that enough time has gone since the previous conversion so the capacitior is loaded.
// (the minimum acquisition time is some 5-10 us). The conversion time is also a few us.
// We wait for it; it only takes one loop.
int getAnalogue(void) {
uint c = 0xffff;
ADGO = 1;
while (ADGO)
if (--c == 0)
return -1;
return ADRESH;
}
// --------------------------------------------------------------------
// --------------------------------------------------------------------
static BANK1 uint tc;
// Setup timer0 to generate interrupts 1000 times per second.
// Using the prescaler value 4 works for the EVB.
void timerSetup(void) {
tc = 0;
SecondFlag = 0;
T0CS = 0; //选择内部时钟作为时钟源(CLKOUT)
PSA = 0; //预分频器分配到TIMER0
PS2 = 1; PS1 = 1; PS0 = 1; // Prescaler = 256 (111) gives tick freq 15 Hz, presc 4 (001) gives 960 Hz.
TMR0=256-4;//装入1MS定时值
T0IE = 1; //TMR0 中断允许
T0IF = 0; //TMR0 中断清除位
GIE = 1; //允许所有非屏蔽中断
}
// Returns the current time in ms. Will wrap at 0xffff, must be "complete",
// i.e., wrap from 0xffff to 0x0.
unsigned int timerValue(void) {
static bank1 unsigned int dummy;
dummy = tc;
return tc;
}
// --------------------------------------------------------------------
// Setup the CAN buffers used by the application.
// We currently use only one for reception and one for transmission.
// It is possible to use several to get a simple form of queue.
//
// We setup the unit to receive all CAN messages.
// As we only have at most 4 different messages to receive, we could use the
// filters to select them for us.
//
// mcp_init() should already have been called.
canStatus canSetup(void) {
// As no filters are active, all messages will be stored in RXB0 only if
// no roll-over is active. We want to recieve all CAN messages (standard and extended)
// (RXM<1:0> = 11).
//SPI_mcp_write_bits(RXB0CTRL, RXB_RX_ANY, 0xFF);
//SPI_mcp_write_bits(RXB1CTRL, RXB_RX_ANY, 0xFF);
// But there is a bug in the chip, so we have to activate roll-over.
SPI_mcp_write_bits(RXB0CTRL, (RXB_BUKT+RXB_RX_ANY), 0xFF);
SPI_mcp_write_bits(RXB1CTRL, RXB_RX_ANY, 0xFF);
return canOK;
}
// Write a CAN message. We use only one buffer, i.e. no queueing, and don't
// check if the buffer is available.
canStatus canWrite(ulong id, const uchar *msg, unsigned char dlc, unsigned int flag) {
mcp_write_can(1, flag & canMSG_EXT, id, dlc, flag & canMSG_RTR, msg);
mcp_transmit(TXB0CTRL);
return canOK;
}
// Test if there is a message pending; if so, read it.
// We check RXB0 first, and if it is empty, RXB1.
// This simple approach may cause the order to be wrong.
// A better way would be to check RXB1 after a message was read from RXB0, and if there is
// a message in RXB1 mark a flag. If on the next call this flag is set, read RXB1 instead of
// RXB0. We know that there will always ne a message in RXB0 before one ends up in RXB1.
//
// Return values: canOK There was a message
// canERR_NOMSG Nothing to be read
BANK1 static uchar crReadB1 = 0;
canStatus canRead(ulong *id, uchar *msg, unsigned char *dlc, unsigned int *flag) {
uchar msgF, extF;
uchar byte, rtr;
msgF = 0;
mcp_read(CANINTF, &byte, 1);
if (crReadB1) {
if (byte & RX1INT) {
mcp_read_can(5, &extF, id, dlc, &rtr, msg);
SPI_mcp_write_bits(CANINTF, ~RX1INT, RX1INT); // Clear interrupt
msgF = 1;
} //else
// printf("<CRerr>\n");
crReadB1 = 0;
}
if (byte & RX0INT) {
mcp_read_can(4, &extF, id, dlc, &rtr, msg);
SPI_mcp_write_bits(CANINTF, ~RX0INT, RX0INT); // Clear interrupt
msgF = 1;
mcp_read(CANINTF, &byte, 1);
if (byte & RX1INT)
crReadB1 = 1;
}
if (msgF) {
if (extF)
*flag = canMSG_EXT;
else
*flag = canMSG_STD;
if (rtr) {
// printf("<RTR>\n\r");
*flag |= canMSG_RTR;
}
return canOK;
} else
return canERR_NOMSG;
}
// Not implemented
void canSetCommMode(commModesT commMode) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -