?? can232.c
字號:
#pragma REGPARMS
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "c8051f020.h"
#include "can20.h"
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SYSCLK 22118400 // SYSCLK frequency in Hz
unsigned char SendBuffer[13];
unsigned char GetBuffer[13];
unsigned char CommBuffer[15];
unsigned char CommP;
int CommRP,CommRCount;
int Can0RP,Can0RCount;
int Can0TP;
int CommTP;
unsigned char xdata CommRBuffer[1024];
//unsigned char xdata CommRBuffer[1024]={0x08,0x55,0x00,0x11,0x22,0x33,0x55,0x44,0x360,0x44,0x36};
unsigned char xdata Can0RBuffer[1024];
void config (void) {
//Local Variable Definitions
WDTCN = 0x07; // Watchdog Timer Control Register
WDTCN = 0xDE; // Disable WDT
WDTCN = 0xAD;
XBR0 = 0xEF; // XBAR0: Initial Reset Value
XBR1 = 0x07; // XBAR1: Initial Reset Value
XBR2 = 0x44; // XBAR2: Initial Reset Value
// Port configuration (1 = Push Pull Output)
P0MDOUT = 0x1D; // Output configuration for P0
P1MDOUT = 0x01; // Output configuration for P1
P2MDOUT = 0x03; // Output configuration for P2
P3MDOUT = 0x00; // Output configuration for P3
P74OUT = 0xff; // Output configuration for P4-7
P1MDIN = 0xFF; // Input configuration for P1
// View port pinout
// The current Crossbar configuration results in the
// following port pinout assignment:
// Port 0
// P0.0 = UART TX0 (Push-Pull Output)
// P0.1 = UART RX0 (Open-Drain Output/Input)
// P0.2 = SPI Bus SCK (Push-Pull Output)
// P0.3 = SPI Bus MISO (Push-Pull Output)
// P0.4 = SPI Bus MOSI (Push-Pull Output)
// P0.5 = SPI Bus NSS (Open-Drain Output/Input)
// P0.6 = SMBus SDA (Open-Drain Output/Input)
// P0.7 = SMBus SCL (Open-Drain Output/Input)
// Port 1
// P1.0 = UART TX1 (Push-Pull Output)(Digital)
// P1.1 = UART RX1 (Open-Drain Output/Input)(Digital)
// P1.2 = PCA CEX0 (Open-Drain Output/Input)(Digital)
// P1.3 = PCA CEX1 (Open-Drain Output/Input)(Digital)
// P1.4 = PCA CEX2 (Open-Drain Output/Input)(Digital)
// P1.5 = PCA CEX3 (Open-Drain Output/Input)(Digital)
// P1.6 = PCA CEX4 (Open-Drain Output/Input)(Digital)
// P1.7 = PCA ECI (Open-Drain Output/Input)(Digital)
// Port 2
// P2.0 = CP0 (Push-Pull Output)
// P2.1 = CP1 (Push-Pull Output)
// P2.2 = T0 (Open-Drain Output/Input)
// P2.3 = /INT0 (Open-Drain Output/Input)
// P2.4 = GP I/O (Open-Drain Output/Input)
// P2.5 = GP I/O (Open-Drain Output/Input)
// P2.6 = GP I/O (Open-Drain Output/Input)
// P2.7 = GP I/O (Open-Drain Output/Input)
// Port 3
// P3.0 = GP I/O (Open-Drain Output/Input)
// P3.1 = GP I/O (Open-Drain Output/Input)
// P3.2 = GP I/O (Open-Drain Output/Input)
// P3.3 = GP I/O (Open-Drain Output/Input)
// P3.4 = GP I/O (Open-Drain Output/Input)
// P3.5 = GP I/O (Open-Drain Output/Input)
// P3.6 = GP I/O (Open-Drain Output/Input)
// P3.7 = GP I/O (Open-Drain Output/Input)
EMI0CF = 0x24; // External Memory Configuration Register
EMI0CN=0x00;
EMI0TC=0x01;
}
void SYSCLK_Init (void)
{
int data i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
void UART0_Init (void)
{
SCON0 = 0x50; // SCON0: T1 mode 1, 8-bit UART, enable RX
TMOD = 0x21; // TMOD: timer 1, mode 2, 8-bit reload
// T0 16 bit Timer
T2CON &= 0x0cf; //select T1 as baudrate
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD00 = 1
ES0=1;
}
unsigned char Can_Init(void)
{
unsigned char tt;
unsigned int i;
P3=0xff;
for(i=0;i<1000;i++);
P3=0x00;
for(i=0;i<1000;i++);
P3=0xff;
for(i=0;i<1000;i++); //ModeControlReg= RM_RR_Bit;改動
ModeControlReg= RM_RR_Bit|STM_Bit;
tt=ModeControlReg;
ClockDivideReg=0x88;
InterruptEnReg=0xff;
BusTiming0Reg= 0x03; //100 K
BusTiming1Reg=0x2f;
AcceptCode0Reg=0x57;
AcceptCode1Reg=0x00;
AcceptCode2Reg=0xaa;
AcceptCode3Reg=0x55;
AccepMask0Reg=0xff;
AccepMask1Reg=0xff;
AccepMask2Reg=0xff;
AccepMask3Reg=0xff;
RxBufstartAdr=0;
TxErrCountReg=0;
RxErrCountReg=0;
ErrCodeCapReg=0;
OutControlReg=0x1a;
ModeControlReg=AFM_Bit;
tt=StatusReg;
while(tt==0x10)
tt=StatusReg;
return tt;
}
void WriteCan()
{
unsigned char * xdata tt;
unsigned int i;
unsigned char tt1,count;
count=(CommRBuffer[CommTP]&0x0f);
if((CommRBuffer[CommTP]&0x80)==0x00)
count+=3;
else
count+=5;
tt1=StatusReg;
if((tt1&RS_Bit)!=0x00)return;
tt1=StatusReg;
if((tt1&TCS_Bit)==0x00)return;
tt=&TxFramInFo;
for(i=0;i<count;i++)
{SendBuffer[i]=CommRBuffer[CommTP++];
if(CommTP==1024)CommTP=0;
}
//EA=0;
for(i=0;i<count;i++)
{*tt=SendBuffer[i];
tt++;
}
CommandReg=TR_Bit;
//EA=1;
CommRCount-=count;
return ;
}
void COMM1_ISR (void) interrupt 4
{
unsigned char i;
unsigned char count;
if(RI0==0x01)
{RI0=0;
CommBuffer[CommP]=SBUF0;
CommP++;
if(CommBuffer[0]!=0xaa)
{CommP=0;
return;
}
if(CommP>2)
{count=(CommBuffer[2]&0x0f);
if((CommBuffer[2]&0x80)==0)
count+=3;
else
count+=5;
if(CommBuffer[1]!=0x55)
{CommP=0;
return;
}
}
else
return;
if(CommP==(count+2))
{
CommP=0;
if((CommRCount+count)<1024)
{for(i=0;i<count;i++)
{CommRBuffer[CommRP++]=CommBuffer[2+i];
if(CommRP==1024)
CommRP=0;
}
CommRCount+=count;
/* SBUF0=0xaa;
while(TI0==0);
TI0=0;
SBUF0=0x55;
while(TI0==0);
TI0=0;*/
}
else
{
i=100;
}
}
}
}
void INTE0_ISR (void) interrupt 0
{
unsigned char tt1;
unsigned char * xdata tt;
int i,count;
tt1=InterruptReg;
if((tt1&0x04)!=0)
{
tt1=StatusReg;
if((tt1&0x80)!=0)
ModeControlReg=AFM_Bit;
return;
}
if((tt1&0x08)!=0)
{CommandReg=0x0c;
return;
}
if((tt1&0x01)!=0)
{tt=&RxFramInFo;
count=(*tt&0x0f);
if((*tt&0x80)==0)
count+=3;
else
count+=5;
for(i=0;i<count;i++)
{GetBuffer[i]=*tt;
tt++;
}
if((Can0RCount+count)<1024)
{Can0RCount+=count;
for(i=0;i<count;i++)
{Can0RBuffer[Can0RP++]=GetBuffer[i];
if(Can0RP==1024)Can0RP=0;
}
}
CommandReg=RRB_Bit;
}
tt1=ArbLostCapReg;
tt1=ErrCodeCapReg;
return;
}
void Write232()
{
int i,count;
count=(Can0RBuffer[Can0TP]&0x0f);
if((Can0RBuffer[Can0TP]&0x80)==0)
count+=3;
else
count+=5;
SBUF0=0xaa;
while(TI0==0);
TI0=0;
SBUF0=0x55;
while(TI0==0);
TI0=0;
for(i=0;i<count;i++)
{SBUF0=Can0RBuffer[Can0TP++];
if(Can0TP==1024)Can0TP=0;
while(TI0==0);
TI0=0;
}
Can0RCount-=count;
}
/*void Write232a()
{
unsigned int i,count;
count=(CommRBuffer[CommTP]&0x0f);
if((CommRBuffer[CommTP]&0x80)==0)
count+=3;
else
count+=5;
SBUF0=0xaa;
while(TI0==0);
TI0=0;
SBUF0=0x55;
while(TI0==0);
TI0=0;
for(i=0;i<count;i++)
{SBUF0=CommRBuffer[CommTP++];
if(CommTP==100)CommTP=0;
while(TI0==0);
TI0=0;
}
CommRCount-=count;
}
*/
main()
{
// unsigned char tt1;
SYSCLK_Init();
config();
UART0_Init();
Can_Init();
EA=1;
EX0=1;
IP=0x01;
CommP=0;
CommRP=0;
CommRCount=0;
CommTP=0;
Can0RP=0;
Can0RCount=0;
Can0TP=0;
while(1)
{if(CommRP!=CommTP)
//Write232a();
WriteCan();
if(Can0RP!=Can0TP)
Write232();
// tt1=StatusReg;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -