//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
Keywords:AVR
Reference address:AVR microcontroller IIC experiment
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);
}
Previous article:AVR microcontroller frequency meter design frequency input source STM32 port
Next article:AVR CTC mode waveform output experiment
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
Guess you like
- PCB copper plating problem
- Several rules for power supply design of RF circuits
- Why does TI C6000 need cache?
- Switching power supply waveform
- [Raspberry Pi 4B Review] + Real-time status monitoring website test service resource usage
- The latency of cache in arm system refers to
- 【W806 Lianshengde Development Board】Real-time performance
- Electric race, how are you guys doing?
- Howland's improved circuit output resistance
- Ov7670 image acquisition source program based on TINYM0 LPC1114