Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------------------------ SegDisp.h ------------------------------------------------ */
- /* SegDisp by xerpi */
- #ifndef _SEGDISP_H_
- #define _SEGDISP_H_
- #include <avr/io.h>
- #ifndef byte
- typedef unsigned char byte;
- #endif
- static const byte segment_bits[11] =
- {
- /*
- PORTD - digital 0-7
- PORTB - digital 8-13
- PORTC - analog 0-5
- a
- ---
- f | g | b
- ---
- e | | c
- --- .
- d DOT
- 7
- ---
- 6 | 5 | 8
- ---
- 1 | | 3
- --- .
- 2 4
- */
- 0b11100111, // 0
- 0b10000100, // 1
- 0b11010011, // 2
- 0b11010110, // 3
- 0b10110100, // 4
- 0b01110110, // 5
- 0b01110111, // 6
- 0b11000100, // 7
- 0b11110111, // 8
- 0b11110100, // 9
- 0b00001000, // DOT
- };
- #define DOT 10
- typedef enum{
- COMMON_CATHODE = 1,
- COMMON_ANODE = 0
- }COMMON_PIN;
- class SegDisp
- {
- public:
- SegDisp(void);
- SegDisp(int _initial_pin, COMMON_PIN _common_pin);
- inline void initialPin(int _initial_pin);
- inline void commonPin(COMMON_PIN _common_pin);
- void showBinary(byte number);
- void show(int number, bool show_dot = false);
- inline void show(int _initial_pin, int number, COMMON_PIN _common_pin, bool show_dot = false);
- private:
- int initial_pin;
- COMMON_PIN common_pin;
- };
- #endif
- /* ------------------------------------------------ SegDisp.cpp ------------------------------------------------ */
- /* SegDisp by xerpi */
- #include "SegDisp.h"
- SegDisp::SegDisp(void)
- {
- SegDisp(0, COMMON_CATHODE);
- };
- SegDisp::SegDisp(int _initial_pin, COMMON_PIN _common_pin)
- {
- initialPin(_initial_pin);
- commonPin(_common_pin);
- showBinary(0);
- }
- void SegDisp::initialPin(int _initial_pin)
- {
- initial_pin = _initial_pin;
- if(initial_pin > 0)
- {
- DDRD |= 0xFF<<initial_pin;
- DDRB |= (0xFF<<initial_pin) ^ 0xFF;
- }
- else
- {
- DDRD = 0xFF;
- }
- }
- void SegDisp::commonPin(COMMON_PIN _common_pin)
- {
- common_pin = _common_pin;
- }
- void SegDisp::showBinary(byte number)
- {
- if(common_pin == COMMON_ANODE)
- {
- number ^= 0xFF;
- }
- if(initial_pin > 0)
- {
- PORTD = (number<<initial_pin) | (PORTD<<(8 - initial_pin));
- PORTB = (number>>(8 - initial_pin)) | ((PORTB>>initial_pin)<<initial_pin);
- }
- else
- {
- PORTD = number;
- }
- }
- void SegDisp::show(int number, bool show_dot)
- {
- if(number < 0 || number > 10)
- {
- showBinary(0x0);
- }
- else
- {
- showBinary(show_dot ? segment_bits[number] | 0b1000 : segment_bits[number]);
- }
- }
- void SegDisp::show(int _initial_pin, int number, COMMON_PIN _common_pin, bool show_dot)
- {
- initialPin(_initial_pin);
- commonPin(_common_pin);
- show(number, show_dot);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement