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

📄 88l89.c

📁 使用AVR162单片机开发的C代码
💻 C
字号:
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0xFF;
 PORTB = 0x10;
 DDRB  = 0xBF;
 PORTC = 0x00; 
 DDRC  = 0x48;
 PORTD = 0xF7;  // 11110111
 DDRD  = 0xFA;  // PD7:RD PD6:WR PD5:RS PD4:CS PD3:A PD2:INT0 PD1:TX0 PD0:RX0
 PORTE = 0x00;
 DDRE  = 0x07;
}

// Watchdog initialisation - prescale:2048K cycles
void watchdog_init(void)
{
 WDR();       //this prevents a timout on enabling
 WDTCR |= (1<<WDCE) | (1<<WDE); // 使能看门狗定时器
 WDTCR= 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}

// TIMER0 initialisation - prescale:1
void timer0_init(void)
{
 TCNT0 = 0x00;
 TCCR0 = 0x05; // 选择CLKi/o作为T0时钟源,没有分频1us
 //TIMSK = 0x02; // 使能T0中断
}

// TIMER1 initialisation - prescale:1
void timer1_init(void)
{
 TCCR1B = 0x00; //stop timer
 TCNT1H = 0x00; //set count value
 TCNT1L = 0x00;
 TCCR1B = 0x01; //start timer
}

// SPI initialisation
void spi_init(void)
{
 SPCR= 0x00; // diable spi
 SPSR= 0x01; //
 SPCR= 0x5D; // setup SPI
}

// call this routine to initialise all peripherals
void cpu_init(void)
{
 // stop errant interrupts until set up
 CLI();           //disable all interrupts
 port_init();
 watchdog_init();
 timer0_init();
 timer1_init();
 spi_init();

 MCUCR= 0x02;   // int0 falling edge
 TIMSK= 0x82;   // timer0 and timer1 interrupt sources
 GICR= 0x40;    // int0 interrupt sources
 SEI(); 	      //re-enable interrupts
 // all peripherals are now initialised
}

void OpenExternalMemory(void)
{
    MCUCR = 0x82;			// enable external memory and I/O
    EMCUCR = 0x14;    // 0x0500-0x1FFF=1 wait, 0x2000-0xFFFF=0 wait
    SFIOR = 0x78;			// enable bus keeper, use all of PC7-PC0 as address
}

void CloseExternalMemory(void)
{
    MCUCR = 0x02;			// disnable external memory and I/O
    EMCUCR = 0x00;    // 0x0500-0x1FFF=1 wait, 0x2000-0xFFFF=0 wait
    SFIOR = 0x00;			// enable bus keeper, No use PC7-PC0 as address
}

void InitDTMF() // PD4:CS, PD5:RS0, PD6:WR, PD7:RD
{
  unsigned char tmp;
  
  DDRA = 0xF0;  // input
      
  PORTD &= 0x6F; // 1
  tmp = PINA;    
  PORTD |= 0xF0;
  
  DDRA = 0xFF;  // output
  
  PORTD &= 0xAF; // 2
  PORTA = 0x00; // b3b2b1b0 = 0000
  PORTD |= 0xF0;
  
  PORTD &= 0xAF; // 3 
  PORTA = 0x00; // b3b2b1b0 = 0000
  PORTD |= 0xF0;
  
  PORTD &= 0xAF; // 4
  PORTA = 0x80; // b3b2b1b0 = 1000
  PORTD |= 0xF0;
  
  PORTD &= 0xAF; // 5
  PORTA = 0x00; //        
  PORTD |= 0xF0;
  
  DDRA = 0xF7; 	// input
  
  PORTD &= 0x6F; // 6
  tmp = PINA;         
  PORTD |= 0xF0;    
 
  // 设置控制寄存器A   
  DDRA = 0xFF;  // output
  
  PORTD &= 0xAF; // 1
  PORTA = 0x0D; // b3b2b1b0 = 1101
  PORTD |= 0xF0;
  
  // 设置控制寄存器B

  PORTD &= 0xAF; // 2
  PORTA = 0x00; 
  PORTD |= 0xF0;
}

void SendDTMF( unsigned char n)
{ 
  DDRA = 0xFF;
  PORTA = n & 0x0F; // b3b2b1b0 = 待发送的数值
  PORTD &= 0x8F;  
  asm("nop");  
  PORTD |= 0xF0; 
}

void INT0_interrupt_handler()
{ 
	int i=0;
	int sd=0;
	int j=0;
	unsigned char tmp;
	
	DDRA = 0xF0;                // ???,???????
  
  	PORTD &= 0x6F;		// ??????
  	asm("nop");
  	tmp = PINA & 0x0F;
	PORTD |= 0xF0;
    
	if((tmp & 0x02)==0x02)      // ????

	{   		
    	sFlag = 1;
	}
	else if((tmp & 0x04)==0x04) // ????
	{
		open_sendcounter=0;       //?????????? 
    
    PORTD &= 0x4F; // ????????
    asm("nop");
    tmp = PINA & 0x0F;   
	PORTD = 0xF0;
	
    syncRev[rPos] = tmp;
		rPos++;
	}
}

⌨️ 快捷键说明

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