add buttons for voltmeters

This commit is contained in:
eddyem
2015-04-19 23:23:40 +03:00
parent f5bd901e9e
commit 8cde19f5b1
26 changed files with 3620 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
NAME=testproj
SDCC=sdcc
CCFLAGS=-DSTM8S003 -I../ -I../../ -I/usr/share/sdcc/include -mstm8 --out-fmt-ihx
LDFLAGS=-mstm8 --out-fmt-ihx -lstm8
FLASHFLAGS=-cstlinkv2 -pstm8s003
SRC=$(wildcard *.c)
OBJ=$(SRC:%.c=%.rel)
TRASH=$(OBJ) $(SRC:%.c=%.rst) $(SRC:%.c=%.asm) $(SRC:%.c=%.lst)
TRASH+=$(SRC:%.c=%.sym) $(NAME).lk $(NAME).map
INDEPENDENT_HEADERS=../stm8l.h Makefile
all: $(NAME).ihx
$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS)
@touch $@
%.h: ;
clean:
rm -f $(TRASH)
load: $(NAME).ihx
stm8flash $(FLASHFLAGS) -w $(NAME).ihx
%.rel: %.c
$(SDCC) $(CCFLAGS) -c $<
$(NAME).ihx: $(OBJ)
$(SDCC) $(LDFLAGS) $(OBJ) -o $(NAME).ihx
.PHONY: all

View File

@@ -0,0 +1,149 @@
/*
* interrupts.c
*
* Copyright 2015 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "stm8l.h"
#include "interrupts.h"
// Top Level Interrupt
INTERRUPT_HANDLER(TLI_IRQHandler, 0){}
// Auto Wake Up Interrupt
INTERRUPT_HANDLER(AWU_IRQHandler, 1){}
// Clock Controller Interrupt
INTERRUPT_HANDLER(CLK_IRQHandler, 2){}
// External Interrupt PORTA
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3){}
// External Interrupt PORTB
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4){}
// External Interrupt PORTC
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5){}
// External Interrupt PORTD
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6){}
// External Interrupt PORTE
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7){}
#ifdef STM8S903
// External Interrupt PORTF
INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8){}
#endif // STM8S903
#if defined (STM8S208) || defined (STM8AF52Ax)
// CAN RX Interrupt routine.
INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8){}
// CAN TX Interrupt routine.
INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9){}
#endif // STM8S208 || STM8AF52Ax
// SPI Interrupt routine.
INTERRUPT_HANDLER(SPI_IRQHandler, 10){}
// Timer1 Update/Overflow/Trigger/Break Interrupt
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11){
if(TIM1_SR1 & TIM_SR1_UIF){ // update interrupt
Global_time++; // increase timer
}
TIM1_SR1 = 0; // clear all interrupt flags
}
// Timer1 Capture/Compare Interrupt routine.
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12){}
#ifdef STM8S903
// Timer5 Update/Overflow/Break/Trigger Interrupt
INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13){}
// Timer5 Capture/Compare Interrupt
INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14){}
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
// Timer2 Update/Overflow/Break Interrupt
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13){}
// Timer2 Capture/Compare Interrupt
// process soft I2C
INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14){
}
#endif // STM8S903
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
// Timer3 Update/Overflow/Break Interrupt
INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15){}
// Timer3 Capture/Compare Interrupt
INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16){}
#endif // STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
// UART1 TX Interrupt
INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17){}
// UART1 RX Interrupt
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18){}
#endif // STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax
// I2C Interrupt
INTERRUPT_HANDLER(I2C_IRQHandler, 19){}
#if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x)
// UART2 TX interrupt
INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20){}
// UART2 RX interrupt
INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21){}
#endif // STM8S105 or STM8AF626x
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// UART3 TX interrupt
INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20){}
// UART3 RX interrupt
INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21){}
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// ADC2 interrupt
INTERRUPT_HANDLER(ADC2_IRQHandler, 22){}
#else
// ADC1 interrupt
INTERRUPT_HANDLER(ADC1_IRQHandler, 22){
}
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#ifdef STM8S903
// Timer6 Update/Overflow/Trigger Interrupt
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23){}
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x
// Timer4 Update/Overflow Interrupt
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23){}
#endif // STM8S903
// Eeprom EEC Interrupt
INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24){}

View File

@@ -0,0 +1,148 @@
/*
* interrupts.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __INTERRUPTS_H__
#define __INTERRUPTS_H__
#include "stm8l.h"
extern U8 ADC_ready; // flag: data ready
extern U32 Global_time; // global time in ms
extern int ADC_value; // value of last ADC measurement
// Top Level Interrupt
INTERRUPT_DEFINITION(TLI_IRQHandler, 0);
// Auto Wake Up Interrupt
INTERRUPT_DEFINITION(AWU_IRQHandler, 1);
// Clock Controller Interrupt
INTERRUPT_DEFINITION(CLK_IRQHandler, 2);
// External Interrupt PORTA
INTERRUPT_DEFINITION(EXTI_PORTA_IRQHandler, 3);
// External Interrupt PORTB
INTERRUPT_DEFINITION(EXTI_PORTB_IRQHandler, 4);
// External Interrupt PORTC
INTERRUPT_DEFINITION(EXTI_PORTC_IRQHandler, 5);
// External Interrupt PORTD
INTERRUPT_DEFINITION(EXTI_PORTD_IRQHandler, 6);
// External Interrupt PORTE
INTERRUPT_DEFINITION(EXTI_PORTE_IRQHandler, 7);
#ifdef STM8S903
// External Interrupt PORTF
INTERRUPT_DEFINITION(EXTI_PORTF_IRQHandler, 8);
#endif // STM8S903
#if defined (STM8S208) || defined (STM8AF52Ax)
// CAN RX Interrupt routine.
INTERRUPT_DEFINITION(CAN_RX_IRQHandler, 8);
// CAN TX Interrupt routine.
INTERRUPT_DEFINITION(CAN_TX_IRQHandler, 9);
#endif // STM8S208 || STM8AF52Ax
// SPI Interrupt routine.
INTERRUPT_DEFINITION(SPI_IRQHandler, 10);
// Timer1 Update/Overflow/Trigger/Break Interrupt
INTERRUPT_DEFINITION(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11);
// Timer1 Capture/Compare Interrupt routine.
INTERRUPT_DEFINITION(TIM1_CAP_COM_IRQHandler, 12);
#ifdef STM8S903
// Timer5 Update/Overflow/Break/Trigger Interrupt
INTERRUPT_DEFINITION(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13);
// Timer5 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM5_CAP_COM_IRQHandler, 14);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
// Timer2 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM2_UPD_OVF_BRK_IRQHandler, 13);
// Timer2 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM2_CAP_COM_IRQHandler, 14);
#endif // STM8S903
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
defined(STM8S005) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
// Timer3 Update/Overflow/Break Interrupt
INTERRUPT_DEFINITION(TIM3_UPD_OVF_BRK_IRQHandler, 15);
// Timer3 Capture/Compare Interrupt
INTERRUPT_DEFINITION(TIM3_CAP_COM_IRQHandler, 16);
#endif // STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x
#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
defined(STM8S003) || defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
// UART1 TX Interrupt
INTERRUPT_DEFINITION(UART1_TX_IRQHandler, 17);
// UART1 RX Interrupt
INTERRUPT_DEFINITION(UART1_RX_IRQHandler, 18);
#endif // STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax
// I2C Interrupt
INTERRUPT_DEFINITION(I2C_IRQHandler, 19);
#if defined(STM8S105) || defined(STM8S005) || defined (STM8AF626x)
// UART2 TX interrupt
INTERRUPT_DEFINITION(UART2_TX_IRQHandler, 20);
// UART2 RX interrupt
INTERRUPT_DEFINITION(UART2_RX_IRQHandler, 21);
#endif // STM8S105 or STM8AF626x
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// UART3 TX interrupt
INTERRUPT_DEFINITION(UART3_TX_IRQHandler, 20);
// UART3 RX interrupt
INTERRUPT_DEFINITION(UART3_RX_IRQHandler, 21);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
// ADC2 interrupt
INTERRUPT_DEFINITION(ADC2_IRQHandler, 22);
#else // STM8S105, STM8S103 or STM8S903 or STM8AF626x
// ADC1 interrupt
INTERRUPT_DEFINITION(ADC1_IRQHandler, 22);
#endif // STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax
#ifdef STM8S903
// Timer6 Update/Overflow/Trigger Interrupt
INTERRUPT_DEFINITION(TIM6_UPD_OVF_TRG_IRQHandler, 23);
#else // STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x
// Timer4 Update/Overflow Interrupt
INTERRUPT_DEFINITION(TIM4_UPD_OVF_IRQHandler, 23);
#endif // STM8S903
// Eeprom EEC Interrupt
INTERRUPT_DEFINITION(EEPROM_EEC_IRQHandler, 24);
#endif // __INTERRUPTS_H__

View File

@@ -0,0 +1,258 @@
/*
* led.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "stm8l.h"
#include "led.h"
/*
* bits no 7 6 5 4 3 2 1 0
* dec value 128 64 32 16 8 4 2 1
*/
/********** current variant **********/
/*
* One digit: TABLE:
* ***A*** 0 1 2 3 4 5 6 7 8 9 A B C D E F - h
* * * (F) PB4 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0
* F B (B) PB5 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 1 1
* * * (A) PC3 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 1
* ***G*** (G) PC7 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
* * * (C) PD1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0
* E C (DP)PC6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
* * * ** (D) PC5 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1
* ***D*** *DP* (E) PC4 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 0
* **
*/
/*
* BUTTONS
* DON'T FORGET: PB4 & PB5 are "real open-drain"! They can't be pullup inputs, so
* you should solder resistor 10..47k to appropriate buttons!
* 0 - PB4, 1 - PB5, 2 - PD1, 3..7 - PC3..PC7
* or:
* 0 - F(10), 1 - B(7), 2 - C(4), 3 - A(11), 4 - E(1), 5 - D(2), 6 - DP(3), 7 - G(5)
*/
volatile U8 buttons_state;
static U8 display_buffer[3] = {' ',' ',' '}; // blank by default
static U8 N_current = 0; // current digit to display
/*
* Number of digit on indicator with common anode
* digis 0..2: PA3, PD4, PD5
*/
#define CLEAR_ANODES() do{PD_ODR &= ~(0x30);PA_ODR &= ~(0x08);}while(0)
/************* arrays for ports *************/
// PB, mask: 0x30 (dec: 48), PB4:0x10=16, PB5:0x20=32
// To light up a segment we should setup it as PPout -> this arrays are inverse!
#define PB_BLANK 0x30
static U8 PB_bits[18] = {48,32,32,32,48,16,16,32,48,48,48,16,16,32,16,16,0,16};
// PC, mask: 0xF8 (dec: 248), PC3:0x08=8, PC4:0x10=16, PC5:0x20=32, PC6:0x40=64, PC7:0x80=128
#define PC_BLANK 0xF8
static U8 PC_bits[18] = {56,0,184,168,128,168,184,8,184,168,152,176,56,176,184,152,128,144};
// PD, mask: 0x02, PD1
static U8 PD_bits[18] = {2,2,0,2,2,2,2,2,2,2,2,2,0,2,0,0,0,2};
#define PD_BLANK 0x02
/**
* Initialize ports
* anodes (PA3, PD4, PD5) are push-pull outputs,
* cathodes (PB4, PB5, PD1, PC3..PC7) are ODouts in active mode, pullup inputs in buttons reading and floating inputs in inactive
* PA3, PB4|5, PC3|4|5|6|7, PD1|4|5
*/
void LED_init(){
PA_DDR |= 0x08; PD_DDR |= 0x30; // anodes are PPout, cathodes will be PPout only in active mode
PA_CR1 |= 0x08; PD_CR1 |= 0x30;
// prepare cathodes ODR
PB_ODR &= ~PB_BLANK; PC_ODR &= ~PC_BLANK; PD_ODR &= ~PD_BLANK;
}
/*
********************* GPIO (page 111) ********************
* Px_ODR - Output data register bits
* Px_IDR - Pin input values
* Px_DDR - Data direction bits (1 - output)
* Px_CR1 - DDR=0: 0 - floating, 1 - pull-up input; DDR=1: 0 - pseudo-open-drain, 1 - push-pull output [not for "T"]
* Px_CR2 - DDR=0: 0/1 - EXTI disabled/enabled; DDR=1: 0/1 - 2/10MHz
*
*/
/**
* Show next digit - function calls from main() by some system time value amount
*/
void show_next_digit(){
U8 L = display_buffer[N_current] & 0x7f;
// first turn all off
CLEAR_ANODES();
// convert all cathodes to pullup inputs (CR1 is already in ones)
PB_DDR &= ~PB_BLANK;
PC_DDR &= ~PC_BLANK;
PD_DDR &= ~PD_BLANK;
PB_CR1 |= PB_BLANK;
PC_CR1 |= PC_BLANK;
PD_CR1 |= PD_BLANK;
// process buttons attached to A1 and cathodes
if(N_current == 1){ // first digit was lighten up, now we light second -- check buttons
// now check button states: 0 - PB4, 1 - PB5, 2 - PD1, 3..7 - PC3..PC7
buttons_state =
((PB_IDR & PB_BLANK) >> 4) |
((PD_IDR & PD_BLANK) << 1) |
(PC_IDR & PC_BLANK);
}
// switch all anodes to floating inputs
PA_DDR &= ~0x08; PA_CR1 &= ~0x08;
PD_DDR &= ~0x30; PD_CR1 &= ~0x30;
// turn off pullups of cathodes (also all outputs now will be OD)
PB_CR1 &= ~PB_BLANK;
PC_CR1 &= ~PC_BLANK;
PD_CR1 &= ~PD_BLANK;
// now set spare segments switching them to ODoutputs
if(L < 18){ // letter
PB_DDR |= PB_bits[L];
PC_DDR |= PC_bits[L];
PD_DDR |= PD_bits[L];
}
if(L & 0x80){ // DP
PC_DDR |= GPIO_PIN6;
}
switch(N_current){
case 0:
PA_DDR |= 0x08; // switch to output
PA_CR1 |= 0x08; // push-pull
PA_ODR |= 0x08;
break;
case 1:
PD_DDR |= 0x10;
PD_CR1 |= 0x10;
PD_ODR |= 0x10;
break;
case 2:
PD_DDR |= 0x20;
PD_CR1 |= 0x20;
PD_ODR |= 0x20;
break;
}
if(++N_current > 2) N_current = 0;
}
/**
* fills buffer to display
* @param str - string to display, contains "0..f" for digits, " " for space, "." for DP
* for example: " 1.22" or "h1ab" (something like "0...abc" equivalent to "0.abc"
* register independent!
* any other letter would be omitted
* if NULL - fill buffer with spaces
*/
void set_display_buf(char *str){
U8 B[3];
char ch, M = 0, i;
N_current = 0; // refresh current digit number
// empty buffer
for(i = 0; i < 3; i++)
display_buffer[i] = ' ';
if(!str) return;
i = 0;
for(;(ch = *str) && (i < 3); str++){
M = 0;
if(ch > '/' && ch < ':'){ // digit
M = '0';
}else if(ch > '`' & ch < 'g'){ // a..f
M = 'a' - 10;
}else if(ch > '@' & ch < 'G'){ // A..F
M = 'A' - 10;
}else if(ch == '-'){ // minus
M = '-' - 16;
}else if(ch == 'h'){ // hex
M = 'h' - 17;
}else if(ch == 'H'){ // hex
M = 'H' - 17;
}else if(ch == '.'){ // DP, set it to previous char
if(i == 0){ // word starts from '.' - make a space with point
B[0] = 0xff;
}else{ // set point for previous character
B[i-1] |= 0x80;
}
continue;
}else if(ch != ' '){ // bad character - continue
continue;
}
B[i] = ch - M;
i++;
}
// now make align to right
ch = 2;
for(M = i-1; M > -1; M--, ch--){
display_buffer[ch] = B[M];
}
}
/**
* convert integer value i into string and display it
* @param i - value to display, -99 <= i <= 999, if wrong, displays "---E"
*/
void display_int(int I, char voltmeter){
int rem;
U8 pos = 0; //DP position
char N = 2, sign = 0, i;
if(I < -99 || I > 999){
set_display_buf("--E");
return;
}
// prepare buffer for voltmeter's values
if(voltmeter){
for(i = 0; i < 3; i++)
display_buffer[i] = 0;
if(I>999){
I /= 10;
pos = 1; // DP is in 2nd position - voltage more than 9.99V
}
}else{
for(i = 0; i < 3; i++)
display_buffer[i] = ' ';
}
if(I == 0){ // just show zero
display_buffer[2] = 0;
return;
}
if(I < 0){
sign = 1;
I *= -1;
}
do{
rem = I % 10;
display_buffer[N] = rem;
I /= 10;
}while(--N > -1 && I);
if(sign && N > -1) display_buffer[N] = 16; // minus sign
if(voltmeter) display_buffer[pos] |= 0x80;
}
/**
* displays digital point at position i
* @param i - position to display DP, concequent calls can light up many DPs
*/
void display_DP_at_pos(U8 i){
if(i > 2) return;
display_buffer[i] |= 0x80;
}

View File

@@ -0,0 +1,35 @@
/*
* led.h
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __LED_H__
#define __LED_H__
#include "stm8l.h"
extern volatile U8 buttons_state; // flags for button: 1 - pressed, 0 - not pressed
void LED_init();
void set_display_buf(char *str);
void show_next_digit();
void display_int(int i, char voltmeter);
void display_DP_at_pos(U8 i);
#endif // __LED_H__

View File

@@ -0,0 +1,132 @@
/*
* main.c
*
* Copyright 2014 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "main.h"
#include "interrupts.h"
#include "led.h"
/*
int temp;
#define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
#define PIX_SWAP(a,b) { temp=(a);(a)=(b);(b)=temp; }
int p[9]; // buffer for median filtering
int opt_med9(){
PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
PIX_SORT(p[4], p[2]) ; return(p[4]) ;
}
*/
U32 Global_time = 0L; // global time in ms
// one digit emitting time
#define LED_delay 1
// pause between buttons actions
#define BTNS_delay 30
int main() {
U32 T_LED = 0L; // time of last digit update
U32 T_time = 0L; // timer
U32 T_btns = 0L; // buttons timer
U8 old_btns_state = 0;
//U8 tmpva = 0;
// int pidx = 0; // index in median buffer
// long cntrin = 0, cntrcap = 0; // counters for average
// Configure clocking
CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz
// Configure pins
CFG_GCR |= 1; // disable SWIM
LED_init();
CCR |= 0x28; // make shure that we are on level 3 - disabled SW priority
// Configure Timer1
// prescaler = f_{in}/f_{tim1} - 1
// set Timer1 to 1MHz: 16/1 - 1 = 15
TIM1_PSCRH = 0;
TIM1_PSCRL = 15; // LSB should be written last as it updates prescaler
// auto-reload each 1ms: TIM_ARR = 1000 = 0x03E8
TIM1_ARRH = 0x03;
TIM1_ARRL = 0xE8;
// interrupts: update
TIM1_IER = TIM_IER_UIE;
// auto-reload + interrupt on overflow + enable
TIM1_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;
// enable all interrupts
enableInterrupts();
set_display_buf("abc");
// Loop
do {
// onse per 300ms refresh displayed value
if(((unsigned int)(Global_time - T_time) > 300) || (T_time > Global_time)){
T_time = Global_time;
// display_int((int)tmpva++,0);
// cntrin = 0;cntrcap = 0;voltagein = 0L;voltagecap = 0L;pidx = 0;
}
if((U8)(Global_time - T_LED) > LED_delay){
T_LED = Global_time;
show_next_digit();
}
if((U8)(Global_time - T_btns) > BTNS_delay){
T_btns = Global_time;
if(old_btns_state != buttons_state){
U8 pressed = old_btns_state & ~buttons_state; // pressed buttons are ones
if(pressed){ // display
//display_int(pressed, 0);
display_int(buttons_state, 0);
}else{ // some buttons released
if(buttons_state == 0xff){ // all buttons released
set_display_buf("abc");
}else{
display_int(~old_btns_state & buttons_state, 0); // released are ones
}
}
old_btns_state = buttons_state;
}
}
} while(1);
}
/*
if(ADC_ready){
// prepare data for rounded output
p[pidx] = ADC_value;
if(++pidx == 9){ // buffer is ready
if(vcap){
voltagecap += (long)(opt_med9());
cntrcap++;
ADC_CSR = 0x24; // switch to ain
vcap = 0;
}else{
voltagein += (long)(opt_med9());
cntrin++;
ADC_CSR = 0x26; // switch to vcap
vcap = 1;
}
pidx = 0;
}
ADC_ready = 0;
}
*/

View File

@@ -0,0 +1,30 @@
/*
* main.h
*
* Copyright 2015 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __MAIN_H__
#define __MAIN_H__
#include "stm8l.h"
#define CONCAT(a,b) a##_##b
#define PORT(a,b) CONCAT(a,b)
#endif // __MAIN_H__

View File

@@ -0,0 +1,58 @@
:2080A000808080808080808080AE5255F644241B90CE000472A90001C60003A90097C60025
:1980C00002A9009590CF0004CF0002AE52557F808080808080808080805F
:208000008200808382000000820080A0820080A1820080A2820080A3820080A4820080A57E
:20802000820080A6820080A78200000082000000820080A8820080A9820080D0820080D1F1
:20804000820080D28200000082000000820080D3820080D4820080D58200000082000000C2
:20806000820080D6820080D7820080D88200000082000000820000008200000082000000EB
:1D808300AE00012707724F00005A26F9AE003E2709D6866ED700015A26F7CC8080C8
:03808000CC80D9D8
:2080D900521D5F1F0C1F0A5F1F081F065F1F041F020F01AE50C67FAE7F60F6AA01F7CD8250
:2080F90008AE7F0AF6AA28F7AE52607FAE5261A60FF7AE5262A603F7AE5263A6E8F7AE5293
:2081190054A601F7AE5250A685F79AAE82041F0E1E0E89CD83A45B02CE000472F008C600DF
:208139000312076B1BC600021206C600056B1390CE0003C600026B10A3012C2211CE0004E2
:208159001308C600031207C600021206240A17077B136B097B106B06C60005887B0E6B1A7E
:20817900841019A101230D170B7B136B0D7B106B0ACD823AC60005887B066B17841016A115
:208199001E2395CE00041F04CE00021F027B01C100012603CC8131C600014314014D271082
:2081B900C60001974F954B0089CD84D55B032030C60001A1FF260A1E0E89CD83A45B0220FF
:2081D9001F7B016B180F171E1753C600010F1489140285979E1414954B0089CD84D55B0362
:0F81F900C600016B01CC81315B1D8161626300A7
:04866F000000000007
:20820800AE5002F6AA08F7AE5011F6AA30F7AE5003F6AA08F7AE5012F6AA30F7AE5005F671
:20822800A4CFF7AE500AF6A407F7AE500FF6A4FDF7815206AE00069FCB0009979EA900951E
:20824800F6A47F6B01AE500FF6A4CFF7AE5000F6A4F7F7AE5007F6A4CFF7AE500CF6A4078E
:20826800F7AE5011F6A4FDF7AE5008F6AA30F7AE500DF6AAF8F7AE5012F6AA02F7C6000983
:20828800A1012621AE5006F6A4304EA40F6B03AE5010F6A402481A036B04AE500BF6A4F897
:2082A8001A04C70001AE5002F6A4F7F7AE5003F6A4F7F7AE5011F6A4CFF7AE5012F6A4CFD7
:2082C800F7AE5008F6A4CFF7AE500DF6A407F7AE5012F6A4FDF77B01A1122448AE5007F662
:2082E8006B06AE000A9F1B01979EA90095F61A06AE5007F7AE500CF66B05AE001C9F1B0118
:20830800979EA90095F61A05AE500CF7AE5011F66B02AE002E9F1B01979EA90095F61A023E
:20832800AE5011F77B01482407AE500CF6AA40F7C60009A1002710C60009A1012720C6003A
:2083480009A10227302043AE5002F6AA08F7AE5003F6AA08F7AE5000F6AA08F7202CAE5089
:2083680011F6AA10F7AE5012F6AA10F7AE500FF6AA10F72015AE5011F6AA20F7AE5012F6D1
:20838800AA20F7AE500FF6AA20F7C600094CC70009A1022304725F00095B06815212725FAA
:2083A8000009AE00061F114F5F9772FB1188A620F7844CA1032FF11E152603CC84D2965CBC
:2083C8001F0F0F041615170D1E0DF66B06887B054A6B0D844D2603CC84A67B04A1032F0364
:2083E800CC84A64F957B06A12F2D0C7B06A13A2E06A63095CC848B7B06A1602C040F0B204F
:2084080004A6016B0B7B06A1672F034F2002A601140B4D2706A65795CC848B7B06A1402CCC
:20842800040F0A2004A6016B0A7B06A1472F034F2002A601140A4D2705A6379520457B0635
:20844800A12D2605A61D95203A7B06A1682605A65795202F7B06A1482605A6379520247B0D
:2084680006A12E26180D0426071E0FA6FFF720265F7B0C9772FB0FF6AA80F720197B06A129
:20848800202613905F7B04909772F90F7B068910018590F70C041E0D5C1F0DCC83D07B0CDC
:2084A8006B057B056B09A6026B067B09A1FF2D1A5F7B069772FB111F075F7B099772FB0FBB
:2084C800F61E07F70A090A0620E05B1281520A0F030F011E0DA3FF9D2F131E0DA303E72C63
:2084E800040F0A2004A6016B0A0D0A270CAE85D889CD83A45B02CC85C00D0F2729AE0006B7
:208508001F044F5F9772FB047F4CA1032FF50D0A27294B0A4B001E0F89CD861A5B041F0D32
:20852800A6016B032015AE00061F064F5F9772FB0688A620F7844CA1032FF11E0D2607AE79
:2085480000087FCC85C01E0DA300002E09A6016B011E0D501F0DAE00061F08A6026B024B81
:208568000A4B001E0F89CD85DC5B04905F7B02909772F9089F90F74B0A4B001E0F89CD8686
:208588001A5B041F0D0A027B02A1FF2C034F2002A6014D27041E0D26C60D01270D4D270A6F
:2085A8005F7B029772FB08A610F70D0F270A5F7B039772FB087999765B0A817B03A1022237
:1485C8000EAE00069F1B03979EA90095799976812D2D450005
:20867300202020003020202030101020303030101020101000103800B8A880A8B808B8A8A7
:1A86930098B038B0B898809002020002020202020202020200020000000223
:2085DC0052051E08A300002F040F012004A6016B010D0127051E085020021E081F041E0AA2
:2085FC00A300002E071E0A501F022004160A170216021E0465930D012701505B058152089E
:20861C001E0BA300002F040F052004A6016B050D0527051E0B5020021E0B1F011E0DA30000
:20863C00002F040F062004A6016B060D0627107B0E406B044F120D6B0316031707200416CB
:13865C000D170716071E01657B0518064D2701505B0881F8
:00000001FF