From 43508f051f92fab6022a64ca19aad421cfe75dde Mon Sep 17 00:00:00 2001 From: Ole Artz Date: Tue, 19 Jul 2022 11:19:56 +0200 Subject: [PATCH] DCDC MDC Converter Board: add comments, code gets a new structure --- atmega32u4/dcdc_mdc/README.md | 4 +- atmega32u4/dcdc_mdc/main.c | 192 +++++++++++++++++++--------------- 2 files changed, 108 insertions(+), 88 deletions(-) diff --git a/atmega32u4/dcdc_mdc/README.md b/atmega32u4/dcdc_mdc/README.md index 79e50e5..8c36e84 100644 --- a/atmega32u4/dcdc_mdc/README.md +++ b/atmega32u4/dcdc_mdc/README.md @@ -60,7 +60,7 @@ Each group has two channels (0, 1) | 6 | Current Offset | | 7 | Voltage V_out (RO) | | 8 | Current C_out (NA) | -| 9 | Sense GND (NA) | +| 9 | Sense GND (NA) | NA := not available RO := read only @@ -72,4 +72,4 @@ RO := read only The DCDC MDC Converter Board get his operating voltage via MicroUSB and receives messages via the LANTelnetServerBoard with UART Baud rate 57600. ------------------------------------------------------------------------------------------------------------------------ -### Version 1.1, 2022-05-30 +### Version 1.2, 2022-07-19 diff --git a/atmega32u4/dcdc_mdc/main.c b/atmega32u4/dcdc_mdc/main.c index 74690db..74f480b 100644 --- a/atmega32u4/dcdc_mdc/main.c +++ b/atmega32u4/dcdc_mdc/main.c @@ -1,5 +1,25 @@ +/* +##################################################### +# ### ### # +# ### CODE for MDC DCDC Converter Board ### # +# ### for remote configuration ### # +# ### via Universal Ethernet Board ### # +# ### with LANTelnetToSerial Code ### # +# ### ### # +# ### useing EEPROM to save settings ### # +# ### ### # +# ### author: O.Artz ### # +# ### ### # +# ### UPDATE 2022-07-19 ### # +# ### ### # +##################################################### +*/ + // #define F_CPU 8000000UL +/* + * #################################### load libraries + */ #include #include #include @@ -7,8 +27,6 @@ #include #include -#define FIRMWARE_VERSION 0x001 - /* //ADC // 0 Sense1 Vin @@ -27,7 +45,7 @@ // PD6 LED1 (yellow) // PD4 LED2 (red) -//PB7 Output enable +// PB7 Output enable //shift register outputs (times 4) Output 0 on MSB, Output 3 on LSB side // CTRL10 (MSB) @@ -39,6 +57,20 @@ // CTRL01 // CTRL02 + +// **Data format:** *XuuGcRvvvv* +// X - command (write: W, read: R, answer: A etc.) +// uu - Controllernumber (Hex value) +// G - Groupnumber (to talk to all channels, that belong together, in one command.) +// c - channelnumber in the group (Hex value) +// R - register (Hex value) +// vvvv - 16 Bit value +// +// All in all 10 characters +// +// close with a "\n" +// e.g. "RF2012FE51\n" + // | Registers | description | // |-----------|--------------------------------------------------------------| // | 0 | DCDC ON/OFF | @@ -51,11 +83,13 @@ // | 7 | Voltage V_out (RO) | // | 8 | Current C_out (NA) | // | 9 | Sense GND (NA) | - -// **Data format:** *XuuGcRvvvv* -// **Data format:** *X000cRvvvv* */ +/* + * #################################### define parameters to fixed values + */ +#define FIRMWARE_VERSION 0x001 + #define ISMYADDR() (rxbuf[1] == '0' && rxbuf[2] == '0') #define LED1_ON() PORTD |= (1 << PD6) @@ -85,7 +119,9 @@ #define SHIFT_EN_OUTPUT() PORTB &= ~(1 << PB7) #define SHIFT_DIS_OUTPUT() PORTB |= (1 << PB7) -// declaration of needed variables +/* + * #################################### declaration of needed variables + */ uint16_t time; @@ -106,6 +142,9 @@ uint16_t V_out; uint16_t C_out; uint16_t GND; +/* + * #################################### converter + */ uint8_t nib_to_hex(uint16_t in, uint8_t nib) { // convert integer or nibbles into hex value uint8_t t = (in >> (nib * 4)) & 0xF; @@ -122,6 +161,9 @@ uint8_t hex_to_int(uint8_t h) {//assumes valid number return h - 0x61 + 10; } +/* + * #################################### communication functions + */ void send_answer_buf(uint8_t *b) { UCSR1B |= (1 << UDRIE1); } // activate transmit interupt "ISR(USART1_UDRE_vect)" @@ -142,6 +184,9 @@ void send_answer_hex(uint8_t *rxbuf, uint16_t v) { send_answer_buf(txbuf); } +/* + * #################################### chaining functions + */ void sub1(uint8_t *c1, uint8_t *c2) { // need for chain boards to count down the board number uint8_t b = hex_to_int(*c1) * 16 + hex_to_int(*c2); @@ -169,6 +214,9 @@ uint8_t is_my_address(uint8_t s) { } } +/* + * #################################### setting functions + */ void setVoltages(void) { // need to change the voltage by dis/enable resistor with the shiftregister "NPIC6C596A" cli(); @@ -252,39 +300,58 @@ void setInfo(uint8_t chan, uint8_t val) { information |= (FIRMWARE_VERSION << 4); } -/* -// Data: XuuGcRvvvv -// Switch deactivated -// X - command (write: W, read: R, answer: A etc.) -// uu - Controllernumber (Hex value) -// G - Groupnumber (to talk to all channels, that belong together, -in one command.) -// c - channelnumber in the group (Hex value) -// R - register (Hex value) -// vvvv - 16 Bit value -// -// All in all 10 characters -// RF2012FE51 -// -// close with a "\n" -// e.g. "RF2012FE51\n" -// -// -// -// | Registers | description | -// |-----------|--------------------------------------------------------------| -// | 0 | DCDC ON/OFF | -// | 1 | DCDC set voltage adjustment resistors | -// | 2 | Voltage V_in (RO) | -// | 3 | Current C_in (RO) | -// | 4 | Temperature (RO) | -// | 5 | [15:4] Firmware; [3:2] reserved; [1] Switch ; [0] LED (RO) | -// | 6 | Current Offset | -// | 7 | Voltage V_out (RO) | -// | 8 | Current C_out (NA) | -// | 9 | Sense GND (RO) | -*/ +/* + * #################################### interupts + */ +ISR(ADC_vect) { + // interupt for reading adc and save the values + static uint8_t channel = 0; + adc[channel] = ADC; + + if (channel == 1) channel = 4; + else if (channel == 7) channel = 13; + else if (channel == 13) channel = 0; + else channel++; + + ADMUX &= 0xe0; + ADMUX |= (channel & 0xf); + if (channel == 13) + ADCSRB |= (1 << MUX5); + else + ADCSRB &= ~(1 << MUX5); + + ADCSRA |= (1 << ADSC); +} + +ISR(USART1_RX_vect) { + // interupt for incoming command + uint8_t buf = UDR1; + if (isMaster == 0) { + getdata(buf); + } else { + // usb_serial_putchar(buf); + } +} + +ISR(USART1_UDRE_vect) { + // interupt for transmitting answer message + if (txbuf[txpoint] != 0) + UDR1 = txbuf[txpoint++]; + if (txpoint > 11 || txbuf[txpoint] == 0) { + txpoint = 0; + UCSR1B &= ~(1 << UDRIE1); // deactivate Transmit + } +} +ISR(TIMER0_OVF_vect) { + // interupt for time, actual only need for watchdoc if programm crashed as reset function + time++; + asm volatile("wdr"); +} + +/* + * #################################### head functions + */ void getdata(uint8_t buf) { // handle the incoming command and call the correct function if (rxcnt != 0 || (buf == 'A' || buf == 'S' || buf == 'W' || buf == 'R')) { @@ -488,53 +555,6 @@ void getdata(uint8_t buf) { } - -ISR(ADC_vect) { - // interupt for reading adc and save the values - static uint8_t channel = 0; - adc[channel] = ADC; - - if (channel == 1) channel = 4; - else if (channel == 7) channel = 13; - else if (channel == 13) channel = 0; - else channel++; - - ADMUX &= 0xe0; - ADMUX |= (channel & 0xf); - if (channel == 13) - ADCSRB |= (1 << MUX5); - else - ADCSRB &= ~(1 << MUX5); - - ADCSRA |= (1 << ADSC); -} - -ISR(USART1_RX_vect) { - // interupt for incoming command - uint8_t buf = UDR1; - if (isMaster == 0) { - getdata(buf); - } else { - // usb_serial_putchar(buf); - } -} - -ISR(USART1_UDRE_vect) { - // interupt for transmitting answer message - if (txbuf[txpoint] != 0) - UDR1 = txbuf[txpoint++]; - if (txpoint > 11 || txbuf[txpoint] == 0) { - txpoint = 0; - UCSR1B &= ~(1 << UDRIE1); // deactivate Transmit - } -} - -ISR(TIMER0_OVF_vect) { - // interupt for time, actual only need for watchdoc if programm crashed as reset function - time++; - asm volatile("wdr"); -} - __attribute__((naked)) void main(void) { // initialize the board -- 2.43.0