我只是想在通过蓝牙发送之前在我的微控制器(C语言)中将ASCII转换为十进制。

Control + F并在此库中找到ASCII。
http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_C18_Libraries_51297f.pdf

这是我代码的相关部分。

PIR1bits.ADIF=0;
while(x ==1)
{
ConvertADC(); //ADCONbits.GO/DONE=1 , Starts the A/D conversion process.
while(BusyADC()){}                  // Wait for completion

Delay10KTCYx(0);
Delay10KTCYx(0);
Delay10KTCYx(0);
Delay10KTCYx(0);
while(BusyUSART());
putcUSART(ADRESH); //prints or places the character into bluetooth
while(BusyUSART());
putcUSART(ADRESL); //prints or places the character into bluetooth
PIR1bits.ADIF=0;
}
//CloseADC();       // Disable A/D converter

}


我尝试过的。

1)atob(ADRESH)atob(ADRESL)

2)atof(ADRESH)atof(ADRESL)

3)atoi(ADRESH)atoi(ADRESL)

4)atol(ADRESH)atol(ADRESL)

5)putCUSART(atob / f / i / l(ADRESH))putCUSART(atob / f / i / l(ADRESL)

这是我的完整代码

/*********************************************************************
 *
 *      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>
#include <stdlib.h>

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


/** V A R I A B L E S **********************************************/
#pragma idata
int i=0;        //number of characters of command
int x=1;
int y=1;
/** 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;

                                while(x ==1)
                                {

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

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

                //putcUSART('\n');
                                Delay10KTCYx(0);
                                Delay10KTCYx(0);
                                Delay10KTCYx(0);
                                Delay10KTCYx(0);
                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)
        {

        //INSERT YOUR MAIN CODE HERE

        }



}//end main





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

最佳答案

ADRESHADRESL都是8位字节变量。它们需要转换为ASCII字符串。

atob() / atof() / atoi() / atol()将ASCII字符串转换为字节/浮点数/整数/长值。您向他们传递一个字符串,然后他们返回一个数字。

char    atob(const char * s); // convert string to 8-bit signed character
double  atof(const char * s); // convert string to floating point value
int     atoi(const char * s); // convert string to 16-bit signed integer
long    atol(const char * s); // convert string to long integer representation


您要使用btoa() / itoa() / ltoa()(字节/整数/长至ASCII)功能。您向他们传递要转换的值以及用于存储字符串的字符缓冲区。它们返回传递值的字符串表示形式。

char *  btoa(char value, char * string); // convert byte value to string
char *  itoa( int value, char * string); // convert integer value to string
char *  ltoa(long value, char * string); // convert long value to string


对于您的情况,您将要使用btoa()(如果将itoa()ADRESH结合使用,则使用ADRESL)和putsUSART()

char string[5];
while(BusyUSART());
putsUSART(btoa(ADRESH, string)); // prints high byte to bluetooth
while(BusyUSART());
putsUSART(btoa(ADRESL, string)); // prints low byte to bluetooth


要么

char string[7];
unsigned int value = (unsigned int) ADRESH * 256 + ADRESL; // reassemble reading
while(BusyUSART());
putsUSART(itoa(value, string)); // prints ADC reading to bluetooth


由于您正在使用字符串库(#include <string.h>),因此另一个选择是使用(sprintf())将格式化的输出打印到字符串。现在的代码是:

char string[9];
sprintf(string, "%d %d", ADRESH, ADRESL); // convert values to string
while(BusyUSART());
putsUSART(string); // prints ADC readings to bluetooth


要么

char string[7];
unsigned int value = (unsigned int) ADRESH * 256 + ADRESL; // reassemble reading
sprintf(string, "%d", value); // convert value to string
while(BusyUSART());
putsUSART(string); // prints ADC reading to bluetooth

08-26 10:06