/* * 函数名:BSP * 描述 :时钟初始化、硬件初始化 */ void BSP_Init(void) { SystemInit(); /* 配置系统时钟为72MHz */ SysTick_Init(); /* 初始化并使能SysTick定时器 */ LED_GPIO_config(); /* LED端口初始化 */ USART1_Inter_Config(); /* 串口-中断 */ LEDDIS_GPIO_Configuration(); /* 数码管 */ ADC1_Init(); /* ADC1 */ EXTI_PA0_Config(); /* 按键0-中断0 */ I2C_EE_Init(); /* EEPROM */ }
/***************************************************************************//** * @brief First, the content of Tx1_Buffer is written to the EEPROM_WriteAddress1 * and the written data are read. The written and the read buffers data are * then compared. Following the read operation, the program wait that the * EEPROM reverts to its Standby state. A second write operation is, then, * performed and this time, Tx2_Buffer is written to EEPROM_WriteAddress2, * which represents the address just after the last written one in the first * write. After completion of the second write operation, the written data * are read. The contents of the written and the read buffers are compared. ******************************************************************************/ void I2C_EEPROM(void) { /* System clocks configuration */ RCC_Configuration(); /* USART configuration */ /* Initialize the I2C EEPROM driver */ I2C_EE_Init(); /* First write in the memory followed by a read of the written data */ /* Write on I2C EEPROM from EEPROM_WriteAddress1 */ // I2C_EE_BufferWrite(Test_Buffer, EEPROM_WriteAddress1, 100); // while(1); /* Read from I2C EEPROM from EEPROM_ReadAddress1 */ // I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, 100); /* Check if the data written to the memory is read correctly */ // TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* Initialize the I2C EEPROM driver ----------------------------------------*/ I2C_EE_Init(); /* First write in the memory followed by a read of the written data --------*/ /* Write on I2C EEPROM from EEPROM_WriteAddress1 */ I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1); /* Read from I2C EEPROM from EEPROM_ReadAddress1 */ I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1); /* Check if the data written to the memory is read correctly */ TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1); /* TransferStatus1 = PASSED, if the transmitted and received data to/from the EEPROM are the same */ /* TransferStatus1 = FAILED, if the transmitted and received data to/from the EEPROM are different */ /* Wait for EEPROM standby state */ I2C_EE_WaitEepromStandbyState(); /* Second write in the memory followed by a read of the written data -------*/ /* Write on I2C EEPROM from EEPROM_WriteAddress2 */ I2C_EE_BufferWrite(Tx2_Buffer, EEPROM_WriteAddress2, BufferSize2); /* Read from I2C EEPROM from EEPROM_ReadAddress2 */ I2C_EE_BufferRead(Rx2_Buffer, EEPROM_ReadAddress2, BufferSize2); /* Check if the data written to the memory is read correctly */ TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BufferSize2); /* TransferStatus2 = PASSED, if the transmitted and received data to/from the EEPROM are the same */ /* TransferStatus2 = FAILED, if the transmitted and received data to/from the EEPROM are different */ while (1) { } }
/** * @brief 主函数 * @param 无 * @retval 无 */ int main(void) { /* 串口1初始化 */ USART1_Config(); printf("\r\n 这是一个I2C外设(AT24C02)读写测试例程 \r\n"); //LED_GPIO_Config(); /* I2C 外设初(AT24C02)始化 */ I2C_EE_Init(); printf("\r\n 这是一个I2C外设(AT24C02)读写测试例程 \r\n"); I2C_Test(); while (1) { } }
int main() { uint8_t index = 0; uint8_t maxhsize = DISPLAY_CHAR_WIDTH; FRESULT fr; uint32_t state = ST_START; uint32_t nameCount = 0; uint32_t vpos = 0; uint32_t hpos = 0; #if defined(PCBTARANIS) wdt_reset(); RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // Enable portA clock #endif pwrInit(); #if defined(PCBSKY9X) MATRIX->CCFG_SYSIO |= 0x000000F0L; // Disable syspins, enable B4,5,6,7 #endif #if defined(PCBSKY9X) init_SDcard(); PIOC->PIO_PER = PIO_PC25; // Enable bit C25 (USB-detect) start_timer0(); #endif lcdInit(); #if defined(PCBSKY9X) extern uint8_t OptrexDisplay; OptrexDisplay = 1; #endif lcd_clear(); lcd_putsLeft(0, BOOTLOADER_TITLE); lcd_invert_line(0); lcdRefresh(); #if defined(PCBSKY9X) OptrexDisplay = 0; lcdRefresh(); #endif #if defined(PCBTARANIS) keysInit(); I2C_EE_Init(); init_hw_timer(); #endif __enable_irq(); init10msTimer(); #if defined(PCBSKY9X) EblockAddress = -1; init_spi(); #endif #if defined(PCBSKY9X) LockBits = readLockBits(); if (LockBits) { clearLockBits(); } #endif #if defined(PCBTARANIS) // SD card detect pin sdInit(); usbInit(); usbStart(); #endif for (;;) { wdt_reset(); if (Tenms) { if (EE_timer) { if (--EE_timer == 0) { #if defined(PCBSKY9X) writeBlock(); #endif } } Tenms = 0; lcd_clear(); lcd_putsLeft(0, BOOTLOADER_TITLE); lcd_invert_line(0); uint8_t event = getEvent(); if (state != ST_USB) { if (usbPlugged()) { state = ST_USB; if (!unlocked) { unlocked = 1; unlockFlash(); } usbPluggedIn(); } } if (state == ST_START) { lcd_putsLeft(2*FH, "\010Write Firmware"); lcd_putsLeft(3*FH, "\010Restore EEPROM"); lcd_putsLeft(4*FH, "\010Exit"); lcd_invert_line(2+vpos); lcd_putsLeft(7*FH, INDENT "Or plug in a USB cable for mass storage"); if (event == EVT_KEY_FIRST(BOOT_KEY_DOWN)) { vpos == 2 ? vpos = 0 : vpos = vpos+1; } else if (event == EVT_KEY_FIRST(BOOT_KEY_UP)) { vpos == 0 ? vpos = 2 : vpos = vpos-1; } else if (event == EVT_KEY_BREAK(BOOT_KEY_MENU)) { switch (vpos) { case 0: state = ST_FLASH_MENU; break; case 1: state = ST_RESTORE_MENU; break; default: state = ST_REBOOT; } } } if (state == ST_USB) { lcd_putsLeft(4*FH, "\026USB Connected"); if (usbPlugged() == 0) { vpos = 0; if (unlocked) { lockFlash(); unlocked = 0; } state = ST_START; } #if defined(PCBSKY9X) usbMassStorage(); #endif } if (state == ST_FLASH_MENU || state == ST_RESTORE_MENU) { sdInit(); memoryType = (state == ST_RESTORE_MENU ? MEM_EEPROM : MEM_FLASH); state = ST_DIR_CHECK; } else if (state == ST_DIR_CHECK) { fr = f_chdir(getBinaryPath()); if (fr == FR_OK) { state = ST_OPEN_DIR; } else { lcd_putsLeft(2*FH, INDENT "Directory is missing!"); if (event == EVT_KEY_BREAK(BOOT_KEY_EXIT) || event == EVT_KEY_BREAK(BOOT_KEY_MENU)) { vpos = 0; state = ST_START; } } } if (state == ST_OPEN_DIR) { index = 0; fr = f_opendir(&Dj, "."); if (fr == FR_OK) { state = ST_FILE_LIST; nameCount = fillNames(0); hpos = 0; vpos = 0; } } if (state == ST_FILE_LIST) { uint32_t limit = 6; if (nameCount < limit) { limit = nameCount; } maxhsize = 0; for (uint32_t i=0; i<limit; i++) { uint32_t x; x = strlen(Filenames[i]); if (x > maxhsize) { maxhsize = x; } if (x > DISPLAY_CHAR_WIDTH) { if (hpos + DISPLAY_CHAR_WIDTH > x) { x = x - DISPLAY_CHAR_WIDTH; } else { x = hpos; } } else { x = 0; } lcd_putsnAtt(INDENT_WIDTH, 16 + FH * i, &Filenames[i][x], DISPLAY_CHAR_WIDTH, 0); } if (event == EVT_KEY_REPT(BOOT_KEY_DOWN) || event == EVT_KEY_FIRST(BOOT_KEY_DOWN)) { if (vpos < limit - 1) { vpos += 1; } else { if (nameCount > limit) { index += 1; nameCount = fillNames(index); } } } else if (event == EVT_KEY_REPT(BOOT_KEY_UP) || event == EVT_KEY_FIRST(BOOT_KEY_UP)) { if (vpos > 0) { vpos -= 1; } else { if (index) { index -= 1; nameCount = fillNames(index); } } } #if !defined(PCBTARANIS) else if (event == EVT_KEY_REPT(BOOT_KEY_RIGHT) || event == EVT_KEY_FIRST(BOOT_KEY_RIGHT)) { if (hpos + DISPLAY_CHAR_WIDTH < maxhsize) { hpos += 1; } } else if (event == EVT_KEY_REPT(BOOT_KEY_LEFT) || event == EVT_KEY_FIRST(BOOT_KEY_LEFT)) { if (hpos) { hpos -= 1; } } #endif else if (event == EVT_KEY_BREAK(BOOT_KEY_MENU)) { // Select file to flash state = ST_FLASH_CHECK; Valid = 0; } else if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT)) { state = ST_START; vpos = 0; } lcd_invert_line(2 + vpos); } else if (state == ST_FLASH_CHECK) { int result = menuFlashFile(vpos, event); FirmwareSize = FileSize[vpos] - BOOTLOADER_SIZE; if (result == 0) { // canceled state = ST_FILE_LIST; } else if (result == 1) { // confirmed firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE; firmwareWritten = 0; eepromAddress = 0; eepromWritten = 0; state = ST_FLASHING; } } else if (state == ST_FLASHING) { // commit to flashing lcd_putsLeft(4*FH, "\032Writing..."); if (!unlocked && (memoryType == MEM_FLASH)) { unlocked = 1; unlockFlash(); } int progress; if (memoryType == MEM_FLASH) { writeFlashBlock(); firmwareWritten += sizeof(Block_buffer); progress = (200*firmwareWritten) / FirmwareSize; } else { writeEepromBlock(); eepromWritten += sizeof(Block_buffer); progress = (200*eepromWritten) / EESIZE; } lcd_rect( 3, 6*FH+4, 204, 7); lcd_hline(5, 6*FH+6, progress, FORCE); lcd_hline(5, 6*FH+7, progress, FORCE); lcd_hline(5, 6*FH+8, progress, FORCE); fr = f_read(&FlashFile, (BYTE *)Block_buffer, sizeof(Block_buffer), &BlockCount); if (BlockCount == 0) { state = ST_FLASH_DONE; // EOF } if (firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE) { state = ST_FLASH_DONE; // Backstop } if (eepromWritten >= EESIZE) { state = ST_FLASH_DONE; // Backstop } } if (state == ST_FLASH_DONE) { if (unlocked) { lockFlash(); unlocked = 0; } lcd_putsLeft(4*FH, "\024Writing Complete"); if (event == EVT_KEY_FIRST(BOOT_KEY_EXIT) || event == EVT_KEY_BREAK(BOOT_KEY_MENU)) { state = ST_START; vpos = 0; } } if (event == EVT_KEY_LONG(BOOT_KEY_EXIT)) { state = ST_REBOOT; } lcdRefresh(); if (PowerUpDelay < 20) { // 200 mS PowerUpDelay += 1; } else { sdPoll10ms(); } } if (pwrCheck() == e_power_off && state != ST_FLASHING && state != ST_USB) { pwrOff(); for (;;) { // Wait for power to go off } } if (state == ST_REBOOT) { if ((~readKeys() & 0x7E) == 0) { NVIC_SystemReset(); } } } return 0; }
int main(void) { u8 readstatus,firstread=0; u8 humi_set_temp=humi_set; u8 temp_set_temp=temp_set; //main1(); /* USART1 config 115200 8-N-1 */ // USART1_Config(); // // printf("\r\n this is a yhx firmware \r\n"); /* 配置SysTick 为1us中断一次 */ SysTick_Init(); // TIM3_Int_Init(100,7199); DelayInit(); // delay_init(72); /*按键输入初始化*/ GPIO_INPUT_INIT(); /*输出初始化*/ IO_OUTPUT_Config(); /* I2C 外设初(AT24C02)始化 */ I2C_EE_Init(); /*当按下-键时,程序会执行默认的IP192.168.3.248,不会从eeprom读取数据*/ if( Key_Scan(KEYPORT,KEY_SUB,1) != KEY_ON ) I2C_EE_Restore(); if(EE_FLAG!=i2c_eeprom_data[0]) { //初始温湿度设定及裕量 i2c_eeprom_data[0]=EE_FLAG; i2c_eeprom_data[1]=temp_set; i2c_eeprom_data[2]=humi_set; i2c_eeprom_data[3]=temp_distance; i2c_eeprom_data[4]=humi_distance; //初始化net //ip i2c_eeprom_data[8]=ipaddress[0]; i2c_eeprom_data[9]=ipaddress[1]; i2c_eeprom_data[10]=ipaddress[2]; i2c_eeprom_data[11]=ipaddress[3]; //mask i2c_eeprom_data[12]=maskaddress[0]; i2c_eeprom_data[13]=maskaddress[1]; i2c_eeprom_data[14]=maskaddress[2]; i2c_eeprom_data[15]=maskaddress[3]; //gate i2c_eeprom_data[16]=gateaddress[0]; i2c_eeprom_data[17]=gateaddress[1]; i2c_eeprom_data[18]=gateaddress[2]; i2c_eeprom_data[19]=gateaddress[3]; //mac i2c_eeprom_data[24]=mymac[0]; i2c_eeprom_data[25]=mymac[1]; i2c_eeprom_data[26]=mymac[2]; i2c_eeprom_data[27]=mymac[3]; i2c_eeprom_data[28]=mymac[4]; i2c_eeprom_data[29]=mymac[5]; I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data, 0, 8); //网络 I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+8, 8, 8); I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+16, 16, 8); I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+24, 24, 8); } //中断开启,配置net管脚和传感器data管脚中断 // NVIC_Configuration(); /*SHT10-模拟I2C读取温湿度,初始化*/ // Sht_GPIO_Config(); // s_connectionreset(); // sht10_read(); /*初始化DTT11的引脚*/ DHT11_GPIO_Config(); SPI1_Init(); //InitNet(); /* 初始化网络设备以及UIP协议栈,配置IP地址 */ //数码管初始化 hc595_init(); /* 配置SysTick 为10ms中断一次 */ // SysTick_Init(); TimingInput=0;//输入节拍20ms IWDG_Init(4,625); //与分频数为64,重载值为625,溢出时间为1s while(1) { /*温湿度测量,massureflag请求测量标志*/ if(massureflag) { //数据伪造1213,温度控制用的是真实的,显示和上传的数据用的是渐变的伪造数据 if( (readstatus=Read_DHT11(&DHT11_Data))==SUCCESS) { if(firstread==0) { temp_val.f=DHT11_Data.temp_int; firstread=1; } else { if(DHT11_Data.temp_int>(int)temp_val.f) temp_val.f=temp_val.f+0.5; else if(DHT11_Data.temp_int<(int)temp_val.f) temp_val.f=temp_val.f-0.5; } humi_val.f=DHT11_Data.humi_int; sensor_error=0; massureflag=0; time18ms_out=0; if(set_wait>1000)//初始化已过 if((temp_val.f>-10)&&(temp_val.f<100)&&(humi_val.f>=0/*)&&(humi_val.f<100*/)&&poweron)//传感器没坏才输出 automation(); }else if(readstatus!=NOTREADY) { sensor_error++; } } if(sensor_error>5)//5次都没有采到温湿度 { temp_val.f=humi_val.f=-100; } //printf("温度:%.1f\t湿度%.1f\r\n",temp_val.f,humi_val.f); //根据检测到的温湿度控制led和继电器,刚开机3s的时候尚未检测到温湿度,此时最好不要有输出,在while之前添加sht10_read()吧,进来之前就读一次数据 if((0==poweron)||((time_s_remainder==0)&&(active!='a'))) { WARM(0); WARM_LED(0); COLD(0); COLD_LED(0); WET(0); WET_LED(0); ALWAYS_WET_LED(0); BEEP_SCREAM(0); work_state=0; } if(network_state==0) { //下面的代码是按键检测,用来设定温湿度设定值的 if(TimingInput)//输入检测20ms一次 { TimingInput=0; key_input_last=key_input; GPIO_INPUT_SCAN(); if(key_input==key_input_last)//检测是否一直按着同一个按键 press_same_key_flag=1;//一直按着同一个按键 else press_same_key_flag=0; //蜂鸣器响应 if((key_input)&&(beeptime<300)) { //蜂鸣器响 BEEP_SCREAM(1); }else { if(key_input==0) beeptime=0; //蜂鸣器关闭 BEEP_SCREAM(0); } //是否进入设定状态,闪烁。 if(0==showdigital)//之前就是闪烁状态,temp_set_temp数据要保留 { temp_set_temp=temp_set; humi_set_temp=humi_set; } if((0==press_same_key_flag)&&(0!=key_input))//有按键按下但不是同一个按键 { //输入处理 switch (key_input) { case 1://set if(set_wait<=1000) set_start_flag=1; showdigital++;//第showdigital位闪烁 if(showdigital>4) showdigital=1; break; case 2://save showdigital=0; set_start_flag=0;//初始化完成标志 set_wait=1001;//设置等待10s结束 //将数据保存到eeprom,温湿度设定值 temp_set=temp_set_temp; humi_set=humi_set_temp; i2c_eeprom_data[1]=temp_set; i2c_eeprom_data[2]=humi_set; //写入eeprom i2c_eeprom_data[0]=EE_FLAG; I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data, 0, 5); break; case 4://+ //第showdigital位+ switch(showdigital) { case 1: temp_set_temp=(temp_set_temp/10+1)%10*10+temp_set_temp%10; break; case 2: temp_set_temp=temp_set_temp/10*10+(temp_set_temp%10+1)%10; break; case 3: humi_set_temp=(humi_set_temp/10+1)%10*10+humi_set_temp%10; break; case 4: humi_set_temp=humi_set_temp/10*10+(humi_set_temp%10+1)%10; break; default:; } break; case 8://- //第showdigital位+ switch(showdigital) { case 1: temp_set_temp=(temp_set_temp<10)?90+temp_set_temp:(temp_set_temp/10-1)%10*10+temp_set_temp%10; break; case 2: temp_set_temp=(temp_set_temp%10==0)?temp_set_temp+9:temp_set_temp-1; break; case 3: humi_set_temp=(humi_set_temp<10)?90+humi_set_temp:(humi_set_temp/10-1)%10*10+humi_set_temp%10; break; case 4: humi_set_temp=(humi_set_temp%10==0)?humi_set_temp+9:humi_set_temp-1; break; default:; } break; default:; } } } }else {//本地状态显示 humi_set_temp=humi_set; temp_set_temp=temp_set; key_input=0; showdigital=0; BEEP_SCREAM(0); beeptime=0; } //试用期写入 if((active!='a')&&(remainder_write>300)) { remainder_write=0; //写入eeprom //if(time_s_remainder>0) { i2c_eeprom_data[32]=(u8)time_s_remainder; i2c_eeprom_data[33]=(u8)(time_s_remainder>>8); i2c_eeprom_data[34]=(u8)(time_s_remainder>>16); i2c_eeprom_data[35]=(u8)(time_s_remainder>>24); I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+32, 32, 4); } } //sht10_j: //数码管输出 digitaldata[0]=digitaldata[1]=digitaldata[2]=digitaldata[3]=digitaldata[4]=digitaldata[5]=12;//12 if((active=='a')||(time_s_remainder>0))//激活态20150421 { //温度 if((temp_val.f<0)&&(temp_val.f>-10))//负值 { digitaldata[0]=10;//- digitaldata[1]=(u8)(-temp_val.f); digitaldata[2]=(u8)(-temp_val.f*10)%10; }else if((temp_val.f>=0)&&(temp_val.f<100))//正数 { digitaldata[0]=((u8)temp_val.f)/10; digitaldata[1]=((u8)temp_val.f)%10;//. digitaldata[2]=(u8)(((u16)(temp_val.f*10))%10); } //湿度 if(humi_val.f>=0)//((humi_val.f>=0)&&(humi_val.f<100)) { if(humi_val.f>=100) { temp_f=humi_val.f;//20150712 add temp xzk humi_val.f=99.9; } digitaldata[3]=((u8)humi_val.f)/10; digitaldata[4]=((u8)humi_val.f)%10;//. digitaldata[5]=(u8)(((u16)(humi_val.f*10))%10); if(humi_val.f>=99.9) humi_val.f=temp_f; } }else //试用期已经到了,显示off 20150421 { digitaldata[0]=0; digitaldata[3]=0; BEEP_SCREAM(1); } //设定温度值 digitaldata[6]=temp_set_temp/10; digitaldata[7]=temp_set_temp%10; //湿度设定 digitaldata[8]=humi_set_temp/10; digitaldata[9]=humi_set_temp%10; if((blickflag)&&(network_state==0))//可以闪烁 switch(showdigital) {//showdigital=0//低位到高位是否显示6 7 8 9,0都显示,1:9不显示;2:8不显示;3:7不显示;4:6不显示 case 1: digitaldata[6]=11; break; case 2: digitaldata[7]=11; break; case 3: digitaldata[8]=11; break; case 4: digitaldata[9]=11; break; default:; } hc595_display(digitaldata); //就地状态或网络状态PC3 if( Key_Scan(GPIOC,GPIO_Pin_3,0) == KEY_ON ) { /*按键有效*/ network_state=1; }else//就地 { network_state=0; //poweron=1; //0716 //启动开关PA12检测 20150719 if( Key_Scan(GPIOA,GPIO_Pin_12,1) == KEY_ON ) { poweron=1; }else { poweron=0; } } /* 下面是网络检测的程序,处理uip事件,必须插入到用户程序的循环体中 */ UipPro(); //中断触发读取网络接收缓存 eth_poll(); //定时查询TCP及UDP连接收发状态 ARP表更新, 并响应 IWDG_Feed();//喂狗 if(request_initnet>250)//等待2s初始化网络 { InitNet(); request_initnet=0; } }
/** eeprom 初始化 */ void eeprom_init(void) { I2C_EE_Init(); }