fork download
  1. #include <xc.h>
  2.  
  3. // Osilatör frekansı tanımı (4 MHz kristal varsayımı)
  4. #define _XTAL_FREQ 4000000 // 4 MHz
  5.  
  6. // Konfigürasyon bitleri
  7. #pragma config FOSC = HS // Yüksek hızlı osilatör
  8. #pragma config WDTE = OFF // Watchdog Timer kapalı
  9. #pragma config PWRTE = OFF // Power-up Timer kapalı
  10. #pragma config BOREN = ON // Brown-out Reset açık
  11. #pragma config LVP = OFF // Düşük voltaj programlama kapalı
  12. #pragma config CPD = OFF // Veri belleği kodu koruma kapalı
  13. #pragma config CP = OFF // Program belleği kodu koruma kapalı
  14.  
  15. // Giriş/Çıkış Tanımlamaları
  16. #define B1 RA0
  17. #define B2 RA1
  18. #define B3 RA2
  19. #define B4 RA3
  20. #define S1 RB0
  21. #define S2 RB1
  22. #define S3 RB2
  23. #define R1 RB3
  24. #define R2 RB4
  25. #define SYSTEM_SWITCH RE0
  26. #define RESET_ERROR RE1
  27. #define M1 RD0
  28. #define M2 RD1
  29. #define M3 RD2
  30. #define M4 RD3
  31. #define Z1 RD4
  32. #define Z2 RD5
  33. #define Z3 RD6
  34. #define S1_ERROR RC0
  35. #define S2_ERROR RC1
  36. #define S3_ERROR RC2
  37. #define R1_ERROR RC3
  38. #define R2_ERROR RC4
  39. #define SYSTEM_ON RC5
  40. #define SYSTEM_OFF RC6
  41.  
  42. // Gecikme fonksiyonu (XC8 için optimize)
  43. void delay_ms(unsigned int ms) {
  44. while (ms--) {
  45. __delay_ms(1); // XC8 dahili gecikme fonksiyonu
  46. }
  47. }
  48.  
  49. // Buton sırası için kuyruk
  50. unsigned char queue[4] = {0};
  51. unsigned char queue_head = 0;
  52. unsigned char queue_tail = 0;
  53. unsigned char processing = 0;
  54.  
  55. void enqueue(unsigned char button) {
  56. if (queue_tail < 4 && !processing) {
  57. queue[queue_tail++] = button;
  58. }
  59. }
  60.  
  61. unsigned char dequeue() {
  62. if (queue_head < queue_tail) {
  63. return queue[queue_head++];
  64. }
  65. return 0xFF;
  66. }
  67.  
  68. void init_system() {
  69. M4 = 1; M3 = 0;
  70. unsigned int reed_timeout = 0;
  71. while (R1 == 0 && reed_timeout < 5) {
  72. delay_ms(100);
  73. reed_timeout++;
  74. }
  75. M4 = 0;
  76. if (reed_timeout >= 5)
  77. R1_ERROR = 1;
  78. }
  79.  
  80. void reset_error_leds() {
  81. S1_ERROR = 0; S2_ERROR = 0;
  82. S3_ERROR = 0;
  83. R1_ERROR = 0; R2_ERROR = 0;
  84. }
  85.  
  86. void main() {
  87. // Port yapılandırması
  88. TRISA = 0xFF; // RA giriş
  89. TRISB = 0xFF; // RB giriş
  90. TRISC = 0x00; // RC çıkış
  91. TRISD = 0x00; // RD çıkış
  92. TRISE = 0xFF; // RE giriş
  93. ADCON1 = 0x06; // Analog girişleri kapat
  94.  
  95. // Başlangıç durumu
  96. S1_ERROR = 0; S2_ERROR = 0;
  97. S3_ERROR = 0;
  98. R1_ERROR = 0; R2_ERROR = 0;
  99. SYSTEM_ON = 0; SYSTEM_OFF = 1;
  100.  
  101. while (1) {
  102. // Hata LED'lerini sıfırlama
  103. if (RESET_ERROR == 1) {
  104. reset_error_leds();
  105. delay_ms(50);
  106. while (RESET_ERROR == 1);
  107. }
  108.  
  109. if (SYSTEM_SWITCH == 1) {
  110. SYSTEM_ON = 1;
  111. SYSTEM_OFF = 0;
  112. if (queue_head == 0 && queue_tail == 0) init_system();
  113.  
  114. // Buton girişleri
  115. if (B1 == 1 && !processing) {
  116. enqueue(0); delay_ms(50); while (B1 == 1);
  117. }
  118. if (B2 == 1 && !processing && queue_tail == 0) {
  119. enqueue(1); delay_ms(50); while (B2 == 1);
  120. }
  121. if (B3 == 1 && !processing && queue_tail < 2) {
  122. enqueue(2); delay_ms(50); while (B3 == 1);
  123. }
  124. if (B4 == 1 && !processing && queue_tail < 3) {
  125. enqueue(3); delay_ms(50); while (B4 == 1);
  126. }
  127.  
  128. // Kuyruk işleme
  129. if (!processing && queue_head < queue_tail) {
  130. processing = 1;
  131. unsigned char current_button = dequeue();
  132.  
  133. if (current_button == 0) {
  134. // B1 (Kol 2 gönderim)
  135. M4 = 1; M3 = 0;
  136. unsigned int reed_timeout = 0;
  137. while (R1 == 0 && reed_timeout < 5) {
  138. delay_ms(100);
  139. reed_timeout++;
  140. }
  141. M4 = 0;
  142. if (reed_timeout >= 5) R1_ERROR = 1;
  143. M2 = 1; M1 = 0;
  144. unsigned int sensor_timeout = 0;
  145. while (S3 == 0 && sensor_timeout < 60) {
  146. delay_ms(100);
  147. sensor_timeout++;
  148. }
  149. M2 = 0;
  150. if (sensor_timeout >= 60) S3_ERROR = 1;
  151. Z3 = 1; delay_ms(500); Z3 = 0;
  152. }
  153. else if (current_button == 1) {
  154. // B2 (Kol 3 gönderim)
  155. M3 = 1; M4 = 0;
  156. unsigned int reed_timeout = 0;
  157. while (R2 == 0 && reed_timeout < 5) {
  158. delay_ms(100);
  159. reed_timeout++;
  160. }
  161. M3 = 0;
  162. if (reed_timeout >= 5) R2_ERROR = 1;
  163. M2 = 1; M1 = 0;
  164. unsigned int sensor_timeout = 0;
  165. while (S2 == 0 && sensor_timeout < 60) {
  166. delay_ms(100);
  167. sensor_timeout++;
  168. }
  169. M2 = 0;
  170. if (sensor_timeout >= 60) S2_ERROR = 1;
  171. Z2 = 1; delay_ms(500); Z2 = 0;
  172. }
  173. else if (current_button == 2) {
  174. // B3 (Kol 2 geri alma)
  175. M3 = 1; M4 = 0;
  176. unsigned int reed_timeout = 0;
  177. while (R2 == 0 && reed_timeout < 5) {
  178. delay_ms(100);
  179. reed_timeout++;
  180. }
  181. M3 = 0;
  182. if (reed_timeout >= 5) R2_ERROR = 1;
  183. M1 = 1; M2 = 0;
  184. unsigned int sensor_timeout = 0;
  185. while (S1 == 0 && sensor_timeout < 60) {
  186. delay_ms(100);
  187. sensor_timeout++;
  188. }
  189. M1 = 0;
  190. if (sensor_timeout >= 60) S1_ERROR = 1;
  191. Z1 = 1; delay_ms(500); Z1 = 0;
  192. }
  193. else if (current_button == 3) {
  194. // B4 (Kol 3 geri alma)
  195. M4 = 1; M3 = 0;
  196. unsigned int reed_timeout = 0;
  197. while (R1 == 0 && reed_timeout < 5) {
  198. delay_ms(100);
  199. reed_timeout++;
  200. }
  201. M4 = 0;
  202. if (reed_timeout >= 5) R1_ERROR = 1;
  203. M1 = 1; M2 = 0;
  204. unsigned int sensor_timeout = 0;
  205. while (S1 == 0 && sensor_timeout < 60) {
  206. delay_ms(100);
  207. sensor_timeout++;
  208. }
  209. M1 = 0;
  210. if (sensor_timeout >= 60) S1_ERROR = 1;
  211. Z1 = 1; delay_ms(500); Z1 = 0;
  212. }
  213. processing = 0;
  214. }
  215. } else {
  216. SYSTEM_ON = 0;
  217. SYSTEM_OFF = 1;
  218. queue_head = 0; queue_tail = 0;
  219. M1 = 0; M2 = 0; M3 = 0; M4 = 0;
  220. Z1 = 0; Z2 = 0; Z3 = 0;
  221. }
  222. }
  223. }
Success #stdin #stdout 0.04s 25656KB
stdin
Standard input is empty
stdout
#include <xc.h>

// Osilatör frekansı tanımı (4 MHz kristal varsayımı)
#define _XTAL_FREQ 4000000 // 4 MHz

// Konfigürasyon bitleri
#pragma config FOSC = HS    // Yüksek hızlı osilatör
#pragma config WDTE = OFF  // Watchdog Timer kapalı
#pragma config PWRTE = OFF // Power-up Timer kapalı
#pragma config BOREN = ON  // Brown-out Reset açık
#pragma config LVP = OFF   // Düşük voltaj programlama kapalı
#pragma config CPD = OFF   // Veri belleği kodu koruma kapalı
#pragma config CP = OFF    // Program belleği kodu koruma kapalı

// Giriş/Çıkış Tanımlamaları
#define B1 RA0
#define B2 RA1
#define B3 RA2
#define B4 RA3
#define S1 RB0
#define S2 RB1
#define S3 RB2
#define R1 RB3
#define R2 RB4
#define SYSTEM_SWITCH RE0
#define RESET_ERROR RE1
#define M1 RD0
#define M2 RD1
#define M3 RD2
#define M4 RD3
#define Z1 RD4
#define Z2 RD5
#define Z3 RD6
#define S1_ERROR RC0
#define S2_ERROR RC1
#define S3_ERROR RC2
#define R1_ERROR RC3
#define R2_ERROR RC4
#define SYSTEM_ON RC5
#define SYSTEM_OFF RC6

// Gecikme fonksiyonu (XC8 için optimize)
void delay_ms(unsigned int ms) {
    while (ms--) {
        __delay_ms(1); // XC8 dahili gecikme fonksiyonu
    }
}

// Buton sırası için kuyruk
unsigned char queue[4] = {0};
unsigned char queue_head = 0;
unsigned char queue_tail = 0;
unsigned char processing = 0;

void enqueue(unsigned char button) {
    if (queue_tail < 4 && !processing) {
        queue[queue_tail++] = button;
    }
}

unsigned char dequeue() {
    if (queue_head < queue_tail) {
        return queue[queue_head++];
    }
    return 0xFF;
}

void init_system() {
    M4 = 1; M3 = 0;
    unsigned int reed_timeout = 0;
    while (R1 == 0 && reed_timeout < 5) {
        delay_ms(100);
        reed_timeout++;
    }
    M4 = 0;
    if (reed_timeout >= 5)
        R1_ERROR = 1;
}

void reset_error_leds() {
    S1_ERROR = 0; S2_ERROR = 0;
    S3_ERROR = 0;
    R1_ERROR = 0; R2_ERROR = 0;
}

void main() {
    // Port yapılandırması
    TRISA = 0xFF; // RA giriş
    TRISB = 0xFF; // RB giriş
    TRISC = 0x00; // RC çıkış
    TRISD = 0x00; // RD çıkış
    TRISE = 0xFF; // RE giriş
    ADCON1 = 0x06; // Analog girişleri kapat

    // Başlangıç durumu
    S1_ERROR = 0; S2_ERROR = 0;
    S3_ERROR = 0;
    R1_ERROR = 0; R2_ERROR = 0;
    SYSTEM_ON = 0; SYSTEM_OFF = 1;

    while (1) {
        // Hata LED'lerini sıfırlama
        if (RESET_ERROR == 1) {
            reset_error_leds();
            delay_ms(50);
            while (RESET_ERROR == 1);
        }

        if (SYSTEM_SWITCH == 1) {
            SYSTEM_ON = 1;
            SYSTEM_OFF = 0;
            if (queue_head == 0 && queue_tail == 0) init_system();

            // Buton girişleri
            if (B1 == 1 && !processing) {
                enqueue(0); delay_ms(50); while (B1 == 1);
            }
            if (B2 == 1 && !processing && queue_tail == 0) {
                enqueue(1); delay_ms(50); while (B2 == 1);
            }
            if (B3 == 1 && !processing && queue_tail < 2) {
                enqueue(2); delay_ms(50); while (B3 == 1);
            }
            if (B4 == 1 && !processing && queue_tail < 3) {
                enqueue(3); delay_ms(50); while (B4 == 1);
            }

            // Kuyruk işleme
            if (!processing && queue_head < queue_tail) {
                processing = 1;
                unsigned char current_button = dequeue();

                if (current_button == 0) {
                    // B1 (Kol 2 gönderim)
                    M4 = 1; M3 = 0;
                    unsigned int reed_timeout = 0;
                    while (R1 == 0 && reed_timeout < 5) {
                        delay_ms(100);
                        reed_timeout++;
                    }
                    M4 = 0;
                    if (reed_timeout >= 5) R1_ERROR = 1;
                    M2 = 1; M1 = 0;
                    unsigned int sensor_timeout = 0;
                    while (S3 == 0 && sensor_timeout < 60) {
                        delay_ms(100);
                        sensor_timeout++;
                    }
                    M2 = 0;
                    if (sensor_timeout >= 60) S3_ERROR = 1;
                    Z3 = 1; delay_ms(500); Z3 = 0;
                }
                else if (current_button == 1) {
                    // B2 (Kol 3 gönderim)
                    M3 = 1; M4 = 0;
                    unsigned int reed_timeout = 0;
                    while (R2 == 0 && reed_timeout < 5) {
                        delay_ms(100);
                        reed_timeout++;
                    }
                    M3 = 0;
                    if (reed_timeout >= 5) R2_ERROR = 1;
                    M2 = 1; M1 = 0;
                    unsigned int sensor_timeout = 0;
                    while (S2 == 0 && sensor_timeout < 60) {
                        delay_ms(100);
                        sensor_timeout++;
                    }
                    M2 = 0;
                    if (sensor_timeout >= 60) S2_ERROR = 1;
                    Z2 = 1; delay_ms(500); Z2 = 0;
                }
                else if (current_button == 2) {
                    // B3 (Kol 2 geri alma)
                    M3 = 1; M4 = 0;
                    unsigned int reed_timeout = 0;
                    while (R2 == 0 && reed_timeout < 5) {
                        delay_ms(100);
                        reed_timeout++;
                    }
                    M3 = 0;
                    if (reed_timeout >= 5) R2_ERROR = 1;
                    M1 = 1; M2 = 0;
                    unsigned int sensor_timeout = 0;
                    while (S1 == 0 && sensor_timeout < 60) {
                        delay_ms(100);
                        sensor_timeout++;
                    }
                    M1 = 0;
                    if (sensor_timeout >= 60) S1_ERROR = 1;
                    Z1 = 1; delay_ms(500); Z1 = 0;
                }
                else if (current_button == 3) {
                    // B4 (Kol 3 geri alma)
                    M4 = 1; M3 = 0;
                    unsigned int reed_timeout = 0;
                    while (R1 == 0 && reed_timeout < 5) {
                        delay_ms(100);
                        reed_timeout++;
                    }
                    M4 = 0;
                    if (reed_timeout >= 5) R1_ERROR = 1;
                    M1 = 1; M2 = 0;
                    unsigned int sensor_timeout = 0;
                    while (S1 == 0 && sensor_timeout < 60) {
                        delay_ms(100);
                        sensor_timeout++;
                    }
                    M1 = 0;
                    if (sensor_timeout >= 60) S1_ERROR = 1;
                    Z1 = 1; delay_ms(500); Z1 = 0;
                }
                processing = 0;
            }
        } else {
            SYSTEM_ON = 0;
            SYSTEM_OFF = 1;
            queue_head = 0; queue_tail = 0;
            M1 = 0; M2 = 0; M3 = 0; M4 = 0;
            Z1 = 0; Z2 = 0; Z3 = 0;
        }
    }
}