|
3線USART程序,MPLAB
0.png (44.32 KB, 下載次數(shù): 38)
下載附件
2018-7-3 17:28 上傳
源程序如下:
- /**********************************************************************
- * ?2008 Microchip Technology Inc.
- *
- * FileName: main.c
- * Dependencies: uart2.h
- * Processor: PIC24F
- * Compiler: MPLAB?C30
- * Tested On: PIC24F Explorer 16
- *
- * SOFTWARE LICENSE AGREEMENT:
- * Microchip Technology Incorporated ("Microchip") retains all ownership and
- * intellectual property rights in the code accompanying this message and in all
- * derivatives hereto. You may use this code, and any derivatives created by
- * any person or entity by or on your behalf, exclusively with Microchip's
- * proprietary products. Your acceptance and/or use of this code constitutes
- * agreement to the terms and conditions of this notice.
- *
- * CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS". NO
- * WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
- * TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIP'S
- * PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
- *
- * YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER
- * IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY),
- * STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL,
- * PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF
- * ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN
- * ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
- * ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO
- * THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO
- * HAVE THIS CODE DEVELOPED.
- *
- * You agree that you are solely responsible for testing the code and
- * determining its suitability. Microchip has no obligation to modify, test,
- * certify, or support the code.
- *
- * REVISION HISTORY:
- *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * Author Date Comments on this revision
- *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * Albert Z. 12/26/08 Original Release
- *
- *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * ADDITIONAL NOTES:
- * Small, bare bones program to guide designers with UART implementation
- * To run the example, plug DB9 cable to Terminal (9600,8,N,1) -
- * Data sent from TERMINAL will be represented as binary equivalent
- * of ASCII on 7 LSB LEDs of Explorer 16 development board
- *
- * Data is sent from Explorer 16 by pressing S3 - S6. A single letter will
- * be transmitted upon each keypress.
- *
- * Peripheral Library was not utilized, Bit Addressing was used in order
- * to show all details involved in initialization of UART.
- *
- * This code example has been tested on Explorer 16 Development Board
- * with PIC24FJ128GA010, PIC24FJ256GA110 and PIC24FJ256GB110 PIMs.
- **********************************************************************/
- #include "p24fxxxx.h"
- #include "uart2.h"
- #if defined (__PIC24FJ256GB110__) //Defined by MPLAB when using 24FJ256GB110 device
- _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
- _CONFIG2( IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI & PLLDIV_DIV2 & IOL1WAY_ON)
- _CONFIG3( WPCFG_WPCFGDIS & WPDIS_WPDIS) //Disable erase/write protect of all memory regions.
- #elif defined (__PIC24FJ256GA110__) //Defined by MPLAB when using 24FJ256GA110 device
- _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
- _CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI & IOL1WAY_ON)
- _CONFIG3( WPCFG_WPCFGDIS & WPDIS_WPDIS) //Disable erase/write protect of all memory regions.
- #elif defined (__PIC24FJ128GA010__)
- // JTAG/Code Protect/Write Protect/Clip-on Emulation mode
- // Watchdog Timer/ICD pins select
- _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
- // Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator,
- // Primary oscillator
- _CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI)
- #endif
- #define TRUE 1
- #define FALSE 0
- unsigned char S3Flag, S4Flag, S5Flag, S6Flag;
- void __attribute__ ((interrupt, no_auto_psv)) _U2RXInterrupt(void) {
- LATA = U2RXREG;
- IFS1bits.U2RXIF = 0;
- }
- void __attribute__ ((interrupt, no_auto_psv)) _U2TXInterrupt(void) {
- IFS1bits.U2TXIF = 0;
- }
- void InitUART2(void)
- {
- // This is an EXAMPLE, so brutal typing goes into explaining all bit sets
- // The Explorer 16 board has a DB9 connector wired to UART2, so we will
- // be configuring this port only
- // configure U2MODE
- U2MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
- U2MODEbits.USIDL = 0; // Bit13 Continue in Idle
- U2MODEbits.IREN = 0; // Bit12 No IR translation
- U2MODEbits.RTSMD = 0; // Bit11 Simplex Mode
- U2MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
- U2MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
- U2MODEbits.LPBACK = 0; // Bit6 No Loop Back
- U2MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
- U2MODEbits.RXINV = 0; // Bit4 IdleState = 1
- U2MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
- U2MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
- U2MODEbits.STSEL = 0; // Bit0 One Stop Bit
-
- U2BRG = BAUDRATEREG2; // baud rate
- // Load all values in for U1STA SFR
- U2STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
- U2STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
- U2STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
- U2STAbits.UTXBRK = 0; //Bit11 Disabled
- U2STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
- U2STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
- U2STAbits.TRMT = 0; //Bit8 *Read Only bit*
- U2STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
- U2STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
- U2STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
- U2STAbits.PERR = 0; //Bit3 *Read Only Bit*
- U2STAbits.FERR = 0; //Bit2 *Read Only Bit*
- U2STAbits.OERR = 0; //Bit1 *Read Only Bit*
- U2STAbits.URXDA = 0; //Bit0 *Read Only Bit*
- IFS1bits.U2TXIF = 0; // Clear the Transmit Interrupt Flag
- IEC1bits.U2TXIE = 1; // Enable Transmit Interrupts
- IFS1bits.U2RXIF = 0; // Clear the Recieve Interrupt Flag
- IEC1bits.U2RXIE = 1; // Enable Recieve Interrupts
- U2MODEbits.UARTEN = 1; // And turn the peripheral on
- U2STAbits.UTXEN = 1;
- }
- void InitPorts(void)
- {
- // S3 (portD Pin 6, chosen as trigger for sending 'M' to UART)
- // S6 (portD Pin 7, chosen as trigger for sending 'C' to UART)
- // S5 (portA Pin 7, chosen as trigger for sending 'H' to UART)
- // S4 (portD Pin 13, chosen as trigger for sending 'P' to UART)
- TRISD = 0x20C0; // D6,7,13 inputs
- TRISA = 0x0080; // only 0th bit needs be output. A7 is input
- S3Flag = S4Flag = S5Flag = S6Flag = 0; // Some Debounce Flags
- }
- void SoftwareDebounce(void)
- {
- if(PORTDbits.RD6 == FALSE) {
- if( S3Flag == FALSE ) {
- S3Flag = TRUE;
- }
- } else {
- if ( S3Flag == TRUE ) {
- U2TXREG = 'M';
- S3Flag = FALSE;
- }
- }
- if(PORTDbits.RD7 == FALSE) {
- if( S6Flag == FALSE ) {
- S6Flag = TRUE;
- }
- } else {
- if ( S6Flag == TRUE ) {
- U2TXREG = 'C';
- S6Flag = FALSE;
- }
- }
- if(PORTAbits.RA7 == FALSE) {
- if( S5Flag == FALSE ) {
- S5Flag = TRUE;
- }
- } else {
- if ( S5Flag == TRUE ) {
- U2TXREG = 'H';
- S5Flag = FALSE;
- }
- }
- if(PORTDbits.RD13 == FALSE) {
- if( S4Flag == FALSE ) {
- S4Flag = TRUE;
- }
- } else {
- if ( S4Flag == TRUE ) {
- U2TXREG = 'P';
- S4Flag = FALSE;
- }
- }
- }
- int main(void)
- {
- unsigned char i;
- // Disable Watch Dog Timer
- RCONbits.SWDTEN = 0;
-
- // I/O remap, PPS
- #if defined (__PIC24FJ256GB110__) || defined (__PIC24FJ256GA110__)
- // remap pins before intialising SPI2
- // Unlock Registers
- __builtin_write_OSCCONL(OSCCON & 0xbf);
- // Configure Input Functions **********************
- // Assign UART2RX To Pin RP10
- RPINR19bits.U2RXR = 10;
- // Configure Output Functions *********************
- // Assign UART2TX To Pin RP17
- RPOR8bits.RP17R = U2TX_IO;
- // Lock Registers
- __builtin_write_OSCCONL(OSCCON | 0x40);
- #endif
- InitUART2(); // Initialize UART2 for 9600,8,N,1 TX/RX
- InitPorts(); // LEDs outputs, Switches Inputs
- while(1) { // The ever versatile Infinite Loop!
- for (i = 0; i < 100; i++);
- SoftwareDebounce();
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
3wire-uart.zip
(7.5 KB, 下載次數(shù): 5)
2018-7-3 16:08 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
|