Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. #define F_CPU 16000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. //PORT DEFINES
  5. #define LED_Direction DDRC
  6. #define LED_PORT PORTC
  7. #define DIGIT_Direction DDRB
  8. #define DIGIT_PORT PORTB
  9. #define BUTTON_Direction DDRA
  10. #define BUTTON_Vcc PORTA
  11. #define BUTTON_PORT PINA
  12. #define STATE_NONE_PRESSED 0x00
  13. #define STATE_SOME_PRESSED 0xff
  14. int main(void){ // setting ports as inputs and output
  15. LED_Direction |= 0xff;
  16. LED_PORT = 0xff;
  17. DIGIT_Direction |= 0xff;
  18. DIGIT_PORT = 0xff;
  19. BUTTON_Direction |= 0x00;
  20. BUTTON_Vcc = 0xff;
  21. BUTTON_PORT = 0xff;
  22. char symbols[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
  23. char pin[]={0,0,0,0};
  24.  
  25. char buttons = 0x00;
  26. char button_state = STATE_NONE_PRESSED;
  27. char key[8];
  28. int tminSeg = 0;
  29. int minSeg = 1;
  30. int tsecSeg = 2;
  31. int secSeg = 3;
  32. int select = 0;
  33.  
  34. for(int i=0; i<8; i++){
  35. key[i] = ~(1<<i);
  36. }
  37.  
  38. void incrementSec(int secSeg){
  39. pin[secSeg]++;
  40. if(pin[secSeg] > 9){
  41. pin[secSeg-1]++;
  42. pin[secSeg] = 0;
  43. }
  44. }
  45.  
  46. void incrementTSec(int tsecSeg){
  47. pin[tsecSeg]++;
  48. if(pin[tsecSeg] > 6){
  49. pin[tsecSeg-1]++;
  50. pin[tsecSeg] = 0;
  51. }
  52. }
  53.  
  54. void incrementMin(int minSeg){
  55. pin[minSeg]++;
  56. if(pin[minSeg] > 9){
  57. pin[minSeg-1]++;
  58. pin[minSeg] = 0;
  59. }
  60. }
  61.  
  62. void incrementTMin(int tminSeg){
  63. pin[tminSeg]++;
  64. if(pin[tminSeg] > 6){
  65. pin[tminSeg] = 0;
  66. }
  67. }
  68.  
  69. void decrementSec(int secSeg){
  70. pin[secSeg]--;
  71. if(pin[secSeg] < 0){
  72. pin[secSeg] = 0;
  73. }
  74. }
  75.  
  76. void decrementTSec(int tsecSeg){
  77. pin[tsecSeg]--;
  78. if(pin[tsecSeg] < 0){
  79. pin[tsecSeg] = 0;
  80. }
  81. }
  82.  
  83. void decrementMin(int minSeg){
  84. pin[minSeg]--;
  85. if(pin[minSeg] < 0){
  86. pin[minSeg] = 0;
  87. }
  88. }
  89.  
  90. void decrementTMin(int tminSeg){
  91. pin[tminSeg]--;
  92. if(pin[tminSeg] < 0){
  93. pin[tminSeg] = symbols[pin[0]];
  94. }
  95. }
  96.  
  97. int settingTimer = 0; // =================== czy jest wlaczone nastawianie minutnika (zbindujemy pod jakis guzik albo wywalisz ifa)
  98. while(1) { //output clear
  99. for(int i=0; i<4; i++){
  100. LED_PORT = symbols[pin[i]];
  101. DIGIT_PORT = ~(0x01 << i); //shift 1bit to output on another digit
  102.  
  103. if(settingTimer){ // =================== mysle ze powinno mrygac jedno, nie wiem czy to ktore chcemy XD
  104. if(select){
  105. if(i == 0 || i == 1){
  106. _delay_ms(40); //odświeżanie
  107. } else {
  108. _delay_ms(4); //odświeżanie
  109. }
  110. }else{
  111. if(i == 2 || i == 3){
  112. _delay_ms(40); //odświeżanie
  113. }else {
  114. _delay_ms(4); //odświeżanie
  115. }
  116. }
  117. } else {
  118. _delay_ms(4); //odświeżanie
  119. }
  120. }
  121.  
  122. buttons = BUTTON_PORT;
  123. if(button_state == STATE_NONE_PRESSED){
  124. if(buttons != 0xff){
  125. button_state = STATE_SOME_PRESSED;
  126. if(settingTimer){
  127. if(buttons == key[2]){ //SELECT
  128. select++;
  129. if(select > 2){
  130. select = 0;
  131. }
  132. }
  133. if(buttons == key[3]){ //SELECT
  134. // nie wiem, start timer czy cos
  135. }
  136.  
  137. switch(select){
  138. case 0:
  139. if(buttons == key[1]){ //MORE MIN AND SEC
  140. incrementTSec(tminSeg);
  141. }
  142. if(buttons == key[0]){
  143. decrementTSec(tminSeg);
  144. }
  145. break;
  146. case 1:
  147. if(buttons == key[1]){ //MORE MIN AND SEC
  148. incrementTMin(tminSeg);
  149. }
  150. if(buttons == key[0]){
  151. decrementTMin(tminSeg);
  152. }
  153. break;
  154. }
  155. } else {
  156. if(buttons == key[0]){ // =================== jesli nacisniemy guzik nr 0 to zaczniemy ustawiac
  157. settingTimer = 1;
  158. }
  159. }
  160. }
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167. if(button_state == STATE_SOME_PRESSED){
  168. // Wait for buttons to stop being pressed
  169. if(buttons == 0xff){
  170. button_state = STATE_NONE_PRESSED;
  171. }
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement