首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >stm32l152作为I2C从站不确认地址

stm32l152作为I2C从站不确认地址
EN

Stack Overflow用户
提问于 2017-12-19 12:56:39
回答 1查看 513关注 0票数 1

我正在使用两只stm32l152发现野猪。一个被配置为主从,另一个配置为从。我已经对它们都启用了acknowladge,但是当主程序发送地址时,从机不会在9时钟脉冲时发送ack位。端口设置为交替功能4并打开排水口。我是使用外部4.7k拉出电阻到3.3V。我检查了所有的寄存器多次,我不知道为什么奴隶不识别它的地址。

这是逻辑分析器的输出。

D3是主服务器的起始位。

D4是从服务器上匹配的地址位。

这是奴隶代码:

代码语言:javascript
复制
#define USE_STDPERIPH_DRIVER
#include "stm32l1xx.h"
#include "stm32l1xx_conf.h"

//Quick hack, approximately 1ms delay
void ms_delay(int ms)
{
    while (ms> 0) {
        volatile int x = 5971;
        while (x> 0) {
            x--;
        }
        ms--;
    }
}
#define SCL 8
#define SDA 9

int main(void)
{
    RCC->AHBENR |= (0x1 << 1);

    //set port to alternate function
    GPIOB->MODER &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5)));
    GPIOB->MODER |= ((0x2 << (2 * SCL)) | (0x2 << (2 * SDA)) | (0x1 << (2 * 5)));

    GPIOB->OTYPER |= ((1 << SCL) | (1 << SDA)); //set output PB6 and PB7 to open drain

    //set PB6 and PB7 to no pullup no pulldown
    GPIOB->PUPDR &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5))); 

    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] &= ~((0b1111 << (4 * 0)) | (0b1111 << (4 * 1)));
    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] |= ((0b0100 << (4 * 0)) | (0b0100 << (4 * 1)));

    RCC->APB1ENR |= (1 << 21);

    //reset I2C
    I2C1->CR1 |= (1 << 15);
    ms_delay(1);
    I2C1->CR1 &= ~(1 << 15);

    I2C1->CR2 |= 0b001000; //peripheral clock set to 8MHz
    I2C1->CR1 |= (1 << 10); //ACK enabled

    I2C1->OAR1 |= (0x05 << 1); //setting primary address

    I2C1->CR1 |= 1; //I2C peripheral enabled when configuration is done

    for (;;) {
        if ((I2C1->SR1&(1 << 1)) != 0) {
            GPIOB->ODR |= (1 << 5);
        }
        else {
            GPIOB->ODR &= ~(1 << 5);
        }
    }
}

这是主代码:

代码语言:javascript
复制
#define USE_STDPERIPH_DRIVER
#include "stm32l1xx.h"
#include "stm32l1xx_conf.h"

#define SCL 8
#define SDA 9

int main(void)
{
    RCC->AHBENR |= (0x1 << 1);

    //set port to alternate function
    GPIOB->MODER &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5)));
    GPIOB->MODER |= ((0x2 << (2 * SCL)) | (0x2 << (2 * SDA)) | (0x1 << (2 * 5)));

    //set output PB6 and PB7 to open drain
    GPIOB->OTYPER |= ((1 << SCL) | (1 << SDA));
    //set PB6 and PB7 to no pullup no pulldown
    GPIOB->PUPDR &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5)));

    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] &= ~((0b1111 << (4 * 0)) | (0b1111 << (4 * 1)));
    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] |= ((0b0100 << (4 * 0)) | (0b0100 << (4 * 1)));

    I2C1->CR1 |= (1 << 15);
    I2C1->CR1 &= ~(1 << 15);

    RCC->APB1ENR |= (1 << 21);

    I2C1->CR2 |= 0x08; //peripheral clock set to 8MHz
    I2C1->CCR |= 0x28; //
    I2C1->TRISE |= 0x09;
    I2C1->CR1 |= (1 << 10); //ACK enabled

    I2C1->CR1 |= 1; //I2C peripheral enabled when configuration is done
    I2C1->CR1 |= (1 << 8); //generate start condition (master mode)

    for (;;) {
        //check start condition
        if ((I2C1->SR1&(1 << 0)) != 0) {
            GPIOB->ODR |= (1 << 5);
            I2C1->DR = 0x0b << 0; //send slave addres
        }
        else {
            GPIOB->ODR &= ~(1 << 5);
        }

        if ((I2C1->SR1&(1 << 1)) != 0) {
            GPIOB->ODR |= (1 << 5);
        }
        else {
            GPIOB->ODR &= ~(1 << 5);
        }
    }
}

我用arm-none-eabi-gcc编译,并使用stsw- stm 32077来自stm的图书馆。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-31 13:06:48

代码的问题是,在启用外围设备后,必须设置ack位,如果您之前这样做,则ack位将自动重置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47887576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档