Code:
// 4 Siebensegmentanzeigen an Mega8 mic 3.11.2011
// https://www.roboternetz.de/community/threads/55253-Zusätzlich-zu-der-M32-noch-ein-mega8
// PC0 - SegA a
// PC1 - SegB ------
// PC2 - SegC | |
// PC3 - SegD f | | b
// | g |
// PD3 - SegE ------
// PD7 - SegF | |
// PD6 - SegG e | | c
// PD5 - SegDF | |
// ------
// PB5 - Anz. 1 d
// PB4 - Anz. 2
// PD2 - Anz. 3
// PB0 - Anz. 4
#define seg_INIT DDRB|=0b00110001; DDRC|=0b00001111; DDRD|=0b11101100; \
PORTB|=0b00110001; PORTC|=0b00001111; PORTD|=0b11101100;
#define segA_PORT PORTC
#define segA_PIN PC0
#define segB_PORT PORTC
#define segB_PIN PC1
#define segC_PORT PORTC
#define segC_PIN PC2
#define segD_PORT PORTC
#define segD_PIN PC3
#define segE_PORT PORTD
#define segE_PIN PD3
#define segF_PORT PORTD
#define segF_PIN PD7
#define segG_PORT PORTD
#define segG_PIN PD6
#define segP_PORT PORTD
#define segP_PIN PD5
#define digit0_PORT PORTB
#define digit0_PIN PB5
#define digit1_PORT PORTB
#define digit1_PIN PB4
#define digit2_PORT PORTD
#define digit2_PIN PD2
#define digit3_PORT PORTB
#define digit3_PIN PB0
#include <avr/io.h>
// Segmentbelegung aus: http://www.mikrocontroller.net/articles/AVR-Tutorial:_7-Segment-Anzeige
uint8_t segmente[10] = {
0b11000000, // 0: a, b, c, d, e, f
0b11111001, // 1: b, c
0b10100100, // 2: a, b, d, e, g
0b10110000, // 3: a, b, c, d, g
0b10011001, // 4: b, c, f, g
0b10010010, // 5: a, c, d, f, g
0b10000010, // 6: a, c, d, e, f, g
0b11111000, // 7: a, b, c
0b10000000, // 8: a, b, c, d, e, f, g
0b10010000 // 9: a, b, c, d, f, g
};
uint16_t wert=1234;
uint8_t dezimalpunkt=4; // 0 bis 3 für Anzeige in Digit, alles andere für aus
void ausgabe(void);
int main(void)
{
seg_INIT;
while(1)
{
ausgabe();
}
return (0);
}
void ausgabe(void)
{
static uint8_t digit=0;
uint16_t temp, dez[5]={1,10,100,1000,10000};
temp = wert / dez[digit+1] * dez[digit+1]; // führende Stellen ausblenden
temp = (wert -temp)/dez[digit]; // Ziffer ermitteln
temp = segmente[temp]; // Segmentmuster ermitteln
if(dezimalpunkt == digit) temp &= ~(1<<7); // Dezimalpunkt an diesem Digit anzeigen?
digit0_PORT &= ~(1<<digit0_PIN); // alle Digits ausschalten
digit1_PORT &= ~(1<<digit1_PIN);
digit2_PORT &= ~(1<<digit2_PIN);
digit3_PORT &= ~(1<<digit3_PIN);
if(temp & (1<<0)) segA_PORT |= (1<<segA_PIN); else segA_PORT &= ~(1<<segA_PIN);
if(temp & (1<<1)) segB_PORT |= (1<<segB_PIN); else segB_PORT &= ~(1<<segB_PIN);
if(temp & (1<<2)) segC_PORT |= (1<<segC_PIN); else segC_PORT &= ~(1<<segC_PIN);
if(temp & (1<<3)) segD_PORT |= (1<<segD_PIN); else segD_PORT &= ~(1<<segD_PIN);
if(temp & (1<<4)) segE_PORT |= (1<<segE_PIN); else segE_PORT &= ~(1<<segE_PIN);
if(temp & (1<<5)) segF_PORT |= (1<<segF_PIN); else segF_PORT &= ~(1<<segF_PIN);
if(temp & (1<<6)) segG_PORT |= (1<<segG_PIN); else segG_PORT &= ~(1<<segG_PIN);
if(temp & (1<<7)) segP_PORT |= (1<<segP_PIN); else segP_PORT &= ~(1<<segP_PIN);
switch(digit)
{
case 0: digit0_PORT |= (1<<digit0_PIN); // Anzeigedigit auswählen
case 1: digit1_PORT |= (1<<digit1_PIN);
case 2: digit2_PORT |= (1<<digit2_PIN);
case 3: digit3_PORT |= (1<<digit3_PIN);
}
digit = (digit+1) & 3; // nächstes Segment auswählen (0-3)
}
(natürlich ungetestet)
Lesezeichen