
//芯片ATMEGA16 晶振8mhz
#include <iom16.h>
#include <intrinsics.h>
#define uchar unsigned char
#define uint unsigned int
//-----------------------------
uchar str1[]={"IIC TEST"};
//---------------------------
void delay(uint k) //延時函數
{
uint i,j;
for(i=0;i<k;i++)
for(j=0;j<1140;j++);
}
#include "1602.h" //1602庫函數
//-------按鍵輸入點設置-------
#define s1 (PIND&0x10) //變量值增加鍵
#define s2 (PIND&0x20) //變量值減少鍵
#define s3 (PIND&0x40) //存儲鍵
#define s4 (PIND&0x80) //讀取鍵
//-------------------
#define TWINT 7 //中斷標志
#define TWEN 2 //中斷時能
#define TWSTA 5 //啟動狀態位
#define TWSTO 4 //停止狀態位
//-----TWI狀態定義,MT主方式傳送,MR主方式接受
#define START 0x08 //啟動
#define RE_START 0x10 // 重新啟動
#define MT_SLA_ACK 0x18 //主機應答
#define MT_SLA_NOACK 0x20 //主機非應答
#define MT_DATA_ACK 0x28 //主機數據傳送后應答
#define MT_DATA_NOACK 0x30 //主機數據傳送后非應答
#define MR_SLA_ACK 0x40 //從機應答
#define MR_SLA_NOACK 0x48 //從機非應答
#define MR_DATA_ACK 0x50 //從機數據應答
#define MR_DATA_NOACK 0x58 //從機數據非應答
//-------------------------
#define start() (TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTA)) //啟動start信號
#define stop() (TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO)) //啟動停止信號
#define wait() {while(!(TWCR&(1<<TWINT)));} //等待TWINT置位說明啟動成功
#define testack() (TWSR&0xf8) //TWI狀態檢測,屏蔽預分頻位
#define setack() (TWCR|(1<<TWEA)) //應答狀態
#define setnoack() (TWCR&=~(1<<TWEA)) //非應答狀態
#define twi() (TWCR=(1<<TWINT)|(1<<TWEN)) //清除TWINT標志位,時能TWI
#define writebit(x) {TWDR=(x);TWCR=(1<<TWINT)|(1<<TWEN);} //輸入傳入TWI數據寄存器TWDR
//------I/0口設置------------
void portinit()
{
PORTA=0xff;
DDRA=0xff;
PORTB=0xff;
DDRB=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0x00;
}
//-----------讀數據函數----------
uchar iicread(uchar address)
{
uchar temp;
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;
}
//---------------寫數據函數----------------
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; //數據值變量
portinit();
delay(200);
init();
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) //值增加鍵
{
delay(100); //軟件延時100ms防止按鍵抖動
if(s1==0)
{
val++;
if(val>30)val=0;
}
}
if(s2==0) //值減少鍵
{
delay(100);
if(s2==0)
{
val--;
if(val<1)val=40;
}
}
if(s3==0) //值存儲鍵
{
delay(100);
if(s3==0)
{
iicwrite(70,val);
}
}
if(s4==0) //值讀取鍵
{
delay(100);
if(s4==0)
{
val=iicread(70);
}
}
}
}
//-------------------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()
{
uchar val;
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=w;
__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<k;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(uchar x,uchar y,uchar *p)
{
uchar add=0x80;
y=y&0x01;
x=x&0x0f;
if(y)add=add+0x40;
writecmd(add+x);
while(*p!='\0')
{
writedata(*p++);
}
}
void displayz(uchar x,uchar y,uchar k)
{
uchar add=0x80;
y=y&0x01;
x=x&0x0f;
if(y)add=add+0x40;
writecmd(add+x);
writedata(k);
}