#include "I2C.h"
#include "1602.h"
#include "delay.h"
/*Write data to IIC through AVR, and read and display the data through the serial port*/
//=======================================================================
void uart_init(void) //UART initialization
{ DDRD = 0x02;
PORTD = 0x00;
UCSRA = 0x02; /*No speed*/
UCSRB = 0x18; /*Allow receiving and sending*/
UCSRC = 0x06; /*8-bit data, 1-bit stop bit, no parity*/
UBRRH = 0x00;
UBRRL = 12; /*9600*/
}
//====================================================================
void USART_TXD(float data) //Sending in query mode
{
while( !(UCSRA & BIT(UDRE)) );
UDR=data;
while( !(UCSRA & BIT(TXC )) );
UCSRA|=BIT(TXC);
}
void main(void)
{
unsigned char i;
//LCD_init();
uart_init(); //TART initialization
SEI(); //Global interrupt enable
while(1)
{/*
I2C_Write('n',0x00);
I2C_Write('c',0x01);
I2C_Write('e',0x02);
I2C_Write('p',0x03);
I2C_Write('u',0x04 );
*/
i=I2C_Read(0x00);
//LCD_write_char(0,0,i);
USART_TXD(i);
i=I2C_Read(0x01);
//LCD_write_data(i);
USART_TXD(i);
i=I2C_Read( 0x02);
//LCD_write_data(i);
USART_TXD(i);
i=I2C_Read(0x03);
//LCD_write_data(i);
USART_TXD(i);
i=I2C_Read(0x04);
//LCD_write_data(i);
USART_TXD( i);
}
}
/*The main function above */
#include
#include "delay.h"
//I2C status definition
//MT master mode transmission MR master mode acceptance
#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 RD_DEVICE_ADDR 0xA1 //The first 4 bits are fixed, the last 3 bits depend on the connection, and the last bit is the read/write instruction bit
#define WD_DEVICE_ADDR 0xA0
//Common TWI operations (master mode write and read) unsigned char I2C_Write(unsigned char Wdata,unsigned char RegAddress); /************************************************ Start(); //I2C starts /************************************************ unsigned char I2C_Read(unsigned RegAddress) { /*The above is the IIC.h header file part, you need to study it carefully according to the technical documentation*/ *----------------------------------------------------------------------- void delay_1us(void) //1us delay function void delay_nus(unsigned int n) //N us delay function void delay_1ms(void) //1ms delay function void delay_nms(unsigned int n) //N ms delay function #endif /*Note: This program has been tested on the experimental board ATMEGA16. If you use the oscilloscope to see the data on the SCL and SDA signal lines, you can transfer them to your own circuit and use them with confidence. You can also use them on ATMEGA32. A2, A1, and A0 of my 24C02 are all grounded. If the address is different, just change it in the corresponding position of the program. This is the basis for debugging the microcontroller on the serial port, so you must know how to use it*/ /*The debugging software environment for this program is ICC6.31*/
#define Start() (TWCR=(1<
#define SetAck (TWCR|=(1<
unsigned char I2C_Read(unsigned RegAddress);
I2C bus writes a byte
Return 0: write successful
Return 1: write failed
**********************************************/
unsigned char I2C_Write(unsigned char Wdata,unsigned char RegAddress)
{
Wait();
if(TestAck()!=START)
return 1; //ACK
Write8Bit(WD_DEVICE_ADDR); //Write I2C slave device address and write mode
Wait();
if(TestAck()!=MT_SLA_ACK)
return 1; //ACK
Write8Bit(RegAddress); //Write device corresponding register address
Wait();
if(TestAck()!=MT_DATA_ACK)
return 1; //ACK
Write8Bit(Wdata); //Write data to device corresponding register
Wait();
if(TestAck()!=MT_DATA_ACK)
return 1; //ACK
Stop(); //I2C stops
delay_nms(10); //Delay
return 0;
}[page]
I2C bus reads a byte
Returns 0: Read successful
Returns 1: Read failed
**********************************************/
unsigned char temp;
Start();//I2C start
Wait();
if (TestAck()!=START)
return 1; //ACK
Write8Bit(WD_DEVICE_ADDR); //Write I2C slave device address and write mode
Wait();
if (TestAck()!=MT_SLA_ACK)
return 1; //ACK
Write8Bit(RegAddress); //Write device corresponding register address
Wait();
if (TestAck()!=MT_DATA_ACK)
return 1;
Start(); //I2C restart
Wait();
if (TestAck()!=RE_START)
return 1;
Write8Bit(RD_DEVICE_ADDR); //Write I2C slave device address and read mode
Wait();
if(TestAck()!=MR_SLA_ACK)
return 1; //ACK
Twi(); //Start master I2C read mode
Wait();
if(TestAck()!=MR_DATA_NOACK)
return 1; //ACK
temp=TWDR; //Read I2C receive data
Stop(); //I2C stops
return temp;
}
Delay function
Compiler: ICC-AVR v6.31A Date: 2005-11-24 20:29:57
Target chip: M16
Clock: 8.0000Mhz
Author: archeng504
-----------------------------------------------------------------------*/
#ifndef __delay_h
#define __delay_h
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
void delay_1us(void);
void delay_1ms(void);
{
asm ("nop");
}
{
unsigned int i=0;
for (i=0;i
}
{
unsigned int i;
for (i=0;i<1140;i++);
}
{
unsigned int i=0;
for (i=0;i
}
/*The above is the delay.h part, plus the iom16v.h and macros.h that come with IIC, it can be compiled*/
Previous article:Homemade AVR ATmega16 JTAG
Next article:Summary of UART communication of AVR microcontroller ATMega16
Recommended ReadingLatest update time:2024-11-16 17:46
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Plant growth monitoring system source code based on Raspberry Pi 5
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- It is recommended that the forum organize the posts of the past year
- RFSOC wireless communication development platform
- Littro MicroBox——Ultra-small terminal deep learning recognition module
- Can the power symbol ^ in the matlab result be replaced by pow in C language?
- Design of WiFi-based environmental information IoT cloud platform monitoring system
- Share: BQ24040: BQ2404X Series Device Schematic and Layout Review
- I searched for information and saw a genius who spent three months making a super mini TV.
- Looking to buy a complete set of information on scooter controller
- EEWORLD University Hall----Live Replay: Microchip Security Series 8 Platform Firmware Security Resilience
- The power solution for high-efficiency SiC FET is here, come and take a look.