From 284802b283cda912db776d8c6b8a3d6de19bf4b8 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Sat, 4 May 2024 19:39:11 +0200 Subject: [PATCH] Moved main to clkgen --- Makefile | 2 +- clkgen.c | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 185 ------------------------------- 3 files changed, 334 insertions(+), 186 deletions(-) create mode 100644 clkgen.c delete mode 100644 main.c diff --git a/Makefile b/Makefile index 2fd7769..645cad4 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ LIBSRC = ports.c adc.c setpin_lo.c setpin_hi.c setpin.c ss_display.c stc15_confi LIBOBJ =$(patsubst %.c,%.rel, $(LIBSRC)) LIBNAME=mctools.lib -PRGSRC = main.c +PRGSRC = clkgen.c PRGOBJ =$(patsubst %.c,%.rel, $(PRGSRC)) diff --git a/clkgen.c b/clkgen.c new file mode 100644 index 0000000..280e82f --- /dev/null +++ b/clkgen.c @@ -0,0 +1,333 @@ + +#include "mctools.h" + +#define FOSC 12000000L //system frequency +#define BAUD 9600L //baud-rate + + +#define DLED P2_6 +#define ILED P2_7 +#define T0_1MS (65536-FOSC/12/10000) + + +#define DISP_A P3_5 +#define DISP_B P3_6 + + +const uint8_t sevensegdef[][7] = { + // 0 + { + (0x35 << 1) | 0, + (0x36 << 1) | 0, + (0x37 << 1) | 0, + (0x20 << 1) | 0, + (0x21 << 1) | 0, + (0x22 << 1) | 0, + (0x23 << 1) | 1, + }, + // 1 + { + (0x35 << 1) | 1, + (0x36 << 1) | 0, + (0x37 << 1) | 0, + (0x20 << 1) | 1, + (0x21 << 1) | 1, + (0x22 << 1) | 1, + (0x23 << 1) | 1, + }, + // 2 + { + (0x35 << 1) | 0, + (0x36 << 1) | 0, + (0x37 << 1) | 1, + (0x20 << 1) | 0, + (0x21 << 1) | 0, + (0x22 << 1) | 1, + (0x23 << 1) | 0, + }, + // 3 + { + (0x35 << 1) | 0, + (0x36 << 1) | 0, + (0x37 << 1) | 0, + (0x20 << 1) | 0, + (0x21 << 1) | 1, + (0x22 << 1) | 1, + (0x23 << 1) | 0, + }, + // 4 + { + (0x35 << 1) | 1, + (0x36 << 1) | 0, + (0x37 << 1) | 0, + (0x20 << 1) | 1, + (0x21 << 1) | 1, + (0x22 << 1) | 0, + (0x23 << 1) | 0, + }, + // 5 + { + (0x35 << 1) | 0, + (0x36 << 1) | 1, + (0x37 << 1) | 0, + (0x20 << 1) | 0, + (0x21 << 1) | 1, + (0x22 << 1) | 0, + (0x23 << 1) | 0, + + }, + // 6 + { + (0x35 << 1) | 0, + (0x36 << 1) | 1, + (0x37 << 1) | 0, + (0x20 << 1) | 0, + (0x21 << 1) | 0, + (0x22 << 1) | 0, + (0x23 << 1) | 0, + }, + // 7 + { + (0x35 << 1) | 0, + (0x36 << 1) | 0, + (0x37 << 1) | 0, + (0x20 << 1) | 1, + (0x21 << 1) | 1, + (0x22 << 1) | 1, + (0x23 << 1) | 1, + }, + //8 + { + (0x35 << 1) | 0, + (0x36 << 1) | 0, + (0x37 << 1) | 0, + (0x20 << 1) | 0, + (0x21 << 1) | 0, + (0x22 << 1) | 0, + (0x23 << 1) | 0, + }, + { + (0x35 << 1) | 0, + (0x36 << 1) | 0, + (0x37 << 1) | 0, + (0x20 << 1) | 0, + (0x21 << 1) | 1, + (0x22 << 1) | 0, + (0x23 << 1) | 0, + } + +}; + +static uint16_t pulse=0; +#define PULSE_LEN 250; + +static uint8_t single_step=1; +static uint8_t single_step_count=0; +static uint8_t speed=40; +static uint8_t disp_nr=25; + +#define MYSW1 P2_7 +#define MYSW2 P1_0 +#define MYSW3 P1_1 + +static void handle_switches() +{ + static uint8_t lastsw1=1; + static uint8_t lastsw2=1; + static uint8_t lastsw3=1; + + if (lastsw3 != MYSW3){ + lastsw3=MYSW3; + if(!lastsw3){ + single_step=1; + if(pulse==0){ + pulse=PULSE_LEN; + } + } + } + + if (lastsw1 != MYSW1){ + lastsw1=MYSW1; + if (!lastsw1){ + if (single_step){ + single_step=0; + return; + } + + disp_nr--; + } + } + + if (lastsw2 != MYSW2){ + lastsw2=MYSW2; + if (!lastsw2) { + if (single_step){ + single_step=0; + return; + } + disp_nr++; + } + } + + +} + +static void update_display() +{ + uint8_t dig; + + static uint8_t pos=0; + switch (pos){ + case 0: + P2_5=0; + P2_4=0; + dig=disp_nr/10; + ss_display(sevensegdef,dig); + P2_5=1; + P2_4=0; + break; + case 1: + P2_5=0; + P2_4=0; + dig=disp_nr%10; + ss_display(sevensegdef,dig); + P2_5=0; + P2_4=1; + break; + } + pos++; + if (pos>1) + pos=0; + +} + +#define COU0 11 +#define COU1 1000 + +static void cou0() +{ + static int count=COU0; + if (count--) + return; + count=COU0; + + update_display(); + handle_switches(); + +} + + +static void pulser() +{ + if(pulse>0){ + P2_6 = 1; + pulse--; + return; + } + + P2_6=0; + +} + + +static void cou1() +{ + static int ctr = 99; + + static int count=COU1; + + + + if (count--) + return; + count=COU1; + + if (single_step) + return; + + + + ctr-=disp_nr; + if (ctr <=0){ + ctr+=99; + pulse=PULSE_LEN; + } + + +// if (disp_nr>99) +// disp_nr=0; + +} + + + +static void timer0_isr() __interrupt(1) __using (1) +{ + EA=0; + cou0(); + cou1(); + pulser(); + EA=1; + return; +} + + +void timer0_init() { + + stc15_timer0_set_sysclk12(); + stc15_timer0_set_mode_16bitauto(); + + + TL0 = T0_1MS; // Initial timer value + TH0 = T0_1MS>>8; // Initial timer value + TF0 = 0; // Clear overflow flag + TR0 = 1; // Timer0 start run + ET0 = 1; // Enable timer0 interrupt + + +} + + +void main() +{ + uint8_t p = P3; + +/* stc15_cfg_p3_opendrain(5); + stc15_cfg_p3_opendrain(6); + stc15_cfg_p3_opendrain(7); +*/ + +// stc15_configure_pin + +// setpin_lo(3,5); +// setpin_hi(3,5); + + //ss_display(sevensegdef,0); + + stc15_configure_pin(3,5,PORTMODE_PUSH); + stc15_configure_pin(3,6,PORTMODE_PUSH); + stc15_configure_pin(3,7,PORTMODE_PUSH); + stc15_configure_pin(2,0,PORTMODE_PUSH); + stc15_configure_pin(2,1,PORTMODE_PUSH); + stc15_configure_pin(2,2,PORTMODE_PUSH); + stc15_configure_pin(2,3,PORTMODE_PUSH); + + stc15_configure_pin(2,4,PORTMODE_PUSH); + stc15_configure_pin(2,5,PORTMODE_PUSH); + + stc15_configure_pin(2,6,PORTMODE_PUSH); + stc15_configure_pin(2,7,PORTMODE_INPUT); + stc15_configure_pin(1,0,PORTMODE_INPUT); + stc15_configure_pin(1,1,PORTMODE_INPUT); + + MYSW3=1; + + P2_4=1; + P2_5=1; + P2_6=0; + + timer0_init(); + EA=1; + while(1); +} + diff --git a/main.c b/main.c deleted file mode 100644 index d9cda25..0000000 --- a/main.c +++ /dev/null @@ -1,185 +0,0 @@ - -#include "mctools.h" - -#define FOSC 12000000L //system frequency -#define BAUD 9600L //baud-rate - - -#define DLED P2_6 -#define ILED P2_7 -#define T0_1MS (65536-FOSC/12/1000) - - -#define DISP_A P3_5 -#define DISP_B P3_6 - - -const uint8_t sevensegdef[][7] = { - // 0 - { - (0x35 << 1) | 0, - (0x36 << 1) | 0, - (0x37 << 1) | 0, - (0x20 << 1) | 0, - (0x21 << 1) | 0, - (0x22 << 1) | 0, - (0x23 << 1) | 1, - }, - // 1 - { - (0x35 << 1) | 1, - (0x36 << 1) | 0, - (0x37 << 1) | 0, - (0x20 << 1) | 1, - (0x21 << 1) | 1, - (0x22 << 1) | 1, - (0x23 << 1) | 1, - }, - // 2 - { - (0x35 << 1) | 0, - (0x36 << 1) | 0, - (0x37 << 1) | 1, - (0x20 << 1) | 0, - (0x21 << 1) | 0, - (0x22 << 1) | 1, - (0x23 << 1) | 0, - }, - // 3 - { - (0x35 << 1) | 0, - (0x36 << 1) | 0, - (0x37 << 1) | 0, - (0x20 << 1) | 0, - (0x21 << 1) | 1, - (0x22 << 1) | 1, - (0x23 << 1) | 0, - }, - // 4 - { - (0x35 << 1) | 1, - (0x36 << 1) | 0, - (0x37 << 1) | 0, - (0x20 << 1) | 1, - (0x21 << 1) | 1, - (0x22 << 1) | 0, - (0x23 << 1) | 0, - }, - // 5 - { - (0x35 << 1) | 0, - (0x36 << 1) | 1, - (0x37 << 1) | 0, - (0x20 << 1) | 0, - (0x21 << 1) | 1, - (0x22 << 1) | 0, - (0x23 << 1) | 0, - - }, - // 6 - { - (0x35 << 1) | 0, - (0x36 << 1) | 1, - (0x37 << 1) | 0, - (0x20 << 1) | 0, - (0x21 << 1) | 0, - (0x22 << 1) | 0, - (0x23 << 1) | 0, - }, - // 7 - { - (0x35 << 1) | 0, - (0x36 << 1) | 0, - (0x37 << 1) | 0, - (0x20 << 1) | 1, - (0x21 << 1) | 1, - (0x22 << 1) | 1, - (0x23 << 1) | 1, - }, - //8 - { - (0x35 << 1) | 0, - (0x36 << 1) | 0, - (0x37 << 1) | 0, - (0x20 << 1) | 0, - (0x21 << 1) | 0, - (0x22 << 1) | 0, - (0x23 << 1) | 0, - }, - { - (0x35 << 1) | 0, - (0x36 << 1) | 0, - (0x37 << 1) | 0, - (0x20 << 1) | 0, - (0x21 << 1) | 1, - (0x22 << 1) | 0, - (0x23 << 1) | 0, - } - -}; - - - -static void timer0_isr() __interrupt(1) -{ - static int seg = 0; - static int count=500; - if (count--) - return; - count = 500; - ILED = !ILED; - - seg++; - if (seg>9) - seg=0; - - ss_display(sevensegdef,seg); - - return; -} - -void timer0_init() { - - stc15_timer0_set_sysclk12(); - stc15_timer0_set_mode_16bitauto(); - - - TL0 = T0_1MS; // Initial timer value - TH0 = T0_1MS>>8; // Initial timer value - TF0 = 0; // Clear overflow flag - TR0 = 1; // Timer0 start run - ET0 = 1; // Enable timer0 interrupt - - -} - -void main() -{ - uint8_t p = P3; - -/* stc15_cfg_p3_opendrain(5); - stc15_cfg_p3_opendrain(6); - stc15_cfg_p3_opendrain(7); -*/ - -// stc15_configure_pin - -// setpin_lo(3,5); -// setpin_hi(3,5); - - ss_display(sevensegdef,0); - - - stc15_cfg_p3_opendrain(6); - -// DISP_A = 1; -// DISP_B = 0; -// P3_5 = 0; -// P3 &= (1<<5; // = 0; - - ILED=0; - timer0_init(); - EA=1; - while(1); -} -