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

📄 init.c

📁 本文档用于描述一体化组合模块485监控板监控对象
💻 C
字号:
//ICC-AVR application builder : 2009-2-11 12:24:03
// Target : M128
// Crystal: 11.059Mhz

#include <iom128.h>
#include <iomacro.h>
#include "init.h"
#include "variable.h"
#include "uart.h"
//unsigned char i;
// unsigned char aa[60];
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0xFF;
 PORTB = 0x00;
 DDRB  = 0xF7;
 PORTC = 0x00;
 DDRC  = 0x78;
 PORTD = 0x00;
 DDRD  = 0xC3;
 PORTE = 0x00;
 DDRE  = 0x56;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;
}

//TIMER0 initialize - prescale:256
// WGM: Normal
// desired value: 1mSec
// actual value:  0.995mSec (0.5%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT0 = 0xD5; //set count
 OCR0  = 0x2B;
 TCCR0 = 0x06; //start timer
}

//#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
#pragma vector = TIMER0_OVF_vect
__interrupt void timer0_ovf_isr(void)
{
  TCNT0 = 0xD5; //reload counter value

  ++ADDesc.Time;
  ++LED_time;
  ++WDT_time;
  ++Check_time;
  ++TransactionTime;
  ++EepromDesc.Time;
  if(EepromDesc.Busy)
  {
    if(EepromDesc.Time>5)
    {
      EepromDesc.Busy=FALSE;
    }
  }
  else
  {
    EepromDesc.Time=0;
  }
}

//SPI initialize
// clock rate: 691187hz
void spi_init(void)
{
 SPCR = 0xD9;//SPCR = 0xD5; //setup SPI
 SPSR = 0x00; //setup SPI
}

//#pragma interrupt_handler spi_stc_isr:iv_SPI_STC
#pragma vector = SPI_STC_vect
__interrupt void spi_stc_isr(void)
{
 //byte in SPDR has been sent/received

 // if((SPSR & 0x80) == 0x80)	//if SPIF = 1				
 // {
//	SPSR &= ~0X80;         //SPIF=0;	
    if(--SpiDesc.Len!=0)
	SPDR=(U8)SpiDesc.Buff[SpiDesc.NO];
    else
	{
		SpiDesc.Busy=0;
		if(SpiDesc.Status[SpiDesc.NO]==1)
		{
                  PORTB |= 0x01;
		}
		SpiDesc.Status[SpiDesc.NO]=0;
	}
//  }
  if((SPSR & 0x40) == 0x40)	//if WCOL=1		
  {
        SPSR &= ~0X40;                //WCOL=0;	
  }

}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = 0x47; //set baud rate low
 UBRR0H = 0x00; //set baud rate high
 UCSR0B = 0xD8;
}

//#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
#pragma vector = USART0_RXC_vect
__interrupt void uart0_rx_isr(void)
{

 //uart has received a character in UDR


  if (UARTContext.RxLen > (UART_SIZE-1))
  {// Data overflow, Reset.
    RESET_RECEIVER();
  }

  if( UARTContext.RxValidFlag )  //Parsing data. wait for next time.
    return;
	

  *UARTContext.RxPt = UDR0;	// Get byte
/*
  if(i>60)
     i=0;
   aa[i++] = *UARTContext.RxPt;
*/


  if( ( PACKAGE_BEGIN == *UARTContext.RxPt ) && (UARTContext.RxBeginFlag != TRUE) )
  {// Begin Receive
	UARTContext.RxBeginFlag=TRUE;	//Begin receive data
	UARTContext.RxLen = 1;
	++UARTContext.RxPt;
  }
  else if( PACKAGE_END == *UARTContext.RxPt )
  {// End Receive
	if( PACKAGE_BEGIN == UARTContext.RxBuff[0])
	{ // A valid Blocks;
		UARTContext.RxValidFlag = TRUE;
		//++UARTContext.RxPt;
		++UARTContext.RxLen;
  	}
  }
  else if( UARTContext.RxBeginFlag )
  {// Receving data
	++UARTContext.RxPt;	//Point to next by RxBuffer array
	++UARTContext.RxLen;
  }	
}

//#pragma interrupt_handler uart0_tx_isr:iv_USART0_TXC
#pragma vector = USART0_TXC_vect
__interrupt void uart0_tx_isr(void)
{
 //character has been transmitted

  /*   if(i>60)
     i=0;
   aa[i++] = *UARTContext.TxPt;
   aa[30] = UARTContext.TxBuff[0];*/
  if(UARTContext.TxLen!=0)
  {
	UDR0 = *UARTContext.TxPt++;	//Transmit byte (UARTContext.TxPt must point to TxBuff)
	--UARTContext.TxLen;
  }
  else
  {	
	PORTE &= ~UART_FLAG;      //UART_FLAG = 0;    //Set receive state
  }

	
}

//ADC initialize
//Conversion time: 2uS
void adc_init(void)
{
 ADCSRA = 0x00; //disable adc
 ADMUX = 0x60; //select adc input 0
 ACSR  = 0x80;
 ADCSRA = 0x8F; //128分频
}

//#pragma interrupt_handler adc_isr:iv_ADC
#pragma vector = ADC_vect
__interrupt void adc_isr(void)
{
 //conversion complete, read value (int) using...
 // value=ADCL;            //Read 8 low bits first (important)
 // value|=(int)ADCH << 8; //read 2 high bits and shift into top byte

  //ADCSRA &= ~0x10; //AD0INT=0;
  ADDesc.ADData[ADDesc.Channel][ADDesc.NO]
	= (U8)(ADC >> 8);  // read AD data, 8 bits

  ++ADDesc.Channel;

  if( ADDesc.Channel > (AD_CHMAX-1) )
  {
	ADDesc.Channel=0;
		
	if( ++ADDesc.NO > (AD_SIZE-1) )
		ADDesc.NO=0;
  }
  //add 2009-02-23/////////
  ADMUX &= ~0x1F;
  /////////////////////////
  ADMUX |= (AD_CH0 + ADDesc.Channel);
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 SREG &= 0x7f; //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory


 port_init();
 timer0_init();
 spi_init();
 uart0_init();
 adc_init();

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x01; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SREG |= 0x80; //re-enable interrupts
 //all peripherals are now initialized
}

⌨️ 快捷键说明

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