1

我正在使用 Uiceblue2 集成蓝牙板。它适用于 MPLAB 上的 C18 编译器。它完全用 C 编写。该程序有一个模板,但由于对这个模块几乎没有支持,我想在这个网站上提出这个问题。这是下面的模板,最后一个 while 语句是他们建议放置主要代码的地方。

我希望它向超级终端或码头灯发送一系列简单的数字。他们都没有收到我发送的数据的任何部分。它已经连接到超级终端,但它不发送任何数据。

    /*********************************************************************
     *
     *      Example User Program to be used with resident Bootloader
     *
     *********************************************************************
     * FileName:        main.c
     * File Version:    2.0, 15-February
     * Dependencies:    See INCLUDES section below
     * Processor:       PIC18
     * Compiler:        C18 2.30.01+
     * Company:         emxys Ltd. 
     *
     *              
     *
     * Copyright: 2009 emxys Ltd.  ALL RIGHTS RESERVED
     *
     * Notes: This is a basic template for PIC18F4550 running 
     *        uIceBlue module. 
     *
     ********************************************************************/

    #define PROGRAMMABLE_WITH_USB_HID_BOOTLOADER


    /** INCLUDES *******************************************************/

    #include <p18cxxx.h>
    #include <portb.h>
    #include <usart.h>
    #include <delays.h>
    #include <adc.h>
    #include <string.h>
    #include <stdio.h>

    /** CONFIGURATION **************************************************/


    /** V A R I A B L E S **********************************************/
    #pragma idata
    int i=0;        //number of characters of command


    /** PRIVATE PROTOTYPES *********************************************/

    void YourHighPriorityISRCode();
    void YourLowPriorityISRCode();


    /** VECTOR REMAPPING ***********************************************/
        #if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
            #define REMAPPED_RESET_VECTOR_ADDRESS           0x1000
            #define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS  0x1008
            #define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS   0x1018
        #endif

        #if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
            extern void _startup (void);        // See c018i.c in your C18 compiler dir
            #pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
            void _reset (void)
            {
                _asm goto _startup _endasm
            }
        #endif

        #pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
            void Remapped_High_ISR (void)
            {
                 _asm goto YourHighPriorityISRCode _endasm
            }

        #pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
            void Remapped_Low_ISR (void)
            {
                 _asm goto YourLowPriorityISRCode _endasm
            }

        #if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
        //Note: If this project is built while one of the bootloaders has
        //been defined, but then the output hex file is not programmed with
        //the bootloader, addresses 0x08 and 0x18 would end up programmed with 0xFFFF.
        //As a result, if an actual interrupt was enabled and occured, the PC would jump
        //to 0x08 (or 0x18) and would begin executing "0xFFFF" (unprogrammed space).  This
        //executes as nop instructions, but the PC would eventually reach the REMAPPED_RESET_VECTOR_ADDRESS
        //(0x1000 or 0x800, depending upon bootloader), and would execute the "goto _startup".  This
        //would effective reset the application.

        //To fix this situation, we should always deliberately place a 
        //"goto REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS" at address 0x08, and a
        //"goto REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS" at address 0x18.  When the output
        //hex file of this project is programmed with the bootloader, these sections do not
        //get bootloaded (as they overlap the bootloader space).  If the output hex file is not
        //programmed using the bootloader, then the below goto instructions do get programmed,
        //and the hex file still works like normal.  The below section is only required to fix this
        //scenario.
            #pragma code HIGH_INTERRUPT_VECTOR = 0x08
            void High_ISR (void)
            {
                 _asm goto REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS _endasm
            }
            #pragma code LOW_INTERRUPT_VECTOR = 0x18
            void Low_ISR (void)
            {
                 _asm goto REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS _endasm
            }
        #endif  //end of "#if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)||defined(PROGRAMMABLE_WITH_USB_LEGACY_CUSTOM_CLASS_BOOTLOADER)"

        #pragma code


        //These are your actual interrupt handling routines.
        #pragma interrupt YourHighPriorityISRCode
        void YourHighPriorityISRCode()
        {
            unsigned char buffer[40];

            if (PIR1bits.RCIF == 1)    //USART Interruption
            {
                buffer[i]= getcUSART();     //characters of the commands. 

                if (buffer[i]=='\n')
                    buffer[i]='\r';

                if (i<4 & buffer[i]=='\r')
                {   
                    RCREG=0;            // clear buffer
                    TXREG=0;      
                    i=0;
                    PIR1bits.RCIF =0;
                    _asm RETFIE 0x0 _endasm

                }
                else  if (i==4 & buffer[4]=='\r')
                {

            /** PORTD commands *************************************************************/

            //COMMAND: 0xhi or 0xHI : set bit x from PORTD

                    if ((buffer [0] == '0' & buffer[2] == 'h' & buffer[3]== 'i') || (buffer[0] == '0' & buffer[2] == 'H' & buffer[3]== 'I'))
                    {
                        switch (buffer[1])
                        {
                            case '0':
                                PORTDbits.RD0 = 1;
                                break;
                            case '1':
                                PORTDbits.RD1 = 1;
                                break;
                            case '2':
                                PORTDbits.RD2 = 1;
                                break;
                            case '3':
                                PORTDbits.RD3 = 1;
                                break;
                            case '4':
                                PORTDbits.RD4 = 1;
                                break;
                            case '5':
                                PORTDbits.RD5 = 1;
                                break;
                            case '6':
                                PORTDbits.RD6 = 1;
                                break;
                            case '7':
                                PORTDbits.RD7 = 1;
                                break;
                        }
                        putcUSART('#');
                        WriteUSART(buffer[1]);
                        putrsUSART("HI");
                    }

            //COMMAND: 0xlo or 0xLO : clear bit x from PORTD

                    if ((buffer[0] == '0' & buffer[2] == 'l' & buffer[3]== 'o') || (buffer [0] == '0' & buffer[2] == 'L' & buffer[3]== 'O'))
                    {

                        switch (buffer[1])
                        {
                            case '0':
                                PORTDbits.RD0 = 0;
                                break;
                            case '1':
                                PORTDbits.RD1 = 0;
                                break;
                            case '2':
                                PORTDbits.RD2 = 0;
                                break;
                            case '3':
                                PORTDbits.RD3 = 0;
                                break;
                            case '4':
                                PORTDbits.RD4 = 0;
                                break;
                            case '5':
                                PORTDbits.RD5 = 0;
                                break;
                            case '6':
                                PORTDbits.RD6 = 0;
                                break;
                            case '7':
                                PORTDbits.RD7 = 0;
                                break;
                        }
                        putcUSART('#');
                        WriteUSART(buffer[1]);
                        putrsUSART("LO");
                    }

            //COMMAND: aclr or ACLR : clear PORTD

                    if ((buffer[0] == 'a' & buffer[1] == 'c' & buffer[2]== 'l' & buffer[3] == 'r') || ( buffer[0] == 'A' & buffer[1] == 'C' & buffer[2]== 'L' & buffer[3] == 'R'))
                    {       
                        PORTD= 0x00;
                    }

            //COMMAND: aset or ASET : set PORTD

                    if ((buffer[0] == 'a' & buffer[1] == 's' & buffer[2]== 'e' & buffer[3] == 't') || ( buffer[0] == 'A' & buffer[1] == 'S' & buffer[2]== 'E' & buffer[3] == 'T'))
                    {   
                        PORTD= 0xFF;
                    }

            //COMMAND: dqst or DQST : query state of PORTD      

                    if ((buffer[0] == 'd' & buffer[1] == 'q' & buffer[2]== 's' & buffer[3] == 't') || ( buffer[0] == 'D' & buffer[1] == 'Q' & buffer[2]== 'S' & buffer[3] == 'T'))
                    {
                        putcUSART('#');
                        WriteUSART(PORTD);
                    }

            //COMMAND: dldx or DLDx : load the value x on PORTD

                    if ((buffer[0] == 'd' & buffer[1]== 'l' & buffer[2] == 'd') || ( buffer[0] == 'D' & buffer[1]== 'L' & buffer[2] == 'D'))
                    {
                        PORTD= buffer[3];
                    }


            /** PORTB commands *************************************************************/       

            //COMMAND: bqst or BQST : query state of PORTB

                    if ((buffer[0] == 'b' & buffer[1] == 'q' & buffer[2]== 's' & buffer[3] == 't') || ( buffer[0] == 'B' & buffer[1] == 'Q' & buffer[2]== 'S' & buffer[3] == 'T'))
                    {
                        putcUSART('#');
                        WriteUSART(PORTB);
                    }


            //COMMAND: 0xst or 0xST : show the state of bit x from PORTB

                    if ((buffer[0] == '0' & buffer[2]== 's' & buffer[3] == 't') || ( buffer[0] == '0' & buffer[2]== 'S' & buffer[3] == 'T'))
                    {
                        unsigned status;
                        switch (buffer[1])
                        {
                            case '0':
                                status = PORTBbits.RB0;
                                break;
                            case '1':
                                status = PORTBbits.RB1;
                                break;
                            case '2':
                                status = PORTBbits.RB2;
                                break;
                            case '3':
                                status = PORTBbits.RB3;
                                break;
                            case '4':
                                status = PORTBbits.RB4;
                                break;
                            case '5':
                                status = PORTBbits.RB5;
                                break;
                            case '6':
                                status = PORTBbits.RB6;
                                break;
                            case '7':
                                status = PORTBbits.RB7;
                                break;
                        }
                        putcUSART('#');
                        WriteUSART(buffer[1]);
                        if (status)
                            putrsUSART("HI");
                        else 
                            putrsUSART("LO");


                    }


            /** AD converter commands *************************************************************/

            //COMMAND: 0xad or 0xAD : x will be the input for the AD converter

                    if ((buffer[0] == '0' & buffer[2]== 'a' & buffer[3] == 'd') || ( buffer[0] == '0' & buffer[2]== 'A' & buffer[3] == 'D'))
                    {

                        switch (buffer[1])
                        {
                            case '0':
                                SetChanADC( ADC_CH0 );
                                break;
                            case '1':
                                SetChanADC( ADC_CH1 );
                                break;
                            case '2':
                                SetChanADC( ADC_CH2 );
                                break;
                            case '3':
                                SetChanADC( ADC_CH3 );
                                break;
                            case '4':
                                SetChanADC( ADC_CH4 );
                                break;
                            case '5':
                                SetChanADC( ADC_CH5 );
                                break;
                            case '6':
                                SetChanADC( ADC_CH6 );
                                break;
                            case '7':
                                SetChanADC( ADC_CH7 );
                                break;
                        }

                    PIR1bits.ADIF=0;

                    ConvertADC();       //ADCONbits.GO/DONE=1 , Starts the A/D conversion process.  

                    while(BusyADC())
                    {}                  // Wait for completion

                    putcUSART('#');
                    while(BusyUSART()); 
                    putcUSART(ADRESH); 
                    while(BusyUSART()); 
                    putcUSART(ADRESL);

                    PIR1bits.ADIF=0;

                    //CloseADC();       // Disable A/D converter

                    }
                i=-1;
                PIR1bits.RCIF =0;
                }
                else if (i>4 & buffer[i]=='\r')
                {
                    RCREG=0;            //Clear the buffer
                    TXREG=0;      
                    i=0;
                    PIR1bits.RCIF =0;   
                    _asm RETFIE 0x0 _endasm 
                }       

                i++;

    }


        }   //This return will be a "retfie fast", since this is in a #pragma interrupt section 
        #pragma interruptlow YourLowPriorityISRCode
        void YourLowPriorityISRCode()
        {
            //Check which interrupt flag caused the interrupt.
            //Service the interrupt
            //Clear the interrupt flag
            //Etc.





        }   //This return will be a "retfie", since this is in a #pragma interruptlow section 


    /** DECLARATIONS ***************************************************/
    #pragma code


    /******************************************************************************
     * Function:        void main(void)
     *
     * PreCondition:    None
     *
     * Input:           None
     *
     * Output:          None
     *
     * Side Effects:    None
     *
     * Overview:        Main program entry point.
     *
     * Note:            None
     *****************************************************************************/

    void main(void)
    {
        //Configure Ports : 1 is input, 0 is output

        TRISA=1;  
        TRISB=1;
        PORTB=0;
        TRISD=0;
        PORTD=0; 
        TRISE=1;

    //Configure Interruptions

        INTCONbits.GIE=1;   // Global Interruption Enable bit
        INTCONbits.PEIE=1;  // Peripheral Interruption Enable bit
        //PIE1bits.TXIE=1;  // EUSART Transmit Interruption Enable bit
        PIE1bits.RCIE=1;    // EUSART Receive Interruption Enable bit
        IPR1bits.RCIP=1;    // EUSART Receive Interruption Priority bit

    //Configure USART

        //configure RC6/TX/CK and RC7/RX/DT/SDO as an EUSART
        TRISCbits.TRISC6=1;
        TRISCbits.TRISC7=1;
        RCSTAbits.SPEN=1;   //Serial Port Enable bit
        BAUDCONbits.BRG16=0;//16-Bit Baud Rate Register Enable bit


        /* EUSART BAUD RATE CONFIGURATION ***********************************
        ;OpenUSART(...,SPBRG) configure the USART                           
        ;                                                                   
        ; SPBRG value =>    312 @ 9600    bauds                             
        ;                   155 @ 19200   bauds                             
        ;                   77  @ 38400   bauds                             
        ;                   51  @ 57600   bauds                             
        ;                   25  @ 115200  bauds                             
        ;                   12  @ 230400  bauds                             
        ;                   6   @ 460800  bauds                             
        ;                   2   @ 921600  bauds                                 
        ;                                                                   
        ; Check the MPLAB C18 Libraries and the 18F4550 datasheet for more  
        ; information                                                                 
        ********************************************************************/

        OpenUSART(USART_TX_INT_OFF &        //Interruption on Transmission
                    USART_RX_INT_ON &       //Interruption on Receipt
                    USART_ASYNCH_MODE &     //USART Mode
                    USART_EIGHT_BIT &       //Transmission Width
                    USART_CONT_RX &         //Reception mode
                    USART_BRGH_HIGH, 155);  //High Baud Rate Select bit, Baud rate at which the USART operates


    //Configure AD converter

        ADCON0bits.ADON=1;      //Enable AD converter

        /* AD converter configuration *******************************************
        ;OpenADC(...,portconfig) configure the AD converter                     
        ;                                                                       
        ;   portconfig value => A/D Port Configuration Control bits in decimal 
        ;                                                                       
        ; The channel for the A/D conversion is selected by a command           
        ;                                                                       
        ; Default Vref+ = Vdd, default Vref- = Vss                              
        ;                                                                       
        ; Check the MPLAB C18 Libraries and the 18F4550 datasheet for more      
        ; information                                                                     
        ************************************************************************/

        OpenADC( ADC_FOSC_64 &              //clock source
                ADC_RIGHT_JUST &            //result justification
                ADC_20_TAD,                 //acquisition time selected must be TAD>0,7us. 
                ADC_INT_OFF &               //interruptions off
                ADC_VREFPLUS_VDD &          //Voltage reference
                ADC_VREFMINUS_VSS, 7 );     //Port configuration is any value from 0 to 15 inclusive


        while(1)
//PUT MAIN CODE HERE
            {
    int sum,i;
        char str[7];
    sum = 0;

    for(i=0; i<100; i++)
    {
    sum = sum + i;
    ultoa( sum, str); // convert sum to string
    putsUSART( str); //Print to USART   

    }
    }

    }//end main




/** EOF main.c *************************************************************/
4

0 回答 0