close
這個範例是使用Microchip EVM:APP001的實驗板搭配MPLAB x IDE + MCC寫程式的功能
 
程式這次需要完成幾個功能讀取可變電阻的電壓值經由AD轉換,
再將AD值透過Uart傳至PC終端機
 
首先先完成讀取可變電阻的電壓值在經由AD轉換的功能
 
翻閱線路圖,查詢可變電阻的的電壓是接到PIC18F45K22的RA0腳位
 
 
 
不過在開始寫功能之前,先研讀PIC18F45K22 Datasheet瞭解Analog相關的規格
ADC module
ADC module是把類比輸入訊號轉換成10-bit寬的數位訊號
ADC module再使用前會選取類比通道去抓取類比訊號,經由取樣完後,並且hold住
再將hold住的結果連接至轉換的輸入,經由連續逼近法產生10-bit寬的結果
並且將結果存在暫存器(ADRESL和ADRESH)
 
ADC的電壓參考可以經由軟體設定,可以選擇電壓參考
為VDD
或是
連接到外部pin腳的參考電壓
 
ADC可以產生中斷當轉換完成時。並且這中斷可以用來wake-up from sleep.
 
Shows the block diagram of the ADC
 
ADC Configuration
當要使用ADC的功能,有些暫存器需要先設定
  • Port Configuration
Register ANSELx和TRISx是用來設定A/D Port pin
 
Note:
1.當GPIO設成Digital Input將可以提高AD轉換的精準度
2.然而使用Digital Input會造成一些電流的消耗
 
  • CHANNEL SELECTION
Register ADCON0內的CHS bits用來決定要用哪一個AD Channel
當改變Channel時,會有一段delay是需要的
 
  • ADC VOLTAGE REFERENCE
Register ADCON1內的PVCFG<1:0>和NVCFG<1:0>bits可用來決定參考正或負的電壓源
正的電壓源有
1.VDD
2.FVR BUF2(the fixed voltage reference)
3.VREF+(an external voltage source)
負的電壓源有
1.VSS
2.VREF-(an external voltage source )
 
  • SELECTING AND CONFIGURING ACQUISITION TIME
Register ADCON2 允許使用者在每次GO/DONE bit被set時去選擇一個acquisition time
我們要設定Acquisition time藉由設定ACQT<2:0> bits, 這個位於ADCON2暫存器內。可以設定delay
time範圍為2 ~ 20 TAD。
 
當GO/DONE bit被設定,A/D Module在acquisition time時間期間取樣類比輸入,然後再開始轉換
  • CONVERSION CLOCK
Register ADCON2內的ADCS用來決定the source of the conversion clock
有七種clock options
FOSC/2
FOSC/4
FOSC/8
FOSC/16
FOSC/32
FOSC/64
Frc(dedicated internal oscillator)
 
The time to complete one bit conversion is defined as TAD.
One full 10-bit conversion requires 11 TAD periods.
 
 
Note: 除非使用Frc,否則只要有動到系統主頻,也將同時改變ADC clock frequency,這有可能會影響到
ADC的結果。
 
 
 
 
 
瞭解完ADC的相關規格後,開始使用MCC來設置ADC
使用MCC來設定ADC,這邊要使用PIC18F45K22的RA0當作類比輸入源
 
上圖使用MCC有些步驟
1.先在Device Resources找到ADC並且Double-Click
2.滑鼠點選Project Resources將會在右邊出現ADC的參數設定畫面
3.把Result Alignment改成right
4.在上述位置勾選
 
更改名稱
滑鼠點選Pin Module
在Custom Name換成POT_CHANNEL
 
 
 
使用MCC產生ADC的code之後,使用ADC可以這樣用
但目前還無法直接看轉換值,所以我是使用PICkit 3 Debugger來看adcResult是否有轉換成功的值被更新
 
 
/**
  Generated Main Source File
 
  Company:
    Microchip Technology Inc.
 
  File Name:
    main.c
 
  Summary:
    This is the main file generated using MPLAB(c) Code Configurator
 
  Description:
    This header file provides implementations for driver APIs for all modules selected in the GUI.
    Generation Information :
        Product Revision  :  MPLAB(c) Code Configurator - v3.00
        Device            :  PIC18F45K22
        Driver Version    :  2.00
    The generated drivers are tested against the following:
        Compiler          :  XC8 1.35
        MPLAB             :  MPLAB X 3.20
*/
 
/*
Copyright (c) 2013 - 2015 released Microchip Technology Inc.  All rights reserved.
 
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
 
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
 
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
 */
 
#include "mcc_generated_files/mcc.h"
 
uint16_t adcResult;
/*
                         Main application
 */
void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();
 
    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:
 
    // Enable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighEnable();
 
    // Enable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowEnable();
 
    // Disable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighDisable();
 
    // Disable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowDisable();
 
    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();
 
    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();
 
    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();
 
    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
    ADC_StartConversion(POT_CHANNEL);
    while (1)
    {
        // Add your application code
        if(ADC_IsConversionDone())
        {
            adcResult = ADC_GetConversionResult();
            ADC_StartConversion(POT_CHANNEL);
        }
    }
}
/**
 End of File
*/
 
接下來開始使用MCC設置UART
Device Resources內Double-click EUSART1,並在Project Resources點選EUSART1,就會跑出右邊的UART設定畫面
 
UART參數設定如下圖
設成asynchronous
啟動Tx
啟動Rx
會在相關pin腳設定為Tx與Rx
 
Redirect STDIO to USART會把printf之類的標準函式用UART1的硬體輸出之類的
 
 
研讀PIC18F45K22 datasheet EUSART
EUSART module是一個serial I/O communications peripheral
包含clock generators, shift registers and data buffers來實現串列輸出與輸入的功能
ECSART可以被設成full-duplex asynchronous system, half-duplex synchronous system
 
Full-Duplex 模式常用於與CR終端機溝通
Half-Duplex synchronous模式與A/D或D/A integrated circuits, serial EEPROMs, other microcontroller
這些裝置典型沒有內部clock來產生baud rate,必須使用一個master sychronous device提供外部clock signal
 
 
 
EUSART module的操作經由三個register
TXSTAx
RCSTAx
BAUDCONx
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 凱文先生的部落格 的頭像
    凱文先生的部落格

    凱文的八卦俱樂部小天地

    凱文先生的部落格 發表在 痞客邦 留言(0) 人氣()