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

📄 oput.c

📁 avr单片机
💻 C
字号:
//======================================================调机自动化控车程序
//ICC-AVR application builder : 2006-9-2 0:12:00
// Target : M128
// Crystal: 16.000Mhz
//======================================================包含文件
#include <iom128v.h>
#include <macros.h>
#include <D:\kcb\kcbtest1.h>
//======================================================定义外部固定地址变量CAN,IO
//define mappings
void mapping_init(void)
{
 asm(
  ".area memory(abs)\n"
  ".org 0xD000\n"
  " _CAN0Addr:: .blkb 50\n"
  ".org 0xE000\n"
  " _CAN1Addr:: .blkb 50\n"
  ".org 0xF000\n"
  " _IOAddr:: .blkb 50\n"
  ".text\n"
 );
}
//======================================================控制器IO口定义
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0xFF;
 DDRB  = 0xFF;
 PORTC = 0xFF; //m103 output only
 DDRC  = 0xFF;
 PORTD = 0xFF;
 DDRD  = 0xF0;
 PORTE = 0xFF;
 DDRE  = 0x08;
 PORTF = 0xFC;
 DDRF  = 0x0C;
 PORTG = 0x1F;
 DDRG  = 0x00;
}
//======================================================内部看门狗初始化
//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
}


//======================================================串口初始化
//UART0 initialisation
// desired baud rate:38400
// actual baud rate:38462 (0.2%)
// char size: 8 bit
// parity: Disabled

void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = 0x19; //set baud rate lo
 UBRR0H = 0x00; //set baud rate hi
 UCSR0B = 0xB8;
}


//======================================================串口0发送中断
#pragma interrupt_handler uart0_udre_isr:20
void uart0_udre_isr(void){
   if(TxOperateSp0!=TxLoadSp0){
	  UDR0=TxBuffer0[TxLoadSp0++]; 
   }
   else{UCSR0B&=0XDF;}
   
}


//======================================================time0 初始化
//TIMER0 initialisation - prescale:32
// WGM: Normal
// desired value: 1KHz
// actual value:  1.000KHz (0.0%)
void timer0_init(void){
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode//同步方式,允许更新计数,比较输出,控制寄存器
 TCNT0 = 0x83; //set count
 OCR0  = 0x7D;
 TCCR0 = 0x05; //start timer//128分频
}
//======================================================time0 中断
#pragma interrupt_handler timer0_comp_isr:16
void timer0_comp_isr(void){
   TCNT0 = 0x83;
   T0Count0++;
   T0Count1++;
   T0Count2++;
   T0Count3++; 
   T0Count4++;
 //compare occured TCNT0=OCR0
}
//======================================================速度中断处理函数

//======================================================CPU初始化
//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider//系统时钟分频控制寄存器
 XMCRA = 0x0E; //external memory//外部存储器控制寄存器
 XMCRB = 0x80;
 mapping_init();
 port_init();
 //watchdog_init();
// timer1_init();
 timer0_init();

 //adc_init();
 
 MCUCR = 0xC0;//MCU控制寄存器
 EICRA = 0x0A; //extended ext ints//外部中断控制寄存器
 EICRB = 0x0F; //extended ext ints
 EIMSK = 0x33; //0x03 extended ext enable
 TIMSK = 0x12; //timer interrupt sources  01:time0
 ETIMSK = 0x00; //extended timer interrupt sources
 uart0_init();
 SEI(); //re-enable interrupts
	  // MyselfCan0Addr=1;
	  // MyselfCan1Addr=2;
}
//======================================================看门狗处理函数
void WatchDog(void){
        DDRE|= 0x08;
		PORTE&= 0xF7;
		PORTE|= 0x08;
		PORTE&= 0xF7;
		PORTE|= 0x08;
		PORTE&= 0xF7;
		PORTE|= 0x08;
		WDR();
     }



//===============================================初始设置
void set_value(void){
   
//   V_average_incept.Word=0;
  // Cr_average_incept.Word=0;
   OutputByte0Reg = 255;
   OutputByte1Reg = 255;
   OutputByte2Reg = 255;
}


//将int型变量转换为bcd码,并填充到TxBuffer0[]中
/********************************************************************************/
void tx0_int_to_bcd(int i)
{
   UC j,head=0;
   if(i<0){
      i=-i;
      TxBuffer0[TxOperateSp0++]='-';
   }	  
   j=i/10000;i%=10000;if(j){TxBuffer0[TxOperateSp0++]=j+0x30;head=1;}
   j=i/1000;i%=1000;if(j||head){TxBuffer0[TxOperateSp0++]=j+0x30;head=1;}
   j=i/100;i%=100;if(j||head){TxBuffer0[TxOperateSp0++]=j+0x30;head=1;}
   j=i/10;i%=10;if(j||head){TxBuffer0[TxOperateSp0++]=j+0x30;head=1;}
   TxBuffer0[TxOperateSp0++]=i+0x30;
}
/*******************************************************************************
将uc型变量转换为bcd码,并填充到TxBuffer0[]中
********************************************************************************/
void tx0_uc_to_bcd(UC i)
{  
   UC j,head=0;
   j=i/100;i%=100;if(j){TxBuffer0[TxOperateSp0++]=j+0x30;head=1;}
   j=i/10;i%=10;if(j||head){TxBuffer0[TxOperateSp0++]=j+0x30;head=1;}
   TxBuffer0[TxOperateSp0++]=i+0x30;
}


					   														   										
void io_test(void){
   UC i=0;
   UI Delay;
   if(T0Count4>500){//||(RxFlag==ON)){
      T0Count4=0;
//	  RxFlag=OFF;  
      OutputByte0Reg=OutputTable[TestIOSp][0];
      OutputByte1Reg=OutputTable[TestIOSp][1];
      OutputByte2Reg=OutputTable[TestIOSp][2];
   
      IOAddr[OutputByte0]=OutputByte0Reg;
      IOAddr[OutputByte1]=OutputByte1Reg;
      IOAddr[OutputByte2]=OutputByte2Reg;
   
      for(Delay=0;Delay<5000;Delay++);
   
      while(i<5){
         if(InputByte0Reg==IOAddr[InputByte0])i++;
         else {InputByte0Reg=IOAddr[InputByte0];i=0;}
      }
      i=0;
      while(i<5){
         if(InputByte1Reg==IOAddr[InputByte1])i++;
         else {InputByte1Reg=IOAddr[InputByte1];i=0;}
      }
      i=0;
      while(i<5){
      if(InputByte2Reg==IOAddr[InputByte2])i++;
         else {InputByte2Reg=IOAddr[InputByte2];i=0;}
      }
      i=0;
      while(i<5){
      if(InputByte3Reg==IOAddr[InputByte3])i++;
         else {InputByte3Reg=IOAddr[InputByte3];i=0;}
      }
      i=0;
      TxBuffer0[TxOperateSp0++]=0x20;
  tx0_uc_to_bcd(OutputByte0Reg);
  TxBuffer0[TxOperateSp0++]=0x20;
  tx0_uc_to_bcd(OutputByte1Reg);
  TxBuffer0[TxOperateSp0++]=0x20;
  tx0_uc_to_bcd(OutputByte2Reg);
  TxBuffer0[TxOperateSp0++]=0x20;
  tx0_uc_to_bcd(InputByte0Reg);
  TxBuffer0[TxOperateSp0++]=0x20;
  tx0_uc_to_bcd(InputByte1Reg);
  TxBuffer0[TxOperateSp0++]=0x20;
  tx0_uc_to_bcd(InputByte2Reg);
  TxBuffer0[TxOperateSp0++]=0x20;
  tx0_uc_to_bcd(InputByte3Reg);
     // if(InputByte0Reg!=InputTable[TestIOSp][0])IOErrorFlag=ON;
     // else if(InputByte1Reg!=InputTable[TestIOSp][1])IOErrorFlag=ON;
      //else if(InputByte2Reg!=InputTable[TestIOSp][2])IOErrorFlag=ON;
      //else if(InputByte3Reg!=InputTable[TestIOSp][3])IOErrorFlag=ON;
	  //else IOErrorFlag=OFF;
	 // tx0_operate();
	 if(InputByte0Reg==InputTable[TestIOSp][0]&& InputByte1Reg==InputTable[TestIOSp][1]
   && InputByte2Reg==InputTable[TestIOSp][2] && InputByte3Reg==InputTable[TestIOSp][3])
   {
    // IOErrorFlag=0;
	 PORTB=0xFF;
	 TxBuffer0[TxOperateSp0++]=0x20;
	 tx0_uc_to_bcd(TestIOSp);
	 TxBuffer0[TxOperateSp0++]= 'O';
	 TxBuffer0[TxOperateSp0++]= 'k';
	 TxBuffer0[TxOperateSp0++]=0x20;
	 UCSR0B|=0X20;
	 
	}
   else 
   {
    //IOErrorFlag=1;
	PORTB=0x00;
	TxBuffer0[TxOperateSp0++]=0x20;
	tx0_uc_to_bcd(TestIOSp);
	TxBuffer0[TxOperateSp0++]= 'E';
	TxBuffer0[TxOperateSp0++]= 'r';
	TxBuffer0[TxOperateSp0++]= 'r';
	TxBuffer0[TxOperateSp0++]= 'O';
	TxBuffer0[TxOperateSp0++]= 'r';
	TxBuffer0[TxOperateSp0++]=0x20;
	UCSR0B|=0X20;
    }
	TestIOSp++;TestIOSp&=0x0F;
  }

} 
//===============================================主程序
void main(void){
   WatchDog();
   watchdog_init();
   init_devices();
   //can_init(); 
   set_value();
   while(1){
      io_test(); 
      WatchDog();
     // speed_measure();
      //can_send_0();
	  //can_send_1();
	  //receive_operate0();
	 // transmit_operate0();
	 // receive_operate1();
	 // transmit_operate1();
	 // rx0_operate();
	  
   } 
}    
	  

⌨️ 快捷键说明

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