AVR microcontroller IIC experiment

Publisher:静逸心境Latest update time:2015-06-23 Source: 51heiKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


//Chip ATMEGA16 crystal oscillator 8mhz

 
#include
#include
#define uchar unsigned char
#define uint unsigned int
//-----------------------------
uchar str1[]={"IIC TEST"};
//---------------------------
void delay(uint k) //delay function
{
  uint i,j;
  for(i=0;i
    for(j=0;j<1140;j++);
}

 
#include "1602.h" //1602 library functions
//-------Key input point settings-------
#define s1 (PIND&0x10) //Variable value increase key
#define s2 (PIND&0x20) // variable value reduction key
#define s3 (PIND&0x40) //storage key
#define s4 (PIND&0x80) //Read key
//-------------------
#define TWINT 7 //interrupt flag
#define TWEN 2 //Interrupt
#define TWSTA 5 //Start status bit
#define TWSTO 4 //Stop status bit
//-----TWI status definition, MT master mode transmission, MR master mode reception
#define START 0x08 //Start
#define RE_START 0x10 // Restart
#define MT_SLA_ACK 0x18 //Host response
#define MT_SLA_NOACK 0x20 // Host does not respond
#define MT_DATA_ACK 0x28 //Host responds after data transmission
#define MT_DATA_NOACK 0x30 // Host does not respond after data transmission
#define MR_SLA_ACK 0x40 //Slave response
#define MR_SLA_NOACK 0x48 //Slave no response
#define MR_DATA_ACK 0x50 //Slave data response
#define MR_DATA_NOACK 0x58 //Slave data no response
//-------------------------
#define start() (TWCR=(1<
#define stop() (TWCR=(1<
#define wait() {while(!(TWCR&(1<
#define testack() (TWSR&0xf8) //TWI status detection, shield pre-scaling bit
#define setack() (TWCR|(1<
#define setnoack() (TWCR&=~(1<
#define twi() (TWCR=(1<
#define writebit(x) {TWDR=(x);TWCR=(1<
//------I/0 port settings------------
void portinit()  
{
  PORT=0xff;
  DDRA=0xff;
  PORTB=0xff;
  DDRB=0xff;
  PORTC=0xff;
  DDRC=0xff;
  PORTD=0xff;
  DDRD=0x00;
}
//-----------Read data function----------
uchar iicread(uchar address)
{
  flying tempo;
  start();
  wait();
  if(testack()!=START) return 0;
  writebit(0xa0);
  wait();
  if(testack()!=MT_SLA_ACK) return 0;
  writebit(address);
  wait();
  if(testack()!=MT_DATA_ACK) return 0;
  start();
  wait();
  if(testack()!=RE_START) return 0;
  writebit(0xa1);
  wait();
  if(testack()!=MR_SLA_ACK) return 0;
  twi();
  wait();
  if(testack()!=MR_DATA_NOACK) return 0;
  temp=TWDR;
  stop();
  return temp;
}
//---------------Write data function----------------
uchar iicwrite(uchar address,uchar data)
{
  start();
  wait();
  if(testack()!=START) return 1;
  writebit(0xa0);
  wait();
  if(testack()!=MT_SLA_ACK) return 1;
  writebit(address);
  wait();
  if(testack()!=MT_DATA_ACK) return 1;
  writebit(data);
  wait();
  if(testack()!=MT_DATA_ACK) return 1;
  stop();
  delay(10);
  return 0;
}

 
void main()
{
  uchar val=0; //data value variable
  portinit();
  delay(200);
  heat();
  delay(200);
  display(2,0,str1);
  delay(200);
  while(1)
  {
    displayz(5,1,val/10+0x30);
    displayz(6,1,val%10+0x30);
    delay(10);
    if(s1==0) //value increase key
    {
      delay(100); //Software delay 100ms to prevent key jitter
      if(s1==0)
      {
      val++;
      if(val>30)val=0;
    }
    }
    if(s2==0) //value decrease key
    {
      delay(100);
      if(s2==0)
      {
      val--;
      if(val<1)val=40;
    }
    }
    if(s3==0) //value storage key
    {
      delay(100);
      if(s3==0)
      {
      iicwrite(70,val);
    }
    }
    if(s4==0) //value read key
    {
      delay(100);
      if(s4==0)
      {
      val=iicread(70);
    }
    }
  }
}
[page]
 
//-------------------1602.h--------------------
//------------1602.h-------------
 
#define RS1 PORTB_Bit0=1 
#define RS0 PORTB_Bit0=0
#define RW1 PORTB_Bit1=1
#define RW0 PORTB_Bit1=0
#define EN1 PORTB_Bit2=1
#define EN0 PORTB_Bit2=0
#define DATAPORT PORTA
#define busy 0x80
void wait()
{
  fly shaft;
  DATAPORT=0xff;
  RS0;
  RW1;
  __no_operation();
  EN1;
   __no_operation();
    __no_operation();
     DDRA=0x00;
     val=PINA;
     while(val&busy)val=PINA;
     EN0;
     DDRA=0xff;
}

 
void writecmd(uchar w)
{
  wait();
  RS0;
  RW0;
   __no_operation();
  DATAPORT=in;
   __no_operation();
   EN1;
    __no_operation();
     __no_operation();
     EN0;
}

 
void writedata(uchar data)
{
  wait();
  RS1;
  RW0;
   __no_operation();
   DATAPORT=data;
    __no_operation();
    EN1;
     __no_operation();
      __no_operation();
      EN0;
      
}

 
void delay_nms(uint k)
{
  uint i,j;
  for(i=0;i
    for(j=0;j<1140;j++);
}

 
void init()
 
{
 delay_nms(15);
writecmd(0x38);
delay_nms(5);
writecmd(0x38);
delay_nms(5);
writecmd(0x38);
writecmd(0x80);
writecmd(0x01);
writecmd(0x06);
writecmd(0x0c);
}

 
void display(fly x,fly y,fly *p)
{

 
fly add=0x80;
y=y&0x01;
x=x&0x0f;
if(y)add=add+0x40;
writecmd(add+x);
while(*p!='')
{
  writedata(*p++);
}
}

 
void displayz(fly x,fly y,fly k)
{
  fly add=0x80;
  y=y&0x01;
  x=x&0x0f;
  if(y)add=add+0x40;
  writecmd(add+x);
  writedata(k);
}
Keywords:AVR Reference address:AVR microcontroller IIC experiment

Previous article:AVR microcontroller frequency meter design frequency input source STM32 port
Next article:AVR CTC mode waveform output experiment

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号