Advertisement
lukicdarkoo

Boilerplate for AVR ATMega328p & KEL Shield

Dec 20th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.70 KB | None | 0 0
  1. // lib.h
  2. // Boilerplate for AVR ATMega328p and KEL shield
  3. // Shield: http://www.elektronika.ftn.uns.ac.rs/images/MPE/Arduino%20KEL%20shield.pdf
  4. // Timers & Interrupts: http://www.elektronika.ftn.uns.ac.rs/images/Elektronika(E2)/Prekidi%20i%20tajmeri.pdf
  5. // BitWise: http://pastebin.com/4Y3vzZVx
  6. // 7-segment Simulator: http://www.uize.com/examples/seven-segment-display.html
  7.  
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include <util/delay.h>
  11.  
  12. #define SCL_HI (PORTC |= (1<<5))
  13. #define SCL_LO (PORTC &= ~(1<<5))
  14. #define SDA (PINC & (1 << 4))
  15. #define SHLD_HI (PORTB |= (1<<5))
  16. #define SHLD_LO (PORTB &= ~(1<<5))
  17.  
  18. // A F B E D . C G
  19. // 0 0 0 0 1 1 0 0 -> 0x0c -> A
  20. #define SEG7_0 0x05
  21. #define SEG7_1 0xdd
  22. #define SEG7_2 0x46
  23. #define SEG7_3 0x54
  24. #define SEG7_4 0x9c
  25. #define SEG7_5 0x34
  26. #define SEG7_6 0x24
  27. #define SEG7_7 0x5d
  28. #define SEG7_8 0x04
  29. #define SEG7_9 0x14
  30. #define SEG7_A 0x0c
  31. #define SEG7_B 0xa4
  32. #define SEG7_C 0x27
  33. #define SEG7_D 0xc4
  34.  
  35. #define SEG7_J 0x55
  36. #define SEG7_E 0x26
  37. #define SEG7_I 0xdd
  38. #define SEG7_T 0xa6
  39. #define SEG7_K 0x8c
  40. #define SEG7_O 0x05
  41. #define SEG7_Z 0x46
  42. #define SEG7_S 0x34
  43. #define SEG7_F 0x2e
  44. #define SEG7_R 0xee
  45. #define SEG7_L 0xa7
  46. #define SEG7_V 0x85
  47. #define SEG7_N 0xec
  48. #define SEG7_P 0x0e
  49. #define SEG7_LINE 0xfe
  50.  
  51. #define SEG7_NULL 0xff
  52.  
  53. void initTimer();
  54. void initInterupt();
  55. void initLEDs();
  56. void init7seg();
  57. void initButtons();
  58. void initSwitches();
  59. unsigned char readButton(unsigned char name);
  60. void makeNumberWord(unsigned char *display_buffer, int num);
  61. unsigned char readSwitches();
  62. unsigned char readSwitch(int position);
  63. void print7seg(unsigned char character, unsigned char position);
  64. void print7segWord(unsigned char *array);
  65.  
  66. void initTimer() {
  67.     TCCR0A = 0x02; //tajmer 0: CTC mod
  68.     TCCR0B = 0x03; //tajmer 0: fclk = fosc/64
  69.     OCR0A = 249; //perioda tajmera 0: 250 Tclk (OCR0A + 1 = 250)
  70.     TIMSK0 = 0x02; //dozvola prekida tajmera 0 usled dostizanja vrednosti registra OCR0A
  71.     sei(); // Globalna dozvola prekida
  72. }
  73.  
  74. void initInterrupt() {
  75.     PCICR = (1 << PCIE1); //dozvola prekida usled promene stanja
  76.     PCMSK1 = 0x05; //pina PCINT10, ili pina PCINT8
  77.     sei(); // Globalna dozvola prekida
  78. }
  79.  
  80. void initLEDs() {
  81.     DDRD = 0xff; // PORTD izlaz, postavljanje LEDova
  82.     DDRB |= 1 << 4; // PORTB4 izlaz, tranzistor koji ukljucuje sve LEDove
  83.     PORTB &= ~(1 << 4); // 0 na PORTB4 znaci 'omoguci'
  84. }
  85.  
  86. void init7seg() {
  87.     DDRD = 0xff; // PORTD izlaz, postavljanje segmenta
  88.     DDRB |= 0xf; // PORTB[3-0] izlaz, za ukljucivanje jednog od cetiri sedmosegmentog
  89. }
  90.  
  91. void initButtons() {
  92.     DDRC &= 0xf0; // PORTC[3-0] ulaz
  93. }
  94.  
  95. // * Prilicno sumnjiv dio, provjeriti na hardveru
  96. void initSwitches() {
  97.     DDRC |= 0x20; // PORTC5 izlaz, radi kao CLK za shift registar na ploci
  98.     DDRC &= 0xef; // PORTC4 ulaz, preko njega se serijski ucitavaju stanja pinova
  99.     DDRB |= 0x20; // PORTB5 izlaz, dozvola upisa u shift registar
  100. }
  101.  
  102. unsigned char readButton(unsigned char name) {
  103.     // 0 - Lijevo, 1 - Dole, 2 - Desno, 3 - Gore
  104.     unsigned char position;
  105.     switch (name) {
  106.         case 'l':
  107.             position = 0;
  108.         break;
  109.         case 'd':
  110.             position = 1;
  111.         break;
  112.         case 'r':
  113.             position = 2;
  114.         break;
  115.         case 'u':
  116.             position = 3;
  117.         break;
  118.     }
  119.  
  120.     unsigned char buttons = PINC & 0x0f;
  121.     if(!(buttons & (1 << position))) {
  122.         return 1;
  123.     }
  124.     return 0;
  125. }
  126.  
  127. void makeNumberWord(unsigned char *display_buffer, int num) {
  128.     unsigned char numbers[] = { SEG7_0, SEG7_1, SEG7_2, SEG7_3, SEG7_4, SEG7_5,
  129.     SEG7_6, SEG7_7, SEG7_8, SEG7_9 };
  130.     unsigned char negative = 0;
  131.  
  132.     if (num < 0) {
  133.         num *= -1;
  134.         negative = 1;
  135.     }
  136.  
  137.     display_buffer[3] = numbers[num % 10];
  138.     display_buffer[2] = numbers[num / 10 % 10];
  139.     display_buffer[1] = numbers[num / 100 % 10];
  140.     display_buffer[0] = numbers[num / 1000 % 10];
  141.  
  142.     if (num < 10) {
  143.         display_buffer[0] = 0xff;
  144.         display_buffer[1] = 0xff;
  145.         display_buffer[2] = 0xff;
  146.         if (negative == 1) {
  147.             display_buffer[2] = 0xfe;
  148.         }
  149.     } else if (num < 100) {
  150.         display_buffer[0] = 0xff;
  151.         display_buffer[1] = 0xff;
  152.         if (negative == 1) {
  153.             display_buffer[1] = 0xfe;
  154.         }
  155.     } else if (num < 1000) {
  156.         display_buffer[0] = 0xff;
  157.         if (negative == 1) {
  158.             display_buffer[0] = 0xfe;
  159.         }
  160.     }
  161. }
  162.  
  163. unsigned char readSwitches() {
  164.     unsigned char i, tmp = 0, mask = 0x80;
  165.     SHLD_HI;
  166.     SHLD_LO;
  167.     SHLD_HI;
  168.     for (i=0; i<8; i++) {
  169.         SCL_LO;
  170.         SCL_HI;
  171.         if (SDA) {
  172.             tmp |= mask;
  173.         }
  174.         mask >>= 1;
  175.     }
  176.     return tmp;
  177. }
  178.  
  179. unsigned char readSwitch(int position) {
  180.     unsigned char switches = readSwitches();
  181.     if (switches & (1 << position)) {
  182.         return 1;
  183.     }
  184.     return 0;
  185. }
  186.  
  187.  
  188. void print7seg(unsigned char character, unsigned char position) {
  189.     PORTB = ~(0x01 << (3 - position));
  190.     PORTD = character;
  191.     _delay_ms(2);
  192. }
  193.  
  194. void print7segWord(unsigned char *array) {
  195.     int i;
  196.     for (i = 0; i < 4; i++) {
  197.         print7seg(array[i], i);
  198.     }
  199. }
  200.  
  201.  
  202. // main.c
  203. #include "lib.h"
  204.  
  205. const unsigned char words_length[2] = { 15, 24 };
  206. const unsigned char words[2][30] = { { SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_NULL,
  207. SEG7_J, SEG7_E, SEG7_D, SEG7_I, SEG7_T, SEG7_E, SEG7_NULL, SEG7_K, SEG7_O,
  208.         SEG7_D, SEG7_NULL, SEG7_D, SEG7_Z, SEG7_O, SEG7_A },
  209.  
  210.         { 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 }
  211. };
  212. unsigned char display[5] = { SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_NULL, SEG7_0 };
  213.  
  214. int timer = 0;
  215. int cursorStart = 0;
  216. int activeWord = 0;
  217. int direction = 1;
  218.  
  219. ISR(PCINT1_vect) {
  220.     // Interrupt
  221. }
  222.  
  223. ISR(TIMER0_COMPA_vect) {
  224.     timer++;
  225.     if (timer == 500) {
  226.         cursorStart += direction;
  227.  
  228.         if (direction == 1 && cursorStart == words_length[activeWord] + 1) {
  229.             cursorStart = 0;
  230.         }
  231.  
  232.         if (direction == -1 && cursorStart == 0) {
  233.             cursorStart = words_length[activeWord];
  234.         }
  235.  
  236.         for (int i = 0; i < 4; i++) {
  237.             display[i] = words[activeWord][i + cursorStart];
  238.         }
  239.  
  240.         timer = 0;
  241.     }
  242. }
  243.  
  244. int main() {
  245.     init7seg();
  246.     initTimer();
  247.     initButtons();
  248.  
  249.     while (1) {
  250.         print7segWord(display);
  251.  
  252.         if (readButton('u') == 1) {
  253.             activeWord = 0;
  254.             cursorStart = (direction == 1) ? 0 : words_length[activeWord] + 1;
  255.  
  256.         }
  257.         if (readButton('d') == 1) {
  258.             activeWord = 1;
  259.             cursorStart = (direction == 1) ? 0 : words_length[activeWord] + 1;
  260.  
  261.         }
  262.  
  263.         if (readButton('l') == 1) {
  264.             direction = 1;
  265.         }
  266.  
  267.         if (readButton('r') == 1) {
  268.             direction = -1;
  269.         }
  270.     }
  271.  
  272.     return 0;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement