AVR单片机Can总线通信控制程序 下载本文

内容发布更新时间 : 2024/5/15 19:18:15星期一 下面是文章的全部内容请认真阅读。

//=================================================================================== // //

工程名称: 功能描述:

汽车041项目目标板3-6程序 CAN通信,继电器控制,AD采集

// IDE环境: ICCAVR // Init2515: 2515初始化程序

//===================================================================================

#include \#include \#include \#include \#include \#include \#include \#include \#include \#include \

#pragma interrupt_handler Extint0:2 #pragma interrupt_handler timer1_isr:9 #define uint unsigned int #define uchar unsigned char

/******************外部中断常量******************/ #define GLOBAL 7

//全局中断位宏定义

//标准输入输出头文件

#define EXTINT0 6 //外部中断0位宏定义

#define H_SRCLK PORTD|=(1<<6) //上升沿移位

#define L_SRCLK PORTD&=~(1<<6) //下降沿不移位 #define H_SER PORTD|=(1<<7) //595串行数据输 #define L_SER PORTD&=~(1<<7) //595串行数据输 #define H_RCLK PORTD|=(1<<5) //595数据锁存放开 #define L_RCLK PORTD&=~(1<<5) //595数据锁存

uchar Flag=0,Flag1=0,pp=0; uchar Receive_Data=0,Start_Data=0; uchar CAN_TX_Buf[8],CAN_RX_Buf[8];

//************************************************************* //* 函数说明:MCU端口初始化 //* 输入: 无 //* 输出: 无 //* 调用函数:

//************************************************************* void port_init(void) {

DDRA=0x00;

//IO端口初始化

PORTA=0x00;

DDRB=0xff; PORTB=0xff; DDRC=0xff; PORTC=0xff;

DDRD=0xff;

PORTD=0xff;

}

/******************************************* 函数名称: Int_init 功 能: 中断初始化 参 数: 无 返回值 : 无

/********************************************/ void Int_init(void) {

SREG|=BIT(GLOBAL); MCUCR|=0x02;

//使能全局中断

//外部中断0采用下降沿触发

GICR|=BIT(EXTINT0);//使能外部中断0 DDRD&=~(BIT(PD2));

//配置中断管脚为输入

PORTD|=BIT(PD2); //使能中断管脚的上拉 }

void timer1_init(void) {

TCCR1B=0x04;//256分频 TCNT1H=0Xff;//赋初值 TCNT1L=0Xfe;//赋初值 TIMSK|=0x04;//中断使能 SREG|=0x80;//总中断开关 }

/*******************************************