Thắc mắc về code lần 2 ^^

Phạm Minh

Thành Viên PIF
Làm sao để nhấn nút trên kit thì led 1 và led sáng tắt luân phiên vậy :D
 

hoangphuoc

Thành Viên PIF
đọc nút nhấn.
Nếu có nút 1 nhấn nhảy vào 1 vòng while
Trong vòng while cơ bản nhất là bật led - delay - tắt led - delay
Hướng là vậy. Lười ghi code quá :1cool_byebye::1cool_byebye::1cool_byebye:
 

MrChips

Thành Viên PIF
Em có thể tham khảo hai code sau:
Dùng Interrupt:
Code:
/****************************************************************
*
* www.payitforward.edu.vn
*
* Author: Jacob Pham
*
****************************************************************/
#include <msp430g2553.h>
 
/****************************************************************
*                MAIN PROGRAM
****************************************************************/
void main(void)
{
    WDTCTL = WDTPW | WDTHOLD;  // Stop watchdog timer
    P1SEL = 0x00;                //Disable external crystal
    P1SEL2 = 0x00;
 
    //Config PIN
/****************************** Output ****************************/
    P1DIR = BIT6;                //P1.6 is output
    P1OUT = BIT6;
/****************************** Input ****************************/
    P1DIR &= ~(BIT3);    //P1.0 & P1.3 are input
    P1REN |= BIT3;      //Enable internal resistor on P1.0 & P1.3
    P1OUT |= BIT3;      //Pull-up resistor
/************************Config button****************************/
    P1IES |= BIT3;      //Pos-edge interrupt
        /* Bit = 0: Interrupt on a fall edge of an input signal
        *    Bit = 1: Interrupt on a rise edge of an input signal
        */
    P1IE |= BIT3;        //Enable GPIO interrupt on P1.0 & P1.3
    P1IFG &= ~(BIT3);    //Clear interrupt flag
    _enable_interrupts();      //Enable Global interrupt
 
    while(1)
    {
        //do nothing
    }
}
#pragma vector = PORT1_VECTOR
__interrupt void port1_isr(void) // can change name interrupt program
{
    if (P1IFG & BIT3)            //Button 1 is pressed
    {
        __delay_cycles(50000);    //delay 50ms
        if ((P1IN & BIT3) == 0)    //Button  is really pressed
        {
            P1OUT ^= BIT6;
        }
        P1IFG &= ~BIT3;            //Clear interrupt flag
    }
}
Dùng cách thông thường như hoangphuoc nói trên:
Code:
/****************************************************************
*
* www.payitforward.edu.vn
*
* Author: Jacob Pham
*
****************************************************************/
#include <msp430g2553.h>
 
/****************************************************************
*                MAIN PROGRAM
****************************************************************/
void main(void)
{
    WDTCTL = WDTPW | WDTHOLD;  // Stop watchdog timer
    //Config PIN
/****************************** Output ****************************/
    P1DIR = BIT6;                //P1.6 is output
    P1OUT = BIT6;
/****************************** Input ****************************/
    P1DIR &= ~(BIT3);    //P1.0 & P1.3 are input
    P1REN |= BIT3;      //Enable internal resistor on P1.0 & P1.3
    P1OUT |= BIT3;      //Pull-up resistor
    while(1)
    {
        if((P1IN&BIT3) == 0)
        {
            P1OUT ^= BIT6;
        }
    }
}
Hai code sẽ hoạt động như em nói, mà nó cũng có điều khác nhau. Đây là vấn đề đặt ra cho em tìm hiểu? :D
Chúc em may mắn!
 
Top