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 -DBUTNS
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,287 @@
/*
* 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 (if BUTNS defined)
* 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)
*/
#ifdef BUTNS
volatile U8 buttons_state;
#endif
/*
* 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, PB4:0x10=16, PB5:0x20=32
#define PB_BLANK 0x30
static U8 PB_bits[18] = {0,16,16,16,0,32,32,16,0,0,0,32,32,16,32,32,48,32};
// PC, mask: 0xF8, PC3:0x08=8, PC4:0x10=16, PC5:0x20=32, PC6:0x40=64, PC7:0x80=128
#define PC_BLANK 0xF8
//static U8 PC_bits[18] = {128,184,0,16,56,16,0,176,0,16,32,8,128,8,0,32,56,40};
static U8 PC_bits[18] = {192,248,64,80,120,80,64,240,64,80,96,72,192,72,64,96,120,104};
// PD, mask: 0x02, PD1
static U8 PD_bits[18] = {0,0,2,0,0,0,0,0,0,0,0,0,2,0,2,2,2,0};
#define PD_BLANK 0x02
/*
********************* 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
*
*/
/**
* Setup for writing a letter
* @param ltr - letter (0..17 for 0..F, - or h | 0x80 for DP, any other value for 'space')
*/
void write_letter(U8 ltr){
U8 L = ltr & 0x7f;
// process buttons attached to A1 and cathodes
#ifdef BUTNS
if(PA_DDR & 0x08){ // first digit was lighten up
// convert all cathodes to pullup inputs (CR1 is already in ones)
PB_DDR &= ~PB_BLANK;
PC_DDR &= ~PC_BLANK;
PD_DDR &= ~PD_BLANK;
// and turn anode into zero
//PA_ODR &= ~0x08;
}
#endif
// first turn all off
CLEAR_ANODES();
// light up all segments
PB_ODR &= ~PB_BLANK;
PC_ODR &= ~PC_BLANK;
PD_ODR &= ~PD_BLANK;
// now clear spare segments
if(L < 18){ // letter
PB_ODR |= PB_bits[L];
PC_ODR |= PC_bits[L];
PD_ODR |= PD_bits[L];
}else{ // space - turn all OFF
PB_ODR |= PB_BLANK;
PC_ODR |= PC_BLANK;
PD_ODR |= PD_BLANK;
}
if(ltr & 0x80){ // DP
PC_ODR &= ~GPIO_PIN6;
}
#ifdef BUTNS
if(PA_DDR & 0x08){ // first digit was lighten up
// 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);
PA_DDR &= ~0x08; // switch first anode to input
// and switch cathodes to output
PB_DDR |= PB_BLANK;
PC_DDR |= PC_BLANK;
PD_DDR |= PD_BLANK;
}
#endif
}
/**
* Turn on anode power for digit N (0..2: PA3, PD4, PD5 -- A0x08, D0x10, D0x20)
* @param N - number of digit (0..2), if other - no action (display off)
* @return
*/
void light_up_digit(U8 N){
switch(N){
case 0:
#ifdef BUTNS
PA_DDR |= 0x08; // switch to output
#endif
PA_ODR |= 0x08;
break;
case 1:
PD_ODR |= 0x10;
break;
case 2:
PD_ODR |= 0x20;
break;
}
}
static U8 display_buffer[3] = {' ',' ',' '}; // blank by default
static U8 N_current = 0; // current digit to display
/**
* 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];
}
}
/**
* Show Nth digit of buffer (function ran by timer)
* @param N - number of digit in buffer (0..3)
*/
void show_buf_digit(U8 N){
if(N > 2) return;
write_letter(display_buffer[N]);
light_up_digit(N);
}
/**
* Show next digit - function calls from main() by some system time value amount
*/
void show_next_digit(){
show_buf_digit(N_current++);
if(N_current > 2) N_current = 0;
}
/**
* Turn off current digit: to change display brightness
*/
void lights_off(){
U8 N;
if(N_current) N = N_current - 1;
else N = 2;
light_up_digit(N);
}
/**
* 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,47 @@
/*
* 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"
#ifdef BUTNS
extern volatile U8 buttons_state; // flags for button: 1 - pressed, 0 - not pressed
#endif
void set_display_buf(char *str);
void show_buf_digit(U8 N);
void show_next_digit();
void lights_off();
void display_int(int i, char voltmeter);
void display_DP_at_pos(U8 i);
/**
* Initialize ports
* PA3, PB4|5, PC3|4|5|6|7, PD1|4|5
*/
#define LED_init() do{ \
PA_DDR = 0x08; PB_DDR = 0x30; PC_DDR = 0xf8; PD_DDR = 0x32; \
PA_CR1 = 0x08; PB_CR1 = 0x30; PC_CR1 = 0xf8; PD_CR1 = 0x32; \
}while(0)
#endif // __LED_H__

View File

@@ -0,0 +1,136 @@
/*
* 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(buttons_state != old_btns_state){ // user has pressed or released any button
;
old_btns_state = buttons_state;
}*/
}
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
:1D808300AE00012707724F00005A26F9AE003E2709D68685D700015A26F7CC8080B1
:03808000CC80D9D8
:2080D900521D5F1F0C1F0A5F1F081F065F1F041F020F01AE50C67F72107F60AE5002A608B5
:2080F900F7AE5007A630F7AE500CA6F8F7AE5011A632F7AE5003A608F7AE5008A630F7AEFA
:20811900500DA6F8F7AE5012A632F7AE7F0AF6AA28F7AE52607FAE5261A60FF7AE5262A68B
:2081390003F7AE5263A6E8F7AE5254A601F7AE5250A685F79AAE822F1F1C1E1C89CD836D2C
:208159005B02CE000472F008C6000312076B19C600021206C600056B1790CE0003C60002B1
:208179006B14A3012C2211CE00041308C600031207C600021206240A17077B176B097B14D4
:208199006B06C60005887B0E6B14841013A101230D170B7B176B0D7B146B0ACD84BEC6007C
:2081B90005887B066B13841012A11E2395CE00041F04CE00021F027B01C100012603CC8163
:2081D9005BC600014314014D2711C60001974F954B0089CD84EC5B03CC815BC60001A1FFC7
:2081F900260A1E1C89CD836D5B02201F7B016B110F101E1053C600010F0E89140285979E45
:1A821900140E954B0089CD84EC5B03C600016B01CC815B5B1D81616263002B
:0486860000000000F0
:2082330052067B09A47F6B01AE5002F6A5082715AE5007F6A4CFF7AE500CF6A407F7AE50DC
:2082530011F6A4FDF7AE500FF6A4CFF7AE5000F6A4F7F7AE5005F6A4CFF7AE500AF6A4076D
:20827300F7AE500FF6A4FDF7AE5005F66B057B01A1122444AE00069F1B01979EA90095F681
:208293001A05AE5005F7AE500AF66B02AE00189F1B01979EA90095F61A02AE500AF7AE5044
:2082B3000FF66B04AE002A9F1B01979EA90095F61A04AE500FF720167B05AA30AE5005F78F
:2082D300AE500AF6AAF8F7AE500FF6AA02F77B09482407AE500AF6A4BFF7AE5002F69095E4
:2082F300A508273EAE5006F6A4304EA40F6B03AE5010F6A402481A036B06AE500BF6A4F801
:208313001A06C70001909EA4F7AE5002F7AE5007F6AA30F7AE500CF6AAF8F7AE5011F6AA8E
:2083330002F75B06817B03A100270E7B03A10127187B03A102271B2020AE5002F6AA08F75A
:20835300AE5000F6AA08F72010AE500FF6AA10F72007AE500FF6AA20F7815212725F003FA9
:20837300AE003C1F094F5F9772FB0988A620F7844CA1032FF11E152603CC849C965C5C1F8F
:20839300070F011615170B1E0BF66B06887B024A6B13844D2603CC84707B01A1032F03CC31
:2083B30084704F957B06A12F2D0C7B06A13A2E06A63095CC84557B06A1602C040F112004B2
:2083D300A6016B117B06A1672F034F2002A60114114D2706A65795CC84557B06A1402C042C
:2083F3000F102004A6016B107B06A1472F034F2002A60114104D2705A6379520457B06A1BC
:208413002D2605A61D95203A7B06A1682605A65795202F7B06A1482605A6379520247B06DD
:20843300A12E26180D0126071E07A6FFF720265F7B129772FB07F6AA80F720197B06A12051
:208453002613905F7B01909772F9077B068910018590F70C011E0B5C1F0BCC839A7B126B08
:20847300057B056B0FA6026B067B0FA1FF2D1A5F7B069772FB091F0D5F7B0F9772FB07F65D
:208493001E0DF70A0F0A0620E05B12817B03A1022218AE003C9F1B03979EA90095F688CDD0
:2084B3008233847B0388CD83388481C6003F95725C003F9E88CD849F84C6003FA1022304CD
:2084D300725F003F81725D003F2706C6003F4A2002A60288CD83388481520A0F030F011EF3
:2084F3000DA3FF9D2F131E0DA303E72C040F082004A6016B080D08270CAE85EF89CD836DEE
:208513005B02CC85D70D0F2729AE003C1F044F5F9772FB047F4CA1032FF50D0827294B0A47
:208533004B001E0F89CD86315B041F0DA6016B032015AE003C1F064F5F9772FB0688A620B9
:20855300F7844CA1032FF11E0D2607AE003E7FCC85D71E0DA300002E09A6016B011E0D50FF
:208573001F0DAE003C1F09A6026B024B0A4B001E0F89CD85F35B04905F7B02909772F90994
:208593009F90F74B0A4B001E0F89CD86315B041F0D0A027B02A1FF2C034F2002A6014D2759
:2085B300041E0D26C60D01270D4D270A5F7B029772FB09A610F70D0F270A5F7B039772FB09
:2085D300097999765B0A817B03A102220EAE003C9F1B03979EA90095799976812D2D4500FE
:20868A00001010100020201000000020201020203020C0F84050785040F040506048C048F0
:1A86AA004060786800000200000000000000000002000202020020202000CC
:2085F30052051E08A300002F040F012004A6016B010D0127051E085020021E081F041E0A8B
:20861300A300002E071E0A501F022004160A170216021E0465930D012701505B0581520886
:208633001E0BA300002F040F052004A6016B050D0527051E0B5020021E0B1F011E0DA300E9
:20865300002F040F062004A6016B060D0627107B0E406B044F120D6B0316031707200416B4
:138673000D170716071E01657B0518064D2701505B0881E1
:00000001FF