본문 바로가기

컴퓨터/STM8

STC1000 WR-032 (STM8S003F3P6)modified firmware

https://youtu.be/ijB6I-NZCNY





[main.c]

#include "stm8s.h"

#include "stc1000.h"


#define S1ADDR      0x4000

#define S2ADDR      0x4004

#define CALADDR     0x4008

#define PWRADDR 0x4012

#define HYSTADDR    0x4016


#define CLKB 4


const int ad_lookup[] = { -1000, -486, -355, -270, -205, -151, -104, -61, -21, 16, 51, 85, 119, 152, 184, 217, 250, 284, 318, 354, 391, 431, 473, 519, 569, 624, 688, 763, 856, 977, 1154, 1482 };


int ctemp=888, s1temp, s2temp, calibrate, hystery, ipwr;

int btn=0;

unsigned int h1=0,h2=0;

volatile unsigned int clk = 0;


typedef enum { OFF, ON, SET1, SET1S, SET2, SET2S, HYS, HYSS, CAL, CALS, IP, IPS } MODE;


void display(int n10, int n1, int n01, int minus, int cool, int heat, int set, int degree, int c) {

int led;

GPIOB->ODR |= (GPIO_PIN_4|GPIO_PIN_5);

GPIOD->ODR |= (GPIO_PIN_4|GPIO_PIN_5);

GPIOD->ODR &= ~(GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1);

GPIOC->ODR &= ~(GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3);

switch(clk%4) {

case 0:

GPIOB->ODR &= ~GPIO_PIN_5;

led = n10;

led = n10;

break;

case 1:

GPIOB->ODR &= ~GPIO_PIN_4;

led = n1;

break;

case 2:

GPIOD->ODR &= ~GPIO_PIN_5;

led = n01;

break;

case 3:

GPIOD->ODR &= ~GPIO_PIN_4;

led = ((cool<<0) | (set<<1) | (heat<<2) | (minus<<3) | (degree<<4) | (c<<5) | 0xC0 );

break;

}

GPIOD->ODR |= ((led & 0b00000001) >> 0 << 3);

GPIOD->ODR |= ((led & 0b00000010) >> 1 << 2);

GPIOD->ODR |= ((led & 0b00000100) >> 2 << 1);

GPIOC->ODR |= ((led & 0b00001000) >> 3 << 7);

GPIOC->ODR |= ((led & 0b00010000) >> 4 << 6);

GPIOC->ODR |= ((led & 0b00100000) >> 5 << 5);

GPIOC->ODR |= ((led & 0b01000000) >> 6 << 4);

GPIOC->ODR |= ((led & 0b10000000) >> 7 << 3);

}


int read_ad(){

int temp=0;

unsigned char a, b;

GPIO_Init(GPIOD, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOC, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOB, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOD, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT);

ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_4, ADC1_PRESSEL_FCPU_D2, \

            ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL4,\

            DISABLE);

ADC1_Cmd(ENABLE);

ADC1_StartConversion();

while(ADC1_GetFlagStatus(ADC1_FLAG_EOC)==RESET);

ADC1_ClearFlag(ADC1_FLAG_EOC);

temp = ADC1_GetConversionValue();

a = temp & 0x1f;

b = (temp>>5) & 0x1f;

temp = ad_lookup[b]+a;

ADC1_Cmd(DISABLE);

GPIO_Init(GPIOC, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_SLOW);

GPIO_Init(GPIOD, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1, GPIO_MODE_OUT_PP_LOW_SLOW);

GPIO_Init(GPIOB, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);

GPIO_Init(GPIOD, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);

return temp;

}


int read_sw() {

int sw;

GPIO_Init(GPIOD, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOC, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3, GPIO_MODE_IN_PU_NO_IT); //pull up for sw

GPIO_Init(GPIOB, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOD, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT);

sw = GPIO_ReadInputPin(GPIOC,GPIO_PIN_3);

sw |= GPIO_ReadInputPin(GPIOC,GPIO_PIN_4);

sw |= GPIO_ReadInputPin(GPIOC,GPIO_PIN_5);

sw |= GPIO_ReadInputPin(GPIOC,GPIO_PIN_6);

GPIO_Init(GPIOC, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_SLOW);

GPIO_Init(GPIOD, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1, GPIO_MODE_OUT_PP_LOW_SLOW);

GPIO_Init(GPIOB, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);

GPIO_Init(GPIOD, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);

return sw;

}


int isBTN(int b, int k) {

if((b & k) == k) return (int)0;

else return (int)1;

}


int eeprom_read2(unsigned int addr) {

    return ((FLASH_ReadByte(addr+1)<<8) | FLASH_ReadByte(addr));

}


void eeprom_write2(unsigned int addr, int d) {

    FLASH_ProgramByte(addr, (unsigned short)(d & 0xff));

    FLASH_ProgramByte(addr+1, (unsigned short)(d>>8));

}


void main() {

    int button=120, pbutton=120;

    volatile unsigned int clk2=0, clkb=0, clkc=0;

MODE mode;

    SEG7 segment;


GPIO_DeInit(GPIOA);

GPIO_DeInit(GPIOB);

GPIO_DeInit(GPIOC);

GPIO_DeInit(GPIOD);

ADC1_DeInit();

FLASH_DeInit();

FLASH_Unlock(FLASH_MEMTYPE_DATA);

FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);


GPIO_Init(GPIOC, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_SLOW);

GPIO_Init(GPIOD, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1, GPIO_MODE_OUT_PP_LOW_SLOW);

GPIO_Init(GPIOB, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);

GPIO_Init(GPIOD, GPIO_PIN_4 | GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);

GPIO_Init(GPIOA, GPIO_PIN_1 | GPIO_PIN_2, GPIO_MODE_OUT_PP_LOW_SLOW);

GPIO_WriteLow(GPIOA,GPIO_PIN_1 | GPIO_PIN_2);

s1temp = eeprom_read2(S1ADDR);

s2temp = eeprom_read2(S2ADDR);

hystery = eeprom_read2(HYSTADDR);

ipwr = eeprom_read2(PWRADDR);

calibrate = eeprom_read2(CALADDR);

if(ipwr==1) mode=ON;

else mode=OFF;


    do {

if(clkc==0 && clkb==0 && clk==3) {

if(mode==ON) {  //mode ON일때 릴레이 출력

if(ctemp >= s1temp) { //off

h1=0; 

GPIO_WriteLow(GPIOA, GPIO_PIN_2);

}   

if(ctemp < s1temp-hystery ) { 

h1=1; 

GPIO_WriteHigh(GPIOA, GPIO_PIN_2);

if(ctemp >= s2temp) { 

h2=0; 

GPIO_WriteLow(GPIOA, GPIO_PIN_1);

}  

if(ctemp < s2temp-hystery) {

h2=1; 

GPIO_WriteHigh(GPIOA, GPIO_PIN_1);

if(ctemp < -990) {

h1=h2=0;

GPIO_WriteLow(GPIOA, GPIO_PIN_1);

GPIO_WriteLow(GPIOA, GPIO_PIN_2);

}

}

else {

h1=h2=0;

GPIO_WriteLow(GPIOA, GPIO_PIN_1);

GPIO_WriteLow(GPIOA, GPIO_PIN_2);

}

}


btn=120;

if((button != pbutton) && (pbutton==120)) {

btn = button;

clk2=0;

}

        

//계속누르고 있으면 계속 키 입력

        if(button==120) clk2=0;

        else {

            if(clk==0) clk2++;

            if((clk2>15) && (isBTN(button,BTN_U) || isBTN(button,BTN_D)) && (clk==0)) btn=button; //계속누르고 있는 시간

        }

            

        if(mode==ON) {

            if(isBTN(btn,BTN_P)) mode=OFF;

            if(isBTN(btn,BTN_S)) mode=SET1;

            segment = value_to_digit(ctemp);

if(ctemp < -990 || ctemp>990) display(LED_E, LED_r, LED_r, segment.negative,h1,h2,0,0,0);

else  display(segment.digit10, LED_dot | segment.digit1, segment.digit01, segment.negative,h1,h2,0,1,1);

        }

        else if(mode==OFF) {

            if(isBTN(btn,BTN_P)) mode=ON;

            if(isBTN(btn,BTN_S)) mode=HYS;

            display(LED_OFF, LED_dot, LED_OFF, 0,0,0,0,0,0);

        }

        else if(mode==SET1) {

            if(isBTN(btn,BTN_P))  mode=OFF;

            if(isBTN(btn,BTN_S)) mode=SET1S;

            if(isBTN(btn,BTN_U)) if(++s1temp>TEMP_MAX) s1temp=TEMP_MAX;

            if(isBTN(btn,BTN_D)) if(--s1temp<TEMP_MIN) s1temp=TEMP_MIN;

            segment = value_to_digit(s1temp);

            if(clkb<CLKB || button<120) //깜빡임 속도

                display(segment.digit10, LED_dot | segment.digit1, segment.digit01, segment.negative,0,0,1,0,0);

            else

                display(LED_OFF, LED_OFF, LED_OFF, 0,0,0,1,0,0);

        }

        else if(mode==SET2) {

            if(isBTN(btn,BTN_P)) mode=OFF;

            if(isBTN(btn,BTN_S))  mode=SET2S;

            if(isBTN(btn,BTN_U)) if(++s2temp>TEMP_MAX) s2temp=TEMP_MAX;

            if(isBTN(btn,BTN_D)) if(--s2temp<TEMP_MIN) s2temp=TEMP_MIN;

            segment = value_to_digit(s2temp);

            if(clkb<CLKB || button<120)

                display(segment.digit10, LED_dot | segment.digit1, segment.digit01, segment.negative,0,0,0,1,0);

            else

                display(LED_OFF, LED_OFF, LED_OFF, 0,0,0,0,1,0);

        }

        else if(mode==HYS) {

            if(isBTN(btn,BTN_P)) mode=OFF;

            if(isBTN(btn,BTN_S)) mode=HYSS;

            if(isBTN(btn,BTN_U)) if(++hystery>H_MAX) hystery=H_MAX;

            if(isBTN(btn,BTN_D)) if(--hystery<H_MIN) hystery=H_MIN;

            segment = value_to_digit(hystery);

            if(clkb<CLKB || button<120)

                display(LED_H | LED_dot, LED_dot | segment.digit1, segment.digit01, segment.negative,0,0,0,0,0);

            else

                display(LED_H | LED_dot, LED_OFF, LED_OFF, 0,0,0,0,0,0);

        }

        else if(mode==IP) {

            if(isBTN(btn,BTN_P)) mode=OFF;

            if(isBTN(btn,BTN_S)) mode=IPS;

            if(isBTN(btn,BTN_U)) if(++ipwr>1) ipwr=1;

            if(isBTN(btn,BTN_D)) if(--ipwr<0) ipwr=0;

            segment = value_to_digit(ipwr);

            if(clkb<CLKB || button<120)

                display(LED_P | LED_dot, LED_OFF, segment.digit01, 0,0,0,0,0,0);

            else

                display(LED_P | LED_dot, LED_OFF, LED_OFF, 0,0,0,0,0,0);

        }

        else if(mode==CAL) {

            if(isBTN(btn,BTN_P)) mode=OFF;

            if(isBTN(btn,BTN_S)) mode=CALS;

            if(isBTN(btn,BTN_U)) if(++calibrate>CAL_MAX) calibrate=CAL_MAX;

            if(isBTN(btn,BTN_D)) if(--calibrate<CAL_MIN) calibrate=CAL_MIN;

            segment = value_to_digit(calibrate);

            if(clkb<CLKB || button<120)

                display(LED_C | LED_dot, LED_dot | segment.digit1, segment.digit01, segment.negative,0,0,0,0,0);

            else

                display(LED_C | LED_dot, LED_OFF, LED_OFF, 0,0,0,0,0,0);

        }

        else if(mode==SET1S) {

            eeprom_write2(S1ADDR, s1temp);

            mode = SET2;

        }

        else if(mode==SET2S) {

            eeprom_write2(S2ADDR, s2temp);

            mode = ON;

        }

        else if(mode==HYSS) {

            eeprom_write2(HYSTADDR, hystery);

            mode = IP;

        }

        else if(mode==IPS) {

            eeprom_write2(PWRADDR, ipwr);

            mode = CAL;

        }

        else if(mode==CALS) {

            eeprom_write2(CALADDR, calibrate);

            mode = ON;

        }

        pbutton = button;


        //Delay_ms(500);    //delay

        clk++;

        if(clk==240) {

            clk=0;

button = read_sw();

            clkb++;

            if(clkb==(CLKB*2)) {

                clkb=0;

                clkc++;

                if(clkc==1) clkc=0;  //출력릴레이 작동주기

//segment = value_to_digit(button);

//display(segment.digit10, segment.digit1, segment.digit01, segment.negative,h1,h2,0,1,1);

ctemp = read_ad()+calibrate;

            }

        }

    } while(1);   // Endless loop

}


#ifdef USE_FULL_ASSERT


/**

  * @brief  Reports the name of the source file and the source line number

  *   where the assert_param error has occurred.

  * @param file: pointer to the source file name

  * @param line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,

     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */

  while (1)

  {

  }

}

#endif






















[stc1000.c]

#include "STC1000.h"


unsigned const char led_lookup[] = { LED_0, LED_1, LED_2, LED_3, LED_4, LED_5, LED_6, LED_7, LED_8, LED_9 };


SEG7 value_to_digit(int value) {

    SEG7 seg;

    int i;

    if(value<0) {

        seg.negative = 1;

        value = -value;

    } else {

        seg.negative = 0;

    }

    

    if(value>=1000) value=999;

    

    if(value>=100) {

        for(i=0; value >= 100; i++) value -= 100;

        seg.digit10 = led_lookup[(unsigned int)i];

    } else {

        seg.digit10 = LED_OFF;

    }

    

    if(value>=10) {

        for(i=0; value >= 10; i++) value -= 10;

        seg.digit1 = led_lookup[(unsigned int)i];

    } else {

        seg.digit1 = LED_0;

    }

    

    seg.digit01 = led_lookup[(unsigned int)value];

    return seg;

}


[stc1000.h]


#ifndef __STC1000_H__

#define __STC1000_H__

#define TEMP_MAX      (800)

#define TEMP_MIN     (-400)

#define CAL_MAX     (20)

#define CAL_MIN     (-20)

#define H_MAX   (99)

#define H_MIN   (0)


#define LED_OFF     0x00

#define LED_dot     0x80

#define LED_0       0x3f

#define LED_1       0x06

#define LED_2       0x5b

#define LED_3       0x4f

#define LED_4       0x66

#define LED_5       0x6d

#define LED_6       0x7d

#define LED_7       0x27

#define LED_8       0x7f

#define LED_9       0x6f


#define LED_H 0x76

#define LED_C 0x39

#define LED_P 0x73

#define LED_E 0x79

#define LED_r 0x50


#define BTN_P  GPIO_PIN_4

#define BTN_S  GPIO_PIN_3

#define BTN_U  GPIO_PIN_6

#define BTN_D  GPIO_PIN_5




typedef struct {

    unsigned negative :1;

    unsigned int digit10;

    unsigned int digit1;

    unsigned int digit01;

} SEG7;


SEG7 value_to_digit(int);

#endif // __STC1000_H__

'컴퓨터 > STM8' 카테고리의 다른 글

STM8 timer interrupt using TIM4  (0) 2016.08.12