mctools/clkgen.c

334 lines
4.4 KiB
C
Raw Normal View History

2024-05-04 19:39:11 +02:00
#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);
}