Introduction: This program has been tested on the experimental board ATMEGA16. If you see data on the SCL and SDA signal lines on the oscilloscope, you can transfer the data to your own circuit and use it with confidence. It can also be used 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.
#include #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 stop bit, no check*/ UBRRH = 0x00; UBRRL = 12; /*9600*/ } //=============================================================== void USART_TXD(float data) //Send using query method { 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 reception #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) #define Start() (TWCR=(1< #define SetAck (TWCR|=(1< unsigned char I2C_Write(unsigned char Wdata,unsigned char RegAddress); 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) { 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 the corresponding register address of the device Wait(); if(TestAck()!=MT_DATA_ACK) return 1; //ACK Write8Bit(Wdata); //Write data to the corresponding register of the device Wait(); if(TestAck()!=MT_DATA_ACK) return 1; //ACK Stop(); //I2C stop delay_nms(10); //delay return 0; } /**************************************************** I2C bus reads a byte Return 0: Read successfully Return 1: Read failed **************************************************/ unsigned char I2C_Read(unsigned RegAddress) { 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 the corresponding register address of the device 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 the main I2C read mode Wait(); if(TestAck()!=MR_DATA_NOACK) return 1; //ACK temp=TWDR; //Read I2C receive data Stop(); //I2C stops return temp; } /*The above is the IIC.h header file part, you need to study it carefully according to the technical documentation*/ *----------------------------------------------------------------------- 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); void delay_1us(void) //1us delay function { asm("nop"); } void delay_nus(unsigned int n) //N us delay function { unsigned int i=0; for (i=0;i } void delay_1ms(void) //1ms delay function { unsigned int i; for (i=0;i<1140;i++); } void delay_nms(unsigned int n) //N ms delay function { unsigned int i=0; for (i=0;i } #endif /*The above is the delay.h part, plus the iom16v.h and macros.h that come with IIC, it can be compiled*/ /*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*/
Previous article:Design scheme of protection circuit based on lithium battery pack of electric vehicle
Next article:A design scheme for digitally controlled low voltage and high current pulse power supply
- 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
- Power amplifier application--piezoelectric ceramic impedance test, Antai Electronics Manufacturer
- Indiemicro iND86201 Ernie, who has used it? ---http://www.indiemicro.com/cn/
- Detailed explanation of the technical architecture of the four major mainstream CPU processors
- Porting Ubuntu to A7
- Ground interference problem? (Attached circuit diagram, powered by USB 5V 1A)
- Causes of blistering on PCB copper plating board
- Power supply obstacles + MOSFET switching losses
- GD32 unboxing photos
- IMX6 Linux system compilation manual
- 【Perf-V Evaluation】+ Construction and basic use of development environment (1)