mikroavr

freq_atmega32_keypad

Jun 18th, 2021
928
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Keypad.h>
  2.  
  3. const byte ROWS = 4;
  4. const byte COLS = 3;
  5.  
  6. char hexaKeys[ROWS][COLS] = {
  7.   {1, 2, 3},
  8.   {4, 5, 6},
  9.   {7, 8, 9},
  10.   {'*', '0', '#'}
  11. };
  12.  
  13. byte rowPins[ROWS] = {7, 6, 5, 4};
  14. byte colPins[COLS] = {3, 2, 1};
  15.  
  16. Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  17. #define BINTANG 42 // nilai BINTANG
  18. #define PAGAR 35 // nilai pagar
  19. long dtKey = 0;
  20. int key;
  21. String line1 = "";
  22. String line2 = "";
  23.  
  24.  
  25. #include <LiquidCrystal_I2C.h>
  26. LiquidCrystal_I2C lcd(0x27, 16, 2);
  27.  
  28. #define rel_pompa 2
  29. #define rel_peltier 4
  30. #define rel_heater 5
  31.  
  32. int state = 0;
  33.  
  34. #define STOP 0
  35. #define RUN 1
  36.  
  37. float set_freq = 0;
  38. float TOP = 0;
  39. const unsigned long crystal = 16000000L;
  40.  
  41. #define led 0
  42.  
  43. unsigned long cur_time_led, old_time_led;
  44. bool stateLed = false;
  45.  
  46. void setup() {
  47.   delay(100);
  48.   pinMode(rel_pompa, OUTPUT);
  49.   pinMode(rel_peltier, OUTPUT);
  50.   pinMode(rel_heater, OUTPUT);
  51.   pinMode(led, OUTPUT);
  52.  
  53.   Serial.begin(115200);
  54.   lcd.init();
  55.   lcd.backlight();
  56.   init_freq();
  57.   Serial.println("system mulai");
  58. }
  59.  
  60. void loop() {
  61.   key = keypad.getKey();
  62.   if (key) {
  63.     baca_key(); lcd.clear(); delay(20);
  64.   }
  65.   update_lcd();
  66.   control();
  67. }
  68.  
  69. void baca_key() {
  70.   switch (state) {
  71.     case STOP:
  72.       if ( key == 48 )key = 0;
  73.       if ( key < 10) {
  74.         dtKey = dtKey * 10 + key;
  75.         //TOP = float(crystal)/((float)dtKey*64)-1;
  76.       }
  77.       if (key == BINTANG) {
  78.         dtKey = 0;
  79.         set_freq = 0;
  80.       }
  81.       if (key == PAGAR) {
  82.         TOP = float(crystal) / ((float)dtKey * 64) - 1;
  83.         init_freq();
  84.         state = RUN;
  85.       }
  86.       break;
  87.     case RUN:
  88.       if (key == BINTANG) {
  89.         state = STOP;
  90.       }
  91.       break;
  92.   }
  93. }
  94.  
  95. void get_text() {
  96.   switch (state) {
  97.     case STOP:
  98.       line1 = "Set Freq";
  99.       line2 = "Freq: " + String(dtKey) + " Hz";
  100.       break;
  101.     case RUN:
  102.       line1 = "Running...";
  103.       //line1 = String(TOP, 2);
  104.       line2 = "Freq: " + String(dtKey) + " Hz";
  105.       break;
  106.   }
  107. }
  108.  
  109. void update_lcd() {
  110.   get_text();
  111.   lcd.setCursor(0, 0);
  112.   lcd.print(line1);
  113.   lcd.setCursor(0, 1);
  114.   lcd.print(line2);
  115. }
  116.  
  117. void control() {
  118.   switch (state) {
  119.     case STOP:
  120.       analogWrite(13, 0);
  121.       break;
  122.  
  123.     case RUN:
  124.       analogWrite(13, 1);
  125.       break;
  126.   }
  127. }
  128.  
  129. void init_freq() {
  130.   DDRD |= (1 << PD5);
  131.   TCNT1 = 0;
  132.   ICR1 = TOP;
  133.   TCCR1A = (1 << WGM11) | (1 << COM1A1);
  134.   TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10) | (1 << CS11);
  135. }
RAW Paste Data