Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lib.h
- // Boilerplate for AVR ATMega328p and KEL shield
- // Shield: http://www.elektronika.ftn.uns.ac.rs/images/MPE/Arduino%20KEL%20shield.pdf
- // Timers & Interrupts: http://www.elektronika.ftn.uns.ac.rs/images/Elektronika(E2)/Prekidi%20i%20tajmeri.pdf
- // BitWise: http://pastebin.com/4Y3vzZVx
- // 7-segment Simulator: http://www.uize.com/examples/seven-segment-display.html
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #define SCL_HI (PORTC |= (1<<5))
- #define SCL_LO (PORTC &= ~(1<<5))
- #define SDA (PINC & (1 << 4))
- #define SHLD_HI (PORTB |= (1<<5))
- #define SHLD_LO (PORTB &= ~(1<<5))
- // A F B E D . C G
- // 0 0 0 0 1 1 0 0 -> 0x0c -> A
- #define SEG7_0 0x05
- #define SEG7_1 0xdd
- #define SEG7_2 0x46
- #define SEG7_3 0x54
- #define SEG7_4 0x9c
- #define SEG7_5 0x34
- #define SEG7_6 0x24
- #define SEG7_7 0x5d
- #define SEG7_8 0x04
- #define SEG7_9 0x14
- #define SEG7_A 0x0c
- #define SEG7_B 0xa4
- #define SEG7_C 0x27
- #define SEG7_D 0xc4
- #define SEG7_J 0x55
- #define SEG7_E 0x26
- #define SEG7_I 0xdd
- #define SEG7_T 0xa6
- #define SEG7_K 0x8c
- #define SEG7_O 0x05
- #define SEG7_Z 0x46
- #define SEG7_S 0x34
- #define SEG7_F 0x2e
- #define SEG7_R 0xee
- #define SEG7_L 0xa7
- #define SEG7_V 0x85
- #define SEG7_N 0xec
- #define SEG7_P 0x0e
- #define SEG7_LINE 0xfe
- #define SEG7_NULL 0xff
- void initTimer();
- void initInterupt();
- void initLEDs();
- void init7seg();
- void initButtons();
- void initSwitches();
- unsigned char readButton(unsigned char name);
- void makeNumberWord(unsigned char *display_buffer, int num);
- unsigned char readSwitches();
- unsigned char readSwitch(int position);
- void print7seg(unsigned char character, unsigned char position);
- void print7segWord(unsigned char *array);
- void initTimer() {
- TCCR0A = 0x02; //tajmer 0: CTC mod
- TCCR0B = 0x03; //tajmer 0: fclk = fosc/64
- OCR0A = 249; //perioda tajmera 0: 250 Tclk (OCR0A + 1 = 250)
- TIMSK0 = 0x02; //dozvola prekida tajmera 0 usled dostizanja vrednosti registra OCR0A
- sei(); // Globalna dozvola prekida
- }
- void initInterrupt() {
- PCICR = (1 << PCIE1); //dozvola prekida usled promene stanja
- PCMSK1 = 0x05; //pina PCINT10, ili pina PCINT8
- sei(); // Globalna dozvola prekida
- }
- void initLEDs() {
- DDRD = 0xff; // PORTD izlaz, postavljanje LEDova
- DDRB |= 1 << 4; // PORTB4 izlaz, tranzistor koji ukljucuje sve LEDove
- PORTB &= ~(1 << 4); // 0 na PORTB4 znaci 'omoguci'
- }
- void init7seg() {
- DDRD = 0xff; // PORTD izlaz, postavljanje segmenta
- DDRB |= 0xf; // PORTB[3-0] izlaz, za ukljucivanje jednog od cetiri sedmosegmentog
- }
- void initButtons() {
- DDRC &= 0xf0; // PORTC[3-0] ulaz
- }
- // * Prilicno sumnjiv dio, provjeriti na hardveru
- void initSwitches() {
- DDRC |= 0x20; // PORTC5 izlaz, radi kao CLK za shift registar na ploci
- DDRC &= 0xef; // PORTC4 ulaz, preko njega se serijski ucitavaju stanja pinova
- DDRB |= 0x20; // PORTB5 izlaz, dozvola upisa u shift registar
- }
- unsigned char readButton(unsigned char name) {
- // 0 - Lijevo, 1 - Dole, 2 - Desno, 3 - Gore
- unsigned char position;
- switch (name) {
- case 'l':
- position = 0;
- break;
- case 'd':
- position = 1;
- break;
- case 'r':
- position = 2;
- break;
- case 'u':
- position = 3;
- break;
- }
- unsigned char buttons = PINC & 0x0f;
- if(!(buttons & (1 << position))) {
- return 1;
- }
- return 0;
- }
- void makeNumberWord(unsigned char *display_buffer, int num) {
- unsigned char numbers[] = { SEG7_0, SEG7_1, SEG7_2, SEG7_3, SEG7_4, SEG7_5,
- SEG7_6, SEG7_7, SEG7_8, SEG7_9 };
- unsigned char negative = 0;
- if (num < 0) {
- num *= -1;
- negative = 1;
- }
- display_buffer[3] = numbers[num % 10];
- display_buffer[2] = numbers[num / 10 % 10];
- display_buffer[1] = numbers[num / 100 % 10];
- display_buffer[0] = numbers[num / 1000 % 10];
- if (num < 10) {
- display_buffer[0] = 0xff;
- display_buffer[1] = 0xff;
- display_buffer[2] = 0xff;
- if (negative == 1) {
- display_buffer[2] = 0xfe;
- }
- } else if (num < 100) {
- display_buffer[0] = 0xff;
- display_buffer[1] = 0xff;
- if (negative == 1) {
- display_buffer[1] = 0xfe;
- }
- } else if (num < 1000) {
- display_buffer[0] = 0xff;
- if (negative == 1) {
- display_buffer[0] = 0xfe;
- }
- }
- }
- unsigned char readSwitches() {
- unsigned char i, tmp = 0, mask = 0x80;
- SHLD_HI;
- SHLD_LO;
- SHLD_HI;
- for (i=0; i<8; i++) {
- SCL_LO;
- SCL_HI;
- if (SDA) {
- tmp |= mask;
- }
- mask >>= 1;
- }
- return tmp;
- }
- unsigned char readSwitch(int position) {
- unsigned char switches = readSwitches();
- if (switches & (1 << position)) {
- return 1;
- }
- return 0;
- }
- void print7seg(unsigned char character, unsigned char position) {
- PORTB = ~(0x01 << (3 - position));
- PORTD = character;
- _delay_ms(2);
- }
- void print7segWord(unsigned char *array) {
- int i;
- for (i = 0; i < 4; i++) {
- print7seg(array[i], i);
- }
- }
- // main.c
- #include "lib.h"
- const unsigned char words_length[2] = { 15, 24 };
- const unsigned char words[2][30] = { { SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_NULL,
- SEG7_J, SEG7_E, SEG7_D, SEG7_I, SEG7_T, SEG7_E, SEG7_NULL, SEG7_K, SEG7_O,
- SEG7_D, SEG7_NULL, SEG7_D, SEG7_Z, SEG7_O, SEG7_A },
- { SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_A, SEG7_F, SEG7_R, SEG7_I, SEG7_C, SEG7_K, SEG7_A, SEG7_NULL, SEG7_S, SEG7_L, SEG7_J, SEG7_I, SEG7_V, SEG7_A, SEG7_NULL, SEG7_LINE, SEG7_NULL, SEG7_N, SEG7_E, SEG7_NULL, SEG7_P, SEG7_A, SEG7_D, SEG7_A }
- };
- unsigned char display[5] = { SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_0 };
- int timer = 0;
- int cursorStart = 0;
- int activeWord = 0;
- int direction = 1;
- ISR(PCINT1_vect) {
- // Interrupt
- }
- ISR(TIMER0_COMPA_vect) {
- timer++;
- if (timer == 500) {
- cursorStart += direction;
- if (direction == 1 && cursorStart == words_length[activeWord] + 1) {
- cursorStart = 0;
- }
- if (direction == -1 && cursorStart == 0) {
- cursorStart = words_length[activeWord];
- }
- for (int i = 0; i < 4; i++) {
- display[i] = words[activeWord][i + cursorStart];
- }
- timer = 0;
- }
- }
- int main() {
- init7seg();
- initTimer();
- initButtons();
- while (1) {
- print7segWord(display);
- if (readButton('u') == 1) {
- activeWord = 0;
- cursorStart = (direction == 1) ? 0 : words_length[activeWord] + 1;
- }
- if (readButton('d') == 1) {
- activeWord = 1;
- cursorStart = (direction == 1) ? 0 : words_length[activeWord] + 1;
- }
- if (readButton('l') == 1) {
- direction = 1;
- }
- if (readButton('r') == 1) {
- direction = -1;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement