This commit is contained in:
Eddy
2014-03-19 00:38:10 +04:00
commit 7919850a6d
103 changed files with 15718 additions and 0 deletions

39
zakwire/Makefile Normal file
View File

@@ -0,0 +1,39 @@
NAME=testproj
SDCC=sdcc
HEX2BIN=hex2bin
CCFLAGS=-DSTM8S003 -I../ -I/usr/share/sdcc/include -mstm8 --out-fmt-ihx
LDFLAGS= -lstm8 -mstm8 --out-fmt-ihx
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
INDEPENDENT_HEADERS=../stm8l.h Makefile
all: $(NAME).bin
$(SRC) : %.c : %.h $(INDEPENDENT_HEADERS)
@touch $@
@echo $@
%.h: ;
clean:
rm -f $(TRASH)
load: $(NAME).bin
stm8flash $(FLASHFLAGS) -wf $(NAME).bin
%.rel: %.c
$(SDCC) $(CCFLAGS) -c $<
$(NAME).ihx: $(OBJ)
$(SDCC) $(LDFLAGS) $(OBJ) -o $(NAME).ihx
$(NAME).bin: $(NAME).ihx
$(HEX2BIN) -p 00 $<
.PHONY: all

176
zakwire/interrupts.c Normal file
View File

@@ -0,0 +1,176 @@
/*
* interrupts.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 "interrupts.h"
#include "zacwire.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){
if((PD_IDR & GPIO_PIN3) == 0){ // next bit start
// TIM2_SR1 = 0;
// TIM2_CR1 = TIM_CR1_OPM | TIM_CR1_CEN;
PD_CR2 &= ~GPIO_PIN3;
ZW_catch_bit();
};
}
// 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
// store the current counter value and wait for next pulse
INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14){
/*
TIM2_CR1 = 0; // stop timer
if(TIM2_SR2){ // overcapture: noice etc.
TIM2_SR2 = 0;
TIM2_SR1 = 0;
ZW_off();
return;
}
ZW_catch_bit();
TIM2_SR1 = 0;
TIM2_CNTRH = 0;
TIM2_CNTRL = 0;
*/
}
/*
TIM2_CCMR2 = 1; // IC2 is mapped on TI2FP2
TIM2_CCMR1 = 2; // IC1 is mapped on TI2FP1
*/
#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_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){}

147
zakwire/interrupts.h Normal file
View File

@@ -0,0 +1,147 @@
/*
* 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 unsigned long 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__

258
zakwire/led.c Normal file
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
*/
/********** one variant **********/
/*
* One digit: TABLE:
* ***A*** 0 1 2 3 4 5 6 7 8 9 A B C D E F - h
* * * (A) PB4 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 1
* F B (F) PB5 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0
* * * (B) PC5 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 1 1
* ***G*** (G) PC6 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
* * * (C) PC7 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0
* E C (E) PD1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 0
* * * ** (D) PD2 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1
* ***D*** *DP* (DP)PD3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
* **
*/
/*
// PB, mask: 0x30, PB4: 0x10, PB5: 0x20
#define PB_BLANK 0x30
static U8 PB_bits[18] = {0,0x30,0x20,0x20,0x10,0,0,0x20,0,0,0,0x10,0,0x30,0,0,0x30,0x10};
// PC, mask: 0xe0, PC5: 0x20, PC6: 0x40, PC7: 0x80
#defin PC_BLANK 0xe0
static U8 PC_bits[18] = {0x40,0x40,0x80,0,0,0x20,0x20,0x40,0,0,0,0x20,0xe0,0,0xa0,0xa0,0xa0,0x20};
// PD, mask: 0x0e, PD1: 0x02, PD2: 0x04, PD3: 0x08
#define PD_BLANK 0x0e
static U8 PD_bits[18] = {0x08,0x0e,0x08,0x0a,0x0e,0x0a,0x08,0x0e,0x08,0x0a,0x0c,0x8,0x08,0x08,0x08,0x0c,0x0e,0x0c};
*/
/*
* Number of digit on indicator with common anode
* digis 0..3: PC3, PC4, PA3, PD4
*/
/********** current variant **********/
/*
* One digit: TABLE:
* ***A*** 0 1 2 3 4 5 6 7 8 9 A B C D E F - h
* * * (F) PA1 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0
* F B (B) PB4 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 1 1
* * * (A) PB5 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 1
* ***G*** (G) PC3 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
* * * (C) PC4 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0
* E C (DP)PC5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
* * * ** (D) PC6 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1
* ***D*** *DP* (E) PC7 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..3: PA3, PD6, PD4, PD1
*/
/************* arrays for ports *************/
// PA, mask: 0x02, PA1
static U8 PA_bits[18] = {0,2,2,2,0,0,0,2,0,0,0,0,0,2,0,0,2,0};
#define PA_BLANK 0x02
// PB, mask: 0x30, PB4:0x10=16, PB5:0x20=32
#define PB_BLANK 0x30
static U8 PB_bits[18] = {0,32,0,0,32,16,16,0,0,0,0,48,16,32,16,16,48,48};
// 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] = {40,232,48,160,224,160,32,232,32,160,96,32,56,32,48,112,240,96};
/**
* 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
PD_ODR &= ~(GPIO_PIN1|GPIO_PIN4|GPIO_PIN6); // turn off other letters
PA_ODR &= ~(PA_BLANK | GPIO_PIN3); // light up everything
PB_ODR &= ~PB_BLANK;
PC_ODR &= ~PC_BLANK;
if(L < 18){ // letter
PA_ODR |= PA_bits[L];
PB_ODR |= PB_bits[L];
PC_ODR |= PC_bits[L];
}else{ // space - turn all OFF
PA_ODR |= PA_BLANK;
PB_ODR |= PB_BLANK;
PC_ODR |= PC_BLANK;
}
if(ltr & 0x80){ // DP
PC_ODR &= ~GPIO_PIN5;
}
}
/**
* 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[4];
char ch, M = 0, i;
N_current = 0; // refresh current digit number
// empty buffer
for(i = 0; i < 4; i++)
display_buffer[i] = ' ';
if(!str) return;
i = 0;
for(;(ch = *str) && (i < 4); 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 = 3;
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 > 3) 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 > 3) 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 = 3;
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){
int rem;
char N = 3, sign = 0;
if(I < -999 || I > 9999){
set_display_buf("---E");
return;
}
set_display_buf(NULL); // empty buffer
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
}
/**
* 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 > 3) return;
display_buffer[i] |= 0x80;
}

50
zakwire/led.h Normal file
View File

@@ -0,0 +1,50 @@
/*
* 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);
void display_DP_at_pos(U8 i);
/**
* Initialize ports
* PA3, PB4|5, PC3|4|5|6|7, PD1|2|3|4
*
#define LED_init() do{ \
PA_DDR = 0x08; PB_DDR = 0x30; PC_DDR = 0xf8; PD_DDR = 0x1e; \
PA_CR1 = 0x08; PB_CR1 = 0x30; PC_CR1 = 0xf8; PD_CR1 = 0x1e; \
}while(0)
*/
// 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__

124
zakwire/main.c Normal file
View File

@@ -0,0 +1,124 @@
/*
* 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 "stm8l.h"
#include "interrupts.h"
#include "led.h"
#include "zacwire.h"
unsigned long Global_time = 0L; // global time in ms
int ADC_value = 0; // value of last ADC measurement
U8 LED_delay = 1; // one digit emitting time
int main() {
unsigned long T_LED = 0L; // time of last digit update
unsigned long T_time = 0L; // timer
U8 dp_err_pos = 0;
U8 curbit = 0;
// Configure clocking
CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz
// Configure pins
CFG_GCR |= 1; // disable SWIM
LED_init();
// configure PD3[TIM2_CH2] as input for zacwire
PD_CR1 |= GPIO_PIN3; // weak pullup
CCR |= 0x28; // make shure that we are on level 3
EXTI_CR1 = 0x80; // PDIS = 10 - falling edge
// PA2 is output for zacwire powering
PA_DDR |= GPIO_PIN2; // output
PA_CR1 |= GPIO_PIN2; // push-pull
// 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;
// Configure Timer2:
// capture/compare channel
// channel CC1 (0->1) stores low pulse length,
// channel CC2 (1->0) stores time period between two consequent zero-transitions
//TIM2_IER = TIM_IER_CC2IE;// |TIM_IER_UIE ;
//TIM2_CCER1 = 0x30; // CC2: enable, capture on 1->0; CC1: disable
// TIM2 frequency == 16MHz / 2^TIM2_PSCR
// main frequency: 1MHz
// TIM2_PSCR = 15;
/*
TIM2_PSCR = 1; // 8MHz
TIM2_CCMR2 = 1;
TIM2_CCER1 = 0x10; // CC2: enable, capture on 0->1
TIM2_IER = TIM_IER_CC2IE;
*/
/*
// configure ADC
// select PD2[AIN3] & enable interrupt for EOC
ADC_CSR = 0x23;
ADC_TDRL = 0x08; // disable Schmitt triger for AIN3
// 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;
ADC_CR1 = 0x73; // turn on ADC (this needs second write operation)
*/
// enable all interrupts
enableInterrupts();
set_display_buf("----"); // on init show ----
show_next_digit();
// Loop
do {
if(((unsigned int)(Global_time - T_time) > 1000) || (T_time > Global_time)){ // once per 3 seconds we start measurement
T_time = Global_time;
if(ZW_data_ready){ // measurement is ready - display results
ZW_data_ready = 0;
display_int(Temperature_value); // fill display buffer with current temperature
display_DP_at_pos(2); // we always show DP here
/*display_int((int)ZW_bits[curbit++]);
if(curbit == 20){
curbit = 0;
ZW_data_ready = 0;
}
display_DP_at_pos(dp_err_pos++); // turn off old DP
if(dp_err_pos > 3)
dp_err_pos = 0;*/
}//else
if(!temp_measurement){ // there's no active measurements
ZW_on();
}else{ // error: no sensor
ZW_off();
set_display_buf("E 0");
display_DP_at_pos(dp_err_pos++); // turn off old DP
if(dp_err_pos > 3)
dp_err_pos = 0;
}
}
if((U8)(Global_time - T_LED) > LED_delay){
T_LED = Global_time;
show_next_digit();
}
} while(1);
}

BIN
zakwire/testproj.bin Normal file

Binary file not shown.

149
zakwire/zacwire.c Normal file
View File

@@ -0,0 +1,149 @@
/*
* zacwire.c - functions for zacwire sensor processing
*
* 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 "zacwire.h"
#include "interrupts.h"
#include "led.h"
// error codes for zacwire bit converting
#define ERR_STROBE_TOO_SHORT 1
#define ERR_BAD_CRC 2
int Temperature_value = 0; // measurement result
U8 ZW_data_ready = 0; // flag - measurement is ready
U8 bit_cntr = 0; // current bit number (in ZW proto - to omit start/crc bits)
U8 temp_measurement = 0; // flag - temperature measurement in progress
U8 ZW_bits[20]; // values of pulse lengths
/**
* Turn on ZW & start measurement
*/
void ZW_on(){
U8 i;
//EXTI_CR1 = 0x80; // PDIS = 10 - falling edge
PD_CR2 |= GPIO_PIN3; // turn on interrupts
for(i = 0; i < 20; i++)
ZW_bits[i] = 0; // clear old values
bit_cntr = 0;
ZW_data_ready = 0;
temp_measurement = 1;
PA_ODR |= GPIO_PIN2; // power on zacwire sensor
}
/**
* Turn off Zacwire
*/
void ZW_off(){
PD_CR2 &= ~GPIO_PIN3;
TIM2_CR1 = 0;
temp_measurement = 0;
PA_ODR &= ~GPIO_PIN2; // turn off ZW
}
/**
* Convert 8 bytes from ptr into 8bit value
* @param ptr - pointer to 10 bit word (strobe + 8 data bits + CRC)
* @param val - 8bit value
* @return error code in case of error
*/
U8 get_byte(U8 *ptr, U8 *val){
U8 i, data = 0, crc = 0;
U8 Strobe = ptr[0];
if(Strobe < 5){ // strobe too short - error!
return ERR_STROBE_TOO_SHORT;
}
for(i = 1; i < 9; i++){ // get bit data
data <<= 1;
if(ptr[i] < Strobe){ // logic 1
data |= 1;
crc ^= 1;
}
}
// now check CRC:
if((ptr[9] < Strobe) ^ crc){ // error: bad parity
return ERR_BAD_CRC;
}
*val = data;
return 0;
}
U8 get_temperature(){
U8 H, L, err;
long temper;
err = get_byte(ZW_bits, &H); // MSB
if(!err)
err = get_byte(&ZW_bits[10], &L); // LSB
if(err){
if(err == ERR_BAD_CRC)
set_display_buf("EBAD");
else
set_display_buf("E DA");
return 0;
}
/*
for(i = 6; i < 19; i++){ // omit first 5 zeros and strobe bit
if(i == 9 || i == 10)
continue;
if(ZW_bits[i] < 10){ // logic 1
Zdata |= 1;
}
Zdata <<= 1;
}*/
// correct temperature is VAL/2047*70-10, but we have integer (float*10), so:
// T = (VAL*700)/2047 - 100, result have only 12 bits, so we can do this
temper = ((long)H<<8 | L) * 700L;
Temperature_value = (int)(temper / 2047L - 100L);
//Temperature_value = ((int)H)<<8 | L;
return 1;
}
/**
* Get bit value
* TIM2_CCR1H & TIM2_CCR1L is zero pulse length
* TIM2_CCR2H & TIM2_CCR2L is time (for previous counter run) since timer up till 1->0 transition
*/
void ZW_catch_bit(){
// U8 H = TIM2_CCR2H; // length of previous high level signal
// U8 L = TIM2_CCR2L;
/*if(H){ // error: pulse too long
ZW_off();
display_int(H << 8 | L);
//set_display_buf("E002");
return;
}
ZW_bits[bit_cntr++] = L;
*/
U8 i;
while((PD_IDR & GPIO_PIN3) == 0){
ZW_bits[bit_cntr]++;
for(i = 0; i < 4; i++)
nop();
}
if(++bit_cntr == 20){ // all bits done
ZW_off();
if(get_temperature())
ZW_data_ready = 1;
else
ZW_on(); // in case of error start measurement again
}else
PD_CR2 |= GPIO_PIN3;
}

34
zakwire/zacwire.h Normal file
View File

@@ -0,0 +1,34 @@
/*
* zacwire.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 __ZAKWIRE_H__
#define __ZAKWIRE_H__
#include "stm8l.h"
extern int Temperature_value; // T*10, in degrC
extern U8 ZW_data_ready;
extern U8 temp_measurement;
void ZW_on();
void ZW_off();
void ZW_catch_bit();
#endif // __ZAKWIRE_H__