用gpio读AM2302的数据?,程序的问题多多指教

2019-07-15 14:09发布

/* ------------------------------------------
* Copyright (c) 2016, Synopsys, Inc. All rights reserved.

* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:

* 1) Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.

* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.

* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* version 2016.01
* date 2014-12-17
* author Wayne Ren(Wei.Ren@synopsys.com)
--------------------------------------------- */

/**
* defgroup        EMBARC_APP_GPIO                embARC GPIO Example
* ingroup        EMBARC_APPS_TOTAL
* ingroup        EMBARC_APPS_BOARD_EMSK
* ingroup        EMBARC_APPS_BAREMETAL
* rief        embARC Example for testing designware gpio module
*
* details
* ### Extra Required Tools
*
* ### Extra Required Peripherals
*
* ### Design Concept
*     This example is designed to show how to use device [GPIO HAL API](dev_gpio.h) in embARC.
*
* ### Usage Manual
*     Test cases for DW GPIO driver. In this example, the functions of the LEDs, buttons, and DIP switches are tested.
*     ![ScreenShot of gpio under baremetal](pic/images/example/emsk/emsk_gpio.jpg)
*
* ### Extra Comments
*
*/

/**
* file
* ingroup        EMBARC_APP_GPIO
* rief        example of timer0 and gpio
*/

/**
* addtogroup        EMBARC_APP_GPIO
* @{
*/

#include "embARC.h"
#include "embARC_debug.h"

static DEV_GPIO_PTR port_th;
static void delay(int32_t z)
{
        int32_t i,j;
        for(i=36;i>0;i--)

                for(j=z;j>0;j--);

}
static  void pushhigh()
{
        uint32_t data0;
        port_th = gpio_get_dev(DW_GPIO_PORT_A);
        port_th->gpio_close();
        port_th->gpio_open(0xB0000);//PORT A的[17:16]、[19]打开
        port_th->gpio_write(0x30000,0xB0000);//PORT A的[19:16]写入数据,串口总线拉高
        port_th->gpio_read(&data0,0xB0000);
        EMBARC_PRINTF("data0 is 0x%x ",data0);
}
static  void pushlow()
{
        uint32_t data1;
        port_th = gpio_get_dev(DW_GPIO_PORT_A);
        port_th->gpio_close();
        port_th->gpio_open(0xB0000);//PORT A的[17:16]、[19]打开
        port_th->gpio_write(0x10000,0xB0000);//PORT A的[19:16]写入数据,串口总线拉低
        port_th->gpio_read(&data1,0xB0000);
        EMBARC_PRINTF("data1 is 0x%x ",data1);
}
int main(void)
{
        uint32_t humity[16];
        uint32_t temperature[16];
        long t;
        DEV_GPIO_BIT_ISR bit_isr;
        DEV_GPIO_INT_CFG int_cfg;
        cpu_lock();
        board_init(); /* board init */       
        while(1)
        {       

                pushhigh();
                delay(20000);//SDA拉高2s
                pushlow();
                delay(8);//SDA拉低800us
                pushhigh();
                delay(2);//SDA拉高20us

                pushlow();
                delay(8);//SDA拉低80us

                pushhigh();
                delay(8);//SDA拉高80us
                pushlow();
                delay(3);//SDA拉低50us
                pushhigh();
                delay(2);//SDA拉高26us
                port_th->gpio_read(&humity[0],0x20000);//读PORT A的[17]的数据
                EMBARC_PRINTF("humity[0] is 0x%x%x ",humity[0]);
                pushlow();
                delay(3);//SDA拉低50us
                pushhigh();
                delay(7);//SDA拉高70us
                port_th->gpio_read(&humity[1],0x20000);//读PORT A的[17]的数据
                EMBARC_PRINTF("humity[1] is 0x%x%x ",humity[1]);
                port_th->gpio_read(&humity[2],0x20000);//读PORT A的[17]的数据
                EMBARC_PRINTF("humity[2] is 0x%x%x ",humity[2]);
                port_th->gpio_read(&humity[3],0x20000);//读PORT A的[17]的数据
                EMBARC_PRINTF("humity[3] is 0x%x%x ",humity[3]);
        /*        port_th->gpio_read(&humity[1],0x20000);//读PORT A的[17]的数据
                EMBARC_PRINTF("humity is %x ",humity);

        //        t   = ((long)humity[0] << 8) + ((long)humity[1] & 0xF8);
        //        t >>= 3;
                //conversion (t = t*0,0625) in Degrees
        //        t *= 6250;
        //        EMBARC_PRINTF("t is %2d.%2d ",t);*/

        }

                cpu_unlock(); /* unlock system */
       
        while (1);
        return E_SYS;
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。