diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/include/zh_avr_ac_dimmer.h b/include/zh_avr_ac_dimmer.h new file mode 100644 index 0000000..2dd9bc7 --- /dev/null +++ b/include/zh_avr_ac_dimmer.h @@ -0,0 +1,63 @@ +#pragma once + +// #include "FreeRTOS.h" +// #include "semphr.h" +#include "avr_err.h" +#include "stdbool.h" +#include "avr/interrupt.h" +#include "avr/pgmspace.h" + +#define ZH_AVR_AC_DIMMER_INIT_CONFIG_DEFAULT() \ + { \ + .task_priority = configMAX_PRIORITIES, \ + .stack_size = 124, \ + .queue_size = 1, \ + .a_gpio_number = 0, \ + .b_gpio_number = 0, \ + .pullup = false, \ + .encoder_min_value = -100, \ + .encoder_max_value = 100, \ + .encoder_step = 1, \ + .encoder_number = 0} + +#ifdef __cplusplus +extern "C" +{ +#endif + + typedef struct // Structure for initial initialization of AC dimmer. + { + uint8_t task_priority; // Task priority for the PCF8574 expander isr processing. @note It is not recommended to set a value less than configMAX_PRIORITIES. + uint8_t stack_size; // Stack size for task for the PCF8574 expander isr processing processing. @note The minimum size is 124 byte. + uint8_t i2c_address; // Expander I2C address. + bool p0_gpio_work_mode; // Expander GPIO PO work mode. True for input, false for output. + bool p1_gpio_work_mode; // Expander GPIO P1 work mode. True for input, false for output. + bool p2_gpio_work_mode; // Expander GPIO P2 work mode. True for input, false for output. + bool p3_gpio_work_mode; // Expander GPIO P3 work mode. True for input, false for output. + bool p4_gpio_work_mode; // Expander GPIO P4 work mode. True for input, false for output. + bool p5_gpio_work_mode; // Expander GPIO P5 work mode. True for input, false for output. + bool p6_gpio_work_mode; // Expander GPIO P6 work mode. True for input, false for output. + bool p7_gpio_work_mode; // Expander GPIO P7 work mode. True for input, false for output. + uint8_t interrupt_gpio; // Interrupt GPIO. @attention Must be same for all PCF8574 expanders. + uint8_t interrupt_port; // Interrupt port. + } zh_avr_pcf8574_init_config_t; + + /** + * @brief Initialize PCF8574 expander. + * + * @param[in] config Pointer to PCF8574 initialized configuration structure. Can point to a temporary variable. + * @param[out] handle Pointer to unique PCF8574 handle. + * + * @attention I2C driver must be initialized first. + * + * @note Before initialize the expander recommend initialize zh_avr_pcf8574_init_config_t structure with default values. + * + * @code zh_avr_pcf8574_init_config_t config = ZH_AVR_PCF8574_INIT_CONFIG_DEFAULT() @endcode + * + * @return AVR_OK if success or an error code otherwise. + */ + avr_err_t zh_avr_pcf8574_init(const zh_avr_pcf8574_init_config_t *config, zh_avr_pcf8574_handle_t *handle); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/triac_400hz_delay.xlsx b/triac_400hz_delay.xlsx new file mode 100644 index 0000000..aa41f37 Binary files /dev/null and b/triac_400hz_delay.xlsx differ diff --git a/zh_avr_ac_dimmer.c b/zh_avr_ac_dimmer.c new file mode 100644 index 0000000..7b5dbeb --- /dev/null +++ b/zh_avr_ac_dimmer.c @@ -0,0 +1,12 @@ +#include "zh_avr_ac_dimmer.h" + +const static uint8_t _triac_400hz_delay[100] PROGMEM = {0, 155, 153, 152, 150, 148, 147, 145, 144, 142, + 141, 139, 137, 136, 134, 133, 131, 130, 128, 127, + 125, 123, 122, 120, 119, 117, 116, 114, 112, 111, + 109, 108, 106, 105, 103, 102, 100, 98, 97, 95, + 94, 92, 91, 89, 87, 86, 84, 83, 81, 80, + 78, 77, 75, 73, 72, 70, 69, 67, 66, 64, + 62, 61, 59, 58, 56, 55, 53, 52, 50, 48, + 47, 45, 44, 42, 41, 39, 37, 36, 34, 33, + 31, 30, 28, 27, 25, 23, 22, 20, 19, 17, + 16, 14, 12, 11, 9, 8, 6, 5, 3, 2};