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