用Pickit3编程PIC32MX250F128B

时间:2018-08-14 16:46:49

标签: pic pic32

我正在使用PIC32MX250F128B成功编程Pickit3。我编写了一个代码,当我按a时,将从振动传感器获取100个数据。现在,如果我想再获取100个数据,要么必须断开连接,然后重新连接连接到MCLR引脚的10k欧姆上拉电阻,要么必须再次运行程序。

我还有其他方法可以重置Pickit吗?

这是我正在使用的代码:

#include <p32xxxx.h>                // include chip specific header file
#include <plib.h>                   // include peripheral library functions

// Configuration Bits
#pragma config FNOSC = FRCPLL       // Internal Fast RC oscillator (8 MHz) w/ PLL
#pragma config FPLLIDIV = DIV_2     // Divide FRC before PLL (now 4 MHz)
#pragma config FPLLMUL = MUL_20     // PLL Multiply (now 80 MHz)
#pragma config FPLLODIV = DIV_2     // Divide After PLL (now 40 MHz)
                                    // see figure 8.1 in datasheet for more info
#pragma config FWDTEN = OFF         // Watchdog Timer Disabled
#pragma config ICESEL = ICS_PGx2    // ICE/ICD Comm Channel Select
#pragma config JTAGEN = OFF         // Disable JTAG
#pragma config FSOSCEN = OFF        // Disable Secondary Oscillator
#pragma config FPBDIV = DIV_1       // PBCLK = SYCLK

// Defines
#define SYSCLK 40000000L

// Macros
// Equation to set baud rate from UART reference manual equation 21-1
#define Baud2BRG(desired_baud)      ( (SYSCLK / (16*desired_baud))-1)

// Function Prototypes
int SerialTransmit(const char *buffer);
unsigned int SerialReceive(char *buffer); //, unsigned int max_size);
int UART2Configure( int baud);

short a2dvals[11000];
int adcptr,num_channels,k,i;
char sampling;
int ADC_RSLT0,totaldata,totaldata1,chunks_sent,data_count,l;
short temp;
BOOL a2don;
volatile unsigned int channel4;
void __ISR(_ADC_VECTOR, IPL2) TIMER3Handler(void) // Fonction d'interruption Timer 3
    {


        temp = ReadADC10(0);

        a2dvals[k] = (temp);
        k++;
        if (k>totaldata1)// && sampling == 's')
        {
            T3CONCLR = 0x8000;
            a2don=FALSE;
            chunks_sent = 0;
            totaldata = k/2;
            k = 1;

        }
     mAD1ClearIntFlag();
}

int main(void)
{
    char   buf[1024];       // declare receive buffer with max size 1024


    // Peripheral Pin Select
    U2RXRbits.U2RXR = 4;    //SET RX to RB8
    RPB9Rbits.RPB9R = 2;    //SET RB9 to TX

   SYSTEMConfigPerformance(SYSCLK);

    UART2Configure(9600);  // Configure UART2 for a baud rate of 9600
    U2MODESET = 0x8000;     // enable UART2

    ANSELBbits.ANSB2 = 1;   // set RB2 (AN4) to analog
    TRISBbits.TRISB2 = 1;   // set RB2 as an input

    //adcConfigureManual();   // Configure ADC
    //AD1CON1SET = 0x8000;    // Enable ADC


    SerialTransmit("Hello! Enter 'a' to do ADC conversion \r\n");

    unsigned int rx_size;

    while( 1){
        rx_size = SerialReceive(buf); //, 1024);     // wait here until data is received
        SerialTransmit(buf);                   // Send out data exactly as received


         SerialTransmit("\r\n");

    }



   return 1;
} // END main()

/* UART2Configure() sets up the UART2 for the most standard and minimal operation
 *  Enable TX and RX lines, 8 data bits, no parity, 1 stop bit, idle when HIGH
 * Input: Desired Baud Rate
 * Output: Actual Baud Rate from baud control register U2BRG after assignment*/
int UART2Configure( int desired_baud){

    U2MODE = 0;         // disable autobaud, TX and RX enabled only, 8N1, idle=HIGH
    U2STA = 0x1400;     // enable TX and RX
    U2BRG = Baud2BRG(desired_baud); // U2BRG = (FPb / (16*baud)) - 1

    // Calculate actual assigned baud rate
    int actual_baud = SYSCLK / (16 * (U2BRG+1));

    return actual_baud;
} // END UART2Configure()


/* SerialTransmit() transmits a string to the UART2 TX pin MSB first
 *
 * Inputs: *buffer = string to transmit */
int SerialTransmit(const char *buffer)
{
    unsigned int size = strlen(buffer);
    while( size)
    {
        while( U2STAbits.UTXBF);    // wait while TX buffer full
        U2TXREG = *buffer; // send single character to transmit buffer



        buffer++;                   // transmit next character on following loop
        size--;                     // loop until all characters sent (when size = 0)

    }

    while( !U2STAbits.TRMT);        // wait for last transmission to finish

    return 0;
}

/* SerialReceive() is a blocking function that waits for data on
 *  the UART2 RX buffer and then stores all incoming data into *buffer
 *
 * Note that when a carriage return '\r' is received, a nul character
 *  is appended signifying the strings end
 *
 * Inputs:  *buffer = Character array/pointer to store received data into
 *          max_size = number of bytes allocated to this pointer
 * Outputs: Number of characters received */
unsigned int SerialReceive(char *buffer) //, unsigned int max_size)
{
    //unsigned int num_char = 0;


    /* Wait for and store incoming data until either a carriage return is received
     *   or the number of received characters (num_chars) exceeds max_size */
    while(1)
    {
        while( !U2STAbits.URXDA);   // wait until data available in RX buffer
        *buffer = U2RXREG;  // empty contents of RX buffer into *buffer pointer



        if (*buffer == 'a')
        {

            int dummy,dummy1;
            unsigned char tempstr[5];


    SYSTEMConfig(SYSCLK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

        // the ADC ///////////////////////////////////////
        // configure and enable the ADC
    CloseADC10();   // ensure the ADC is off before setting the configuration

    // define setup parameters for OpenADC10
    // Turn module on | ouput in integer | trigger mode auto | enable autosample
        // ADC_CLK_AUTO -- Internal counter ends sampling and starts conversion (Auto convert)
        // ADC_AUTO_SAMPLING_ON -- Sampling begins immediately after last conversion completes; SAMP bit is automatically set
        // ADC_AUTO_SAMPLING_OFF -- Sampling begins with AcquireADC10();
        #define PARAM1  ADC_MODULE_ON|ADC_FORMAT_INTG32 | ADC_CLK_TMR | ADC_AUTO_SAMPLING_ON //

    // define setup parameters for OpenADC10
    // ADC ref external  | disable offset test | disable scan mode | do 1 sample | use single buf | alternate mode off
    #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
        //
    // Define setup parameters for OpenADC10
        // use peripherial bus clock | set sample time | set ADC clock divider
        // ADC_CONV_CLK_Tcy2 means divide CLK_PB by 2 (max speed)
        // ADC_SAMPLE_TIME_5 seems to work with a source resistance < 1kohm
        #define PARAM3 ADC_CONV_CLK_SYSTEM | ADC_SAMPLE_TIME_5 | ADC_CONV_CLK_Tcy2 //ADC_SAMPLE_TIME_15| ADC_CONV_CLK_Tcy2

    // define setup parameters for OpenADC10
    // set AN4 and  as analog inputs
    #define PARAM4  ENABLE_AN4_ANA 

    // define setup parameters for OpenADC10
    // do not assign channels to scan
    #define PARAM5  SKIP_SCAN_ALL

    // use ground as neg ref for A | use AN4 for input A     
    // configure to sample AN4 
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 ); // configure to sample AN4 
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above

    ConfigIntADC10(ADC_INT_PRI_2 | ADC_INT_ON);


    EnableADC10(); // Enable the ADC

       INTEnableSystemMultiVectoredInt();

    OpenTimer3(T3_OFF | T3_SOURCE_INT | T3_PS_1_1 ,0x3e8);

    num_channels = 1;
    totaldata1 = 10500;
    a2don=TRUE;
    T3CONSET = 0x8000;
    k=0;
    while(1)
    {       
        while(a2don);

        for(i=0;i<100;i++)
        {
            dummy = a2dvals[i]/1000 ;
            tempstr[0] = dummy + 0x30;
            dummy1 = a2dvals[i]- dummy*1000;
            dummy = dummy1/100;
            tempstr[1] = dummy + 0x30;
            dummy1 = dummy1 - dummy*100;
            dummy = dummy1/10;
            tempstr[2] = dummy + 0x30;
            dummy1 = dummy1 - dummy*10;
            tempstr[3] = dummy1 + 0x30;
            //tempstr[4] = "\0";

            printf("%c%c%c%c \n", tempstr[0],tempstr[1],tempstr[2],tempstr[3]);
        }
        a2don=TRUE;   
    }

        }

    }

    return 1;
}// END SerialReceive()

enter image description here

感谢您的建议。

2 个答案:

答案 0 :(得分:1)

您不需要重置Pickit。如果有的话,那可能是效率最低的方法(可以说)。

宁可尝试这样的事情。请注意,这是高水平。您需要使它自己工作。

void(main){

    // Setup your things here

    while(1){ // Your infinite loop

        // Check if you received 'a' here

        if (received_a == 1){ // You received a 'a'
             send_data(); // Send your data
        }      
    }
}

如果不提供您编写的实际代码,我们将无法为您提供帮助。

答案 1 :(得分:0)

使用while(1)到处循环,如果不使用break的话;或返回命令,您将永远停留在该循环中。 我认为除了main()之外,您不需要while(1)在函数中循环。删除这些,它应该可以工作。

尝试在流程图中绘制程序流,它应该清除所有内容。还可以考虑通过开关/机箱使用状态机。它使您在代码中的位置更加清楚,并且更易于调试。另外,对ADC和串行端口使用中断可能更好。您可以腾出图片去做其他事情,而外围设备则需要花费一些时间。