Thông báo

Collapse
No announcement yet.

[Help] Code cho LPC 2103 sử dụng RTClock

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • [Help] Code cho LPC 2103 sử dụng RTClock

    Mạch thực tế:
    Mạch RTC.mp4

    Trong khi đó em nạp code này cho con LPC2103 thì nó chỉ ở chế độ t(time) chứ nó không qua chế độ c(calendar). Vặn nút màu đỏ thì trạng thái không thay đổi được.
    Mọi người có thể cho mình ít lời khuyên về đoạn code này không.

    #include <LPC21xx.H>

    #define uint unsigned int
    #define uchar unsigned char

    // Config oscillation
    #define FOSC 14745000 // External clock input frequency (must be between 10 MHz and 25 MHz)
    #define USE_PLL 1 // 0 = do not use on-chip PLL, 1 = use on-chip PLL)
    #define PLL_MUL 4 // PLL multiplication factor (1 to 32)
    #define PBSD 3 // Peripheral bus speed divider (1, 2, or 4)
    #define PCLK (FOSC * PLL_MUL / PBSD)
    #define CRYSTAL_FREQUENCY FOSC
    #define PLL_FACTOR PLL_MUL
    #define VPBDIV_FACTOR PBSD

    // Binary conversion
    #define HEX_(n) 0x##n##LU
    #define B8(x) ((x&0x0FLU)?1:0) + ((x&0xF0LU)?2:0) + ((x&0x0F00LU)?4:0) + ((x&0xF000LU)?8:0) \
    + ((x&0x0F0000LU)?16:0) + ((x&0xF00000LU)?32:0) + ((x&0x0F000000LU)?64:0) + ((x&0xF0000000LU)?128:0)
    #define B(d) ((unsigned char)B8(HEX_(d)))
    #define H(d) ((unsigned char)B8(HEX_(d)) << 4)

    // Hex value
    #define blank 11
    #define hex_blank H(00000000)
    #define dash 10
    #define hex_dash H(00000010)
    #define dot (0x01 << 4)
    uint hexvalue[16] = { H(11111100), H(01100000), H(11011010), H(11110010), H(01100110),
    H(10110110), H(10111110), H(11100000), H(11111110), H(11110110),
    hex_dash, hex_blank};

    // Direction
    #define RGB_dir (0x07 << 19)
    #define _7seg_dir (0x7c07 << 16)
    #define data7s_dir (0xff << 4)
    #define sel_dir (0x07 << 23)
    #define button_dir (~(0x0f << 12))

    #define flashtime 110

    typedef enum {disp_time, disp_calendar, setsecond, setminute, sethour, setyear, setmonth, setday} MODE;
    uint hex[8];
    uint curr_hex;
    uint timer;
    uint mode;

    void delay(uint t)
    {
    while (t--);
    }

    void initRTC()
    {
    CCR = 0x02; // reset clock
    ILR = 0x00; // clear interrupt flags
    CIIR = 0x00;
    AMR = 0xff; // do not compare any alarm

    PREINT = PCLK / 32768 - 1;
    PREFRAC = PCLK - (PCLK / 32768) * 32768;

    HOUR = 12; MIN = SEC = 0;
    DOM = 15; MONTH = 6; YEAR = 2010;

    CCR = 0x01; // start RTC, use core clock
    }

    void setInt7seg(uint n)
    {
    #define SEG_A 0x00010000
    #define SEG_B 0x00020000
    #define SEG_C 0x00040000
    #define SEG_D 0x04000000
    #define SEG_E 0x08000000
    #define SEG_F 0x10000000
    #define SEG_G 0x20000000
    #define SEG_DOT 0x40000000

    IOSET0 = _7seg_dir;
    switch (n)
    {
    // case 0x0: IOCLR0 = SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F; break;
    // case 0x1: IOCLR0 = SEG_B | SEG_C; break;
    // case 0x2: IOCLR0 = SEG_A | SEG_B | SEG_D | SEG_E | SEG_G; break;
    // case 0x3: IOCLR0 = SEG_A | SEG_B | SEG_C | SEG_D | SEG_G; break;
    // case 0x4: IOCLR0 = SEG_B | SEG_C | SEG_F | SEG_G; break;
    // case 0x5: IOCLR0 = SEG_A | SEG_C | SEG_D | SEG_F | SEG_G; break;
    // case 0x6: IOCLR0 = SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G; break;
    // case 0x7: IOCLR0 = SEG_A | SEG_B | SEG_C; break;
    // case 0x8: IOCLR0 = SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G; break;
    // case 0x9: IOCLR0 = SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G; break;
    case 0xA: IOCLR0 = SEG_D | SEG_E | SEG_F | SEG_G; break;
    case 0xB: IOCLR0 = SEG_A | SEG_D | SEG_E | SEG_F; break;
    // case 0xA: IOCLR0 = SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G; break;
    // case 0xb: IOCLR0 = SEG_C | SEG_D | SEG_E | SEG_F | SEG_G; break;
    // case 0xC: IOCLR0 = SEG_A | SEG_D | SEG_E | SEG_F; break;
    // case 0xd: IOCLR0 = SEG_B | SEG_C | SEG_D | SEG_E | SEG_G; break;
    // case 0xE: IOCLR0 = SEG_A | SEG_D | SEG_E | SEG_F | SEG_G; break;
    // case 0xF: IOCLR0 = SEG_A | SEG_E | SEG_F | SEG_G; break;
    default: break;
    }
    }

    uint maxdayofmonth()
    {
    uint dayofmonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (MONTH == 2)
    if (YEAR & 0x03 == 0) return 29;
    else return 28;
    else return dayofmonth[MONTH-1];
    }

    void interuptTIMER1(void)__irq
    {
    T1IR = 0x3;
    IOCLR0 = data7s_dir | sel_dir;
    IOSET0 = hex[7 - curr_hex] | (curr_hex << 23);
    curr_hex = (curr_hex + 1) & 0x07;
    timer++;
    VICVectAddr = 0xFF;
    }

    uint getADC()
    {
    ADCR = (ADCR & 0xFFFFFF00) | (1 << 0) | (1 << 24); // start conversion now (for selected channel)
    while((ADDR & 0x80000000) == 0); // wait til done
    return (ADDR >> 6) & 0x3FF; // get result and adjust to 10-bit integer
    }

    void initADC(void)
    {
    static uint res;
    PINSEL1 &= ~0x00003000;
    PINSEL1 |= 0x00003000;
    //initialize ADC
    ADCR = (1 << 0) | //SEL = 1, dummy channel #0
    ((CRYSTAL_FREQUENCY *
    PLL_FACTOR /
    VPBDIV_FACTOR) / 4500000 - 1) << 8 | //set clock division factor, so ADC clock is 4.5MHz
    (0 << 16) | //BURST = 0, conversions are SW controlled
    (0 << 17) | //CLKS = 0, 11 clocks = 10-bit result
    (1 << 21) | //PDN = 1, ADC is active
    (1 << 24) | //START = 1, start a conversion now
    (0 << 27); //EDGE = 0, not relevant when start=1

    //short delay and dummy read
    delay(8);
    res = ADDR;
    }

    void initializeTIMER1(void)
    {
    T1PR = 500;
    T1TC = 0;
    T1PC = 0;
    T1MR0 = 100; // Setting the Match value
    // Reset and interrupt on match
    T1MCR = 0x3; // |0x08;

    // VIC Configuration
    VICIntEnable = 0x20;
    VICIntSelect = 0x00;
    VICVectCntl1 = 0x25; // Address of the ISR
    VICVectAddr1 = (unsigned long)interuptTIMER1;
    //Timer 1 Configuration
    T1TCR = 0x1; //enable TC,PC
    }

    uint buttonpressed()
    {
    if ((IOPIN0 & 0x00004000) == 0)
    {
    while ((IOPIN0 & 0x00004000) == 0);
    return 1;
    }
    else
    return 0;
    }

    int main()
    {
    uint t, t1, t2;
    MODE mode;
    // init
    PINSEL0 = 0x50; // I2C
    PINSEL0 = (PINSEL0 & 0xfffffff0) | 0x00000005; // UART
    PINSEL1 = 0x00003000; // ADC 0

    IODIR0 = (RGB_dir | _7seg_dir | data7s_dir | sel_dir) & button_dir;

    initRTC();
    hex[7] = hex[6] = hex[5] = hex[4] = hex[3] = hex[2] = hex[1] = hex[0] = hex_dash;
    initializeTIMER1();
    initADC();

    mode = disp_time;
    setInt7seg(0);
    curr_hex = 5;
    timer = 0;

    while (1)
    {
    switch (mode)
    {
    case disp_time:
    setInt7seg(10);
    if (buttonpressed()) { mode = setsecond; break; }
    if (getADC() > 511) { mode = disp_calendar; break; }

    t = HOUR / 10; t1 = HOUR % 10;
    hex[7] = (t == 0) ? hex_blank : hexvalue[t]; hex[6] = hexvalue[t1];
    hex[5] = hex_dash;
    hex[4] = hexvalue[MIN / 10]; hex[3] = hexvalue[MIN % 10];
    hex[2] = hex_blank;
    hex[1] = hexvalue[SEC / 10]; hex[0] = hexvalue[SEC % 10];
    setInt7seg(t1);
    break;

    //-----------------------------------------------------------------------//
    case disp_calendar:
    setInt7seg(11);
    if (buttonpressed()) { mode = setyear; timer = 0; break; }
    if (getADC() <= 511) { mode = disp_time; break; }

    hex[7] = hexvalue[DOM / 10]; hex[6] = hexvalue[DOM % 10] | dot;
    hex[5] = hexvalue[MONTH / 10]; hex[4] = hexvalue[MONTH % 10] | dot;
    t1 = YEAR / 1000; t2 = YEAR % 1000;
    hex[3] = hexvalue[t1]; t = t2 / 100; t1 = t2 % 100;
    hex[2] = hexvalue[t]; t2 = t1 / 10; t = t1 % 10;
    hex[1] = hexvalue[t2];
    hex[0] = hexvalue[t];
    break;

    //-----------------------------------------------------------------------//
    case setsecond:
    if (buttonpressed()) { mode = sethour; timer = 0; break; }

    hex[7] = hexvalue[HOUR / 10]; hex[6] = hexvalue[HOUR % 10];
    hex[5] = hex_dash;
    hex[4] = hexvalue[MIN / 10]; hex[3] = hexvalue[MIN % 10];
    hex[2] = hex_blank;
    if (timer < flashtime)
    {
    hex[1] = hexvalue[SEC / 10]; hex[0] = hexvalue[SEC % 10];
    }
    else if (timer < 2*flashtime)
    hex[1] = hex[0] = hex_blank;
    else
    timer = 0;
    break;

    //-----------------------------------------------------------------------//
    case setminute:
    if (buttonpressed()) { mode = setyear; timer = 0; break; }

    t = getADC() / 16;
    MIN = t > 59 ? 59 : t;
    hex[7] = hexvalue[HOUR / 10]; hex[6] = hexvalue[HOUR % 10];
    hex[5] = hex_dash;
    if (timer < flashtime)
    {
    hex[4] = hexvalue[MIN / 10]; hex[3] = hexvalue[MIN % 10];
    }
    else if (timer < 2*flashtime)
    hex[4] = hex[3] = hex_blank;
    else
    timer = 0;
    hex[2] = hex_blank;
    hex[1] = hexvalue[SEC / 10]; hex[0] = hexvalue[SEC % 10];
    break;

    //-----------------------------------------------------------------------//
    case sethour:
    if (buttonpressed()) { mode = setminute; timer = 0; break; }

    t = getADC() / 43;
    HOUR = t > 23 ? 23 : t;
    if (timer < flashtime)
    {
    hex[7] = hexvalue[HOUR / 10]; hex[6] = hexvalue[HOUR % 10];
    }
    else if (timer < 2*flashtime)
    hex[7] = hex[6] = hex_blank;
    else
    timer = 0;
    hex[5] = hex_dash;
    hex[4] = hexvalue[MIN / 10]; hex[3] = hexvalue[MIN % 10];
    hex[2] = hex_blank;
    hex[1] = hexvalue[SEC / 10]; hex[0] = hexvalue[SEC % 10];
    break;

    //-----------------------------------------------------------------------//
    case setyear:
    if (buttonpressed()) { mode = setmonth; timer = 0; break; }

    t = getADC() / 100;
    YEAR = (t > 10 ? 10 : t) + 2010;
    hex[7] = hexvalue[DOM / 10]; hex[6] = hexvalue[DOM % 10] | dot;
    hex[5] = hexvalue[MONTH / 10]; hex[4] = hexvalue[MONTH % 10] | dot;
    if (timer < flashtime)
    {
    t1 = YEAR / 1000; t2 = YEAR % 1000;
    hex[3] = hexvalue[t1]; t = t2 / 100; t1 = t2 % 100;
    hex[2] = hexvalue[t]; t2 = t1 / 10; t = t1 % 10;
    hex[1] = hexvalue[t2];
    hex[0] = hexvalue[t];
    }
    else if (timer < 2*flashtime)
    hex[3] = hex[2] = hex[1] = hex[0] = hex_blank;
    else
    timer = 0;
    break;

    //-----------------------------------------------------------------------//
    case setmonth:
    if (buttonpressed()) { mode = setday; timer = 0; break; }

    t = getADC() / 90 + 1;
    MONTH = t > 12 ? 12 : t;
    hex[7] = hexvalue[DOM / 10]; hex[6] = hexvalue[DOM % 10] | dot;
    if (timer < flashtime)
    {
    hex[5] = hexvalue[MONTH / 10]; hex[4] = hexvalue[MONTH % 10] | dot;
    }
    else if (timer < 2*flashtime)
    hex[5] = hex[4] = hex_blank;
    else
    timer = 0;
    t1 = YEAR / 1000; t2 = YEAR % 1000;
    hex[3] = hexvalue[t1]; t = t2 / 100; t1 = t2 % 100;
    hex[2] = hexvalue[t]; t2 = t1 / 10; t = t1 % 10;
    hex[1] = hexvalue[t2];
    hex[0] = hexvalue[t];
    break;

    //-----------------------------------------------------------------------//
    case setday:
    if (buttonpressed()) { mode = disp_time; break; }

    t1 = maxdayofmonth();
    t = (getADC() * (t1 - 1)) / 1000 + 1;
    DOM = t > t1 ? t1 : t;
    if (timer < flashtime)
    {
    hex[7] = hexvalue[DOM / 10]; hex[6] = hexvalue[DOM % 10] | dot;
    }
    else if (timer < 2*flashtime)
    hex[7] = hex[6] = hex_blank;
    else
    timer = 0;
    hex[5] = hexvalue[MONTH / 10]; hex[4] = hexvalue[MONTH % 10] | dot;
    t1 = YEAR / 1000; t2 = YEAR % 1000;
    hex[3] = hexvalue[t1]; t = t2 / 100; t1 = t2 % 100;
    hex[2] = hexvalue[t]; t2 = t1 / 10; t = t1 % 10;
    hex[1] = hexvalue[t2];
    hex[0] = hexvalue[t];
    break;

    //-----------------------------------------------------------------------//
    default:
    mode = disp_time;
    break;
    }
    }
    }

Về tác giả

Collapse

supbaxoi Tìm hiểu thêm về supbaxoi

Bài viết mới nhất

Collapse

Đang tải...
X