📄 main.c
字号:
#include <absacc.h>
#include <string.h>
#include <stdio.h>
#include <REG51CC01.H>
#include "can_drv.h" // Interface to the CAN Functions
char buffer[8]; // buffer for receiving objects
unsigned short analog [4]; // voltage on analog Pins AN0 .. AN3
#define Port_D XBYTE [0xFFA0] // bit 0: switch S1
// bit 1: switch S2
// bit 4: LED D1
// bit 5: LED D2
#define DipSwitch XBYTE [0xFF80]
static unsigned char IOinfo = 0xFF;
static bit SendInfo;
/******************************************************************************/
/* Timer 0 interrupt service function */
/* executes each 1ms @ 20 MHz Crystal Clock */
/******************************************************************************/
#define INT_CLOCK 0.001 // timer interval 0.001Sec
#define XTAL 20000000
#define X2 0 // set to 1 if in X2 Mode
#define _CLOCKVAL ((unsigned int) ((XTAL/(12/(X2+1))) * INT_CLOCK))
#define T0_CLOCK ((-_CLOCKVAL)+13)
void timer0 (void) interrupt 1 using 1 { /* Int Vector at 000BH, Reg Bank 1 */
unsigned char i;
unsigned int v;
TR0 = 0; // stop timer
v = TL0 | (TH0>>8);
v += (unsigned int) (T0_CLOCK);
TL0 = (unsigned char) v;
TH0 = (unsigned char) (v >> 8);
TR0 = 1; // start timer
for (i = 0; i != 4; i++) { // loop for 4 A/D inputs
ADCON = (0x28 | i); // select A/D input
while (!(ADCON & 0x10)); // wait for A/D result
analog[i] = (ADDH<<2) | (ADDL); // result of A/D process
}
CanSetRemote(6, (void *) analog); // update remote message with new A/D values
}
/*
* 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 (CanSendIsr (2, buffer) < 0) return; // send message
}
}
/*
* 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 * 3.0) / 1024);
v++;
}
printf ("\n");
}
/*
* Read a string from the Can Interface
*/
static void ReadString (void) {
unsigned char i;
if (CanReadIsr (2, 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
}
}
/*
* Read Switches
*/
static void ReadIO (void) {
unsigned char newIOinfo;
newIOinfo = (Port_D & 0x3); // switches!
if (IOinfo != newIOinfo) { // if the information changed...
IOinfo = newIOinfo;
SendInfo = 1; // ..., give the permission to send the next time
}
}
/*
* main function
*/
void main (void) {
bit CanMessageReq = 0; // Remote Frame Requested
// init serial interface
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 213; /* TH1: reload value for 1200 baud @ 20MHz */
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
// ToDo take timer from RTX51 Tiny
#define PERIOD -500 /* 250 usec interrupt period */
/* setup the timer 0 interrupt */
TL0 = (unsigned char) (T0_CLOCK);
TH0 = (unsigned char) (T0_CLOCK >> 8);
TMOD = TMOD | 0x01; /* select mode 1 */
TR0 = 1; /* start timer 0 */
ET0 = 1; /* enable timer 0 interrupt */
/* setup ADC */
ADCF = 0x0F; /* open ports for the ADC (P1.0 - P1.3) */
ADCLK = 20/4; // set ADCLK
CanInit ((DipSwitch & 0x01) << 3);
EA = 1; // enable interrupts
while(1) {
if (CanRead (1, buffer) == 0) {
buffer[0] <<= 4; // switch to LED bit position
Port_D = (Port_D & ~0x30) | (buffer[0] & 0x30);
}
if (!SendInfo) { // old switch info processed?
ReadIO (); // read new values from switches
}
ReadString(); // read CAN message received in interrupt
if (SendInfo) { // switches have changed!
if (IOinfo & 0x01) { // switch S1 pressed
SendString("This is a string send via CAN interrupt\n");
}
if (!CanMessageReq && // no CAN remote frame requested
(IOinfo & 0x02)) { // switch S2 pressed
if (CanReqRemote (5) == 0) { // request CAN remote frame
CanMessageReq = 1; // set marker
}
}
if (CanSend (0, &IOinfo) == 0) { // transfer S1 & S2 status;
SendInfo = 0;
}
}
if (CanMessageReq) { // CAN remote frame requested before?
if (CanGetRemote (5, buffer) == 0) { // check & read requested remote frame
CanMessageReq = 0; // remote frame is there
PrintAnalogValues ((unsigned short *) buffer);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -