/** ****************************************************************************** * @file main.c * @author Alexander * @version V1.0 * @date 2022-xx-xx * @brief 测试led ****************************************************************************** * @attention * * 实验平台:HK32F030M开发板 * 论坛 :https://bbs.21ic.com/iclist-1010-1.html * ****************************************************************************** */ #include "hk32f030m.h" #include "demo.h" // #include "usart_printf.h" #define SOFT_DELAY Delay(0x0FFFFF); void Delay(__IO uint32_t nCount); typedef struct time_t { int hour; int min; int second; } time_t; time_t time = { .hour = 0, .min = 0, .second = 0, }; time_t time_close; void time_put_tick() { time.second++; if (time.second == 60) { time.min++; time.second = 0; if (time.min == 60) { time.min = 0; time.hour++; if (time.hour == 24) { time.hour = 0; } } } } // 0 0 0 0 0 0 0 0 // 0 * * * 0 0 0 0 // 0 * 0 * 0 0 0 0 // 0 * * * 0 0 0 0 // 0 0 0 0 0 0 0 0 // 0 0 0 * * * * 0 // 0 0 * 0 0 0 0 0 // 0 0 * 0 0 0 0 0 // 0 0 * 0 0 0 0 0 // 0 0 * 0 0 0 0 0 // 0 0 * 0 0 0 0 0 // 0 0 * 0 0 0 0 0 // 0 0 0 * * * * 0 // 0 0 0 0 0 0 0 0 // 0 0 0 0 0 0 0 0 u8 temp_pic[16] = { 0x00, 0x00, 0x70, 0x50, 0x70, 0x00, 0x1E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x1E, 0x00, 0x00}; void show_time() { char buff[17] = {0}; int hour = time.hour; int min = time.min; int second = time.second; sprintf(buff, "run: %2d:%2d:%2d", hour, min, second); LCD_SetCharPos(0, 1); LCD_ShowString(buff, 0x00, 0xffff); } /** * float a[141]={ 95,90,84,80,75,71,67,64,60,57, 54,51,48,46,44,41.8,39.72,37.7,35.8,34,32.371, 30.780,29.277,27.855,26.510,25.238,24.033,22.893,21.812,20.789,19.819, 18.8,18.0,17.2,16.4,15.66,14.96,14.29,13.65,13.04,12.46, 11.92,11.4,10.9,10.4,10,9.55,9.151,8.764,8.395,8.044, 7.709,7.395,7.085,6.795,6.518,6.254,6.002,6.761,5.532,5.312, 5.103,4.903,4.711,4.529,4.354,4.187,4.027,3.874,3.728,3.588, 3.453,3.325,3.202,3.085,2.972,2.864,2.760,2.661,2.566,2.474, 2.387,2.303,2.222,2.145,2.071,1.999,1.931,1.865,1.802,1.741, 1.683,1.627,1.573,1.521,1.471,1.423,1.377,1.333,1.290,1.249, 1.209,1.171,1.134,1.099,1.065,1.032,1.000,0.969,0.940,0.911, 0.884,0.858,0.832,0.807,0.784,0.761,0.739,0.717,0.696,0.677, 0.657,0.639,0.621,0.603,0.586,0.570,0.554,0.539,0.524,0.510, 0.496,0.483,0.470,0.457,0.445,0.434,0.422,0.411,0.400,0.390, }; int main(void){ for (int i = 0; i < 141; i++) { if(i%10==0){ printf("\n"); } printf("%d,",(int)(4096*((a[i])/(a[i]+10.0)))); } } */ // -20 - 120度 u16 temp_search[141] = { 3705, 3686, 3660, 3640, 3614, 3590, 3564, 3542, 3510, 3484, 3456, 3424, 3389, 3364, 3337, 3305, 3272, 3237, 3201, 3165, 3129, 3091, 3053, 3013, 2974, 2933, 2892, 2850, 2808, 2765, 2722, 2673, 2633, 2590, 2544, 2499, 2454, 2409, 2364, 2318, 2272, 2227, 2181, 2136, 2088, 2048, 2000, 1957, 1913, 1869, 1825, 1783, 1741, 1698, 1657, 1616, 1576, 1536, 1652, 1458, 1420, 1383, 1347, 1311, 1276, 1242, 1208, 1175, 1143, 1112, 1081, 1051, 1022, 993, 965, 938, 911, 885, 860, 836, 812, 789, 766, 744, 723, 702, 682, 662, 643, 625, 607, 590, 573, 556, 540, 525, 510, 495, 481, 468, 454, 441, 429, 417, 405, 394, 383, 372, 361, 351, 341, 332, 323, 314, 305, 297, 289, 281, 274, 266, 259, 252, 246, 239, 232, 226, 220, 215, 209, 203, 198, 193, 188, 183, 179, 174, 170, 165, 161, 157, 153}; extern volatile unsigned short ADC_ConvertedValue; int temp_recal = -5; char target = 50; int sw = 0; void show_temp_ctl() { char buff[17] = {0}; LCD_SetCharPos(0, 3); sprintf(buff, "tar:%3d", target); LCD_ShowString(buff, 0x00, 0xffff); LCD_ShowPic(temp_pic, 0x00, 0xffff); sprintf(buff, " sw:%s", sw ? " ON" : "OFF"); LCD_ShowString(buff, 0x00, 0xffff); } void sw_ctrl(int tmp) { if (tmp >= target) { sw = 1; GPIO_OUT_H(GPIOA,GPIO_Pin_1); time_close.hour = time.hour; time_close.min = time.min; time_close.second = time.second; time_close.min += 15; if (time_close.min >= 60) { time_close.min -= 60; time_close.hour += 1; if (time_close.hour == 24) { time_close.hour = 0; } } } else { if (time_close.hour == time.hour && time_close.min == time.min && time_close.second == time.second) { sw = 0; GPIO_OUT_L(GPIOA,GPIO_Pin_1); } } } void show_temp() { char buff[17] = {0}; unsigned short vol = ADC_ConvertedValue; // sprintf(buff, "temp: %d",vol); // LCD_SetCharPos(0, 2); // LCD_ShowString(buff,0x00, 0xffff); if (vol > temp_search[0]) { sprintf(buff, "temp err:%s", "too low"); LCD_SetCharPos(0, 2); LCD_ShowString(buff, 0x00, 0xffff); } else if (vol < temp_search[140]) { sprintf(buff, "temp err:%s", "too high"); LCD_SetCharPos(0, 2); LCD_ShowString(buff, 0x00, 0xffff); } else { int i = 0; for (; i < 141; i++) { if (temp_search[i] >= vol && temp_search[i + 1] < vol) { break; } } unsigned int temp_low_vol = temp_search[i] * 1000; unsigned int temp_high_vol = temp_search[i + 1] * 1000; unsigned int temp_tis = vol * 1000; unsigned int delta = temp_low_vol - temp_high_vol; unsigned int delta2 = temp_low_vol - temp_tis; unsigned int dot = (delta2 * 100) / delta; sw_ctrl(i - 20 + temp_recal); sprintf(buff, "temp: %3d.%d%d", (i - 20 + temp_recal), (dot / 10), (dot % 10)); LCD_SetCharPos(0, 2); LCD_ShowString(buff, 0x00, 0xffff); LCD_ShowPic(temp_pic, 0x00, 0xffff); } } int menu = 0; int index = 0; void menu_1() { LCD_SetCharPos(0, 0); LCD_ShowString("Rabbit Cooler ", 0x00, 0xffff); show_time(); show_temp(); show_temp_ctl(); } void menu_2() { char buff[17] = {0}; sprintf(buff, "target temp:%3d", target); LCD_SetCharPos(0, 0); LCD_ShowString(buff, (index == 1) ? 0xffff : 0x00, (index == 1) ? 0x00 : 0xffff); LCD_ShowPic(temp_pic, (index == 1) ? 0xffff : 0x00, (index == 1) ? 0x00 : 0xffff); LCD_SetCharPos(0, 1); LCD_ShowString(" ", (index == 2) ? 0xffff : 0x00, (index == 2) ? 0x00 : 0xffff); LCD_SetCharPos(0, 2); LCD_ShowString(" ", (index == 3) ? 0xffff : 0x00, (index == 3) ? 0x00 : 0xffff); LCD_SetCharPos(0, 3); LCD_ShowString(" ", (index == 4) ? 0xffff : 0x00, (index == 4) ? 0x00 : 0xffff); } typedef struct key { int key_flag; int ord; int key_cnt; int key_state; int (*get_value)(int); } key; int key_value(int ord) { switch (ord) { case 1: return GPIO_READ(GPIOC, 5); case 2: return GPIO_READ(GPIOC, 4); case 3: return GPIO_READ(GPIOC, 3); case 4: return GPIO_READ(GPIOD, 1); default: break; } return 1; } key key1 = { .get_value = key_value, .key_flag = 0, .key_cnt = 0, .key_state = 0, .ord = 1, }; key key2 = { .get_value = key_value, .key_flag = 0, .key_cnt = 0, .key_state = 0, .ord = 4, }; key key3 = { .get_value = key_value, .key_flag = 0, .key_cnt = 0, .key_state = 0, .ord = 3, }; key key4 = { .get_value = key_value, .key_flag = 0, .key_cnt = 0, .key_state = 0, .ord = 2, }; void key_det(key *k) { char val = k->get_value(k->ord); if (val == 0) { if (k->key_flag == 0) { if (k->key_cnt < 30) { k->key_cnt++; } else { k->key_state = 1; k->key_flag = 1; } } } else { k->key_flag = 0; k->key_cnt = 0; k->key_state = 0; } } void key_det_core() { key_det(&key1); key_det(&key2); key_det(&key3); key_det(&key4); } void deal_core() { if (menu == 0) { if (key1.key_state == 1) { key1.key_state = 0; menu = 1; index = 1; } } else if (menu == 1) { if (key1.key_state == 1) { key1.key_state = 0; menu = 0; index = 1; } if (index == 1) { if (key2.key_state == 1) { key2.key_state = 0; target -= 1; EEPROM_WriteByte(0, target); menu_2(); } else if (key4.key_state == 1) { key4.key_state = 0; target += 1; EEPROM_WriteByte(0, target); menu_2(); } } } } void ms_task() { static unsigned int cnt = 0; if (cnt % 1000 == 0) { if (menu == 0) { menu_1(); } else if (menu == 1) { menu_2(); } time_put_tick(); } key_det_core(); deal_core(); cnt++; } /** * @brief 主函数 * @param 无 * @retval 无 */ int main(void) { SPI_HW_Init(); USART_Config(); LCD_Init(); LCD_SetCharPos(0, 0); LCD_ShowString("Rabbit Cooler", 0x00, 0xffff); // extern void Audio_8bit_init(void); // Audio_8bit_init(); HAL_GPIO_Init(GPIOA, GPIO_Pin_1, GPIO_Mode_OUT, GPIO_PuPd_NOPULL, GPIO_OType_PP); GPIO_OUT_L(GPIOA, GPIO_Pin_1); extern void BASIC_TIM_Init(void); BASIC_TIM_Init(); extern void ADCx_Init(void); ADCx_Init(); EERPOM_ReadByte(0, &target); // HAL_GPIO_Init(GPIOC, GPIO_Pin_5, GPIO_Mode_IN, GPIO_PuPd_UP, GPIO_OType_OD); HAL_GPIO_Init(GPIOC, GPIO_Pin_4, GPIO_Mode_IN, GPIO_PuPd_UP, GPIO_OType_OD); HAL_GPIO_Init(GPIOC, GPIO_Pin_3, GPIO_Mode_IN, GPIO_PuPd_UP, GPIO_OType_OD); HAL_GPIO_Init(GPIOD, GPIO_Pin_1, GPIO_Mode_IN, GPIO_PuPd_UP, GPIO_OType_OD); while (1) { extern volatile uint32_t sys_tick; static uint32_t tick_now = 0; if (sys_tick != tick_now) { ms_task(); tick_now++; } } } void Delay(__IO uint32_t nCount) // 简单的延时函数 { for (; nCount != 0; nCount--) ; } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(char *file, uint32_t line) { /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif /* USE_FULL_ASSERT */