打印
[PIC®/AVR®/dsPIC®产品]

PIC18F46K80 CAN环回自测试模式

[复制链接]
1934|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
RW506575086|  楼主 | 2017-11-1 16:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
初次使用PIC单片机,学习CAN总线,从网上找到这个程序,仿真时程序会停止在 while( TXB0CONbits.TXREQ){;},一直未发送完成,使用的是内部时钟,哪位大神帮忙看看,感激不尽!



#include <p18cxxx.h>
/************************************************************/
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = SWDTDIS  // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-03FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 04000-07FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 08000-0BFFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 0C000-0FFFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-03FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 04000-07FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 08000-0BFFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 0C000-0FFFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-03FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 04000-07FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 08000-0BFFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 0C000-0FFFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)
/**************************************************************************8*/


int CAN_FLAG;
//unsigned int CAN_Delay_count=0;
char CAN_Adress_H,CAN_Adress_L;
void initcan()
{
TRISB=(TRISB|0X08)&0XeB; //设置CANRX/RB3为输入,CANTX/RB2为输出
//设置CAN的波特率为250K,在Fosc=8M时,Tbit=8us,假设BRP=01h
//则TQ=[2*(1+BRP)]/Fosc=2*(1+1)/4=1us。
//NOMINAL BIT RATE=8TQ,SJW=1,Sync_Seg=1TQ,Prog _Seg=1TQ,Phase_Seg1=3TQ,Phase_Seg2=3TQ
CANCON=0X80; //请求进入CAN配置模式REQOP=100
while(CANSTAT&0X80==0){;} //等待进入CAN配置模式OPMODE=100
BRGCON1=0X01; //设置SJW和BRP,SJW=1TQ,BRP=01H
BRGCON2=0X90; //设置Phase_Seg1=3TQ和Prog _Seg=1TQ
BRGCON3=0X42; //设置Phase_Seg2=3TQ
//设置发送邮箱0标识符号和发送的数据
TXB0CON=0X03; //发送优先级为最高优先级,TXPRI=11
TXB0SIDH=CAN_Adress_H; //设置发送缓冲器0的标准标识符,本程序采用标准识符
TXB0SIDL=CAN_Adress_L&0XE0;
TXB0DLC=0X08; //设置数据长度为8个字节
TXB0D0=0X00; //写发送缓冲器数据区的数据
TXB0D1=0X01;
TXB0D2=0X02;
TXB0D3=0X03;
TXB0D4=0X04;
TXB0D5=0X05;
TXB0D6=0X06;
TXB0D7=0X07;
//设置接收邮箱0的标识符和初始化数据
RXB0SIDH=CAN_Adress_H; //设置接收缓冲器0的标识符
RXB0SIDL=CAN_Adress_L&0XE0;
RXB0CON=0X20; //仅仅接收标准标识符的有效信息,FILHIT0=0表示RXB0采用filter0
RXB0DLC=0X08; //设置接收缓冲器0的数据区长度
RXB0D0=0X00; //初始化接收缓冲器0的数据区数据
RXB0D1=0X00;
RXB0D2=0X00;
RXB0D3=0X00;
RXB0D4=0X00;
RXB0D5=0X00;
RXB0D6=0X00;
RXB0D7=0X00;
//初始化接收滤波器0和接收屏蔽,
RXF0SIDH=CAN_Adress_H;
RXF0SIDL=CAN_Adress_L;
RXM0SIDH=0XFF;
RXM0SIDL=0XE0;

//初始化CAN模块的I/O控制寄存器
//CIOCON=0X00;
//CIOCONbits.CLKSEL = 1;
//使CAN进入某种工作模式模式
CANCON=0X40; //=0X40,进入自测试模式;=0x00,正常操作模式
//while(CANSTAT_0&0XE0!=0){;}
while(CANSTAT&0X40==0){;}
//初始化CAN的中断
PIR5=0X00; //清所有中断标志
PIE5=PIE5|0X01; //使能接收缓冲器0的接收中断
IPR5=IPR5|0X01; //接收缓冲器0的接收中断为最高优先级
;
}
#pragma interrupt can_isr
#pragma code high_ISR=0x08
void high_ISR()
{
_asm
goto can_isr
_endasm
}
#pragma code
void can_isr()
{
   CAN_FLAG=1;
PIR5bits.RXB0IF=0;
RXB0CONbits.RXFUL=0;
}
void delay()
{
int i,j;
for(i=0;i<500;i++)
for(j=0;j<100;j++)
{;}
}
void main()
{
  CAN_Adress_H=0X33;
CAN_Adress_L=0XE0;
INTCON=0x00; //禁止所有中断
CAN_FLAG=0;
initcan();
RCONbits.IPEN=1;
WDTCON=0; //=1,看门狗使能;=0,禁止看门狗
INTCON=INTCON|0xc0; //使能中断
while(1)
{
    if(CAN_FLAG==1)
{
CAN_FLAG=0; //清接收到标志
  TXB0CONbits.TXREQ=0; //禁止发送
TXB0D0=RXB0D0+1; //用接收数据加1来更新发送数据
TXB0D1=RXB0D1+1;
TXB0D2=RXB0D2+1;
TXB0D3=RXB0D3+1;
TXB0D4=RXB0D4+1;
TXB0D5=RXB0D5+1;
TXB0D6=RXB0D6+1;
TXB0D7=RXB0D7+1;
}
else
{
TXB0CON=TXB0CON|0X08;
while( TXB0CONbits.TXREQ){;} //等待发送完成
// while(!TXB0IF) {;}
//PORTB&=0xEF;
}
}
}
沙发
duan050790| | 2017-11-3 13:32 | 只看该作者
我做的回环测试的时候,设置loop位和分频,在主循环发送,就可以了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

5

帖子

0

粉丝