1
测试了下LP中的DeepSleep,代码如下
- LP_EnableGPIOWakeup((gpio_cfg_t*)&pb_pin[0]);printf("EnableGPIOWakeup .n");
- MXC_GCR->scon |= 0x4000; // Disable SWD
- printf("Entering DEEPSLEEP mode.n");
- LP_DisableBandGap();
- LP_DisableVCorePORSignal();
- LP_EnableRamRetReg();
- LP_DisableBlockDetect();
- LP_EnableFastWk();
- LP_EnterDeepSleepMode();
- MXC_GCR->scon &= 0xBFFF; // Enable SWD
- printf("Entering not DEEPSLEEP mode.n");
- rDeepSleepMode=0;
复制代码
进入和出SLEEP同时出现,没成功。
准备进行与DHT11湿度传感器连接的工作。
- /***** Includes *****/
- #include
- #include
- #include "mxc_config.h"
- #include "nvic_table.h"
- #include "board.h"
- #include "rtc.h"
- #include "led.h"
- #include "pb.h"
- #include "tmr_utils.h"
- #include "gpio.h"
- #include "lp.h"
- #include "icc.h"
- #include "uart.h"
- /***** Definitions *****/
- #define LED_ALARM 0
- #define SUBSECOND_MSEC_0 250
- #define SECS_PER_MIN 60
- #define SECS_PER_HR (60 * SECS_PER_MIN)
- #define SECS_PER_DAY (24 * SECS_PER_HR)
- #define MSEC_TO_RSSA(x) (0 - ((x * 256) / 1000)) /* Converts a time in milleseconds to the equivalent RSSA register value. */
- #define DEEPSLEEP 1
- /*LED Control PIN red*/
- #define GPIO_PORT_OUT PORT_0
- #define GPIO_PIN_OUT PIN_9
- /*DHT11 Control PIN */
- #define GPIO_PIN_OUT2 PIN_4
- /***** Globals *****/
- uint32_t ss_interval = SUBSECOND_MSEC_0;
- volatile int buttonPressed = 0;
- volatile int rtcalarm = 0;
- volatile int rDeepSleepMode = 0;
- gpio_cfg_t dht_in = {PORT_0, GPIO_PIN_OUT2, GPIO_FUNC_IN, GPIO_PAD_NONE};
- /***** Functions *****/
- void MicroSeconds(int us) {
- /* Demonstrates the TMR driver delay */
- // TMR_Delay(MXC_TMR0, USEC(us), NULL); // todo check if this is µsec or msec
- uint32_t i = 0;
- for( i=0; i < us * 4; i++) {
- asm(" nop");
- }
- }
- void RTC_IRQHandler(void)
- {
- int flags = RTC_GetFlags();
- rtcalarm = 1;
- /* Check sub-second alarm flag. */
- if (flags & MXC_F_RTC_CTRL_ALSF) {
- RTC_ClearFlags(MXC_F_RTC_CTRL_ALSF);
- }
- /* Check time-of-day alarm flag. */
- if (flags & MXC_F_RTC_CTRL_ALDF) {
- RTC_ClearFlags(MXC_F_RTC_CTRL_ALDF);
- /* Set a new alarm 10 seconds from current time. */
- }
- }
- void buttonHandler(void *pb)
- {
- buttonPressed = 1;
- }
- /*从DHT11读取一个位,返回值:1/0。
- 每一bit数据都以50us的低电平时序开始,高电平的宽度决定了bit数据位的0或1,
- 高电平状态在26~28us时,表示数据位为0,高电平状态在70us时,表示数据位为1.
- DHT11_DQ_IN是读取对应端口引脚的输入电平,在读之前,该引脚要配置为上拉下拉输入模式
- */
- char DHT11_Read_Bit(void)
- {
- char retry=0;
- while(GPIO_InGet(&dht_in)&&retry<100)//读取高电平中,等待变为低电平,/*&&与
<的优先级是<高于&&*
的优先级是<高于&&*>
- {
- retry++;
- MicroSeconds(1);//SysTickDelay(1);
- }
- retry=0;
- while(!GPIO_InGet(&dht_in)&&retry<100)//读取低电平中,等待变高电平
- {
- retry++;
- MicroSeconds( 1);//SysTickDelay(1);
- }
- MicroSeconds( 40);//SysTickDelay(40);//等待40us
- if(GPIO_InGet(&dht_in))return 1;
- else return 0;
- }
- char DhtReadByte(void)
- {
- char j,dat;
- dat=0;
- for(j=0;j<8;j++)
- {
- /*while (!GPIO_InGet(&dht_in)); // 等待低电平结束
- MicroSeconds(uint32_t 25); // 延时 Delay25us
- if(GPIO_InGet(&dht_in)) // 检测数据线是高电平还是低电平
- { bit_i=1; while(TRH); }
- else { bit_i=0; }
- dat<<=1; // 将该位移位保存到dat变量中
- dat|=bit_i; */
- dat<<=1;
- dat|=DHT11_Read_Bit();
- }
- return(dat); }
- void printTime(void)
- {
- int day, hr, min, sec;
- double subsec;
- subsec = RTC_GetSubSecond() / 256.0;
- sec = RTC_GetSecond();
- day = sec / SECS_PER_DAY;
- sec -= day * SECS_PER_DAY;
- hr = sec / SECS_PER_HR;
- sec -= hr * SECS_PER_HR;
- min = sec / SECS_PER_MIN;
- sec -= min * SECS_PER_MIN;
- subsec += sec;
- printf("Current Time (dd:hh:mm:ss): %02d:%02d:%02d:%05.2fn", day, hr, min, subsec);
- }
- int main(void)
- {
- const sys_cfg_tmr_t sys_tmr_cfg = {0}; // Do not enable timer output.
- //sys_cfg_rtc_t sys_cfg;
- gpio_cfg_t gpio_out0;
- /* Setup output pin FOR led. */
- gpio_out0.port = GPIO_PORT_OUT;
- gpio_out0.mask = GPIO_PIN_OUT;
- gpio_out0.pad = GPIO_PAD_NONE;
- gpio_out0.func = GPIO_FUNC_OUT;
- GPIO_Config(&gpio_out0);
- gpio_cfg_t dht_in = {PORT_0, GPIO_PIN_OUT2, GPIO_FUNC_IN, GPIO_PAD_NONE};
- gpio_cfg_t dht_out = {PORT_0, GPIO_PIN_OUT2, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- /*
- gpio_cfg_t dht_in = {PORT_0, GPIO_PIN_OUT2, GPIO_FUNC_IN, GPIO_PAD_NONE};
- gpio_cfg_t dht_out = {PORT_0, GPIO_PIN_OUT2, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- gpio_cfg_t d0 = {PORT_0, PIN_2, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- gpio_cfg_t d1 = {PORT_0, PIN_3, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- gpio_cfg_t d2 = {PORT_0, PIN_4, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- gpio_cfg_t d3 = {PORT_0, PIN_5, GPIO_FUNC_OUT, GPIO_PAD_NONE}; */
- int count = 0;
- printf("nn***** DHT11 on P0.4 ******nn");
- printf(
- "1. An interrupt is set up on P0.12. when that interrupt occurs.using a push button (S1) to start DHT11nn");
- printf("2. This example outputs the same state onto P0.9 (led_R) and outputs the same state onto P0.13 (led_G).nn");
- printf("3.space led_G Init led_R 15s DATA_IN led_Y %d ms.n", SUBSECOND_MSEC_0);
- NVIC_EnableIRQ(RTC_IRQn);
- // Setup callback to receive notification of when button is pressed.
- PB_RegisterCallback(0, buttonHandler);
- // Turn LED off initially
- GPIO_OutClr(&gpio_out0);
- LED_Off(LED_ALARM);
- TMR_Delay(MXC_TMR0, MSEC(1000), &sys_tmr_cfg);
- int i=0;
- while (1) {
- if (buttonPressed) {
- // Show the time elapsed.
- printTime();
- // Delay for switch debouncing.
- TMR_Delay(MXC_TMR0, MSEC(100), &sys_tmr_cfg);
- printf("buttonPressed count = %dn", count++);
- // Re-arm switch detection.
- i++;
- buttonPressed = 0;
- char check; // 校验字节
- char TemHig,TemLow,HumiHig,HumiLow;
- GPIO_Config(&dht_out);
- GPIO_OutClr(&dht_out);
- // 主机拉低18ms DelayMs(18);
- TMR_Delay(MXC_TMR0, MSEC(18), &sys_tmr_cfg);
- GPIO_OutSet(&dht_out); // DATA总线由上拉电阻拉高 主机延时20us
- MicroSeconds( 20);
- GPIO_Config(&dht_in); // 主机设为输入
- TMR_Delay(MXC_TMR0, MSEC(15000), &sys_tmr_cfg); // 主机延时15s
- GPIO_Config(&dht_out);
- GPIO_OutClr(&dht_out);
- // 主机拉低18ms DelayMs(18);
- TMR_Delay(MXC_TMR0, MSEC(18), &sys_tmr_cfg);
- GPIO_OutSet(&dht_out); // DATA总线由上拉电阻拉高 主机延时20us
- MicroSeconds( 20);
- GPIO_Config(&dht_in); // 主机设为输入
- while (!GPIO_InGet(&dht_in)); // 等待DHT 80us的低电平结束
- while (GPIO_InGet(&dht_in)); // 等待DHT 80us的高电平结束
- HumiHig = DhtReadByte(); // 湿度高8位
- HumiLow = DhtReadByte(); // 湿度低8为,总为0
- TemHig = DhtReadByte(); // 温度高8位
- TemLow = DhtReadByte(); // 温度低8为,总为0
- check = DhtReadByte(); // 8位校验码,其值等于读出的四个字节相加之和的低8位
- //TRH=1; // 释放总线
- if(check==HumiHig + HumiLow + TemHig + TemLow) // 如果收到的数据无误
- {printf("HumiHig = %d. %dn", HumiHig,HumiLow);
- printf("TemHig = %d. %dn", TemHig,TemLow);
- printf("check = %dn", check);}
- }
- }
- }
复制代码
检测运行成功
|
|