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

📄 func.c

📁 这是Mega8I2c通讯时的主机访问程序.
💻 C
字号:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/delay.h>
#include <compat/twi.h>
#include "main.h"
#include "init.h"
#include "inter.h"
extern volatile  uint16_t cnt_t0; //记录t0发生的次数

/*延时N毫秒*/
void delay_ms(uint ms)
{
    uint i;
	  for (i=0;i<ms;i++)
	  _delay_loop_2(4*250);
}

/*延时微秒*/
void delay_us(int time)
{
  	do 
		  time--;
	  while (time>1);
}

/*字符输出函数*/
void putchar(uchar c)
{	 
    while (!(UCSRA&(1<<UDRE)));
    UDR=c;    
}

/*按键程序*/
uint8_t read_key(void)
 {
 	uint16_t temp; 
  if ((PINB & _BV(PB0)) == 0)
  	 {
  	 	temp=cnt_t0;
  	  while(((cnt_t0-temp)<=1500));
  	  if((PINB & _BV(PB0))==0)
  	  	{
         return 1; // 0键按下
        }
     }
  if ((PINB & _BV(PB1)) == 0)
  	 {
  	 	temp=cnt_t0;
  	  while(((cnt_t0-temp)<=1500));
  	  if((PINB & _BV(PB1))==0)
  	  	{
  	 	  return 2; //1键按下
  	  	}
  	 }
  else {
    return 0; // 键没按下
  }
  
  return;

}

/* 主模式TWI应用函数开始*/

uchar twi_start(void)
{
	TWCR=_BV(TWINT)|_BV(TWSTA)|_BV(TWEN);
	while((TWCR&_BV(TWINT))==0);
	return TW_STATUS;
}

void twi_stop(void)
{
	TWCR=_BV(TWINT)|_BV(TWSTO)|_BV(TWEN);
}

uchar twi_writebyte(uchar c)
{
	TWDR = c;
	TWCR = _BV(TWINT)|_BV(TWEN);
	while((TWCR&_BV(TWINT))==0);
	return TW_STATUS;
}

uchar twi_readbyte(uchar *c,uchar ack)
{
	uchar tmp;
	tmp=_BV(TWINT)|_BV(TWEN);
  if(ack)
  	{
  		tmp|=_BV(TWEA);
  	}
  TWCR = tmp;
  while((TWCR&_BV(TWINT))==0);
  *c=TWDR;
  return TW_STATUS;
 }

 

/*------------------------------解码程序---------------------------------------- 
结果:得到uchar型的键码ir_code,如果解码出错,则ir_code=0xff 
-----------------------------------------------------------------------------*/ 
/*void check_code(void)
{ 
     
      if((custom + _custom)==0xff)
      	{
      		ir_key = custom;
      	}
      else
      	{
      		ir_key = 0xff;
      	}
      
      
  
    // “键码”与“键码反码相与,如果为0则解码正确      
      if((code + _code) == 0xff)
      	{ 
         ir_code = code; 
        } 
      else
        { 
        ir_code = 0xff; //解码出错 
        }    
     
}  */
 

⌨️ 快捷键说明

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