STM8 DISCOVERY – 7segment serial link (1)

stm8 board
stm8 board posted by (C)takao21203

This is a STM8 Discovery board. I have added a PIC 16f884 and a small 7segment display. I want to program a serial link between the two controllers. Later, a display will be connected to the STM8 chip directly. However, I have some ideas to improve the 7segment decoding/driving, and it is easier to test them on the PIC microcontroller.
The 16f884 has 4K memory, a hardware serial port, and there is enough extra I/O left. Actually, the ISCP worked correctly on the first attempt yesterday (this is not always the case).

How to use a 7segment display together with a microcontroller.<

1. The I/O tristate registers must be set to output for all I/O bits connected to the display module. This can be done manually. Often a complete 8bit port is used so it is easy to compute. However, sometimes bits from different I/O ports are used. The computation becomes more difficult. And it is also not easy to change later.

Why not use the microcontroller to compute the values and setup the tristate registers?
All what’s needed is a table with the 7segment to I/O port bit assignments. This table is very easy to change, since it is human readable. The code was tested and works. The tristate registers are setup correctly.
Actually it could be done with only one loop. As for now it is not required to remove the redundant code.

unsigned char v_PORTA,v_PORTB,v_PORTC,v_PORTD,v_PORTE;
unsigned char v_TRISA,v_TRISB,v_TRISC,v_TRISD,v_TRISE;

unsigned char c_digits=7;
const unsigned char c_anodes=2;

const unsigned char led7seg_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,\

#define v_PORTA_idx 0
#define v_PORTB_idx 1
#define v_PORTC_idx 2
#define v_PORTD_idx 3
#define v_PORTE_idx 4

const unsigned char* portvars[]={&v_PORTA,&v_PORTB,&v_PORTC,&v_PORTD,&v_PORTE};

const unsigned char* trisvars[]={&TRISA,&TRISB,&TRISC,&TRISD,&TRISE};

const unsigned char led7seg_decode[]={v_PORTD_idx,6,\

const unsigned char led7seg_anodes[]={v_PORTD_idx,5,\

// OR – set to 1
const unsigned char led7seg_decode_reset[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
// AND – set to 0
//const unsigned char led7seg_decode_set[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
const unsigned char led7seg_decode_set2[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

// AND – set to 0
const unsigned char led7seg_anodes_reset[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfb,0xfe};
// OR – set to 1
const unsigned char led7seg_anodes_set[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

void configure_TRIS()
{unsigned char i,i2,v_port,v_portbit,v_trisdata;
unsigned char* v_trisvar;

for(i=0;i<=c_digits;i++) { i2=i<<1; v_port=led7seg_decode[i2]; v_trisvar=trisvars[v_port]; v_trisdata=*v_trisvar; v_portbit=led7seg_decode[i2+1]; v_trisdata&=led7seg_decode_set2[v_portbit]; *v_trisvar=v_trisdata; } for(i=0;i<=c_anodes;i++) { i2=i<<1; v_port=led7seg_anodes[i2]; v_trisvar=trisvars[v_port]; v_trisdata=*v_trisvar; v_portbit=led7seg_anodes[i2+1]; v_trisdata&=led7seg_decode_set2[v_portbit]; *v_trisvar=v_trisdata; } } [/prettify]

Leave a Reply

Your email address will not be published. Required fields are marked *