AVR单片机IIC实验
![](../img/eep/emb/emb-64210nopmjnxkwy4.jpg)
//芯片ATMEGA16 晶振8mhz
#include
#include
#define uchar unsigned char
#define uint unsigned int
//-----------------------------
uchar str1[]={"IIC TEST"};
//---------------------------
void delay(uint k) //延时函数
{
uint i,j;
for(i=0;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 #define stop() (TWCR=(1 #define wait() {while(!(TWCR&(1 #define testack() (TWSR&0xf8) //TWI状态检测,屏蔽预分频位 #define setack() (TWCR|(1 #define setnoack() (TWCR&=~(1 #define twi() (TWCR=(1 #define writebit(x) {TWDR=(x);TWCR=(1 //------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;
AVR单片机IIC实 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)