Count from 0 to 8

This commit is contained in:
7u83 2024-05-04 02:30:54 +02:00
parent ad80ab8502
commit 299c0feb72
6 changed files with 257 additions and 15 deletions

View File

@ -12,7 +12,7 @@ FLASHFILE ?= main.ihx
SYSCLK ?= 12000
CFLAGS ?= -DWITH_ALT_LED9 -DWITHOUT_LEDTABLE_RELOC -DSHOW_TEMP_DATE_WEEKDAY
LIBSRC = ports.c adc.c
LIBSRC = ports.c adc.c setpin_lo.c setpin_hi.c setpin.c ss_display.c stc15_configure_pin.c
LIBOBJ =$(patsubst %.c,%.rel, $(LIBSRC))

156
main.c
View File

@ -10,13 +10,131 @@
#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 count=1;
static int seg = 0;
static int count=500;
if (count--)
return;
count = 1;
count = 500;
ILED = !ILED;
seg++;
if (seg>9)
seg=0;
ss_display(sevensegdef,seg);
return;
}
@ -33,22 +151,32 @@ void timer0_init() {
ET0 = 1; // Enable timer0 interrupt
/* AUXR &=0x7F; // T0 in 12T mode
TMOD = 0x00; // T0 16-bit-auto-reload
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;

View File

@ -1,6 +1,8 @@
#ifndef MCTOOLS_H_
#define MCTOOLS_H_
#include <stdint.h>
#include "stc15.h"
@ -15,4 +17,59 @@
#define stc15_timer2_set_sysclk1() AUXR |= 0x04;
#define stc15_timer2_set_clk_internal() AUXR &=0xF7; // T2 use internal clck
#define stc15_cfg_p2_bidrectional(pin) \
P2M0 &= (0xFF^(1<<pin)) \
P2M1 &= (0xFF^(1<<pin))
#define stc15_cfg_p2_push(pin) \
P2M0 |(1<<pin) \
P2M1 &= (0xFF^(1<<pin))
#define stc15_cfg_p2_input(pin) \
P2M0 &= (0xFF^(1<<pin)) \
P2M1 | (1<<pin)
#define stc15_cfg_p2_opendrain(pin) \
P2M0 | (1<<pin) \
P2M1 | (1<<pin)
#define stc15_cfg_p3_bidrectional(pin) \
P3M0 &= (0xFF^(1<<pin)) \
P3M1 &= (0xFF^(1<<pin))
#define stc15_cfg_p3_push(pin) \
P3M0 |(1<<pin) \
P3M1 &= (0xFF^(1<<pin))
#define stc15_cfg_p3_input(pin) \
P3M0 &= (0xFF^(1<<pin)); \
P3M1 | (1<<pin);
#define stc15_cfg_p3_opendrain(pin) \
P3M0 | (1<<pin); \
P3M1 | (1<<pin);
extern void setpin_lo(uint8_t port, uint8_t pin);
extern void setpin_hi(uint8_t port, uint8_t pin);
extern void setpin(uint8_t port, uint8_t pin, uint8_t val);
extern const uint8_t sevensegdef[][7];
void ss_display(const uint8_t d[][7],uint8_t n);
#define PORTMODE_BI 0x00
#define PORTMODE_PUSH 0x01
#define PORTMODE_INPUT 0x02
#define PORTMODE_OPENDRAIN 0x03
#define setbit(addr,n,val) \
if (val) \
addr |= (1<<n); \
else \
addr &= (1<<n) ^0xFF;
void stc15_configure_pin(uint8_t port, uint8_t pin, uint8_t mode);
#endif

21
setpin_hi.c Normal file
View File

@ -0,0 +1,21 @@
#include "mctools.h"
void setpin_hi(uint8_t port, uint8_t pin)
{
switch(port){
case 0:
P0 |= (1<<pin);
break;
case 1:
P1 |= (1<<pin);
break;
case 2:
P2 |= (1<<pin);
break;
case 3:
P3 |= (1<<pin);
break;
}
}

21
setpin_lo.c Normal file
View File

@ -0,0 +1,21 @@
#include "mctools.h"
void setpin_lo(uint8_t port, uint8_t pin)
{
switch(port){
case 0:
P0 &= (1<<pin) ^ 0xFF;
break;
case 1:
P1 &= (1<<pin) ^ 0xFF;
break;
case 2:
P2 &= (1<<pin) ^ 0xFF;
break;
case 3:
P3 &= (1<<pin) ^ 0xFF;
break;
}
}

15
ss_display.c Normal file
View File

@ -0,0 +1,15 @@
#include "mctools.h"
void ss_display(const uint8_t d[][7],uint8_t n)
{
for (uint8_t i=0; i<7; i++){
uint8_t val = d[n][i];
uint8_t port = val >> 5;
uint8_t pin = (val>>1) & 0x0F;
if (val &1)
setpin_hi(port,pin);
else
setpin_lo(port,pin);
}
}