如何在12864按键翻页中显示得到的CAN总线传输的刷新数据
时间:10-02
整理:3721RD
点击:
在通过参数转换函数得到CAN总线传输到的数值,想把不停刷新的数值显示在12864上,程序中有按键函数,实现12864两页的切换,这两页中都有数值接收显示,但是我每次主函数进入按键函数切换12864的界面时,数据就不能实时更新,因为显示函数在按键函数里。求大神解决方法。程序如下:
#include "userdefines.h"
//#include "can.h"
_FBS(BWRP_WRPROTECT_OFF & BSS_NO_FLASH & RBS_NO_RAM); //f80000
_FSS(SWRP_WRPROTECT_OFF & SSS_NO_FLASH & RSS_NO_RAM); //f80002
_FGS(GCP_OFF & GWRP_OFF & GSS_OFF); //f80004
_FOSCSEL(FNOSC_PRIPLL & IESO_ON); //f80006
_FOSC(POSCMD_XT& OSCIOFNC_OFF & IOL1WAY_OFF & FCKSM_CSECMD); //f80008
_FWDT(WDTPOST_PS32768 & WDTPRE_PR128 & WINDIS_OFF & FWDTEN_OFF); //f8000a
_FPOR(FPWRT_PWR128 & ALTI2C_OFF & LPOL_OFF & HPOL_OFF & PWMPIN_ON); //f8000c
_FICD( ICS_PGD3 & JTAGEN_OFF); //f8000e
#define E PORTBbits.RB13
#define RW PORTAbits.RA10
#define RS PORTAbits.RA7
#define RST PORTBbits.RB8
#define Key_enter PORTAbits.RA0
#define Key_mode PORTAbits.RA1
#define Key_down PORTBbits.RB0
#define Key_up PORTBbits.RB1
#define Key_blank PORTBbits.RB15
#define ON_OFF PORTBbits.RC0
#define Normal PORTBbits.RB3
#define Error PORTBbits.RB2
#define STB LATBbits.LATB4
#define uchar unsigned char
#define uint unsigned int
#define D0 PORTBbits.RB12
#define D1 PORTBbits.RB11
#define D2 PORTBbits.RB10
#define D3 PORTCbits.RC9
#define D4 PORTCbits.RC8
#define D5 PORTCbits.RC7
#define D6 PORTCbits.RC6
#define D7 PORTBbits.RB9
uchar kaiji1[]=("欢迎来到");
uchar kaiji2[]=("机电学院");
uchar canshu1[]=("设定转速0 0 0 0");
uchar canshu2[]=("当前转速: ");
uchar canshu3[]=("设置功率0 0 . 0");
uchar canshu4[]=("母线电压: V");
uchar canshu5[]=("母线电流: A");
uchar canshu6[]=(" 相电流: A");
#define ATC_CAN_ID 0X34C
#define ACP_CAN_ID 0X4A0
#define HCU_CAN_ID 0X4A2
#define BC1_CAN_ID 0X5B3
#define BC2_CAN_ID 0X533
#define ECAN_MSG_BUF_LENGTH 4
#define FCAN 40000000 // FCAN = Fcy = 40MHz
#define BITRATE 500000 // Baude Rate = 500kHz
#define NTQ 20 // 20 Time Quanta in a Bit Time
#define CAN_BRP_VAL ((FCAN/(2*NTQ*BITRATE))-1) // 40MHz/(500kHz*2)]-1 = 39 BRP_VAL should be 0~63
void can_init(void);
void ecanClkInit(void);
void ecanWriteRxAcptFilter(int n, long identifier, unsigned int exide, unsigned int bufPnt,unsigned int maskSel);
void ecanWriteRxAcptMask(int m, long identifier, unsigned int mide, unsigned int exide);
void dma0init(void);
void dma2init(void);
void ecanWriteTxMsgBufId(unsigned int buf, long txIdentifier, unsigned int ide, unsigned int remoteTransmit);
void ecanWriteTxMsgBufData(unsigned int buf, unsigned int dataLength, unsigned int data1, unsigned int data2, unsigned int data3, unsigned int data4);
void ecanWriteMessage(unsigned int data1,unsigned int data2,unsigned int data3,unsigned int data4);
void C1_P(void);
unsigned int can_msgBuf[ECAN_MSG_BUF_LENGTH][8] __attribute__((space(dma),aligned(ECAN_MSG_BUF_LENGTH*16)));
unsigned int can_msgBuf_r[ECAN_MSG_BUF_LENGTH][8] __attribute__((space(dma),aligned(ECAN_MSG_BUF_LENGTH*16)));
unsigned int ACP_10=0;
unsigned int ACP_11=0;
unsigned int ACP_12=0;
unsigned int ACP_13=0;
unsigned int ACP_14=0;
unsigned int ACP_15=0;
unsigned int ACPSpdFeedback=0;
unsigned int ACPDCCurrent=0;
unsigned int ACPDCVoltage=0;
unsigned char result1[4]={0,0,0,0};
unsigned char result2[2]={0,0};
uchar Key_num1;
uchar Key_num;
void System_Colck(void);
void configio(void);
void lcd_init(void);
void chk_busy();
void write_cmd(unsigned char c);
void write_data(unsigned char c);
void get_canshu1(void);
void get_canshu2(unsigned int ACPDC);
void display(void);
void display1(void);
void display2(void);
void yidong(void);
void scan_key();
void main ()
{
System_Colck();
configio();
can_init();
lcd_init();
delay_ms (100);//延迟8ms
display();
delay_s(3000);
write_cmd(0x01);
delay_ms (100);//延迟8ms
display1();
while(1)
{
//display1();
get_canshu1();
get_canshu2(ACPDCVoltage);
get_canshu2(ACPDCCurrent);
scan_key();
}
}
void LED_DAT (uchar d)
{
D0=d&0x01;
D1=(d&0x02)>>1;
D2=(d&0x04)>>2;
D3=(d&0x08)>>3;
D4=(d&0x10)>>4;
D5=(d&0x20)>>5;
D6=(d&0x40)>>6;
D7=(d&0x80)>>7;
}
void System_Colck(void)
{
//Fosc = 80MHz 40MIPS
CLKdivbits.PLLPRE = 0;//N1 = 2 7.3728MHz/2= 3.6864MHz
PLLFBDbits.PLLdiv = 38;//M = 40 3.6864*43 = 158.5152MHz
CLKdivbits.PLLPOST = 0;//N2 = 2 158.5152/2 = 79.2576MHz
}
/*------------------------------------------------
缁崵绮洪柊宥囩枂閸戣姤鏆?
------------------------------------------------*/
void configio(void)
{
TRISA= 0x0003;//RA0,RA1娑撻缚绶崗銉礉閸忔湹缍戞稉楦跨翻閸?
TRISB= 0x8003;//RB0,RB1,RB15娑撻缚绶崗銉礉閸忔湹缍戞稉楦跨翻閸?
TRISC= 0x0003;//RC0娑撻缚绶崗銉礉閸忔湹缍戞稉楦跨翻閸?
TRISBbits.TRISB8 = 0;//RB8
TRISBbits.TRISB9 = 0;//RB9
TRISBbits.TRISB10 = 0;//RB10
TRISBbits.TRISB11 = 0;//RB11
TRISBbits.TRISB12 = 0;//RB12
TRISBbits.TRISB13 = 0;//RA13
TRISCbits.TRISC6 = 0;//RC6
TRISCbits.TRISC7 = 0;//RC7
TRISCbits.TRISC8 = 0;//RC8
TRISCbits.TRISC9 = 0;//RC9
_TRISB4 = 0 ;
AD1PCFGL=0x01ff;
RPINR26bits.C1RXR = 17; // RP17 used for U1RX
RPOR9bits.RP18R = 16; // RP18 used for U1TX
_TRISC2 = 0 ; //C1TX
_TRISC1 = 1 ; //C1RX
STB=1;
}
void lcd_init(void)
{
RST=0; //复位
delay_ms(850); //延时70ms
RST=1; //复位置高
delay_ms(120); //延时10ms
write_cmd(0x34); //选择基本指令集
delay_us(2500); //延时166us
write_cmd(0x34);
delay_us(1000); //延时66us
write_cmd(0x30);
delay_us(1000);
write_cmd(0x0c); //开显示,有游标,游标位置允许反白
delay_us(2500);
write_cmd(0x01); //清除显示,并设定地址指针为00H
delay_ms(250); //延迟20ms
write_cmd(0x06); //指定在数据的读取与写入时,设定游标从右向左加1移动
delay_us(3000); //延时200us
}
/*------------------------------------------------
测忙函数
------------------------------------------------*/
void chk_busy()
{
TRISB = 0x9e03; //RB9,10,11,12设置为输入
TRISC = 0x3c03; //RC6,7,8设置为输入
E = 0; //使能无效
RS = 0; //指令方式
RW = 1; //读操作
delay_us(800); //延时50us
while(D7)
{
delay_us(800); //延时50us
E=1;
delay_us(800); //延时50us
E=0;
}
E=0;
TRISB = 0x8003; //RB9,10,11,12还原为输出
TRISC = 0x0003; //RC6,7,8还原为输出
}
/*------------------------------------------------
写指令函数
------------------------------------------------*/
void write_cmd(unsigned char c)
{
chk_busy();
RS=0;
RW=0;
delay_us(800);//延时50us
E=1;
delay_us(800);//延时50us
LED_DAT (c);
delay_us(8000);//延时500us
E=0;
delay_us(1500);//延时500us
}
/*------------------------------------------------
写数据函数
------------------------------------------------*/
void write_data(unsigned char c)
{
chk_busy();
RS=1;
RW=0;
delay_us(800);//延时50us
E=1;
delay_us(800);//延时50us
LED_DAT (c);
delay_us(8000);//延时500us
E=0;
delay_us(1500);//延时500us
}
/*------------------------------------------------
CAN总线输入数据转换函数
------------------------------------------------*/
void get_canshu1(void)
{
result1[0]=((ACPSpdFeedback/2)/1000)+0x30; //整数千位
result1[1]=(((ACPSpdFeedback/2)%1000)/100)+0x30; //整数百位
result1[2]=(((ACPSpdFeedback/2)%100)/10)+0x30; //整数十位
result1[3]=((ACPSpdFeedback/2)%10)+0x30; //整数个位
}
void get_canshu2(unsigned int ACPDC)
{
result2[0]=((ACPDC/2)/10)+0x30; //整数十位
result2[1]=((ACPDC/2)%10)+0x30; //整数个位
}
/*------------------------------------------------
显示函数
------------------------------------------------*/
void display(void)
{
uchar i;
write_cmd(0x81);
for(i=0;i<14;i++)
{
write_data(kaiji1);
delay_ms(100);//延时8ms
};
write_cmd(0x8A);
for(i=0;i<12;i++)
{
write_data(kaiji2);
delay_ms(100);//延时8ms
}
}
void display1(void)
{
uchar i;
write_cmd(0x80);
for(i=0;i<16;i++)
{
write_data(canshu1);
delay_ms(100);//延时8ms
};
write_cmd(0x90);
for(i=0;i<16;i++)
{
write_data(canshu2);
delay_ms(100);
}
write_cmd(0x95);
for(i=0;i<4;i++)
{
write_data(result1);
delay_ms(100);//延时8ms
}
write_cmd(0x88);
for(i=0;i<15;i++)
{
write_data(canshu3);
delay_ms(100);//延时8ms
}
write_cmd(0x98);
for(i=0;i<14;i++)
{
write_data(canshu4);
delay_ms(100);//延时8ms
}
write_cmd(0x9D);
for(i=0;i<2;i++)
{
write_data(result2);
delay_ms(100);//延时8ms
}
write_cmd(0x80+4 );
write_cmd(0x0D);
}
void display2(void)
{
uchar i;
write_cmd(0x80);
for(i=0;i<14;i++)
{
write_data(canshu5);
delay_ms(100);//延时8ms
}
write_cmd(0x85);
for(i=0;i<2;i++)
{
write_data(result2);
delay_ms(100);//延时8ms
}
write_cmd(0x90);
for(i=0;i<14;i++)
{
write_data(canshu6);
delay_ms(100);//延时8ms
}
}
/*------------------------------------------------
按键功能函数
------------------------------------------------*/
void scan_key()
{
if(Key_blank==0) //按键换页
{
delay_ms(250);
if(Key_blank==0)
{ while(!Key_blank);
Key_num++;
if(Key_num%2==0)
{
write_cmd(0x01);
delay_us(800);
display1();
delay_us(800);
}
if(Key_num%2==1)
{
write_cmd(0x01);
delay_us(800);
display2();
write_cmd(0x0C);
delay_us(800);
}
}
}
if(Key_mode==0) //按键光标移动
{
delay_ms(15);
if(Key_mode==0)
{ while(!Key_mode);
Key_num1++;
switch(Key_num1)
{
case 0:write_cmd(0x84);break;
case 1:write_cmd(0x85);break;
case 2:write_cmd(0x86);break;
case 3:write_cmd(0x8C);break;
case 4:write_cmd(0x8D);break;
case 5:write_cmd(0x8F);break;
case 6:Key_num1=0;write_cmd(0x84);break;
default:break;
}
}
}
if(Key_up==0) //按键加
{
write_cmd(0x0c);
delay_ms(15);
if(Key_up==0)
{ while(!Key_up);
//Key_num1++;
switch(Key_num1)
{
case 0:
canshu1[8]++;
if(canshu1[8]==':')
// canshu1[8]=='9';
canshu1[8]='0';
write_data(canshu1[8]);
delay_us(800);
write_cmd(0x84);break;
case 1:
canshu1[10]++;
if(canshu1[10]==':')
canshu1[10]='0';
write_data(canshu1[10]);
delay_us(800);
write_cmd(0x85);break;
case 2:
canshu1[12]++;
if(canshu1[12]==':')
canshu1[12]='0';
write_data(canshu1[12]);
delay_us(800);
write_cmd(0x86);break;
case 3:
canshu3[8]++;
if(canshu3[8]==':')
canshu3[8]='0';
write_data(canshu3[8]);
delay_us(800);
write_cmd(0x8C);break;
case 4:
canshu3[10]++;
if(canshu1[10]==':')
canshu1[10]='0';
write_data(canshu3[10]);
delay_us(800);
write_cmd(0x8D);break;
case 5:
canshu3[14]++;
if(canshu3[14]==':')
canshu3[14]='0';
write_data(canshu3[14]);
delay_us(800);
write_cmd(0x8F);break;
case 6:Key_num1=0;write_cmd(0x84);break;
default:break;
}
write_cmd(0x0d);
}
}
if(Key_down==0) //按键减
{
write_cmd(0x0c);
delay_ms(15);
if(Key_down==0)
{ while(!Key_down);
//Key_num1++;
switch(Key_num1)
{
case 0:
canshu1[8]--;
if(canshu1[8]=='/')
canshu1[8]='9';
write_data(canshu1[8]);
delay_us(800);
write_cmd(0x84);break;
case 1:
canshu1[10]--;
if(canshu1[10]=='/')
canshu1[10]='9';
write_data(canshu1[10]);
delay_us(800);
write_cmd(0x85);break;
case 2:
canshu1[12]--;
if(canshu1[12]=='/')
canshu1[12]='9';
write_data(canshu1[12]);
delay_us(800);
write_cmd(0x86);break;
case 3:
canshu3[8]--;
if(canshu3[8]=='/')
canshu3[8]='9';
write_data(canshu3[8]);
delay_us(800);
write_cmd(0x8C);break;
case 4:
canshu3[10]--;
if(canshu1[10]=='/')
canshu1[10]='9';
write_data(canshu3[10]);
delay_us(800);
write_cmd(0x8D);break;
case 5:
canshu3[14]--;
if(canshu3[14]=='/')
canshu3[14]='9';
write_data(canshu3[14]);
delay_us(800);
write_cmd(0x8F);break;
case 6:Key_num1=0;write_cmd(0x84);break;
default:break;
}
write_cmd(0x0d);
}
}
}
/*------------------------------------------------
CAN功能
------------------------------------------------*/
void can_init(void)
{
/* Request Configuration Mode */
C1CTRL1bits.REQOP=4;
while(C1CTRL1bits.OPMODE!=4);
ecanClkInit();
C1FCTRLbits.DMABS=0; /* 4 CAN Message Buffers in DMA RAM */
ecanWriteRxAcptFilter(0,ACP_CAN_ID,0,0,0); //Filter0,SID=34C
ecanWriteRxAcptFilter(1,ACP_CAN_ID,0,1,0); //Filter1,SID=
// ecanWriteRxAcptFilter(2,HCU2_CAN_ID,0,2,0); //Filter0,SID=
// ecanWriteRxAcptFilter(3,HCU3_CAN_ID,0,3,0); //Filter1,SID=
ecanWriteRxAcptMask(1,0x0,1,0);// Mask1,SID = 203
/* Enter Normal Mode */
C1CTRL1bits.REQOP=0; // 0---Normal Mode,2---Loopback Mode
while(C1CTRL1bits.OPMODE!=0);
/* ECAN transmit/receive message control */
C1RXFUL1=C1RXFUL2=C1RXOVF1=C1RXOVF2=0x0000;
C1TR01CONbits.TXEN0=1; /* ECAN1, Buffer 0 is a Transmit Buffer */
C1TR01CONbits.TXEN1=0; /* ECAN1, Buffer 1 is a Receive Buffer */
C1TR01CONbits.TX0PRI=0b11; /* Message Buffer 0 Priority Level */
C1TR01CONbits.TX1PRI=0b11; /* Message Buffer 1 Priority Level */
dma0init();
dma2init();
IPC8bits.C1IP=2;
IEC2bits.C1IE = 1;
C1INTEbits.TBIE = 1;
C1INTEbits.RBIE = 1;
}
void ecanClkInit(void)
{
C1CTRL1bits.CANCKS = 0x1; //FCAN is selected to be FCY = 40MHz
C1CFG1bits.SJW = 0x3; //Synchronization Jump Width set to 4 TQ
C1CFG2bits.SEG1PH=0x7; //Phase Segment 1 time is 8 TQ
C1CFG2bits.SEG2PHTS = 0x1; //Phase Segment 2 time is set to be programmable
C1CFG2bits.SEG2PH = 0x5; //Phase Segment 2 time is 6 TQ
C1CFG2bits.PRSEG = 0x4; //Propagation Segment time is 5 TQ
C1CFG2bits.SAM = 0x1; //Bus line is sampled three times at the sample point
C1CFG1bits.BRP = CAN_BRP_VAL; //Baud Rate Prescaler
}
void ecanWriteRxAcptFilter(int n, long identifier, unsigned int exide, unsigned int bufPnt,unsigned int maskSel)
{
unsigned long sid10_0=0, eid15_0=0, eid17_16=0;
unsigned int *sidRegAddr,*bufPntRegAddr,*maskSelRegAddr, *fltEnRegAddr;
C1CTRL1bits.WIN=1;
// Obtain the Address of CiRXFnSID, CiBUFPNTn, CiFMSKSELn and CiFEN register for a given filter number "n"
sidRegAddr = (unsigned int *)(&C1RXF0SID + (n << 1));
bufPntRegAddr = (unsigned int *)(&C1BUFPNT1 + (n >> 2));
maskSelRegAddr = (unsigned int *)(&C1FMSKSEL1 + (n >> 3));
fltEnRegAddr = (unsigned int *)(&C1FEN1);
if(exide==1)
{ // Filter Extended Identifier
eid15_0 = (identifier & 0xFFFF);
eid17_16= (identifier>>16) & 0x3;
sid10_0 = (identifier>>18) & 0x7FF;
*sidRegAddr=(((sid10_0)<<5) + 0x8) + eid17_16; // Write to CiRXFnSID Register
*(sidRegAddr+1)= eid15_0; // Write to CiRXFnEID Register
}
else
{ // Filter Standard Identifier
sid10_0 = (identifier & 0x7FF);
*sidRegAddr=(sid10_0)<<5; // Write to CiRXFnSID Register
*(sidRegAddr+1)=0; // Write to CiRXFnEID Register
}
*bufPntRegAddr = (*bufPntRegAddr) & (0xFFFF - (0xF << (4 *(n & 3)))); // clear nibble
*bufPntRegAddr = ((bufPnt << (4 *(n & 3))) | (*bufPntRegAddr)); // Write to C1BUFPNTn Register
*maskSelRegAddr = (*maskSelRegAddr) & (0xFFFF - (0x3 << ((n & 7) * 2))); // clear 2 bits
*maskSelRegAddr = ((maskSel << (2 * (n & 7))) | (*maskSelRegAddr)); // Write to C1FMSKSELn Register
*fltEnRegAddr = ((0x1 << n) | (*fltEnRegAddr)); // Write to C1FEN1 Register
C1CTRL1bits.WIN=0;
}
void ecanWriteRxAcptMask(int m, long identifier, unsigned int mide, unsigned int exide)
{
unsigned long sid10_0=0, eid15_0=0, eid17_16=0;
unsigned int *maskRegAddr;
C1CTRL1bits.WIN=1;
maskRegAddr = (unsigned int *)(&C1RXM0SID + (m << 1));
// Bit-filed manupulation to write to Filter Mask register
if(exide==1)
{ // Filter Extended Identifier
eid15_0 = (identifier & 0xFFFF);
eid17_16= (identifier>>16) & 0x3;
sid10_0 = (identifier>>18) & 0x7FF;
if(mide==1)
*maskRegAddr=((sid10_0)<<5) + 0x0008 + eid17_16;// Write to CiRXMnSID Register
else
*maskRegAddr=((sid10_0)<<5) + eid17_16; // Write to CiRXMnSID Register
*(maskRegAddr+1)= eid15_0; // Write to CiRXMnEID Register
}
else
{ // Filter Standard Identifier
sid10_0 = (identifier & 0x7FF);
if(mide==1)
*maskRegAddr=((sid10_0)<<5) + 0x0008; // Write to CiRXMnSID Register
else
*maskRegAddr=(sid10_0)<<5; // Write to CiRXMnSID Register
*(maskRegAddr+1)=0; // Write to CiRXMnEID Register
}
C1CTRL1bits.WIN=0;
}
void dma0init(void)
{
DMACS0=0;
DMA0CON=0x2020;
DMA0PAD=0x0442; /* ECAN 1 (C1TXD) */
DMA0CNT=0x0007;
DMA0REQ=0x0046; /* ECAN 1 Transmit */
DMA0STA= __builtin_dmaoffset(can_msgBuf);
DMA0CONbits.CHEN=1;
}
/* Dma Initialization for ECAN1 Reception */
void dma2init(void)
{
DMACS0=0;
DMA2CON=0x0020;
DMA2PAD=0x0440; /* ECAN 1 (C1RXD) */
DMA2CNT=0x0007;
DMA2REQ=0x0022; /* ECAN 1 Receive */
DMA2STA= __builtin_dmaoffset(can_msgBuf_r);
DMA2CONbits.CHEN=1;
}
void ecanWriteTxMsgBufId(unsigned int buf, long txIdentifier, unsigned int ide, unsigned int remoteTransmit)
{
unsigned long word0=0, word1=0, word2=0;
unsigned long sid10_0=0, eid5_0=0, eid17_6=0;
eid5_0 = (txIdentifier & 0x3F);
eid17_6 = (txIdentifier>>6) & 0xFFF;
sid10_0 = (txIdentifier>>18) & 0x7FF;
word1 = eid17_6;
if(remoteTransmit==1)
{ // Transmit Remote Frame
word0 = ((sid10_0 << 2) | ide | 0x2);
word2 = ((eid5_0 << 10)| 0x0200);
}
else
{
word0 = ((sid10_0 << 2) | ide);
word2 = (eid5_0 << 10);
}
can_msgBuf[0][0] = word0;
can_msgBuf[0][1] = word1;
can_msgBuf[0][2] = word2;
}
void ecanWriteTxMsgBufData(unsigned int buf, unsigned int dataLength, unsigned int data1, unsigned int data2, unsigned int data3, unsigned int data4)
{
can_msgBuf[0][2] = ((can_msgBuf[0][2] & 0xFFF0) + dataLength) ;
can_msgBuf[0][3] = data1;
can_msgBuf[0][4] = data2;
can_msgBuf[0][5] = data3;
can_msgBuf[0][6] = data4;
}
void ecanWriteMessage(unsigned int data1,unsigned int data2,unsigned int data3,unsigned int data4)
{
unsigned long txIdentifier;
unsigned int Data1;
unsigned int Data2;
unsigned int Data3;
unsigned int Data4;
txIdentifier = ACP_CAN_ID;
Data1 = data1;
Data2 = data2;
Data3 = data3;
Data4 = data4;
ecanWriteTxMsgBufId(0,txIdentifier<<18,0,0);
ecanWriteTxMsgBufData(0,8,Data1,Data2,Data3,Data4);
C1TR01CONbits.TXREQ0=1;
}
void C1_P(void)
{
IFS2bits.C1IF = 0; // clear interrupt flag
if(C1INTFbits.TBIF)
{
C1INTFbits.TBIF = 0;
}
if(C1INTFbits.RBIF)
{
C1INTFbits.RBIF = 0;
// read the message
if(C1RXFUL1bits.RXFUL1==1)
{
C1RXFUL1bits.RXFUL1=0;
}
if(((can_msgBuf_r[1][0] & 0x1FFC) >> 2) == ACP_CAN_ID)
{
ACP_10= can_msgBuf_r[1][5];
ACP_11 = ((ACP_10 & 0x00FF)<<8);
ACP_11 += ((ACP_10 & 0xFF00)>>8);
ACPDCCurrent=ACP_11;
ACP_12= can_msgBuf_r[1][3];
ACP_13 = ((ACP_12 & 0x00FF)<<8);
ACP_13 += ((ACP_12 & 0xFF00)>>8);
ACPDCVoltage=ACP_13;
ACP_14= can_msgBuf_r[1][4];
ACP_15 = ((ACP_14 & 0x00FF)<<8);
ACP_15 += ((ACP_14 & 0xFF00)>>8);
ACPSpdFeedback=ACP_15;
}
}
}
void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void)
{
IFS2bits.C1IF = 0; // clear interrupt flag
C1_P();
return;
}
void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void)
{
IFS0bits.DMA0IF = 0;
return;
}
void __attribute__((interrupt, no_auto_psv)) _DMA2Interrupt(void)
{
IFS1bits.DMA2IF = 0;
return;
}
#include "userdefines.h"
//#include "can.h"
_FBS(BWRP_WRPROTECT_OFF & BSS_NO_FLASH & RBS_NO_RAM); //f80000
_FSS(SWRP_WRPROTECT_OFF & SSS_NO_FLASH & RSS_NO_RAM); //f80002
_FGS(GCP_OFF & GWRP_OFF & GSS_OFF); //f80004
_FOSCSEL(FNOSC_PRIPLL & IESO_ON); //f80006
_FOSC(POSCMD_XT& OSCIOFNC_OFF & IOL1WAY_OFF & FCKSM_CSECMD); //f80008
_FWDT(WDTPOST_PS32768 & WDTPRE_PR128 & WINDIS_OFF & FWDTEN_OFF); //f8000a
_FPOR(FPWRT_PWR128 & ALTI2C_OFF & LPOL_OFF & HPOL_OFF & PWMPIN_ON); //f8000c
_FICD( ICS_PGD3 & JTAGEN_OFF); //f8000e
#define E PORTBbits.RB13
#define RW PORTAbits.RA10
#define RS PORTAbits.RA7
#define RST PORTBbits.RB8
#define Key_enter PORTAbits.RA0
#define Key_mode PORTAbits.RA1
#define Key_down PORTBbits.RB0
#define Key_up PORTBbits.RB1
#define Key_blank PORTBbits.RB15
#define ON_OFF PORTBbits.RC0
#define Normal PORTBbits.RB3
#define Error PORTBbits.RB2
#define STB LATBbits.LATB4
#define uchar unsigned char
#define uint unsigned int
#define D0 PORTBbits.RB12
#define D1 PORTBbits.RB11
#define D2 PORTBbits.RB10
#define D3 PORTCbits.RC9
#define D4 PORTCbits.RC8
#define D5 PORTCbits.RC7
#define D6 PORTCbits.RC6
#define D7 PORTBbits.RB9
uchar kaiji1[]=("欢迎来到");
uchar kaiji2[]=("机电学院");
uchar canshu1[]=("设定转速0 0 0 0");
uchar canshu2[]=("当前转速: ");
uchar canshu3[]=("设置功率0 0 . 0");
uchar canshu4[]=("母线电压: V");
uchar canshu5[]=("母线电流: A");
uchar canshu6[]=(" 相电流: A");
#define ATC_CAN_ID 0X34C
#define ACP_CAN_ID 0X4A0
#define HCU_CAN_ID 0X4A2
#define BC1_CAN_ID 0X5B3
#define BC2_CAN_ID 0X533
#define ECAN_MSG_BUF_LENGTH 4
#define FCAN 40000000 // FCAN = Fcy = 40MHz
#define BITRATE 500000 // Baude Rate = 500kHz
#define NTQ 20 // 20 Time Quanta in a Bit Time
#define CAN_BRP_VAL ((FCAN/(2*NTQ*BITRATE))-1) // 40MHz/(500kHz*2)]-1 = 39 BRP_VAL should be 0~63
void can_init(void);
void ecanClkInit(void);
void ecanWriteRxAcptFilter(int n, long identifier, unsigned int exide, unsigned int bufPnt,unsigned int maskSel);
void ecanWriteRxAcptMask(int m, long identifier, unsigned int mide, unsigned int exide);
void dma0init(void);
void dma2init(void);
void ecanWriteTxMsgBufId(unsigned int buf, long txIdentifier, unsigned int ide, unsigned int remoteTransmit);
void ecanWriteTxMsgBufData(unsigned int buf, unsigned int dataLength, unsigned int data1, unsigned int data2, unsigned int data3, unsigned int data4);
void ecanWriteMessage(unsigned int data1,unsigned int data2,unsigned int data3,unsigned int data4);
void C1_P(void);
unsigned int can_msgBuf[ECAN_MSG_BUF_LENGTH][8] __attribute__((space(dma),aligned(ECAN_MSG_BUF_LENGTH*16)));
unsigned int can_msgBuf_r[ECAN_MSG_BUF_LENGTH][8] __attribute__((space(dma),aligned(ECAN_MSG_BUF_LENGTH*16)));
unsigned int ACP_10=0;
unsigned int ACP_11=0;
unsigned int ACP_12=0;
unsigned int ACP_13=0;
unsigned int ACP_14=0;
unsigned int ACP_15=0;
unsigned int ACPSpdFeedback=0;
unsigned int ACPDCCurrent=0;
unsigned int ACPDCVoltage=0;
unsigned char result1[4]={0,0,0,0};
unsigned char result2[2]={0,0};
uchar Key_num1;
uchar Key_num;
void System_Colck(void);
void configio(void);
void lcd_init(void);
void chk_busy();
void write_cmd(unsigned char c);
void write_data(unsigned char c);
void get_canshu1(void);
void get_canshu2(unsigned int ACPDC);
void display(void);
void display1(void);
void display2(void);
void yidong(void);
void scan_key();
void main ()
{
System_Colck();
configio();
can_init();
lcd_init();
delay_ms (100);//延迟8ms
display();
delay_s(3000);
write_cmd(0x01);
delay_ms (100);//延迟8ms
display1();
while(1)
{
//display1();
get_canshu1();
get_canshu2(ACPDCVoltage);
get_canshu2(ACPDCCurrent);
scan_key();
}
}
void LED_DAT (uchar d)
{
D0=d&0x01;
D1=(d&0x02)>>1;
D2=(d&0x04)>>2;
D3=(d&0x08)>>3;
D4=(d&0x10)>>4;
D5=(d&0x20)>>5;
D6=(d&0x40)>>6;
D7=(d&0x80)>>7;
}
void System_Colck(void)
{
//Fosc = 80MHz 40MIPS
CLKdivbits.PLLPRE = 0;//N1 = 2 7.3728MHz/2= 3.6864MHz
PLLFBDbits.PLLdiv = 38;//M = 40 3.6864*43 = 158.5152MHz
CLKdivbits.PLLPOST = 0;//N2 = 2 158.5152/2 = 79.2576MHz
}
/*------------------------------------------------
缁崵绮洪柊宥囩枂閸戣姤鏆?
------------------------------------------------*/
void configio(void)
{
TRISA= 0x0003;//RA0,RA1娑撻缚绶崗銉礉閸忔湹缍戞稉楦跨翻閸?
TRISB= 0x8003;//RB0,RB1,RB15娑撻缚绶崗銉礉閸忔湹缍戞稉楦跨翻閸?
TRISC= 0x0003;//RC0娑撻缚绶崗銉礉閸忔湹缍戞稉楦跨翻閸?
TRISBbits.TRISB8 = 0;//RB8
TRISBbits.TRISB9 = 0;//RB9
TRISBbits.TRISB10 = 0;//RB10
TRISBbits.TRISB11 = 0;//RB11
TRISBbits.TRISB12 = 0;//RB12
TRISBbits.TRISB13 = 0;//RA13
TRISCbits.TRISC6 = 0;//RC6
TRISCbits.TRISC7 = 0;//RC7
TRISCbits.TRISC8 = 0;//RC8
TRISCbits.TRISC9 = 0;//RC9
_TRISB4 = 0 ;
AD1PCFGL=0x01ff;
RPINR26bits.C1RXR = 17; // RP17 used for U1RX
RPOR9bits.RP18R = 16; // RP18 used for U1TX
_TRISC2 = 0 ; //C1TX
_TRISC1 = 1 ; //C1RX
STB=1;
}
void lcd_init(void)
{
RST=0; //复位
delay_ms(850); //延时70ms
RST=1; //复位置高
delay_ms(120); //延时10ms
write_cmd(0x34); //选择基本指令集
delay_us(2500); //延时166us
write_cmd(0x34);
delay_us(1000); //延时66us
write_cmd(0x30);
delay_us(1000);
write_cmd(0x0c); //开显示,有游标,游标位置允许反白
delay_us(2500);
write_cmd(0x01); //清除显示,并设定地址指针为00H
delay_ms(250); //延迟20ms
write_cmd(0x06); //指定在数据的读取与写入时,设定游标从右向左加1移动
delay_us(3000); //延时200us
}
/*------------------------------------------------
测忙函数
------------------------------------------------*/
void chk_busy()
{
TRISB = 0x9e03; //RB9,10,11,12设置为输入
TRISC = 0x3c03; //RC6,7,8设置为输入
E = 0; //使能无效
RS = 0; //指令方式
RW = 1; //读操作
delay_us(800); //延时50us
while(D7)
{
delay_us(800); //延时50us
E=1;
delay_us(800); //延时50us
E=0;
}
E=0;
TRISB = 0x8003; //RB9,10,11,12还原为输出
TRISC = 0x0003; //RC6,7,8还原为输出
}
/*------------------------------------------------
写指令函数
------------------------------------------------*/
void write_cmd(unsigned char c)
{
chk_busy();
RS=0;
RW=0;
delay_us(800);//延时50us
E=1;
delay_us(800);//延时50us
LED_DAT (c);
delay_us(8000);//延时500us
E=0;
delay_us(1500);//延时500us
}
/*------------------------------------------------
写数据函数
------------------------------------------------*/
void write_data(unsigned char c)
{
chk_busy();
RS=1;
RW=0;
delay_us(800);//延时50us
E=1;
delay_us(800);//延时50us
LED_DAT (c);
delay_us(8000);//延时500us
E=0;
delay_us(1500);//延时500us
}
/*------------------------------------------------
CAN总线输入数据转换函数
------------------------------------------------*/
void get_canshu1(void)
{
result1[0]=((ACPSpdFeedback/2)/1000)+0x30; //整数千位
result1[1]=(((ACPSpdFeedback/2)%1000)/100)+0x30; //整数百位
result1[2]=(((ACPSpdFeedback/2)%100)/10)+0x30; //整数十位
result1[3]=((ACPSpdFeedback/2)%10)+0x30; //整数个位
}
void get_canshu2(unsigned int ACPDC)
{
result2[0]=((ACPDC/2)/10)+0x30; //整数十位
result2[1]=((ACPDC/2)%10)+0x30; //整数个位
}
/*------------------------------------------------
显示函数
------------------------------------------------*/
void display(void)
{
uchar i;
write_cmd(0x81);
for(i=0;i<14;i++)
{
write_data(kaiji1);
delay_ms(100);//延时8ms
};
write_cmd(0x8A);
for(i=0;i<12;i++)
{
write_data(kaiji2);
delay_ms(100);//延时8ms
}
}
void display1(void)
{
uchar i;
write_cmd(0x80);
for(i=0;i<16;i++)
{
write_data(canshu1);
delay_ms(100);//延时8ms
};
write_cmd(0x90);
for(i=0;i<16;i++)
{
write_data(canshu2);
delay_ms(100);
}
write_cmd(0x95);
for(i=0;i<4;i++)
{
write_data(result1);
delay_ms(100);//延时8ms
}
write_cmd(0x88);
for(i=0;i<15;i++)
{
write_data(canshu3);
delay_ms(100);//延时8ms
}
write_cmd(0x98);
for(i=0;i<14;i++)
{
write_data(canshu4);
delay_ms(100);//延时8ms
}
write_cmd(0x9D);
for(i=0;i<2;i++)
{
write_data(result2);
delay_ms(100);//延时8ms
}
write_cmd(0x80+4 );
write_cmd(0x0D);
}
void display2(void)
{
uchar i;
write_cmd(0x80);
for(i=0;i<14;i++)
{
write_data(canshu5);
delay_ms(100);//延时8ms
}
write_cmd(0x85);
for(i=0;i<2;i++)
{
write_data(result2);
delay_ms(100);//延时8ms
}
write_cmd(0x90);
for(i=0;i<14;i++)
{
write_data(canshu6);
delay_ms(100);//延时8ms
}
}
/*------------------------------------------------
按键功能函数
------------------------------------------------*/
void scan_key()
{
if(Key_blank==0) //按键换页
{
delay_ms(250);
if(Key_blank==0)
{ while(!Key_blank);
Key_num++;
if(Key_num%2==0)
{
write_cmd(0x01);
delay_us(800);
display1();
delay_us(800);
}
if(Key_num%2==1)
{
write_cmd(0x01);
delay_us(800);
display2();
write_cmd(0x0C);
delay_us(800);
}
}
}
if(Key_mode==0) //按键光标移动
{
delay_ms(15);
if(Key_mode==0)
{ while(!Key_mode);
Key_num1++;
switch(Key_num1)
{
case 0:write_cmd(0x84);break;
case 1:write_cmd(0x85);break;
case 2:write_cmd(0x86);break;
case 3:write_cmd(0x8C);break;
case 4:write_cmd(0x8D);break;
case 5:write_cmd(0x8F);break;
case 6:Key_num1=0;write_cmd(0x84);break;
default:break;
}
}
}
if(Key_up==0) //按键加
{
write_cmd(0x0c);
delay_ms(15);
if(Key_up==0)
{ while(!Key_up);
//Key_num1++;
switch(Key_num1)
{
case 0:
canshu1[8]++;
if(canshu1[8]==':')
// canshu1[8]=='9';
canshu1[8]='0';
write_data(canshu1[8]);
delay_us(800);
write_cmd(0x84);break;
case 1:
canshu1[10]++;
if(canshu1[10]==':')
canshu1[10]='0';
write_data(canshu1[10]);
delay_us(800);
write_cmd(0x85);break;
case 2:
canshu1[12]++;
if(canshu1[12]==':')
canshu1[12]='0';
write_data(canshu1[12]);
delay_us(800);
write_cmd(0x86);break;
case 3:
canshu3[8]++;
if(canshu3[8]==':')
canshu3[8]='0';
write_data(canshu3[8]);
delay_us(800);
write_cmd(0x8C);break;
case 4:
canshu3[10]++;
if(canshu1[10]==':')
canshu1[10]='0';
write_data(canshu3[10]);
delay_us(800);
write_cmd(0x8D);break;
case 5:
canshu3[14]++;
if(canshu3[14]==':')
canshu3[14]='0';
write_data(canshu3[14]);
delay_us(800);
write_cmd(0x8F);break;
case 6:Key_num1=0;write_cmd(0x84);break;
default:break;
}
write_cmd(0x0d);
}
}
if(Key_down==0) //按键减
{
write_cmd(0x0c);
delay_ms(15);
if(Key_down==0)
{ while(!Key_down);
//Key_num1++;
switch(Key_num1)
{
case 0:
canshu1[8]--;
if(canshu1[8]=='/')
canshu1[8]='9';
write_data(canshu1[8]);
delay_us(800);
write_cmd(0x84);break;
case 1:
canshu1[10]--;
if(canshu1[10]=='/')
canshu1[10]='9';
write_data(canshu1[10]);
delay_us(800);
write_cmd(0x85);break;
case 2:
canshu1[12]--;
if(canshu1[12]=='/')
canshu1[12]='9';
write_data(canshu1[12]);
delay_us(800);
write_cmd(0x86);break;
case 3:
canshu3[8]--;
if(canshu3[8]=='/')
canshu3[8]='9';
write_data(canshu3[8]);
delay_us(800);
write_cmd(0x8C);break;
case 4:
canshu3[10]--;
if(canshu1[10]=='/')
canshu1[10]='9';
write_data(canshu3[10]);
delay_us(800);
write_cmd(0x8D);break;
case 5:
canshu3[14]--;
if(canshu3[14]=='/')
canshu3[14]='9';
write_data(canshu3[14]);
delay_us(800);
write_cmd(0x8F);break;
case 6:Key_num1=0;write_cmd(0x84);break;
default:break;
}
write_cmd(0x0d);
}
}
}
/*------------------------------------------------
CAN功能
------------------------------------------------*/
void can_init(void)
{
/* Request Configuration Mode */
C1CTRL1bits.REQOP=4;
while(C1CTRL1bits.OPMODE!=4);
ecanClkInit();
C1FCTRLbits.DMABS=0; /* 4 CAN Message Buffers in DMA RAM */
ecanWriteRxAcptFilter(0,ACP_CAN_ID,0,0,0); //Filter0,SID=34C
ecanWriteRxAcptFilter(1,ACP_CAN_ID,0,1,0); //Filter1,SID=
// ecanWriteRxAcptFilter(2,HCU2_CAN_ID,0,2,0); //Filter0,SID=
// ecanWriteRxAcptFilter(3,HCU3_CAN_ID,0,3,0); //Filter1,SID=
ecanWriteRxAcptMask(1,0x0,1,0);// Mask1,SID = 203
/* Enter Normal Mode */
C1CTRL1bits.REQOP=0; // 0---Normal Mode,2---Loopback Mode
while(C1CTRL1bits.OPMODE!=0);
/* ECAN transmit/receive message control */
C1RXFUL1=C1RXFUL2=C1RXOVF1=C1RXOVF2=0x0000;
C1TR01CONbits.TXEN0=1; /* ECAN1, Buffer 0 is a Transmit Buffer */
C1TR01CONbits.TXEN1=0; /* ECAN1, Buffer 1 is a Receive Buffer */
C1TR01CONbits.TX0PRI=0b11; /* Message Buffer 0 Priority Level */
C1TR01CONbits.TX1PRI=0b11; /* Message Buffer 1 Priority Level */
dma0init();
dma2init();
IPC8bits.C1IP=2;
IEC2bits.C1IE = 1;
C1INTEbits.TBIE = 1;
C1INTEbits.RBIE = 1;
}
void ecanClkInit(void)
{
C1CTRL1bits.CANCKS = 0x1; //FCAN is selected to be FCY = 40MHz
C1CFG1bits.SJW = 0x3; //Synchronization Jump Width set to 4 TQ
C1CFG2bits.SEG1PH=0x7; //Phase Segment 1 time is 8 TQ
C1CFG2bits.SEG2PHTS = 0x1; //Phase Segment 2 time is set to be programmable
C1CFG2bits.SEG2PH = 0x5; //Phase Segment 2 time is 6 TQ
C1CFG2bits.PRSEG = 0x4; //Propagation Segment time is 5 TQ
C1CFG2bits.SAM = 0x1; //Bus line is sampled three times at the sample point
C1CFG1bits.BRP = CAN_BRP_VAL; //Baud Rate Prescaler
}
void ecanWriteRxAcptFilter(int n, long identifier, unsigned int exide, unsigned int bufPnt,unsigned int maskSel)
{
unsigned long sid10_0=0, eid15_0=0, eid17_16=0;
unsigned int *sidRegAddr,*bufPntRegAddr,*maskSelRegAddr, *fltEnRegAddr;
C1CTRL1bits.WIN=1;
// Obtain the Address of CiRXFnSID, CiBUFPNTn, CiFMSKSELn and CiFEN register for a given filter number "n"
sidRegAddr = (unsigned int *)(&C1RXF0SID + (n << 1));
bufPntRegAddr = (unsigned int *)(&C1BUFPNT1 + (n >> 2));
maskSelRegAddr = (unsigned int *)(&C1FMSKSEL1 + (n >> 3));
fltEnRegAddr = (unsigned int *)(&C1FEN1);
if(exide==1)
{ // Filter Extended Identifier
eid15_0 = (identifier & 0xFFFF);
eid17_16= (identifier>>16) & 0x3;
sid10_0 = (identifier>>18) & 0x7FF;
*sidRegAddr=(((sid10_0)<<5) + 0x8) + eid17_16; // Write to CiRXFnSID Register
*(sidRegAddr+1)= eid15_0; // Write to CiRXFnEID Register
}
else
{ // Filter Standard Identifier
sid10_0 = (identifier & 0x7FF);
*sidRegAddr=(sid10_0)<<5; // Write to CiRXFnSID Register
*(sidRegAddr+1)=0; // Write to CiRXFnEID Register
}
*bufPntRegAddr = (*bufPntRegAddr) & (0xFFFF - (0xF << (4 *(n & 3)))); // clear nibble
*bufPntRegAddr = ((bufPnt << (4 *(n & 3))) | (*bufPntRegAddr)); // Write to C1BUFPNTn Register
*maskSelRegAddr = (*maskSelRegAddr) & (0xFFFF - (0x3 << ((n & 7) * 2))); // clear 2 bits
*maskSelRegAddr = ((maskSel << (2 * (n & 7))) | (*maskSelRegAddr)); // Write to C1FMSKSELn Register
*fltEnRegAddr = ((0x1 << n) | (*fltEnRegAddr)); // Write to C1FEN1 Register
C1CTRL1bits.WIN=0;
}
void ecanWriteRxAcptMask(int m, long identifier, unsigned int mide, unsigned int exide)
{
unsigned long sid10_0=0, eid15_0=0, eid17_16=0;
unsigned int *maskRegAddr;
C1CTRL1bits.WIN=1;
maskRegAddr = (unsigned int *)(&C1RXM0SID + (m << 1));
// Bit-filed manupulation to write to Filter Mask register
if(exide==1)
{ // Filter Extended Identifier
eid15_0 = (identifier & 0xFFFF);
eid17_16= (identifier>>16) & 0x3;
sid10_0 = (identifier>>18) & 0x7FF;
if(mide==1)
*maskRegAddr=((sid10_0)<<5) + 0x0008 + eid17_16;// Write to CiRXMnSID Register
else
*maskRegAddr=((sid10_0)<<5) + eid17_16; // Write to CiRXMnSID Register
*(maskRegAddr+1)= eid15_0; // Write to CiRXMnEID Register
}
else
{ // Filter Standard Identifier
sid10_0 = (identifier & 0x7FF);
if(mide==1)
*maskRegAddr=((sid10_0)<<5) + 0x0008; // Write to CiRXMnSID Register
else
*maskRegAddr=(sid10_0)<<5; // Write to CiRXMnSID Register
*(maskRegAddr+1)=0; // Write to CiRXMnEID Register
}
C1CTRL1bits.WIN=0;
}
void dma0init(void)
{
DMACS0=0;
DMA0CON=0x2020;
DMA0PAD=0x0442; /* ECAN 1 (C1TXD) */
DMA0CNT=0x0007;
DMA0REQ=0x0046; /* ECAN 1 Transmit */
DMA0STA= __builtin_dmaoffset(can_msgBuf);
DMA0CONbits.CHEN=1;
}
/* Dma Initialization for ECAN1 Reception */
void dma2init(void)
{
DMACS0=0;
DMA2CON=0x0020;
DMA2PAD=0x0440; /* ECAN 1 (C1RXD) */
DMA2CNT=0x0007;
DMA2REQ=0x0022; /* ECAN 1 Receive */
DMA2STA= __builtin_dmaoffset(can_msgBuf_r);
DMA2CONbits.CHEN=1;
}
void ecanWriteTxMsgBufId(unsigned int buf, long txIdentifier, unsigned int ide, unsigned int remoteTransmit)
{
unsigned long word0=0, word1=0, word2=0;
unsigned long sid10_0=0, eid5_0=0, eid17_6=0;
eid5_0 = (txIdentifier & 0x3F);
eid17_6 = (txIdentifier>>6) & 0xFFF;
sid10_0 = (txIdentifier>>18) & 0x7FF;
word1 = eid17_6;
if(remoteTransmit==1)
{ // Transmit Remote Frame
word0 = ((sid10_0 << 2) | ide | 0x2);
word2 = ((eid5_0 << 10)| 0x0200);
}
else
{
word0 = ((sid10_0 << 2) | ide);
word2 = (eid5_0 << 10);
}
can_msgBuf[0][0] = word0;
can_msgBuf[0][1] = word1;
can_msgBuf[0][2] = word2;
}
void ecanWriteTxMsgBufData(unsigned int buf, unsigned int dataLength, unsigned int data1, unsigned int data2, unsigned int data3, unsigned int data4)
{
can_msgBuf[0][2] = ((can_msgBuf[0][2] & 0xFFF0) + dataLength) ;
can_msgBuf[0][3] = data1;
can_msgBuf[0][4] = data2;
can_msgBuf[0][5] = data3;
can_msgBuf[0][6] = data4;
}
void ecanWriteMessage(unsigned int data1,unsigned int data2,unsigned int data3,unsigned int data4)
{
unsigned long txIdentifier;
unsigned int Data1;
unsigned int Data2;
unsigned int Data3;
unsigned int Data4;
txIdentifier = ACP_CAN_ID;
Data1 = data1;
Data2 = data2;
Data3 = data3;
Data4 = data4;
ecanWriteTxMsgBufId(0,txIdentifier<<18,0,0);
ecanWriteTxMsgBufData(0,8,Data1,Data2,Data3,Data4);
C1TR01CONbits.TXREQ0=1;
}
void C1_P(void)
{
IFS2bits.C1IF = 0; // clear interrupt flag
if(C1INTFbits.TBIF)
{
C1INTFbits.TBIF = 0;
}
if(C1INTFbits.RBIF)
{
C1INTFbits.RBIF = 0;
// read the message
if(C1RXFUL1bits.RXFUL1==1)
{
C1RXFUL1bits.RXFUL1=0;
}
if(((can_msgBuf_r[1][0] & 0x1FFC) >> 2) == ACP_CAN_ID)
{
ACP_10= can_msgBuf_r[1][5];
ACP_11 = ((ACP_10 & 0x00FF)<<8);
ACP_11 += ((ACP_10 & 0xFF00)>>8);
ACPDCCurrent=ACP_11;
ACP_12= can_msgBuf_r[1][3];
ACP_13 = ((ACP_12 & 0x00FF)<<8);
ACP_13 += ((ACP_12 & 0xFF00)>>8);
ACPDCVoltage=ACP_13;
ACP_14= can_msgBuf_r[1][4];
ACP_15 = ((ACP_14 & 0x00FF)<<8);
ACP_15 += ((ACP_14 & 0xFF00)>>8);
ACPSpdFeedback=ACP_15;
}
}
}
void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void)
{
IFS2bits.C1IF = 0; // clear interrupt flag
C1_P();
return;
}
void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void)
{
IFS0bits.DMA0IF = 0;
return;
}
void __attribute__((interrupt, no_auto_psv)) _DMA2Interrupt(void)
{
IFS1bits.DMA2IF = 0;
return;
}
谢谢小编愿意把这些东西和大家分享,相信会对很多人产生好的影响
谢谢小编愿意把这些东西和大家分享,相信会对很多人产生好的影响