




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/* ?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 Microchips* 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 MICROCHIPS * 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, MICROCHIPS 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 TRUE1#define FALSE0unsigned 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 U2MODEU2MODEbits.UARTEN = 0;/ Bit15 TX, RX DISABLED, ENABLE at end of funcU2MODEbits.USIDL = 0;/ Bit13 Continue in IdleU2MODEbits.IREN = 0;/ Bit12 No IR translationU2MODEbits.RTSMD = 0;/ Bit11 Simplex ModeU2MODEbits.UEN = 0;/ Bits8,9 TX,RX enabled, CTS,RTS notU2MODEbits.WAKE = 0;/ Bit7 No Wake up (since we dont sleep here)U2MODEbits.LPBACK = 0;/ Bit6 No Loop BackU2MODEbits.ABAUD = 0;/ Bit5 No Autobaud (would require sending 55)U2MODEbits.RXINV = 0;/ Bit4 IdleState = 1U2MODEbits.BRGH = 0;/ Bit3 16 clocks per bit periodU2MODEbits.PDSEL = 0;/ Bits1,2 8bit, No ParityU2MODEbits.STSEL = 0;/ Bit0 One Stop BitU2BRG = BAUDRATEREG2;/ baud rate/ Load all values in for U1STA SFRU2STAbits.UTXISEL1 = 0;/Bit15 Int when Char is transferred (1/2 config!)U2STAbits.UTXINV = 0;/Bit14 N/A, IRDA configU2STAbits.UTXISEL0 = 0;/Bit13 Other half of Bit15U2STAbits.UTXBRK = 0;/Bit11 DisabledU2STAbits.UTXEN = 0;/Bit10 TX pins controlled by periphU2STAbits.UTXBF = 0;/Bit9 *Read Only Bit*U2STAbits.TRMT = 0;/Bit8 *Read Only bit*U2STAbits.URXISEL = 0;/Bits6,7 Int. on character recievedU2STAbits.ADDEN = 0;/Bit5 Address Detect DisabledU2STAbits.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 FlagIEC1bits.U2TXIE = 1;/ Enable Transmit InterruptsIFS1bits.U2RXIF = 0;/ Clear the Recieve Interrupt FlagIEC1bits.U2RXIE = 1;/ Enable Recieve InterruptsU2MODEbits.UARTEN = 1;/ And turn the peripheral onU2STAbits.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 inputsTRISA = 0x0080;/ only 0th bit needs be output. A7 is inputS3Flag = S4Flag = S5Flag = S6Flag = 0;/ Some Debounce Flagsvoid 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 TimerRCONbits.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 RP10RPINR19bits.U2RXR = 10;/ Configure Output Functions */ Assign UART2TX To Pin RP17RPOR8bits.RP17R = U2TX_IO;/ Lock Registers_builtin_write_OSCCONL(OSCCON | 0x40);#endifInitUART2();/ Initialize UART2 for 9600,8,N,1 TX/RXInitPorts();/ LEDs outputs, Switches Inputswhile(1) / The ever versatile Infinite Loop!for (i = 0; i 255#error Cannot set up UART2 for the SYSCLK and BAUDRATE. Correct values in main.h and uart2.h files.#endif#define BAUDRATE_MISTAKE 1000*(BAUDRATE2-SYSCLK
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第三节 生活中的圆周运动说课稿-2025-2026学年高中物理粤教版2019必修 第二册-粤教版2019
- 第5课 四点底与皿字底教学设计-2025-2026学年小学书法练习指导五年级上册西泠版
- 化肥厂咨询供应商考核办法
- 2025合同样本:网络直播主播合同示范文本
- 广东省廉江市实验学校高中政治 6.2 股票、债券和保险1说课稿(必修1)
- 第1课 走进人工智能 说课稿- 2024-2025学年浙教版(2023)初中信息技术八年级下册
- 木材销售合同
- 第3节 测量液体和固体的密度说课稿-2025-2026学年初中物理人教版2024八年级上册-人教版2024
- 2.2.2大气热力环流-教学设计2023-2024学年高中地理人教版(2019)必修一
- 古诗词诵读《桂枝香·金陵怀古》教学设计高中语文必修下册同步教学设计(统编版2019)
- 宏村简介课件
- 潍坊市2026届高三开学调研监测考试数学试题及答案
- 车辆产品公告管理办法
- 2025喀什经济开发区兵团分区招聘(10人)考试参考试题及答案解析
- 2025江西南昌市西湖城市建设投资发展集团有限公司及下属子公司招聘40人考试参考试题及答案解析
- 2024教科版一年级科学上册全册教案设计
- (2025秋新版)外研版八年级英语上册全册教案
- 汽车维修工具使用教学设计
- 医学影像阅片肺部课件
- 数据备份课件
- 反洗钱身份识别培训课件
评论
0/150
提交评论