问题遇到的现象和发生背景
我正在学习pico,用I2C读写AT24C02时遇到一些问题
我希望在地址0x00写入数据0x12,代码如下,但用串口助手读出来的数据却不一样
有人用过树莓派pico的sdk吗?
AT24C02模块是在淘宝买的,三个引脚接地,写地址应当是0xA0,读地址应当是A1。
代码
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "pico/binary_info.h"
// I2C defines
// This example will use I2C0 on GPIO8 (SDA) and GPIO9 (SCL) running at 400KHz.
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define I2C_PORT i2c0
#define I2C_SDA 8
#define I2C_SCL 9
#define ADDR_W 0xA0 //AT24写地址
#define ADDR_R 0xA1 //AT24读地址
int main()
{
stdio_init_all();
// I2C Initialisation. Using it at 400Khz.
i2c_init(I2C_PORT, 400*1000);
gpio_set_function(I2C_SDA, GPIO_FUNC_I2C);
gpio_set_function(I2C_SCL, GPIO_FUNC_I2C);
gpio_pull_up(I2C_SDA);
gpio_pull_up(I2C_SCL);
//bi_decl(bi_2pins_with_func(I2C_SDA,I2C_SCL,GPIO_FUNC_I2C));
uint8_t buf[2]={0x00,0x12};
uint8_t buffer[1];
uint8_t reg_addr=0x00;
i2c_write_blocking(I2C_PORT,ADDR_W,buf,2,false);//写入数据
printf("completed");
//puts("Hello, world!");
while (1)
{
i2c_write_blocking(I2C_PORT,ADDR_W,®_addr,1,true);//写入要读取的地址
i2c_read_blocking(I2C_PORT,ADDR_R,buffer,1,false);//读取数据
printf("now output results\n");
printf("%d\n",buffer[0]);
printf("completed\n");
sleep_ms(1000);
}
return 0;
}