[C7] Tổng kết bài ADC

Manhdd

Cố Vấn CLB
Staff member
Silde bài giảng ADC các bạn download tại:
<<Tổng hợp Bài giảng và Drivers C7 >>

Code mẫu bài tập thí dụ 1: Đọc biến trở
Code:
/******************************************************************************
*
* WWW.PAYITFORWARD.EDU.VN
*
******************************************************************************/
 
/******************************************************************************
*
* C7 TRAINING
*
******************************************************************************/
 
/******************************************************************************
*
*  Module        : VOLTAGE MESURE
*  Description    : This file describes API functions that support for
*                    ADC module on MSP430G2553
*
*  Tool          : CCS 5.1
*  Chip          : MSP430G2553
*  History        : 04-11-2012
*  Version        : 1
*
*  Author        : NHH
*  Notes          : To apply these functions, you must include the header file lcd.h and
*                  source file lcd.c to your project.
*
*
******************************************************************************/
 
/*****************************************************************************
* Pin Description
******************************************************************************
*
*                            MSP430G2x53
*                        -----------------
*                    /|\|              XIN|-
*                      | |                |
*                      --|RST          XOUT|-
*                        |                |
* Analog signal input >--|P1.1/A1          |--> ||
*                        |                |-->    || Out to LCD
*                        |                  |--> || Described in LCD.c
*                        |                  |--> ||
*                        |                  |
*
******************************************************************************/
 
/*****************************************************************************
* IMPORT
******************************************************************************/
#include <msp430g2553.h>
#include "LCD.h"
 
/*****************************************************************************
* DECLARATIONS
******************************************************************************/
 
#define Vref 3.3    //Define Vcc voltage when select Vref = VCC
 
unsigned long adc_result, volt;
 
/*****************************************************************************
*                        SUBROUTINES
******************************************************************************/
 
//*****************************************************************************
// Initialization for ADC10 module
//*****************************************************************************
void ADC10_Init(void)
{
    ADC10CTL0 = SREF_0 + ADC10SHT_1 + ADC10ON + ADC10IE;
    /*
    * Vref = VCC
    * ADC sample and hold time = 8 ADC clocks
    * Turn on ADC10
    * Enable ADC10 Interrupt
    */
    ADC10CTL1 = INCH_1 + ADC10DIV_1 + ADC10SSEL_3;
    /*
    * Select Input chanel 1
    * ADC10DF = 0: The 10-bit conversion results are right justified
    * ADC10 Clock divider: 2
    * ADC10 clock source select: SMCLK
    */
    ADC10AE0 |= BIT1;    //Enable analog input on A1 chanel
}
 
/*****************************************************************************
*                        MAIN PROGRAM
******************************************************************************/
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;    //Stop Watchdog Timer
    ADC10_Init();                //Initialize ADC10
    lcd_init();                    //Initialize LCD
    P2SEL = 0;                    //Turn off external crystal
    P2SEL2 = 0;
    lcd_clear();                //Clear LCD
 
    while (1)
    {
        ADC10CTL0 |= ENC + ADC10SC;    //Enable ADC10, Start sample - conversion
        _bis_SR_register(LPM0_bits + GIE);    //Enter LPM0, wait for sample-conversion finish
        lcd_clear();
        lcd_puts("Result: ");
        volt = (adc_result * Vref * 100) / 1024;    //Calculate result (= 100 * Voltage)
        //Display result
        lcd_putc((volt / 100) + 48);                //ASCII code (of a number) = number + 48
        lcd_puts(".");
        lcd_putc((volt / 10) % 10 + 48);
        lcd_putc((volt % 10) + 48);
        lcd_puts("V");
        _delay_cycles(500000);
    }
}
 
/*****************************************************************************
*                        INTERRUPT PROGRAM
******************************************************************************/
#pragma vector = ADC10_VECTOR
__interrupt void ADC10_Interrupt(void)
{
    adc_result = ADC10MEM;                    //Save Result
    _bic_SR_register_on_exit(LPM0_bits);    //Exit LPM0
}
/*****************************************************************************
*                        END OF MAIN.C
******************************************************************************/
Code mẫu bài tập thí dụ 2: Đọc cảm biến nhiệt nội
Code:
/******************************************************************************
*
* www.payitforward.edu.vn
*
******************************************************************************/
 
/******************************************************************************
*
* C7 TRAINING
*
******************************************************************************/
 
/******************************************************************************
*
*  Module        : TEMPERATURE SENSOR
*  Description    : This file describes API functions that support for
*                    temperature sensor on MSP430G2553
*
*  Tool          : CCS 5.1
*  Chip          : MSP430G2553
*  History        : 04-11-2012
*  Version        : 1
*
*  Author        :
*  Notes          : To apply these functions, you must include the header file lcd.h and
*                  source file lcd.c to your project.
*
*
******************************************************************************/
 
/****************************************************************************
* IMPORT
******************************************************************************/
#include <msp430g2553.h>
#include "LCD.h"
 
/****************************************************************************
* DECLARATIONS
******************************************************************************/
unsigned char temp;
 
/*****************************************************************************
*                        SUBROUTINES
******************************************************************************/
 
//*****************************************************************************
// Initialization for ADC10 module
//*****************************************************************************
void ADC10_Init(void)
{
    ADC10CTL0 = SREF_1 + ADC10SHT_3 + ADC10ON + ADC10IE + REFON;
    /*
    * Vr+ = Vref+
    * ADC sample and hold time = 64 ADC clocks: When using the temperature
      sensor, the sample period must be greater than 30 µs
    * Turn on ADC10
    * Enable ADC10 Interrupt
    * Turn on reference generator
    */
    ADC10CTL1 = INCH_10 + ADC10DIV_1 + ADC10SSEL_3 ;
    /*
    * Select Input chanel: Temperature Sensor
    * ADC10DF = 0: The 10-bit conversion results are right justified
    * ADC10 Clock divider: 4
    * ADC10 clock source select: SMCLK
    */
}
 
/*****************************************************************************
*                        MAIN PROGRAM
******************************************************************************/
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;    //Stop Watchdog Timer
    ADC10_Init();                //Initialize ADC10
    lcd_init();                    //Initialize LCD
    P2SEL = 0;                    //Turn off external crystal
    P2SEL2 = 0;
    lcd_clear();                //Clear LCD
    while (1)
    {
        ADC10CTL0 |= ENC + ADC10SC;            //Enable ADC10, Start sample - conversion
        _bis_SR_register(LPM0_bits + GIE);    //Enter LPM0, wait for sample-conversion finish
        lcd_clear();
        lcd_gotoxy(2,0);
        lcd_puts("Temperature:");
        lcd_gotoxy(7,1);
        lcd_putc((temp / 10) + 48);
        lcd_putc((temp % 10) + 48);
        _delay_cycles(500000);
 
    }
}
 
/*****************************************************************************
*                        INTERRUPT PROGRAM
******************************************************************************/
#pragma vector = ADC10_VECTOR
__interrupt void ADC10_Interrupt(void)
{
    temp=((ADC10MEM - 673) * 423) / 1024;    //Calculate temperature
    /*
    * VTEMP=0.00355(TEMPC)+0.986
    * Vref = 1.5V
    */
    _bic_SR_register_on_exit(LPM0_bits);    //Exit LPM0
}
/*****************************************************************************
*                        END
******************************************************************************/
Note: Code viết bởi bạn Nghiêm Hồng Hiệp

Và đây là bài tập cho các bạn tự luyện:
1. Kết hợp ngắt ADC với ngắt timer, đọc ADC theo chu kỳ ngắt của timer.
2. Kết hợp với PWM, thay đổi độ sáng của led bằng ADC (đọc từ biến trở, cảm biến nhiệt nội)
3. Dùng ADC để đọc và xử lý tín hiệu 1 cảm biến thông dụng: Cảm biến nhiệt LM35, quang trở, ...
...
 
hôm nay post trước cái code bài tập 2 cho các em về coi nhé:
2. Kết hợp với PWM, thay đổi độ sáng của led bằng ADC (đọc từ biến trở, cảm biến nhiệt nội)
...
Code:
#include  <msp430g2553.h>
 
#define LED        BIT2
#define INPUT    BIT0
 
void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;                // Stop WDT
 
    P1DIR |= LED;                                // P1.2 output
    P1SEL |= LED;                                // P1.2 TA0.1 function
 
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;
 
    TA0CCR0 = 0xffff;                            // PWM Period
    TA0CCTL1 |= OUTMOD_7;                        // CCR1 reset/set
    TA0CCR1 = 0;                              // CCR1 PWM duty cycle
    TACTL = TASSEL_2 + MC_1;                  // SMCLK, up mode
 
    ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
    ADC10CTL1 = INCH_0;                          // input A0
    ADC10AE0 |= INPUT;                            // PA.1 ADC option select
 
    while(1)
    {
        ADC10CTL0 |= ENC + ADC10SC;            // Sampling and conversion start
        __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
        TA0CCR1 = (ADC10MEM*65535)/1024;
    }
}
 
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
    __bic_SR_register_on_exit(CPUOFF);        // Clear CPUOFF bit from 0(SR)
}
Chân cẳng thì nhìn 2 cái define mà xác định hoặc thay đổi tùy ý.
 

doancongthang

Thành Viên PIF
Cho e hỏi vấn đề cũ, e đang xài con 28 chân và muốn dùng PORT3 của nó để xuất ra LCD thì mình defind file LCD.C như thế nào, e đã sửa P2 thành P3 trong LCD.C rồi, sửa trong thanh ghi PSEL trong chương trình chính nữa, nhưng nó chẳng ra cái gì cả. Và cho e hỏi ý nghĩa các dòng này nó có liên quan gì tới việc xuất ra LCD bằng PORT 3 ko?
union reg* P2_dir = (union reg*)0x2a;
union reg* P2_out = (union reg*)0x29;

union reg* P1_dir = (union reg*)0x22;
union reg* P1_out = (union reg*)0x21;
 

zealotnt

Cố Vấn CLB
nếu xem trong user guide trang 341 của g2553 thì mình nghĩ bạn có thể tự define địa chỉ của port3dir và port3out. sau đó có thể sử dụng các chân mcu để sử dụng bình thường.
code có thể như sau (mình ko có con 28 chân nên ko thử đc)
union reg* P3_dir = (union reg*)0x1a;
union reg* P3_out = (union reg*)0x19;
và các dòng define chân out và dir ở dưới sửa tương tự
 
Top