This is an old revision of the document!


7-segmendise numberindikaatori kasutamine

Järgnevalt on toodud 7 segmendilise numbri indikaatori kasutamise näide. Selleks on vaja ATmega128 põhiplaadi külge lisada Studyboard. Näites kasutatakse indikaatorit läbi selleks loodud funktsioonide mida on võimalik kiirelt ka teistesse projektidesse kopeerida. Kasutusel on viikude operatsioonid (vaata lähemalt).

//
// Simple up/down counting single 7-segment digit example.
//
// Raivo Sell, Erki Leitaru, Mikk Leini
//
// 2009
//
 
// Include avrlibc
#include <avr/io.h>
#include <util/delay.h>
 
// Include common library
#include "pin.h"
 
// Configure pins
#define SEGMENT_DISPLAY_LATCH      PORTPIN(G, 2)
#define SEGMENT_DISPLAY_DATA_OUT   PORTPIN(C, 6)
#define SEGMENT_DISPLAY_CLOCK      PORTPIN(C, 7)
 
//
// 7 segment display initialization
//
void segment_display_init(void)
{
	// Set latch, data out and clock pins as output
	pin_setup_output(SEGMENT_DISPLAY_LATCH);
	pin_setup_output(SEGMENT_DISPLAY_DATA_OUT);
	pin_setup_output(SEGMENT_DISPLAY_CLOCK);	
}
 
//
// Digit writing to 7 segment display
//
void segment_display_write(unsigned char digit)
{
	unsigned char map;
 
	// Decimal to segment map
	switch (digit)
	{
		case 0 : map = 0b00111111; break; // Every bit corresponds to one segment
		case 1 : map = 0b00000110; break; // "1"
		case 2 : map = 0b01011011; break; // "2"
		case 3 : map = 0b01001111; break; // "3" and so on
		case 4 : map = 0b01100110; break; 
		case 5 : map = 0b01101101; break; 
		case 6 : map = 0b01111100; break; 
		case 7 : map = 0b00000111; break;
		case 8 : map = 0b01111111; break; 
		case 9 : map = 0b01100111; break;  
		default: map = 0b01111001;        // E like Error
	}	
 
	// Latch low
	pin_clear(SEGMENT_DISPLAY_LATCH);
 
	// Send every bit in the byte. MSB (most significant bit) first.
	for (signed char i = 7; i >= 0; i--)
	{
		// If bit is set, sets the data out pin, otherwise not		
		pin_set_to(SEGMENT_DISPLAY_DATA_OUT, IS_BIT_SET(map, i));
 
		// Clock high for certain period
		pin_set(SEGMENT_DISPLAY_CLOCK)
		_delay_us(1);
 
		// Clock low for certain period		
		pin_clear(SEGMENT_DISPLAY_CLOCK)
		_delay_us(1);
	}
 
	// Latch high 	
	pin_set(SEGMENT_DISPLAY_LATCH);
}
 
//
// Program entrance function
//
int main(void)
{ 
	unsigned char digit = 0; // Digit to display on segment
	signed char delta = 1;   // Digit modifying value
 
	//digit = int (void) { return 2; }; 
 
	// 7 segment display initialization
	segment_display_init();	 
 
 	// Endless loop
	while (1)
	{
		// Write digit
		segment_display_write(digit);
 
		// Increase digit value by delta (which can be negative also)
		digit += delta;
 
		// Swap counting direction on 0 and 9
		if (digit == 0) delta = +1;
		if (digit == 9) delta = -1;		
 
		// Wait 1000 ms
		_delay_ms(1000);
	}
}
et/examples/digi/7seg.1239644466.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0