×
单片机 > 单片机程序设计 > 详情

armega16与24c16的操作

发布时间:2020-06-22 发布时间:
|
atmega16有硬件IIC(TWI)接口,不需模拟,直接操作寄存器即可

 

 

#include
#include
#define uchar unsigned char
#define uint unsigned int

#define Start() TWCR|=(1< #define Stop() TWCR|=(1< //终止信号
#define Wait() while(!(TWCR&(1<             //等待数据发送完
#define Test() (TWSR&0XF8)                            //检查twsr的状态码
#define Write(X) {TWDR=X;TWCR=(1< //写字节
#define Read() TWCR|=(1<            //读字节

extern const uchar table[]={
~0x3f,~0x06,~0x5b,~0x4f,
~0x66,~0x6d,~0x7d,~0x07,
~0x7f,~0x6f,~0x77,~0x7c,
~0x39,~0x5e,~0x79,~0x71};
void delay(uint t)
{
   uint i,j;
   for(i=0;i<10;i++)
   for(j=0;j }

void init()
{
   TWSR=0;
  TWBR=0X23;
  TWCR=0X44;
}
//可以通过读出Test()来看TWSR处于什么状态,也可以不用使用Test()

uchar write()
{
    Start();
   Wait();
//   if(Test()!=0x08)
//   return 0;
   Write(0);
   Wait();
  
   if(Test()!=0x18)
   return 0;
   Write(3);
  
   Wait();
 
//   if(Test()!=0x28)
 //  return 0;
   Write(56);
   Wait();
   
  // if(Test()!=0x28)
  // return 0;
 
   Stop();
   delay(1000);
}
uchar read()
{
  
 
   uchar dat;
   TWSR=0;
   Start();
   Wait();
   //if(Test()!=0x08)
  // return 0;
 
   Write(0);
 
   Wait();
  
 
   //if(Test()!=0X18)
   //return 0;
   Write(3);
   Wait();
  
  // if(Test()!=0x28)
  // return 0;
 
 
   Start();
   Wait(); 
  // if(Test()!=0x10)//这里必须写10,因为是第二次开始
  // return 0;
   Write(0xa1);
   Wait();

   if(Test()!=0x40)
   return 0;
   Read();
   Wait();
    
  // if(Test()!=0x58)   //读完无应答
  // return 0;
   dat=TWDR;
  
   Stop();
  
  
}
void main()
{
     uchar z,shi,ge;
  init();
  write();
 
  z=read();
  DDRA=0XFF;
 
  shi=z/10;
  ge=z;
 
 
 while(1) {
 
 
    PORTB=0xfe;
  PORTA=table[shi];
  delay(10);
  PORTB=0xfd;
        PORTA=table[ge];
  delay(10); 
    
  }
}

关键字:armega16  24c16 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
C51 特殊功能寄存器SFR的名称和地址