---
# System prepended metadata

title: '[2021/06/10] 微處裡器設計'

---

# [2021/06/10] 微處裡器設計
## 上課小考
- BREND
    #### 截圖
    1. ![](https://i.imgur.com/vMFGTcq.png)
    2. ![](https://i.imgur.com/mjfQnqa.png)
    3. ![](https://i.imgur.com/OmELIyI.png)
    4. ![](https://i.imgur.com/NeGn4uK.png)
    
    #### Code
    ```
    #include "AT89X52.H"

    void SpeakAlert(unsigned char i);
    void Display_clock(void);
    sbit SPEAK= 0xA6; //P2^6
    unsigned char hor = 23, min = 59, sec = 56;
    unsigned char count = 0;
    bit timer_start = 0;

    void main(void) {
        TMOD = 0x01; // 0000 0001 Timer 0 mode 1

        TL0 = (65536 - 46080) % 256; // 最低的8位元
        TH0 = (65536 - 46080) / 256; // 最低的8位元

        P0 = sec;
        EA = 1;
        ET0 = 1; //Timer 0 interrup enable: Timer 1 ET1
        // TR0 = 1; //Timer 0 run/stop
        EX0 = 1; //INT 0 intterupt enable
        IT0 = 1; //Negative Edge enable

        EX1 = 1;
        IT1 = 1;

        while(timer_start == 0) Display_clock();

        if(timer_start == 1) TR0 = 1;

        while(1) {
            Display_clock();
        }
    }

    void SpeakAlert(unsigned char i) {
        unsigned int j;

        while(i--) {
            for (j=0;j<400;++j) {
                SPEAK = !SPEAK;
                Delay_ms(1);
            }
            Delay_ms(200);
        }
    }

    void Display_clock(void) {
        if(sec >= 60){
            sec = 0;
            ++min;
            if(min>=60){
                min = 0;
                ++hor;
                if(hor >=24){
                        hor = 0;
                }
            }
        }
        P0 = sec;
    }

    void INT1_int(void) interrupt 2 { 
        if(timer_start == 1) TR0 = !TR0;
    }

    void INT0_int(void) interrupt 0 { //INT0 #0
        ++sec;
        SpeakAlert(1);
        if(sec>=60 || sec==0) {
            timer_start = 1;
        }
    }

    void Timer0_int(void) interrupt 1 {
        TL0 = (65536 - 46080) % 256; // 最低的8位元
        TH0 = (65536 - 46080) / 256; // 最低的8位元
        ++count;
        if(count == 20){
            count = 0;
            ++sec;
            SpeakAlert(1);
        }
    }
    ```