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

📄 can162.c

📁 Atmel mcu can interface design example
💻 C
字号:
//ICC-AVR application builder : 2007-7-21 15:11:59
// Target : M162
// Crystal: 16.000Mhz

#include <iom162v.h>
#include <macros.h>
#include "can.h"
//extern unsigned char RX_Buffer[13];
//#define uchar unsigned char 
//#define uint  unsigned int
unsigned char TX_Buffer[13];
unsigned char RX_Buffer[13];
unsigned char timer_ok;
unsigned char timer_count;
unsigned char RevFlag;
unsigned int count;

/**********************************************************
                     软延时
**********************************************************/					 
					 
void delay_1ms(void) 
{
 unsigned int k; 
 for(k = 0; k < (16*142 - 2); k++) // 定时1ms公式:xtal*142-2
 	 ;  
	 //for(k = 0; k < 2; k++);
}
void Delay(unsigned int n)
{
 unsigned int p; 
 for(p = 0; p < n; p++) 
 {
	   delay_1ms();
 }
}

void port_init(void)
{
   PORTA = 0x00;
   DDRA  = 0xFF;
   PORTB = 0xF0;
   DDRB  = 0x00;
   PORTC = 0x7F; 
   DDRC  = 0xFF;
   PORTD = 0x04;
   DDRD  = 0xFA;
   PORTE = 0x07;
   DDRE  = 0x07;
}

//Watchdog initialisation
// prescale: 2048K cycles
void watchdog_init(void)
{
 WDR(); //this prevents a timout on enabling
 WDTCR= 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}

//TIMER1 initialisation - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 50Hz
// actual value: 50.000Hz (0.0%)
void timer1_init(void)
{
 TCCR1B= 0x00; //stop
 TCNT1H= 0xFB; //setup
 TCNT1L= 0x1E;
 OCR1AH= 0x04;
 OCR1AL= 0xE2;
 OCR1BH= 0x04;
 OCR1BL= 0xE2;
 TCCR1A= 0x00;
 TCCR1B= 0x04; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:16
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed 
 TCNT1H= 0xFB; //reload counter high value
 TCNT1L= 0x1E; //reload counter low value
 timer_ok = 1;
 count++;
 if(count == 30)
 {
    count = 0;
    PORTD ^= (1<<PD4); //程序在运行
 }

}
/*
//TIMER0 initialisation - prescale:256
// WGM: Normal
// desired value: 1000Hz
// actual value: 1008.065Hz (0.8%)
void timer0_init(void)
{
 TCCR0= 0x00; //stop
 TCNT0= 0xFE; //set count
 OCR0= 0x3E; //set compare value
 TCCR0= 0x04; //start timer
 TIMSK = 0x02;
}

#pragma interrupt_handler timer0_ovf_isr:18
void timer0_ovf_isr(void)
{
 TCNT0= 0xFE; //reload counter value
 timer_ok = 1;
 count++;
 if(count == 1)
 {
    count = 0;
    PORTD ^= (1<<PD4); //程序在运行
 }
}*/

#pragma interrupt_handler int0_isr:2
//中断处理可以考虑放外边
void int0_isr(void)
{
   unsigned char reg;
   unsigned char CompCount;
   unsigned char can_status;
   CLI();						 //关CPU中断
   RevFlag = 1;
   reg = read_sja(IER);				 //保存SJA1000的中断允许状态
   write_sja(IER, 0x00);			 //重设中断允许状态为不允许任何中断
   can_status = CanReceive();		 //接收消息
   if(can_status == 0x01)
   {
	  for(CompCount = 5; CompCount < 13; CompCount++)				//检查接收到的数据是否正确
	  {
	     if(RX_Buffer[CompCount] != TX_Buffer[CompCount])
         {
		    RevFlag = 0;	   						  	//发现有不一致时,则清0标志位 
	     } 
      } 
   }
   else
   Init_CAN();		 	 	 	 //如果不是正确接收,则重新初始化
   write_sja(IER, reg);			 //重新写回中断允许模式
   SEI();
}     

// **Section [PCHINT1] not found**
//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 watchdog_init();
 //timer1_init();

 MCUCR = 0x82; 
 EMCUCR = 0x00;
 //GIMSK= 0x00;
 TIMSK = 0x80; //timer interrupt sources
 //ETIMSK = 0x00;
 GICR = 0x40;
 //PCMSK0 = 0x00;
 //PCMSK1 = 0x00;
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

void main(void)
{  
   init_devices();
   WDR();
   
   //Delay(1000);
   
   Init_CAN();			     //CAN初始化	
   WDR();
   timer1_init();
   WDR();
   
   while(1)
   {
	  PORTD |= (1<<PD1);//初始化成功
	  WDR();
	  if(timer_ok == 1)
	  {
	     timer_ok = 0;
		 timer_count++;
		 
		 if(timer_count == 60)
		 {
		    timer_count = 0;
			//TCCR1B = 0x00;
			CanTransmit(); 	 //发送数据
		    PORTD ^= (1<<PD3);
			//TCCR1B = 0x04;
		 }
		 
	  }
	  //Delay(50);
	  //Delay(100);
	  WDR();  
	  if(RevFlag == 1)		 //如果正确接收到数据
	  {
	     RevFlag = 0;
		 PORTD ^= (1<<PD5); 
		 //write_sja(CMR, 0x04);	
	  }
	  
   }
}

⌨️ 快捷键说明

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