You are on page 1of 5

/********************************************************************** * ?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, WHETHE R * IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY) , * STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPE CIAL, * PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXP ENSE OF * ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP H AS BEEN * ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXT ENT * 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 SPECIFICA LLY 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 24FJ256GB1 10 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 pr otect of all memory regions. #elif defined (__PIC24FJ256GA110__) //Defined by MPLAB when using 24FJ256GA1 10 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 pr otect 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_PG x2) // 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 #define FALSE 1 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 set s // 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; U2MODEbits.RTSMD = 0; U2MODEbits.UEN = 0; U2MODEbits.WAKE = 0; U2MODEbits.LPBACK = 0; U2MODEbits.ABAUD = 0; U2MODEbits.RXINV = 0; U2MODEbits.BRGH = 0; U2MODEbits.PDSEL = 0; U2MODEbits.STSEL = 0; U2BRG = BAUDRATEREG2;

// Bit12 No IR translation // Bit11 Simplex Mode // Bits8,9 TX,RX enabled, CTS,RTS not // Bit7 No Wake up (since we don't sleep here) // Bit6 No Loop Back // Bit5 No Autobaud (would require sending '55') // Bit4 IdleState = 1 // Bit3 16 clocks per bit period // Bits1,2 8bit, No Parity // Bit0 One Stop Bit // baud rate

// Load all values in for U1STA SFR U2STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config !) U2STAbits.UTXINV = 0; U2STAbits.UTXISEL0 = 0; U2STAbits.UTXBRK = 0; U2STAbits.UTXEN = 0; U2STAbits.UTXBF = 0; U2STAbits.TRMT = 0; U2STAbits.URXISEL = 0; U2STAbits.ADDEN = 0; U2STAbits.RIDLE = 0; U2STAbits.PERR = 0; U2STAbits.FERR = 0; U2STAbits.OERR = 0; U2STAbits.URXDA = 0; IFS1bits.U2TXIF IEC1bits.U2TXIE IFS1bits.U2RXIF IEC1bits.U2RXIE = = = = 0; 1; 0; 1; //Bit14 N/A, IRDA config //Bit13 Other half of Bit15 //Bit11 Disabled //Bit10 TX pins controlled by periph //Bit9 *Read Only Bit* //Bit8 *Read Only bit* //Bits6,7 Int. on character recieved //Bit5 Address Detect Disabled //Bit4 *Read Only Bit* //Bit3 *Read Only Bit* //Bit2 *Read Only Bit* //Bit1 *Read Only Bit* //Bit0 *Read Only Bit* // // // // Clear the Transmit Interrupt Flag Enable Transmit Interrupts Clear the Recieve Interrupt Flag Enable Recieve Interrupts

U2MODEbits.UARTEN = 1; // And turn the peripheral on U2STAbits.UTXEN = 1; } void InitPorts(void) { // S3 (portD // S6 (portD // S5 (portA // S4 (portD

Pin Pin Pin Pin

6, chosen as trigger for sending 'M' to UART) 7, chosen as trigger for sending 'C' to UART) 7, chosen as trigger for sending 'H' to UART) 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(); InitPorts();

// Initialize UART2 for 9600,8,N,1 TX/RX // LEDs outputs, Switches Inputs

while(1) { // The ever versatile Infinite Loop! for (i = 0; i < 100; i++); SoftwareDebounce(); } }

You might also like