⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Avr单片机 At90can32 CAN 总线通信原代码 c语言
💻 C
字号:
#include "display.h"
#include "key.h"
#include "K_Line.h"
#include "Blue_Tooth.h"
#include "can_lib.h"
#include "can_drv.h"

//*******************************************
#define		F_CPU	8000000		/* 单片机主频为8MHz,用于延时子程序 */

//***************************
//extern void CAN(void);
//****************************
void DelayMs(uint t)
{
	uint i;
	for(i =0;i<t;i++)
		_delay_loop_2( 250*FREQ	);
}

//K线中断接受
SIGNAL(SIG_UART0_RECV)
{
	uchar c = UDR0;
	if(g_bRxdLen>0)
	{
		g_aRecvBuf[g_bRxdPos++] = c;
		g_bRxdLen--;
	}
}

//K线中断发送
SIGNAL(SIG_UART0_TRANS)
{
	if(--g_bTxdLen>0)
		UDR0 = g_aSendBuf[++g_bTxdPos];
}

					//Blue_Tooth
//=====================================================
//Blue_Tooth中断接受
SIGNAL(SIG_UART1_RECV)
{
	uchar c = UDR1;
	if(Blue_g_bRxdLen>0)
	{
		Blue_g_aRecvBuf[Blue_g_bRxdPos++] = c;
		Blue_g_bRxdLen--;
	}
}

//Blue_Tooth中断发送
SIGNAL(SIG_UART1_TRANS)
{
	if(--g_bTxdLen>0)
		UDR1 = Blue_g_aSendBuf[++Blue_g_bTxdPos];
}


//=====================================================
uchar Can_data[9];
U8 Can_cammand[8];
U32 CAN_ID[10];

//****************************
int		main()
{
	uchar Key;
	uchar i,b;		
	Pin = 1;	
//	Xs();	
	CAN_INIT();
	i = CAN_TEST_SHOU(Can_data,&CAN_ID[0]);
	i = CAN_SHOU(Can_data);
	Can_cammand[0] = 0x33;
	Can_cammand[1] = 0x44;
	Can_cammand[2] = 0x55;
	Can_cammand[3] = 0x66;
	Can_cammand[4] = 0x77;
	i = CAN_FA(5,Can_cammand);
//	K_Line_Init();
//	Blue_Tooth_Init();
//	Key_InIt();
//	Key = GetKey();
//	sei();

					//液晶
	//==========================================================
			DDRC |=_BV(PC0) | _BV(PC1) | _BV(PC2);
			PORTC|=_BV(PC0) | _BV(PC1) | _BV(PC2);
			DDRG |=_BV(PG0) | _BV(PG1);
			PORTG|=_BV(PG0) | _BV(PG1);
			lcd_init();			/* 初始化函数 */
			lcd_clear();         /* 清屏函数 */
			Xs();
			while(1)
			{
				i = 0;
				i = 1;
			}

}
//==========================================================
				//CAN应用程序部分
//==========================================================
/*
void CAN(void)
{
	U8 i, u8_temp;
	U8 led_out = 0;
	
    CLKPR = 0x80;  CLKPR = 0x00;	
    CAN_PORT_DIR &= ~(1<<CAN_INPUT_PIN );	
    CAN_PORT_DIR &= ~(1<<CAN_OUTPUT_PIN);		
    CAN_PORT_OUT |=  (1<<CAN_INPUT_PIN );			
    CAN_PORT_OUT |=  (1<<CAN_OUTPUT_PIN);			
//    while ((CAN_PORT_IN & (1<<CAN_INPUT_PIN)) != 0);        
    Can_reset();        
    can_init((U16)CAN_BAUDRATE);        
    CANTCON = CANBT1;                   
    while(1)
    {
        message.pt_data = &buffer[0];
        for(i=0; i<8; i++) buffer[i]=0;		// --- Init Rx data
        message.cmd = CMD_RX_DATA_MASKED;//CMD_RX_DATA;//        		// --- Rx Command
		//========================
			message.id.ext = 0x7e8;
			message.ctrl.ide = 1;
		//========================
        while(can_cmd(&message) != CAN_CMD_ACCEPTED);	// --- Enable Rx
        while(1)
        {												// --- Wait for Rx completed
            u8_temp = can_get_status(&message);
            if (u8_temp != CAN_STATUS_NOT_COMPLETED) break; // Out of while
        }
        if (u8_temp == CAN_STATUS_ERROR) break; // Out of the function
        // --- Tx Command
		//===========================
			message.pt_data = &buffer[0];
            for(i=0; i<8; i++) 
			{
				buffer[i]=i+8;
			 }
		//=============================
			message.id.ext = 0x7e0;			
			message.ctrl.ide  = 1;
			message.ctrl.rtr  = 1;			
		//==============================
        message.dlc = 8;
        message.cmd = CMD_TX_DATA;        //CMD_TX
        while(can_cmd(&message) != CAN_CMD_ACCEPTED);	// --- Enable Tx
        while(1)										// --- Wait for Tx completed        
        {
            u8_temp = can_get_status(&message);
            if (u8_temp != CAN_STATUS_NOT_COMPLETED) break; // Out of while
        }        
//zx        if (u8_temp == CAN_STATUS_ERROR) break;     // ---- Exit if CAN error(s)
        
    }
}	
//==========================================================
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -