add 1-wire

This commit is contained in:
2015-04-04 20:27:02 +03:00
parent 1a0095a9ae
commit b861211e29
23 changed files with 2236 additions and 22 deletions

View File

@@ -0,0 +1,34 @@
NAME=testproj
SDCC=sdcc
CCFLAGS=-DSTM8S003 -I../ -I/usr/share/sdcc/include -mstm8 --out-fmt-ihx
LDFLAGS=-mstm8 --out-fmt-ihx -lstm8
FLASHFLAGS=-cstlinkv2 -pstm8s003
SRC=$(wildcard *.c)
# ATTENTION: FIRST in list should be file with main()
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,156 @@
/*
* 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"
U8 ADC_ready = 0; // flag: data ready
int ADC_value = 0;// value of last ADC measurement
// 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){
ADC_value = ADC_DRL; // in right-alignment mode we should first read LSB
ADC_value |= ADC_DRH << 8;
ADC_ready = 1;
ADC_CSR &= 0x3f; // clear EOC & AWD flags
}
#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,241 @@
/*
* 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
* **
*/
/*
* 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};
// 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
/**
* 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;
// 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;
}
}
/**
* Turn on anode power for digit N (0..3: PA3, PD6, PD4, PD1 -- A0x08, D0x40, D0x10, D0x02)
* @param N - number of digit (0..3), if other - no action (display off)
* @return
*/
void light_up_digit(U8 N){
switch(N){
case 0:
PA_ODR |= 0x08;
break;
case 1:
PD_ODR |= 0x40;
break;
case 2:
PD_ODR |= 0x10;
break;
case 3:
PD_ODR |= 0x02;
break;
}
}
static U8 display_buffer[4] = {' ',' ',' ',' '}; // 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, -999 <= i <= 9999, 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 < -999 || I > 9999){
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[3] = 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,43 @@
/*
* 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"
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
* PA1|3, PB4|5, PC3|4|5|6|7, PD1|4|6
*/
#define LED_init() do{ \
PA_DDR = 0x0a; PB_DDR = 0x30; PC_DDR = 0xf8; PD_DDR = 0x52; \
PA_CR1 = 0x0a; PB_CR1 = 0x30; PC_CR1 = 0xf8; PD_CR1 = 0x52; \
}while(0)
#endif // __LED_H__

View File

@@ -0,0 +1,164 @@
/*
* 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
//eeprom_data *saved_data = (eeprom_data*)EEPROM_START_ADDR;
//U32 testtimer;
// one digit emitting time
#define LED_delay 1
/*
U8 change_eeprom_value(U8 *addr, U8 *val, U8 len){
U8 i;
// unlock memory
FLASH_DUKR = EEPROM_KEY1;
FLASH_DUKR = EEPROM_KEY2;
// check bit DUL=1 in FLASH_IAPSR
if(!(FLASH_IAPSR & 0x08))
return 0;
for(i = 0; i < len; i++)
*addr = val[i];
while(!(FLASH_IAPSR & 0x04)); // wait till end
// clear DUL to lock write
FLASH_IAPSR &= ~0x08;
return 1;
}
void eeprom_default_setup(){
eeprom_data template = {
.magick = EEPROM_MAGICK,
.ADU_to_mV = DEFAULT_ADU_TO_MV,
.max_ADU = DEFAULT_MAX_ADU
};
if(saved_data->magick != EEPROM_MAGICK){
if(change_eeprom_value((U8*)saved_data, (U8*)&template, sizeof(eeprom_data)))
testtimer = saved_data->ADU_to_mV;
else
testtimer = 999;
}else{
testtimer = 0; // other times - from zero
}
}
*/
int main() {
U32 T_LED = 0L; // time of last digit update
U32 T_time = 0L; // timer
int U = 0;
U8 vcap = 0; // ADC selection: 1 - vcap, 0 - ain
int pidx = 0; // index in median buffer
long voltagein = 0L, voltagecap = 0L; // variables for average values: vin and vcap
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;
//eeprom_default_setup();
// configure ADC
// select PD3[AIN4] - AIN .. PD6[AIN6] - Vcap & enable interrupt for EOC
ADC_CSR = 0x24; // 0x24 - AIN4, 0x26 - AIN6
ADC_TDRL = 0x50; // disable Schmitt triger for AIN4 & AIN6
// right alignment
ADC_CR2 = 0x08; // don't forget: first read ADC_DRL!
// f_{ADC} = f/18 & continuous non-buffered conversion & wake it up
ADC_CR1 = 0x73; // 0x71 - single, 0x73 - continuous
ADC_CR1 = 0x73; // turn on ADC (this needs second write operation)
// enable all interrupts
enableInterrupts();
set_display_buf(" E0 "); // low power -> infinitive rebooting
// Loop
do {
// onse per 300ms refresh displayed value
if(((unsigned int)(Global_time - T_time) > 300) || (T_time > Global_time)){
T_time = Global_time;
voltagein /= cntrin; // average
voltagecap /= cntrcap;
// voltage = 1800 * ADUin / ADUcap
voltagein *= 1800;
U = (int)(voltagein/voltagecap);
display_int(U, 1);
cntrin = 0;
cntrcap = 0;
voltagein = 0L;
voltagecap = 0L;
pidx = 0;
}
if((U8)(Global_time - T_LED) > LED_delay){
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;
}
T_LED = Global_time;
show_next_digit();
}
} while(1);
}

View File

@@ -0,0 +1,40 @@
/*
* 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 EEPROM_MAGICK (0x1234)
#define DEFAULT_ADU_TO_MV (35840)
#define DEFAULT_MAX_ADU (119800)
typedef struct{
U16 magick;
U32 ADU_to_mV;
U32 max_ADU;
}eeprom_data;
*/
#define CONCAT(a,b) a##_##b
#define PORT(a,b) CONCAT(a,b)
#endif // __MAIN_H__

View File

@@ -0,0 +1,106 @@
:2080A000808080808080808080AE5255F644241B90CE001A72A90001C60019A90097C600F9
:2080C00018A9009590CF001ACF0018AE52557F80808080808080AE5405F65F97CF0016AE10
:2080E0005404F65F9758585858585858589FCA0017979ECA001695CF001635010015AE5420
:0881000000F6A43FF780808027
:038C380000000039
:208000008200808382000000820080A0820080A1820080A2820080A3820080A4820080A57E
:20802000820080A6820080A78200000082000000820080A8820080A9820080D0820080D1F1
:20804000820080D28200000082000000820080D3820080D4820080D58200000082000000C2
:20806000820080D6820081068200810782000000820000008200000082000000820000008B
:1D808300AE00142707724F00005A26F9AE00422709D68C37D700145A26F7CC8080CF
:03808000CC83634B
:208108005238AE00031F051E055C5C1F011E01FE160572A900041703160390FE171D131D81
:208128002D0FCF00011E01161DFF1E0390CE0001FF1E051C00081F1B1E1BFE160572A9006D
:208148000A1723162390FE172113212D0FCF00011E1B1621FF1E2390CE0001FF1E051C0047
:208168000E1F1F1E1FFE160572A900101719161990FE171713172D0FCF00011E1F1617FF70
:208188001E1990CE0001FF1E05FE160190FE171513152D0FCF00011E051615FF1E0190CE52
:2081A8000001FF1E051C00061F131E13FE161B90FE171113112D0FCF00011E131611FF1E85
:2081C8001B90CE0001FF1E051C000C1F0F1E0FFE161F90FE170D130D2D0FCF00011E0F1624
:2081E8000DFF1E1F90CE0001FF1E01FE160390FE170B130B2D0FCF00011E01160BFF1E0360
:2082080090CE0001FF1E1BFE162390FE170913092D0FCF00011E1B1609FF1E2390CE0001BB
:20822800FF1E1FFE161990FE170713072D0FCF00011E1F1607FF1E1990CE0001FF1E05FEEC
:20824800161390FE173713372D0FCF00011E051637FF1E1390CE0001FF1E23FE161990FEC1
:20826800173513352D0FCF00011E231635FF1E1990CE0001FF1E1BFE161F90FE17331333AC
:208288002D0FCF00011E1B1633FF1E1F90CE0001FF1E13FE160F90FE173113312D0FCF0035
:2082A800011E131631FF1E0F90CE0001FF1E01FE161B90FE172F132F2D0FCF00011E01160E
:2082C8002FFF1E1B90CE0001FF1E03FE162390FE172D132D2D0FCF00011E03162DFF1E23B7
:2082E80090CE0001FF1E1BFE161F90FE172B132B2D0FCF00011E1B162BFF1E1F90CE00017D
:20830800FF1E1BFE160390FE172913292D0FCF00011E1B1629FF1E0390CE0001FF1E0FFECF
:20832800161B90FE172713272D0FCF00011E0F1627FF1E1B90CE0001FF1E1BFE160390FE14
:20834800172513252D0FCF00011E1B1625FF1E0390CE0001FF1E1BFE5B388152425F1F1A2C
:208368001F185F1F161F140F135F1F115F1F221F205F1F3D1F3B5F1F031F015F1F071F0508
:20838800AE50C67F72107F60AE5002A60AF7AE5007A630F7AE500CA6F8F7AE5011A652F71B
:2083A800AE5003A60AF7AE5008A630F7AE500DA6F8F7AE5012A652F7AE7F0AF6AA28F7AEF7
:2083C80052607FAE5261A60FF7AE5262A603F7AE5263A6E8F7AE5254A601F7AE5250A68500
:2083E800F7AE5400A624F7AE5407A650F7AE5402A608F7AE5401A673F7AE5401A673F79A5C
:20840800AE858989CD866B5B02CE001A72F016C6001912156B25C600181214A3012C2211F7
:20842800CE001A1316C600191215C6001812142476CE001A1F16CE00181F141E03891E037E
:20844800891E26891E2689CD88E95B081F0F170D1E07891E07891E41891E4189CD88E95B39
:20846800081F0B17091E0F891E0F894B084B075F89CD8A7A5B081F321E0B891E0B891E366C
:20848800899089CD88E95B084B0189CD87EB5B035F1F031F015F1F071F055F1F221F205F97
:2084A8001F3D1F3B5F1F11C6001B887B1C6B39841038A1012203CC8411725D00152603CCFE
:2084C8008576AE00031F391E115872FB3990CE0016FF1E115C1F111E11A300092703CC85DF
:2084E800720D132740CD81089E494FA2006B3572FB3D887B36193D6B4184193B1F3D6B3BEE
:208508007B406B3C1E071C00017B06A9006B2D7B05A9001F076B057B2D6B06AE5400A62449
:20852800F70F132042CD81089F905F5D2A02905A9772FB22909F19216B29909E19201F2296
:208548006B207B296B211E031C00017B02A9006B1D7B01A9001F036B017B1D6B02AE5400B2
:20856800A626F7A6016B135F1F11725F0015CE001A1F1ACE00181F18CD87BDCC84115B4249
:06858800812045302000B7
:048C3B000000000035
:20858E0052047B07A47F6B01AE500FF6A4CFF7AE5000F6A4F7F7AE5005F6A4CFF7AE500A08
:2085AE00F6A407F7AE500FF6A4FDF7AE5005F66B047B01A1122444AE001C9F1B01979EA918
:2085CE000095F61A04AE5005F7AE500AF66B03AE002E9F1B01979EA90095F61A03AE500A59
:2085EE00F7AE500FF66B02AE00409F1B01979EA90095F61A02AE500FF720167B04AA30AE97
:20860E005005F7AE500AF6AAF8F7AE500FF6AA02F77B07482407AE500AF6A4BFF75B048196
:20862E007B03A10027147B03A10127177B03A102271A7B03A103271D2022AE5000F6AA08C4
:20864E00F72019AE500FF6AA40F72010AE500FF6AA10F72007AE500FF6AA02F781521272F0
:20866E005F0056AE00521F0C4F5F9772FB0C88A620F7844CA1032FF11E152603CC879B9695
:20868E001C00041F070F01161517091E09F66B03887B024A6B0C844D2603CC876F7B01A101
:2086AE00032F03CC876F4F957B03A12F2D0C7B03A13A2E06A63095CC87547B03A1602C04FC
:2086CE000F122004A6016B127B03A1672F034F2002A60114124D2706A65795CC87547B03FC
:2086EE00A1402C040F112004A6016B117B03A1472F034F2002A60114114D2705A637952014
:20870E00457B03A12D2605A61D95203A7B03A1682605A65795202F7B03A1482605A6379546
:20872E0020247B03A12E26180D0126071E07A6FFF720265F7B0B9772FB07F6AA80F72019DA
:20874E007B03A1202613905F7B01909772F9077B038910018590F70C011E095C1F09CC8661
:20876E00997B0B6B027B026B10A6026B037B10A1FF2D1A5F7B039772FB0C1F0E5F7B109744
:20878E0072FB07F61E0EF70A100A0320E05B12817B03A1022218AE00529F1B03979EA90033
:2087AE0095F688CD858E847B0388CD862E8481C6005695725C00569E88CD879E84C600561B
:2087CE00A1022304725F005681725D00562706C600564A2002A60288CD862E848152090F1F
:2087EE00010F021E0CA3FC192F071E0CA3270F2D0CAE88E589CD866B5B02CC88CD0D0E27E3
:20880E002CAE00521F064F5F9772FB067F4CA1032FF51E0CA303E72D294B0A4B001E0E894C
:20882E00CD8BE35B041F0CA6016B012015AE00521F044F5F9772FB0488A620F7844CA1038B
:20884E002FF11E0C2607AE00557FCC88CD1E0CA300002E09A6016B021E0C501F0CAE005233
:20886E001F08A6026B034B0A4B001E0E89CD8A3C5B04905F7B03909772F9089F90F74B0AE4
:20888E004B001E0E89CD8BE35B041F0C0A037B03A1FF2C034F2002A6014D27041E0C26C605
:2088AE000D02270D4D270A5F7B039772FB08A610F70D0E270A5F7B019772FB087999765B3D
:1B88CE0009817B03A102220EAE00529F1B03979EA90095799976812D2D4500DC
:208C3F0000101010002020100000002020102020302080B80010381000B0001020088008B5
:1B8C5F00002038280000020000000000000000000200020202002020202000F0
:2088E900521E1E23A300007B22A2007B21A2002F040F112004A6016B110D1127151E235019
:208909004F12226B1C4F12211F146B127B1C6B13200816231714162117121614171916128F
:2089290017171E27A300007B26A2007B25A2002F040F162004A6016B160D16271E7B2840A4
:208949006B104F12276B0F4F12266B0E4F12256B0D160F170B160D170920081627170B166C
:20896900251709160B170716091E078990891E1D891E1D89CD899D5B0817017B1118164D87
:20898900270D504F120290974F12019095200216015B1E8152125F1F051F03A6206B027B4F
:2089A90015484F496B0116171E1590585917171F157B036B0F1E04887B076B1384081259B1
:2089C900090F1F047B126B067B0F6B030D01271A7B06AA016B0A7B056B097B046B087B0309
:2089E9006B0716091705160717031E05131B7B04121A7B031219252B160572F21B7B04126A
:208A09001A6B0C7B03121917056B037B0C6B047B18AA0190977B1790957B16977B15951713
:208A2900171F150A020D022703CC89A81E1716155B128152051E08A300002F040F012004CB
:208A4900A6016B010D0127051E085020021E081F041E0AA300002E071E0A501F022004160C
:208A69000A170216021E0465930D012701505B058152409096905C961C00431F051E05E66B
:208A890003961C00471F0B1E0B1F0D1E0D1F3F1E3F88E60197844290FF72A900021E05E6EB
:208AA900031E0B1F071E071F091E091F0F1E0F88E60397844290FF965C1F151E15F66B175E
:208AC9001E05F697160B90E603429F1B171E15F71E15F66B1C1E05E60197160B90E60242DA
:208AE9009F1B1C1E15F79096905C93FE1F1D1E05E6011E0B1F231E231F251E251F271E27C6
:208B090088E60397844272FB1D90FF93FE1F291E05E6021E0B1F2B1E2B1F2D1E2D1F311EC0
:208B29003188E60297844272FB2990FF16051E05E6021E0B1F331E331F351E351F371E37C5
:208B490088E6019784429F90F71E055C1F391E05E60290971E0BE60390421E39FF160B1E98
:208B690005E6031E0B1F3D1E3D1F2F1E2F88F69784429F90F71E0B5C1F3B1E05E603909770
:208B89001E0BE60290421E3BFF1E0B1C00037F1E051C00037F965CE6036B14E6026B13E663
:208BA900016B12F616431718164572F9131721887B13191A6B218419186B1F1621EF02164D
:208BC9001FFFE603E602FE16491E4772F9219F1920979E191F95515B408152081E0BA300E2
:208BE900002F040F052004A6016B050D0527051E0B5020021E0B1F011E0DA300002F040FB8
:208C0900062004A6016B060D0627107B0E406B044F120D6B03160317072004160D170716F9
:0F8C2900071E01657B0518064D2701505B08816A
:00000001FF

View File

@@ -1,31 +1,29 @@
NAME=testproj
SDCC=sdcc
HEX2BIN=hex2bin
CCFLAGS=-DSTM8S003 -I../ -I/usr/share/sdcc/include -mstm8 --out-fmt-ihx
LDFLAGS= -lstm8 -mstm8 --out-fmt-ihx
LDFLAGS=-mstm8 --out-fmt-ihx -lstm8
FLASHFLAGS=-cstlinkv2 -pstm8s003
SRC=$(wildcard *.c)
# ATTENTION: FIRST in list should be file with main()
OBJ=$(SRC:%.c=%.rel)
TRASH=$(OBJ) $(SRC:%.c=%.rst) $(SRC:%.c=%.asm) $(SRC:%.c=%.lst)
TRASH+=$(SRC:%.c=%.sym) $(NAME).ihx $(NAME).lk $(NAME).map
TRASH+=$(SRC:%.c=%.sym) $(NAME).lk $(NAME).map
INDEPENDENT_HEADERS=../stm8l.h Makefile
all: $(NAME).bin
all: $(NAME).ihx
$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS)
@touch $@
@echo $@
%.h: ;
clean:
rm -f $(TRASH)
load: $(NAME).bin
stm8flash $(FLASHFLAGS) -w $(NAME).bin
load: $(NAME).ihx
stm8flash $(FLASHFLAGS) -w $(NAME).ihx
%.rel: %.c
$(SDCC) $(CCFLAGS) -c $<
@@ -33,7 +31,4 @@ load: $(NAME).bin
$(NAME).ihx: $(OBJ)
$(SDCC) $(LDFLAGS) $(OBJ) -o $(NAME).ihx
$(NAME).bin: $(NAME).ihx
$(HEX2BIN) -p 00 $<
.PHONY: all

View File

@@ -205,10 +205,10 @@ void display_int(int I, char voltmeter){
// prepare buffer for voltmeter's values
if(voltmeter){
for(i = 1; i < 4; i++)
display_buffer[i] = ' ';
display_buffer[i] = 0;
}else{
for(i = 1; i < 4; i++)
display_buffer[i] = 0;
display_buffer[i] = ' ';
}
if(I == 0){ // just show zero
display_buffer[3] = 0;

View File

@@ -1,31 +1,29 @@
NAME=testproj
SDCC=sdcc
HEX2BIN=hex2bin
CCFLAGS=-DSTM8S003 -I../ -I/usr/share/sdcc/include -mstm8 --out-fmt-ihx
LDFLAGS= -lstm8 -mstm8 --out-fmt-ihx
LDFLAGS=-mstm8 --out-fmt-ihx -lstm8
FLASHFLAGS=-cstlinkv2 -pstm8s003
SRC=$(wildcard *.c)
# ATTENTION: FIRST in list should be file with main()
OBJ=$(SRC:%.c=%.rel)
TRASH=$(OBJ) $(SRC:%.c=%.rst) $(SRC:%.c=%.asm) $(SRC:%.c=%.lst)
TRASH+=$(SRC:%.c=%.sym) $(NAME).ihx $(NAME).lk $(NAME).map
TRASH+=$(SRC:%.c=%.sym) $(NAME).lk $(NAME).map
INDEPENDENT_HEADERS=../stm8l.h Makefile
all: $(NAME).bin
all: $(NAME).ihx
$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS)
@touch $@
@echo $@
%.h: ;
clean:
rm -f $(TRASH)
load: $(NAME).bin
stm8flash $(FLASHFLAGS) -w $(NAME).bin
load: $(NAME).ihx
stm8flash $(FLASHFLAGS) -w $(NAME).ihx
%.rel: %.c
$(SDCC) $(CCFLAGS) -c $<
@@ -33,7 +31,4 @@ load: $(NAME).bin
$(NAME).ihx: $(OBJ)
$(SDCC) $(LDFLAGS) $(OBJ) -o $(NAME).ihx
$(NAME).bin: $(NAME).ihx
$(HEX2BIN) -p 00 $<
.PHONY: all