锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

AVR单片机IIC实验

时间:2024-05-19 12:37:12



//芯片AT 16 晶振8mhz
#include #include #define uchar unsigned char #define uint unsigned int //----------------------------- uchar str1[]={"IIC "}; //--------------------------- void delay(uint k) //延时函数 { uint i,j; for(i=0;i
#include "1602.h" //1602库函数 //-------按键输入点设置------- #define s1 (PIND&0x10) //变量值增加键 #define s2 (PIND&0x20) //变量值减少键 #define s3 (PIND&0x40) //存储键 #define s4 (PIND&0x80) //读取键 //------------------- #define TWI 7 //中断标志 #define TWEN 2 //中断时能 #define T TA 5 //启动状态位 #define TWSTO 4 //停止状态位 //-----TWI状态定义,MT主方式传送,MR主方式接受 #define START 0x08 //启动 #define RE_START 0x10 // 重新启动 #define MT_ A_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< 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 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); }
-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章