Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef HWDEF_thefreeman_BST_FWAA_MP3432_HDR_DAC_RGB_H
- #define HWDEF_thefreeman_BST_FWAA_MP3432_HDR_DAC_RGB_H
- /* thefreeman’s FWAA AA/li-ion Boost driver based on MP3432 and attiny1616 with high dynamic range and DAC control, AUX : RGB
- * hardware version : 1.0
- *
- *
- * Pin / Name / Function
- * 1 PA2 BattLVL (ADC0 - AIN2)
- * 2 PA3
- * 3 GND GND
- * 4 VCC VCC
- * 5 PA4 EN
- * 6 PA5 HDR
- * 7 PA6 DAC
- * 8 PA7
- * 9 PB5 B
- * 10 PB4 G
- * 11 PB3 R
- * 12 PB2 IN- NFET
- * 13 PB1
- * 14 PB0
- * 15 PC0
- * 16 PC1
- * 17 PC2
- * 18 PC3 SWITCH
- * 19 PA0 UDPI
- * 20 PA1
- *
- * EN enable the boost regulator and op-amp
- * DAC pin sets the current, max current depends on Vset voltage divider and Rsense
- * HDR FET pin switches between high value Rsense (low current range, pin low), and low value Rsense (high current range, pin high)
- * IN- NFET : pull up after BST enable to elimiante startup flash, pull down otherwise
- */
- #define LAYOUT_DEFINED
- #ifdef ATTINY
- #undef ATTINY
- #endif
- #define ATTINY 1616
- #include <avr/io.h>
- #define PWM_CHANNELS 1
- #define USE_DYN_PWM // dynamic frequency and speed
- #ifndef SWITCH_PIN
- #define SWITCH_PIN PIN3_bp
- #define SWITCH_PORT VPORTC.IN
- #define SWITCH_ISC_REG PORTC.PIN3CTRL
- #define SWITCH_VECT PORTC_PORT_vect
- #define SWITCH_INTFLG VPORTC.INTFLAGS
- #endif
- #define PWM1_LVL DAC0.DATA // use this for DAC voltage output
- // PWM parameters of both channels are tied together because they share a counter
- #define PWM1_TOP VREF.CTRLA // holds the TOP value for for variable-resolution PWM
- //BST enable
- #define LED2_ENABLE_PIN PIN4_bp
- #define LED2_ENABLE_PORT PORTA_OUT
- //HDR
- #define LED_ENABLE_PIN PIN5_bp
- #define LED_ENABLE_PORT PORTA_OUT
- //IN- NFET
- // TODO
- // Voltage divider battLVL
- #define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened
- #define DUAL_VOLTAGE_FLOOR 21 // for AA/14500 boost drivers, don't indicate low voltage if below this level
- #define DUAL_VOLTAGE_LOW_LOW 7 // the lower voltage range's danger zone 0.7 volts (NiMH)
- #define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN2_gc // which ADC channel to read
- // Raw ADC readings at 4.4V and 2.2V
- // calibrate the voltage readout here
- // estimated / calculated values are:
- // (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
- // Resistors are 330k and 100k
- #ifndef ADC_44
- #define ADC_44 951 // raw value at 4.40V
- #endif
- #ifndef ADC_22
- #define ADC_22 476 // raw value at 2.20V
- #endif
- // this driver allows for aux LEDs under the optic
- #ifndef AUXLED_R_PIN
- #define AUXLED_R_PIN PIN3_bp
- #endif
- #ifndef AUXLED_G_PIN
- #define AUXLED_G_PIN PIN4_bp
- #endif
- #ifndef AUXLED_B_PIN
- #define AUXLED_B_PIN PIN5_bp
- #endif
- #define AUXLED_RGB_PORT PORTB // PORTA or PORTB or PORTC
- // with so many pins, doing this all with #ifdefs gets awkward...
- // ... so just hardcode it in each hwdef file instead
- inline void hwdef_setup() {
- // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
- // TODO: for this DAC controlled-light, try to decrease the clock speed or use the ULP
- _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
- VPORTA.DIR = PIN4_bm | PIN5_bm | PIN6_bm; // EN, HDR, DAC
- VPORTB.DIR = PIN2_bm | PIN3_bm | PIN4_bm | PIN5_bm; // RGB, IN- NFET
- //VPORTC.DIR = PIN0_bm | PIN1_bm; //
- // enable pullups on the input pins to reduce power
- PORTA.PIN0CTRL = PORT_PULLUPEN_bm; //
- PORTA.PIN1CTRL = PORT_PULLUPEN_bm; //
- //PORTA.PIN2CTRL = PORT_PULLUPEN_bm; // BattLVL
- PORTA.PIN3CTRL = PORT_PULLUPEN_bm; //
- //PORTA.PIN4CTRL = PORT_PULLUPEN_bm; // EN
- //PORTA.PIN5CTRL = PORT_PULLUPEN_bm; // HDR
- //PORTA.PIN6CTRL = PORT_PULLUPEN_bm; // DAC
- PORTA.PIN7CTRL = PORT_PULLUPEN_bm; //
- PORTB.PIN0CTRL = PORT_PULLUPEN_bm; //
- PORTB.PIN1CTRL = PORT_PULLUPEN_bm; //
- //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // IN- NFET
- //PORTB.PIN3CTRL = PORT_PULLUPEN_bm; // R
- //PORTB.PIN4CTRL = PORT_PULLUPEN_bm; // G
- //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // B
- PORTC.PIN0CTRL = PORT_PULLUPEN_bm; //
- PORTC.PIN1CTRL = PORT_PULLUPEN_bm; //
- PORTC.PIN2CTRL = PORT_PULLUPEN_bm; //
- PORTC.PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; // e-switch
- // set up the DAC
- // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
- // DAC ranges from 0V to (255 * Vref) / 256
- VREF.CTRLA |= VREF_DAC0REFSEL_2V5_gc; // also VREF_DAC0REFSEL_0V55_gc and VREF_DAC0REFSEL_1V1_gc and VREF_DAC0REFSEL_2V5_gc
- VREF.CTRLB |= VREF_DAC0REFEN_bm;
- DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm;
- DAC0.DATA = 255; // set the output voltage
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement