如何利用GD32F310读取HDC1080环境温湿度

发布者:HappyExplorer最新更新时间:2024-11-18 来源: elecfans关键字:环境温湿度 手机看文章 扫描二维码
随时随地手机看文章

我拿到的开发板实际板载的MCU是GD32F310G8,QFN28pin封装,基于ARM CORTEX M4内核,主频72MHz,芯片内置64KB flash,8KB SRAM,两路I2C外设。

poYBAGJZHUuANGnBAAFPRa5cvtA192.png

本次试用目的是利用GD32F310的I2C0实现对HDC1080的初始化及读取环境温湿度,并利用UART口在电脑上显示出来。

1、新建工程

①首先建立一个新的项目文件夹,并在此文件夹下建立对应子文件夹,我建立的文件如下图所示,这个依个人习惯会有不同:

pYYBAGJZHUaANI-lAABHV_LSPPU501.png

② Document文件夹中存放对项目的说明文件readme.txt;将系统支持包中的GD32F3x0_Firmware_Library_V2.2.0/Firmware文件夹中的内容复制到Libraries文件夹中;Listing/Output文件夹用于存放项目编译时生成的目标文件及list文件,在MDK的魔术棒中进行设置路径;users文件夹中存放main文件及我们自已写的代码;Project文件夹中存放MDK项目文件;

③打开MDK keil5,首先要先导入GD32F310G文件支持包。

poYBAGJZHUGAYE_hAAIuprokS6U913.png

④建立新的项目,选择所使用的MCU,我们选择GD32F330G8,如下图所示

pYYBAGJZHT2ACcnKAAIiA6Rz0Z4597.png

⑤新项目中建立如下文件组,并导入启动文件和外设支持包。

poYBAGJZHTqAArcDAAHD3JCcZ-o737.png

⑥点击魔术棒工具进行项目设置。在C/C++选项中添加include文件路径;

pYYBAGJZHTaAQD2xAAFXbSyTJts568.png

⑦点击Debug选项进行GDLink的设置,要选择CMSIS_DAP Debugger,并将Reset and Run打勾。

pYYBAGJZHTCAYknXAAFsWb10O54560.png

⑧这样整个项目的配置基本就结束了,可以在main.c文件中建立一个空循环,编译测试项目建立是否正确无误。

2、硬件映射

硬件映射的目的是按目前的硬件连接建立.h文件,如下图所示:

pYYBAGJZHSyARUsWAAKHXC6G9oM705.png

因此基于开发板I2C0采用的是PB6/PB7引脚,因为需要打印读出的值,UART0使用的是PA9/PA10引脚。


3、编写代码


①UART0初始化和printf函数重定向:


void UART_TypeInit(void)

{

    gpio_deinit(UART0_GPIO_PORT);

    usart_deinit(USART0);

    rcu_periph_clock_enable(UART0_GPIO_PORT_CLK);

    gpio_mode_set(UART0_GPIO_PORT,GPIO_MODE_AF,GPIO_PUPD_NONE,UART0_TX_PIN | UART0_RX_PIN);

    gpio_output_options_set(UART0_GPIO_PORT,GPIO_OTYPE_OD,GPIO_OSPEED_MAX,UART0_TX_PIN | UART0_RX_PIN);

    gpio_af_set(UART0_GPIO_PORT,GPIO_AF_1,UART0_TX_PIN | UART0_RX_PIN);

    

    rcu_periph_clock_enable(UART0_CLK);

    usart_baudrate_set(USART0,UART0_Baudrate);

    usart_parity_config(USART0,USART_PM_NONE);

    usart_word_length_set(USART0,USART_WL_8BIT);

    usart_stop_bit_set(USART0,USART_STB_1BIT);

    usart_enable(USART0);

    usart_transmit_config(USART0,USART_TRANSMIT_ENABLE);

    usart_receive_config(USART0,USART_RECEIVE_ENABLE);

}


/* retarget the C library printf function to the USART */

int fputc(int ch, FILE *f)

{

    usart_data_transmit(USART0, (uint8_t)ch);

    while(RESET == usart_flag_get(USART0,USART_FLAG_TBE)); 

        

    return ch;

}

②I2C0外设初始化:


void I2C0_TypeInit(void)

{

    gpio_deinit(I2C0_GPIO_PORT);

    i2c_deinit(I2C0);

    rcu_periph_clock_enable(I2C0_GPIO_PORT_CLK);

    gpio_mode_set(I2C0_GPIO_PORT,GPIO_MODE_AF,GPIO_PUPD_NONE,I2C0_SCL_PIN | I2C0_SDA_PIN);

    gpio_output_options_set(I2C0_GPIO_PORT,GPIO_OTYPE_OD,GPIO_OSPEED_MAX,I2C0_SCL_PIN | I2C0_SDA_PIN);

    gpio_af_set(I2C0_GPIO_PORT,GPIO_AF_1,I2C0_SCL_PIN | I2C0_SDA_PIN);

    

    rcu_periph_clock_enable(I2C0_CLK);

    i2c_clock_config(I2C0,I2C0_Frequence,I2C_DTCY_2);

    i2c_mode_addr_config(I2C0,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,I2C0_OWN_Address);

    i2c_enable(I2C0);

    i2c_ack_config(I2C0,I2C_ACK_ENABLE);

}

③读寄存器操作:


void  I2C0_Register_Read(uint8_t* B_buffer, uint8_t read_address,uint16_t number_of_byte)

{

     /* wait until I2C bus is idle */

    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));


    if(2 == number_of_byte){

        i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT);

    }

    

    /* send a start condition to I2C bus */

    i2c_start_on_bus(I2C0);

    

    /* wait until SBSEND bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));

    

    /* send slave address to I2C bus */

    i2c_master_addressing(I2C0, HDC1080_Addr, I2C_TRANSMITTER);

    

    /* wait until ADDSEND bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));

    

    /* clear the ADDSEND bit */

    i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);

    

    /* wait until the transmit data buffer is empty */

    while(SET != i2c_flag_get( I2C0 , I2C_FLAG_TBE));


    /* enable I2C0*/

    i2c_enable(I2C0);

    

    /* send the EEPROM's internal address to write to */

    i2c_data_transmit(I2C0, read_address);  

    

    /* wait until BTC bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

        

        delay_1ms(20);

    

    /* send a start condition to I2C bus */

    i2c_start_on_bus(I2C0);

    

    /* wait until SBSEND bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));

    

    /* send slave address to I2C bus */

    i2c_master_addressing(I2C0, HDC1080_Addr, I2C_RECEIVER);


    if(number_of_byte < 3){

        /* disable acknowledge */

        i2c_ack_config(I2C0,I2C_ACK_DISABLE);

    }

    

    /* wait until ADDSEND bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));

    

    /* clear the ADDSEND bit */

    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);

    

    if(1 == number_of_byte){

        /* send a stop condition to I2C bus */

        i2c_stop_on_bus(I2C0);

    }

    

    /* while there is data to be read */

    while(number_of_byte){

        if(3 == number_of_byte){

            /* wait until BTC bit is set */

            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));


            /* disable acknowledge */

            i2c_ack_config(I2C0,I2C_ACK_DISABLE);

        }

        if(2 == number_of_byte){

            /* wait until BTC bit is set */

            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

            

            /* send a stop condition to I2C bus */

            i2c_stop_on_bus(I2C0);

        }

        

                delay_1ms(1);

        /* wait until the RBNE bit is set and clear it */

        if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){

            /* read a byte from the EEPROM */

            *B_buffer = i2c_data_receive(I2C0);

            

            /* point to the next location where the byte read will be saved */

            B_buffer++; 

            

            /* decrement the read bytes counter */

            number_of_byte--;

        } 

    }

    

    /* wait until the stop condition is finished */

    while(I2C_CTL0(I2C0)&0x0200);

    

    /* enable acknowledge */

    i2c_ack_config(I2C0, I2C_ACK_ENABLE);


    i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);

}

④写寄存器操作:


void I2C0_Byte_Write(uint8_t* P_buffer, uint8_t write_address)

{

    uint8_t i;

    /* wait until I2C bus is idle */

    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));


    /* send a start condition to I2C bus */

    i2c_start_on_bus(I2C0);


    /* wait until SBSEND bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));


    /* send slave address to I2C bus */

    i2c_master_addressing(I2C0, HDC1080_Addr, I2C_TRANSMITTER);


    /* wait until ADDSEND bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));


    /* clear the ADDSEND bit */

    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);


    /* wait until the transmit data buffer is empty */

    while(SET != i2c_flag_get(I2C0, I2C_FLAG_TBE));


    /* send the EEPROM's internal address to write to : only one byte address */

    i2c_data_transmit(I2C0, write_address);


    /* wait until BTC bit is set */

    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

    

    for(i=0;i<2;i++)

    {


        /* send the byte to be written */

        i2c_data_transmit(I2C0, *(P_buffer+i)); 


        /* wait until BTC bit is set */

        while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));

    

    }


    /* send a stop condition to I2C bus */

    i2c_stop_on_bus(I2C0);


    /* wait until the stop condition is finished */

    while(I2C_CTL0(I2C0)&0x0200);

}

⑤对HDC1080进行初始化并读取其ID:


void HDC1080_Init(void)

{

    uint8_t    IDBuffer[2]={0};

    uint8_t InitSetup[2]={0x90,0x00};

    uint32_t SID[3]={0},MID=0,DID=0;

    I2C0_Byte_Write(InitSetup,CONFIGURATION);

    delay_1ms(20);

    

    I2C0_Register_Read(IDBuffer,DEVICE_ID,2);

    DID = (((uint32_t)IDBuffer[0])<<8 | IDBuffer[1]);

    printf('The DeviceID is 0x%xnr',DID);

    I2C0_Register_Read(IDBuffer,MANUFACTURE_ID,2);

    MID = ((uint16_t) IDBuffer[0]<<8 | IDBuffer[1]);

    printf('The ManufactureID is 0x%xnr',MID);

    I2C0_Register_Read(IDBuffer,FIRST_SID,2);

    SID[0] = ((uint16_t) IDBuffer[0]<<8 | IDBuffer[1]); 

    I2C0_Register_Read(IDBuffer,MID_SID,2);

    SID[1] = ((uint16_t) IDBuffer[0]<<8 | IDBuffer[1]);

    I2C0_Register_Read(IDBuffer,LAST_SID,2);

    SID[2] = ((uint16_t) IDBuffer[0]<<8 | IDBuffer[1]);

    printf('The First bytes of the serial ID of the part is 0x%xnr',SID[0]);

[1] [2]
关键字:环境温湿度 引用地址:如何利用GD32F310读取HDC1080环境温湿度

上一篇:GD32F303固件库开发(11)----ADC在DMA模式下扫描多个通道
下一篇:最后一页

小广播
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved