At24c02/04/08 Address Understanding

Source: Internet
Author: User

This kind of device to be operated by the IIC bus, the read and write process must be addressed first, this type of device address has two bytes, such as the following table, 1010 is fixed, a means the device address, can pull up and pull down, the IIC bus can be connected to 2 of a few devices. P indicates the specific number of internal addresses, such as AT24C02 total 256 bytes, the second address byte can be completely satisfied without p. But the AT24C04 one has 512 bytes, requires 9-bit address lines, the first byte of P represents the address line, P=0 represents a low 256 bytes, and 1 represents a high 256 bytes.
Note: General Page writes can write 8 data in a row. Each byte sent by the host accepts the slave's answer signal. This kind of device is operated by the IIC bus, the device address varies slightly depending on the capacity, as follows


For the a0,a1,a2 foot of the chip:
24c01/02,a0,a1,a2 are all from the device address.
24C04,A1,A2 is from the device address, A0 useless
24C08,A2 is from the device address, a0,a1 useless
24C16 and above, a0,a1,a2 are useless.

Where a means the period address, p is also the address, at the time of reading and writing first is the starting condition + the device address
The device address as shown above in 16k, requires two bytes to represent the internal address, exactly p2/1/0 and 8-bit specific addresses

AT24C08 for example

//Port Definition#definescl_l GPIOB-&GT;BSRR |= 1<< (16+6)#defineScl_h GPIOB-&GT;BSRR |= 1<<6#definesda_l GPIOB-&GT;BSRR |= 1<< (16+7)#defineSda_h GPIOB-&GT;BSRR |= 1<<7#defineSda_red Gpiob->idr & 1<<7//SDA#defineAt24c08address 0xA0//8k 1010 a_2 p_1 p_0 r/w data address p1,p2+8 bit a total of 10 address bits                                                              //p1 p0 4 blocks (256 bytes) Piece 16 Page one page 16 bytesvoidGpio_init (void);//InitializeU8 Iic_readbyte (void); U8 Iic_writebyte (U8 sendbyte); U8 Iic_stop (void); U8 Iic_start (void); U8 At24c02read (U8 romaddr); U8 at24c02write (U8 romaddr,u8 regdata); U8 Iic_nack (void); U8 Iic_ack (void); U8 Iic_waitack (void); U8 Initat24c02 (void);/Initialize the PB6 and PB7 PC13 for export. And enable these two-port clocksvoidGpio_init (void) {RCC->apb2enr|=1<<3;//Enable PORTB clockrcc->apb2enr|=1<<4;//Enable PORTC clockgpiob->crl&=0x00ffffff; Gpiob->crl|=0x77000000;//PB6 PB7 Open-Drain output//ledGPIOC-&GT;CRH &=0xff0fffff; GPIOC-&GT;CRH |=0x00300000;//PC13 push-Pull outputGpioc->odr |=1<< -;} U8 Iic_start () {scl_l;//Preparesda_h; Scl_h; //SCL High This time changes the SDA end or start flag    if(! sda_red)//Test SDA has not occupied if SDA is grounded from the machine to pull down the host can not pull high        return 0;    sda_l; if(sda_red)//Bus Error        return 0;       sda_l; //Host Clamp I²c bus, ready to send or receive data    return 1;    }u8 Iic_stop () {scl_l;    sda_l; Delay_us (4);    Scl_h; Delay_us (4);         Sda_h; //send I²c bus end signalDelay_us (4); if(! sda_red)//SDA is pulled from the machine to ground the low host can not pull high        return 0; return 1; }u8 iic_writebyte (U8 sendbyte) {U8 I=8;  scl_l; //pull low clock start data transfer     while(i--) {scl_l; if(Sendbyte &0x80) Sda_h; Elsesda_l; Sendbyte<<=1; Delay_us (4);//SCL generates pulsesScl_h; Delay_us (4); } scl_l;} U8 Iic_readbyte (void) {U8 I=8, Receivebyte; //sda_h;     while(i--) {scl_l; Delay_us (4);//give me a pulse first .Scl_h;//pull High clock start data receive SDA handed to slaveReceivebyte <<=1; if(sda_red) receivebyte|=1; Delay_us (4);    } scl_l; returnReceivebyte; }u8 Iic_waitack (void)/*Stm32 When sending*/{scl_l; //SCL low host side can change SDADelay_us (4);     Sda_h; //host High SDA wait reply slave will pull down SDADelay_us (2);     Scl_h; //Generate pulses        if(sda_red)//from the machine does not pull low SDA that does not reply        return 0;    scl_l; return 1;} U8 Iic_ack (void)//the host must produce a response when the slave emits the last byte, only the primary send from the receiving//notification data end from the machine sender "you know?"                /****stm32 when receiving*/{scl_l;    __nop ();        sda_l; //pull Low SDA when responding to SCL low from machine Stm32Delay_us (4);    Scl_h; Delay_us (4);   scl_l; } U8 Iic_nack (void)/****stm32 when receiving*/{scl_l;    __nop ();        Sda_h; //slave Stm32 does not respond to SDA mounting heightDelay_us (4);    Scl_h; Delay_us (4);    scl_l; } /******** AT24C02 Read and write operation ********/U8 at24c02write (U8 romaddr,u8 regdata) {if(!Iic_start ())return 0; Iic_writebyte (at24c08address); //Write mode    if(!Iic_waitack ())        {iic_stop (); return 0;    } iic_writebyte (ROMADDR); if(!Iic_waitack ())        {iic_stop (); return 0;    } iic_writebyte (Regdata); if(!Iic_waitack ())        {iic_stop (); return 0;    } iic_stop (); return 1;}    U8 At24c02read (U8 romaddr) {U8 receive; if(!Iic_start ())        {iic_stop (); return 0;    } iic_writebyte (at24c08address); if(!Iic_waitack ())        {iic_stop (); return 0;    } iic_writebyte (ROMADDR); if(!Iic_waitack ())        {iic_stop (); return 0;    } iic_start (); Iic_writebyte (at24c08address+1);//Configuring read -only mode    if(!Iic_waitack ())        {iic_stop (); return 0; } Receive=Iic_readbyte ();    Iic_nack ();    Iic_stop (); returnreceive;  }u8 H; intMainvoid) {Stm32_clock_init (9);//System Clock SettingsDelay_init ( the);//Delay InitializationGpio_init ();//initializing the hardware interface to the LED connectionAt24c02write (0x00,0xf5); H=at24c02read (0x00); if(h==0xf5)    {         while(1) {GPIOC->odr &=0<< -; Delay_ms ( -); GPIOC->odr |=1<< -; Delay_ms ( -); }        }    ElseGPIOC->odr |=1<< -; }//data consistent led flashing

At24c02/04/08 Address Understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.