Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed function set_display_from_int, did not display the most signifi… #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
802 changes: 802 additions & 0 deletions EWSTM8/vc288.ewp

Large diffs are not rendered by default.

92 changes: 61 additions & 31 deletions src/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,50 @@
#include "adc.h"
#include <stdint.h>

#define VAR_ADC_SCAN
//#define VAR_ADC_SCAN

#define ADC_DB(n,ml) REGVAR(REGVAR(ADC_DB,n), ml);
#define ADC_DB_VOLTS_RL ADC_DB(CANELADC_VOLTS,_RL)
#define ADC_DB_VOLTS_RH ADC_DB(CANELADC_VOLTS,_RH)

#if CANELADC_AMPS < CANELADC_VOLTS
#define CANELADC_HIGH CANELADC_VOLTS
#else
#define CANELADC_HIGH CANELADC_AMPS
#endif

#define FREQ_ADC_SAMPLING ((FREQ_AVERAGE_NETWORK*2)*(1<<ADC_AVERAGE_SAMPLES_BASE_2))

//ADC_AVERAGE_SAMPLES_BASE_2 5
#ifdef VAR_ADC_SCAN
#if FREQ_ADC_SAMPLING < 7200
//FREQ_AVERAGE_NETWORK 112
#define ADC_PRESCALE_DEF (ADC_CR1_SPSEL2) // |> Prescaler = fmaster/8.
#elif FREQ_ADC_SAMPLING < 9600
//FREQ_AVERAGE_NETWORK 150
#define ADC_PRESCALE_DEF (ADC_CR1_SPSEL0|ADC_CR1_SPSEL1) // |> Prescaler = fmaster/6.
#elif FREQ_ADC_SAMPLING < 28800
//FREQ_AVERAGE_NETWORK 450
#define ADC_PRESCALE_DEF (ADC_CR1_SPSEL1) // |> Prescaler = fmaster/4.
#else
#error FREQ_ADC_SAMPLING > 30719
#endif
#else
#if FREQ_ADC_SAMPLING < 16384
//FREQ_AVERAGE_NETWORK 255
#define ADC_PRESCALE_DEF (ADC_CR1_SPSEL2) // |> Prescaler = fmaster/8.
#elif FREQ_ADC_SAMPLING < 21845
//FREQ_AVERAGE_NETWORK 341
#define ADC_PRESCALE_DEF (ADC_CR1_SPSEL0|ADC_CR1_SPSEL1) // |> Prescaler = fmaster/6.
#elif FREQ_ADC_SAMPLING < 32768
//FREQ_AVERAGE_NETWORK 510
#define ADC_PRESCALE_DEF (ADC_CR1_SPSEL1) // |> Prescaler = fmaster/4.
#else
#error FREQ_ADC_SAMPLING > 32767
#endif
#endif



static volatile uint16_t adc_meas_cnt = 0;
static volatile uint32_t adc_ch3_avg = 0;
Expand Down Expand Up @@ -32,18 +75,18 @@ ISR(ADC1_EOC_IRQHandler, ITC_IRQ_ADC1)
temp16 = ((uint16_t)templ)<<8 | ADC_DRL;
// Reset the EOC flag, otherwise it will fire again straight away.
CLRBIT(ADC_CSR, ADC_CSR_EOC);
if((ADC_CSR & ADC_CSR_CH2)==0){
if((ADC_CSR & 0x0F) == CANELADC_AMPS){
adc_ch3_avg += temp16;
CLRBIT(ADC_CSR, ADC_CSR_CH0); // -
CLRBIT(ADC_CSR, ADC_CSR_CH1); // |> ADC samples AIN4 only.
SETBIT(ADC_CSR, ADC_CSR_CH2); // |

ADC_CSR = (ADC_CSR & 0xF0) | CANELADC_VOLTS;

SETBIT(ADC_CR1, ADC_CR1_ADON);
}
else{
adc_ch4_avg += temp16;
SETBIT(ADC_CSR, ADC_CSR_CH0); // -
SETBIT(ADC_CSR, ADC_CSR_CH1); // |> ADC samples AIN3 only.
CLRBIT(ADC_CSR, ADC_CSR_CH2); // |

ADC_CSR = (ADC_CSR & 0xF0) | CANELADC_AMPS;

if (adc_meas_cnt++ == (1 << ADC_AVERAGE_SAMPLES_BASE_2))
{
adc_ch3 = adc_ch3_avg >> ADC_AVERAGE_SAMPLES_BASE_2;
Expand All @@ -56,11 +99,11 @@ ISR(ADC1_EOC_IRQHandler, ITC_IRQ_ADC1)
}
}
#else
templ= ADC_DB3RH;
temp16= ((uint16_t)templ << 8) | ADC_DB3RL;
templ= ADC_DB(CANELADC_AMPS,RH);
temp16= ((uint16_t)templ << 8) | ADC_DB(CANELADC_AMPS,RL);
adc_ch3_avg += temp16;
templ= ADC_DB4RH;
temp16= ((uint16_t)templ << 8) | ADC_DB4RL;
templ= ADC_DB(CANELADC_VOLTS,RH);
temp16= ((uint16_t)templ << 8) | ADC_DB(CANELADC_VOLTS,RL);
adc_ch4_avg += temp16;
// Reset the EOC flag, otherwise it will fire again straight away.
CLRBIT(ADC_CSR, ADC_CSR_EOC);
Expand Down Expand Up @@ -94,16 +137,9 @@ void setup_adc(void)
//SETBIT(ADC_CR1, ADC_CR1_ADON);
CLRBIT(ADC_CR1, ADC_CR1_ADON);

SETBIT(ADC_CSR, ADC_CSR_CH0); // -
SETBIT(ADC_CSR, ADC_CSR_CH1); // |> ADC samples AIN3 only.
CLRBIT(ADC_CSR, ADC_CSR_CH2); // |
CLRBIT(ADC_CSR, ADC_CSR_CH3); // |

SETBIT(ADC_CSR, ADC_CSR_EOCIE); // Enable the interrupt after conversion completed.
ADC_CSR = (ADC_CSR & 0xF0) | CANELADC_AMPS | ADC_CSR_EOCIE;

CLRBIT(ADC_CR1, ADC_CR1_SPSEL0); // -
CLRBIT(ADC_CR1, ADC_CR1_SPSEL1); // |> Prescaler = fmaster/8.
SETBIT(ADC_CR1, ADC_CR1_SPSEL2); // -
ADC_CR1 = (ADC_CR1 & ~(ADC_CR1_SPSEL0|ADC_CR1_SPSEL1|ADC_CR1_SPSEL2)) | ADC_PRESCALE_DEF; //Prescaler = fmaster/ADC_PRESCALE_DEF.
//CLRBIT(ADC_CR1, ADC_CR1_CONT); // -


Expand All @@ -118,16 +154,10 @@ void setup_adc(void)
//SETBIT(ADC_CR1, ADC_CR1_ADON);
CLRBIT(ADC_CR1, ADC_CR1_ADON);

CLRBIT(ADC_CSR, ADC_CSR_CH0); // -
CLRBIT(ADC_CSR, ADC_CSR_CH1); // |> ADC samples AIN4 only.
SETBIT(ADC_CSR, ADC_CSR_CH2); // |
CLRBIT(ADC_CSR, ADC_CSR_CH3); // |
ADC_CSR = (ADC_CSR & 0xF0) | CANELADC_HIGH | ADC_CSR_EOCIE;

SETBIT(ADC_CSR, ADC_CSR_EOCIE); // Enable the interrupt after conversion completed.
ADC_CR1 = (ADC_CR1 & ~(ADC_CR1_SPSEL0|ADC_CR1_SPSEL1|ADC_CR1_SPSEL2)) | ADC_PRESCALE_DEF; //Prescaler = fmaster/ADC_PRESCALE_DEF.

CLRBIT(ADC_CR1, ADC_CR1_SPSEL0); // -
CLRBIT(ADC_CR1, ADC_CR1_SPSEL1); // |> Prescaler = fmaster/8.
SETBIT(ADC_CR1, ADC_CR1_SPSEL2); // -
//CLRBIT(ADC_CR1, ADC_CR1_CONT); // -

SETBIT(ADC_CR3, ADC_CR3_DBUF); ///< When DBUF is set, converted values are stored in the ADC_DBxRH and ADC_DBxRL registers instead of the ADC_DRH and ADC_DRL registers.
Expand All @@ -140,8 +170,8 @@ void setup_adc(void)

/*------------------TDR configuration ---------------------------*/
ADC_TDRH = 0x00;
ADC_TDRL = (1<<4)|(1<<3);

ADC_TDRL = (1<<CANELADC_AMPS)|(1<<CANELADC_VOLTS);
SETBIT(ADC_CR1, ADC_CR1_ADON);
}

Expand Down
10 changes: 9 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
*/
//#define SWIM_DEBUG_ENABLED

/**
* Measured network frequency
* Частота измеряемой сети
*/
#define FREQ_AVERAGE_NETWORK 2 /*Hz */

/**
* Defines how many samples are going to be averaged.
* Note: the resulting number of samples is 2^ADC_AVERAGE_SAMPLES_BASE_2
* Number of averaging measurement points per period for calculating the root-mean-square value
* Количество точек измерения усреднения за период для вычисления средне квадратичного значения
*/
#define ADC_AVERAGE_SAMPLES_BASE_2 5
#define ADC_AVERAGE_SAMPLES_BASE_2 12

/**
* Set to float or double, depending on compiler availability
Expand Down
114 changes: 106 additions & 8 deletions src/config_8s003.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
#define SEVEN_SEG_COMMON_ANODE 2


//#define BOARD_HW_V1
#define BOARD_HW_V1
//#define BOARD_HW_V2
#define BOARD_HW_V3
//#define BOARD_HW_V3
//#define BOARD_HW_W1209 /*Для отладки на плате термостата W1209*/

/**
* PDF from UpcycleElectronics states a common anode display, but
* mine is a common cathode.
* Use either COMMON_CATHODE or COMMON_ANODE.
*/
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1)
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1) || defined(BOARD_HW_W1209)
#define DISPLAY_TYPE SEVEN_SEG_COMMON_CATHODE
#elif defined(BOARD_HW_V3)
#define DISPLAY_TYPE SEVEN_SEG_COMMON_ANODE
Expand All @@ -35,7 +36,7 @@
#define ADC_SCALE (3.3 / 1024.0)

/* Resistance value for the combined elements R3 and RV2 from the sch */
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1)
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1) || defined(BOARD_HW_W1209)
#define AMPS_RV2 20000.0
#define AMPS_R3 180000.0
#elif defined(BOARD_HW_V3)
Expand All @@ -47,7 +48,7 @@
#define AMPS_R3_RV2 (AMPS_R3+(AMPS_RV2/2))

/* Resistance value for R4 from the sch */
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1)
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1) || defined(BOARD_HW_W1209)
#define AMPS_R4 8200.0
#elif defined(BOARD_HW_V3)
#define AMPS_R4 4200.0
Expand All @@ -56,7 +57,7 @@
#endif

/* Resistance value for the on-board shunt */
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1)
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1) || defined(BOARD_HW_W1209)
#define AMPS_SHUNT 0.02
#elif defined(BOARD_HW_V3)
#define AMPS_SHUNT 1.6
Expand All @@ -67,7 +68,7 @@
#define GAIN_FOR_AMPS (((1 + (AMPS_R3_RV2 / AMPS_R4)) / AMPS_SHUNT) * ADC_SCALE)

/* Resistance value for the combined elements R2 and RV1 from the sch */
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1)
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1) || defined(BOARD_HW_W1209)
#define VOLTS_RV1 20000.0
#define VOLTS_R2 270000.0
#elif defined(BOARD_HW_V3)
Expand All @@ -79,10 +80,11 @@
#define VOLTS_R2_RV1 (VOLTS_R2+(VOLTS_RV1/2))

/* Resistance value for R1 from the sch */
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1)
#if defined(BOARD_HW_V2) || defined(BOARD_HW_V1) || defined(BOARD_HW_W1209)
#define VOLTS_R1 8200.0
#elif defined(BOARD_HW_V3)
#define VOLTS_R1 2200.0
//#define VOLTS_R1 4700.0 /*V max 52 volts*/
#else
#error defined BOARD_HW_V?
#endif
Expand All @@ -93,6 +95,9 @@

#if defined(BOARD_HW_V1)

#define CANELADC_VOLTS 4 /*PD3*/
#define CANELADC_AMPS 3 /*PD2*/

#define STPROG_PORT PD
#define STPROG_PIN PIN6

Expand Down Expand Up @@ -140,9 +145,13 @@

#elif defined(BOARD_HW_V2)

#define CANELADC_VOLTS 4 /*PD3*/
#define CANELADC_AMPS 3 /*PD2*/

#define STPROG_PORT PD
#define STPROG_PIN PIN6

#define SWIM_DEBUG_MCD_A
#define MCD_A_PORT PD
#define MCD_A_PIN PIN1

Expand Down Expand Up @@ -187,9 +196,13 @@

#elif defined(BOARD_HW_V3)

#define CANELADC_VOLTS 4 /*PD3*/
#define CANELADC_AMPS 3 /*PD2*/

#define STPROG_PORT PD
#define STPROG_PIN PIN6

#define SWIM_DEBUG_MCD_A
#define MCD_A_PORT PD
#define MCD_A_PIN PIN1

Expand Down Expand Up @@ -232,9 +245,94 @@
#define MCD2_AD3_PORT PA
#define MCD2_AD3_PIN PIN1

#elif defined(BOARD_HW_W1209)
/*Для отладки на плате термостата W1209*/

#define STPROG_PORT PC
#define STPROG_PIN PIN3

#define MCD_A_PORT PD
#define MCD_A_PIN PIN5

#define MCD_B_PORT PA
#define MCD_B_PIN PIN2

#define MCD_C_PORT PC
#define MCD_C_PIN PIN7

#define MCD_D_PORT PD
#define MCD_D_PIN PIN3

#define SWIM_DEBUG_MCD_E
#define MCD_E_PORT PD
#define MCD_E_PIN PIN1

#define MCD_F_PORT PA
#define MCD_F_PIN PIN1

#define MCD_G_PORT PC
#define MCD_G_PIN PIN6

#define MCD_DP_PORT PD
#define MCD_DP_PIN PIN2

#if 1
/*Вывод значения напряжения с ADC6 */
#define CANELADC_VOLTS 6 /*PA6*/
#define CANELADC_AMPS 2 /*PC4*/
#define MCD1_AD1_PORT PD
#define MCD1_AD1_PIN PIN4

#define MCD1_AD2_PORT PB
#define MCD1_AD2_PIN PIN5

#define MCD1_AD3_PORT PB
#define MCD1_AD3_PIN PIN4

#define MCD2_AD1_PORT PC
#define MCD2_AD1_PIN PIN3

#define MCD2_AD2_PORT PA
#define MCD2_AD2_PIN PIN5

#define MCD2_AD3_PORT PC
#define MCD2_AD3_PIN PIN5
#else
/*Выод значения тока с ADC6 */
#define CANELADC_VOLTS 2 /*PC4*/
#define CANELADC_AMPS 6 /*PA6*/

#define MCD2_AD1_PORT PD
#define MCD2_AD1_PIN PIN4

#define MCD2_AD2_PORT PB
#define MCD2_AD2_PIN PIN5

#define MCD2_AD3_PORT PB
#define MCD2_AD3_PIN PIN4

#define MCD1_AD1_PORT PC
#define MCD1_AD1_PIN PIN3

#define MCD1_AD2_PORT PA
#define MCD1_AD2_PIN PIN5

#define MCD1_AD3_PORT PC
#define MCD1_AD3_PIN PIN5
#endif

#else
#error Not defined BOARD_HW_V?(1,2,3)!!!
#endif


#if !defined(CANELADC_VOLTS) || CANELADC_VOLTS < 2 || CANELADC_VOLTS > 6 || CANELADC_VOLTS == CANELADC_AMPS
#error defined CANELADC_VOLTS!
#endif

#if !defined(CANELADC_AMPS) || CANELADC_AMPS < 2 || CANELADC_AMPS > 6
#error defined CANELADC_AMPS!
#endif


#endif /* __CONFIG_8S003_H */
14 changes: 6 additions & 8 deletions src/display_7seg.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ void set_display_from_int(uint16_t number,
uint8_t digits)
{
// Note: 8th bit controls the decimal place dot
display_data[row+2] = (digits & 1 ? seven_seg_lut[number % 10] : 0) |
(decimal_dots & 1 ? SEG_MASK_DP : 0);
display_data[row+2] = (digits & SEVEN_SEG_DIGITS_RIGHTMOST ? seven_seg_lut[number % 10] : 0) |
(decimal_dots & SEVEN_SEG_DP_RIGHTMOST ? SEG_MASK_DP : 0);
number /= 10;
decimal_dots >>= 1;
display_data[row+1] = (digits & 1 ? seven_seg_lut[number % 10] : 0) |
(decimal_dots & 1 ? SEG_MASK_DP : 0);
display_data[row+1] = (digits & SEVEN_SEG_DIGITS_MIDDLE ? seven_seg_lut[number % 10] : 0) |
(decimal_dots & SEVEN_SEG_DP_MIDDLE ? SEG_MASK_DP : 0);
number /= 10;
decimal_dots >>= 1;
display_data[row+0] = (digits & 1 ? seven_seg_lut[number % 10] : 0) |
(decimal_dots & 1 ? SEG_MASK_DP : 0);
display_data[row+0] = (digits & SEVEN_SEG_DIGITS_LEFTMOST ? seven_seg_lut[number % 10] : 0) |
(decimal_dots & SEVEN_SEG_DP_LEFTMOST ? SEG_MASK_DP : 0);
}
Loading