Thông báo

Collapse
No announcement yet.

PIC32 Ngắt ưu tiên...

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • PIC32 Ngắt ưu tiên...

    Các anh giúp em với, em dùng ngắt ưu tiên ở PIC32 mà sao không được ấy...
    Sau đây là bài của em ...trình dịch C32..
    Khi chạy thì có ngắt ngoài INT0 thì nó bị tràn Stack chịp không làm việc nữa ạ????

    Ai biết giúp em với em cảm ơn nhiều!!!



    // Day la chuong trinh dung ngat ngoai INT1
    // Dieu khien 1 led o PORTG
    // Khi nhan button int1 thi led RG12 thay doi trang thai
    // Chuong trinh su dung cho kit pic32
    // Lien he theo dia chi web sau de biet them chi tiet ve kit pic32
    // http://www.freewebs.com/picdevelopmenttool/kitpic.htm

    #include <p32xxxx.h>
    #include <plib.h>
    #include <stdlib.h>

    /*
    #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_2, FWDTEN = OFF
    #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1, BWP = OFF
    */
    #pragma config FNOSC = PRIPLL // Oscillator Selection
    #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (PIC32 Starter Kit: use divide by 2 only)
    #pragma config FPLLMUL = MUL_20 // PLL Multiplier
    #pragma config FPLLODIV = DIV_2 // PLL Output Divider
    #pragma config FPBDIV = DIV_8 // Peripheral Clock divisor
    #pragma config FWDTEN = OFF // Watchdog Timer
    #pragma config WDTPS = PS1 // Watchdog Timer Postscale
    #pragma config FCKSM = CSDCMD // Clock Switching & Fail Safe Clock Monitor
    #pragma config OSCIOFNC = OFF // CLKO Enable
    #pragma config POSCMOD = HS // Primary Oscillator
    #pragma config IESO = OFF // Internal/External Switch-over
    #pragma config FSOSCEN = OFF // Secondary Oscillator Enable
    #pragma config CP = OFF // Code Protect
    #pragma config BWP = OFF // Boot Flash Write Protect
    #pragma config PWP = OFF // Program Flash Write Protect
    #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select
    #pragma config DEBUG = OFF // Debugger Disabled for Starter Kit


    #define SYSCLK (40000000)
    #define DESIRED_BAUDRATE (9600) // The desired BaudRate

    // Let compile time pre-processor calculate the PR1 (period)
    //#define SYS_FREQ (40000000L)
    #define PB_DIV 128
    #define PRESCALE 200
    #define TOGGLES_PER_SEC 1
    #define T1_TICK (SYSCLK/PB_DIV/PRESCALE/TOGGLES_PER_SEC)


    #define SCLK PORTBbits.RB8 //
    #define SD0 PORTBbits.RB9 //


    void guiso(unsigned int so);
    void docHN(void);
    void nhayLED(unsigned int dr,unsigned int dg,unsigned int db, unsigned int nDots);

    unsigned int data[34];
    unsigned int t = 0, k = 0;
    unsigned int i=0, j=0, Mask=0;
    unsigned int ds =0;
    unsigned long flas=0;



    //************************************************** ****************************
    //* DelayMs()
    //*
    //* This functions provides a software millisecond delay
    //************************************************** ****************************/
    void delayMS(unsigned int msec)
    {
    unsigned int tWait, tStart;

    tWait=(SYSCLK/2000)*msec;
    tStart=ReadCoreTimer();
    while((ReadCoreTimer()-tStart)<tWait); // wait for the time to pass

    }

    //************************************************** ****************************
    //* DelayUs()
    //*
    //* This functions provides a software millisecond delay
    //************************************************** ****************************/
    void delayUS(unsigned int usec)
    {
    unsigned int tWait, tStart;

    tWait=(SYSCLK/2000000)*usec;
    tStart=ReadCoreTimer();
    while((ReadCoreTimer()-tStart)<tWait); // wait for the time to pass

    }



    void __ISR(0, ipl1SOFT) INT0Interrupt( void)
    {

    mINT0ClearIntFlag();// Xoa co ngat INT0
    if(t>=33) t=32;
    data[t]=0;
    while(mPORTBReadBits(BIT_7))
    {
    data[t]+=1;
    if(data[t]>8000)break;
    }

    if(data[t]>5000 && data[t]<7000)
    {
    data[0]= data[t];
    t=0;
    }

    t+=1;
    mPORTBToggleBits(BIT_6);
    }



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
    // STEP 3. configure the Timer 1 interrupt handler

    void __ISR(_TIMER_1_VECTOR, ipl2AUTO) Timer1Handler(void)
    {
    // clear the interrupt flag
    mT1ClearIntFlag();

    // .. in this case, toggle the LED
    mPORTBToggleBits(BIT_5);
    }



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
    void main()
    {
    DDPCONbits.JTAGEN = 0; // disable JTAG port
    int pbClk;

    PPSOutput(4,RPB10,U2TX); //Assign U2TX to pin RPB10
    PPSInput(2,U2RX, RPB11); //Assign U2RX to pin RPB11
    TRISB = 0x088f;
    TRISA = 0x000F;
    // STEP1. Configure the device for maximum performance.
    // This macro sets flash wait states, PBCLK divider and DRM wait states based on the specified
    // clock frequency. It also turns on the cache mode if avaialble.
    // We're using the PB clock frequency that was set automatically by this call.
    // Another option is to set the PB divider explicitely and then use a #define for the pbClk value.
    //pbClk= SYSTEMConfigPerformance(SYSCLK);
    pbClk=SYSTEMConfig(SYSCLK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // STEP2. Open UART2 with Receive and Transmitter enable.
    OpenUART2(UART_EN, UART_RX_ENABLE | UART_TX_ENABLE, pbClk/16/DESIRED_BAUDRATE-1);
    //UART is On Enable Rx Enable Tx Calculate Baud Rate


    mINT0IntEnable(0);
    mINT0SetIntPriority(INT_PRIORITY_LEVEL_1);
    INTEnableSystemSingleVectoredInt(); // cho phep ngat
    mINT0SetEdgeMode(1); // 1 rising, 0 falling
    mINT0IntEnable(1);


    ConfigINT0(EXT_INT_PRI_1 | RISING_EDGE_INT | EXT_INT_ENABLE);
    // STEP 2. configure Timer 1 using internal clock, 1:256 prescale

    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_TICK);

    // set up the timer interrupt with a priority of 2
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);


    // 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
    #define PARAM1 ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON

    // define setup parameters for OpenADC10
    // ADC ref external | disable offset test | disable scan mode | perform 2 samples | use dual buffers | use alternate mode
    #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON

    // define setup parameters for OpenADC10
    // use ADC internal clock | set sample time
    #define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15


    // define setup parameters for OpenADC10
    // set AN0 and AN1 as analog inputs
    #define PARAM4 ENABLE_AN0_ANA | ENABLE_AN1_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 | use ground as neg ref for A | use AN5 for input B

    // configure to sample AN0 & AN1
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0 | ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN1); // configure to sample AN4 & AN5
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above

    EnableADC10(); // Enable the ADC



    mPORTBSetPinsDigitalOut(BIT_5);// setup chan RG5 la output
    mPORTBSetPinsDigitalOut(BIT_6);// setup chan RG5 la output
    mPORTBSetPinsDigitalOut(BIT_8);// setup chan RG5 la output
    mPORTBSetPinsDigitalOut(BIT_9);// setup chan RA4 la output


    PORTSetPinsDigitalIn(IOPORT_B, BIT_7);



    putsUART2("*** OLIMEX PIC32MX BASIC UART Example ***\r\n");
    putsUART2("*** Type some characters and observe echo ***\r\n");

    DBPRINTF("The build date and time is ... \n");

    NVMWriteWord((void*)0xBD008010,1);
    flas= *(int *)(0xBD008010);
    guiso(flas/10000); guiso(flas%10000);

    while( 1)
    {
    // docHN();
    guiso(ReadADC10(0));
    ds= ReadADC10(0)/34;
    putsUART2(" ");
    guiso(ReadADC10(1));
    delayMS(1);
    nhayLED(ds,0,0,2048); putsUART2("\r\n");



    }
    }



    /////////////////////////////////// Gui So UART/////////////////////////////////
    void guiso(unsigned int so)
    {

    unsigned char dn,dt,dc,dv;
    dv= so%10;
    so= so/10;
    dc= so%10;
    so= so/10;
    dt= so%10;
    dn= so/10;
    putcUART2(48+dn);
    putcUART2(48+dt);
    putcUART2(48+dc);
    putcUART2(48+dv);

    }


    /////////////////////////////////// Doc Hong Ngoai//////////////////////////////
    void docHN()
    {

    if(t>=33)
    {
    unsigned long thhn =0, ts =0;
    //DISABLE_INTERRUPTS(INT_EXT1);
    for(k=0;k<=32;k++)
    {
    guiso(data[k]);
    putsUART2(" ");
    }
    putsUART2("\r");
    if(data[0]>5000 && data[0]<7000)
    {
    ts=1;thhn=0;
    for(k=1;k<=32;k++)
    {
    if(data[k]>1200) thhn=thhn+ts;
    ts=ts*2;
    }
    } else thhn=0;

    // if(thhn==0xBA45FF00) ;// CH-
    // if(thhn==0xB847FF00) ;// CH+
    // if(thhn==0xB946FF00) ;// CH

    // if(thhn==0xBB44FF00) ; // |<<
    // if(thhn==0xBC43FF00) ; // >||
    // if(thhn==0xBF40FF00) ;// >>|
    // if(kk==0)thhn=0x00000000;

    // if(thhn==0xF609FF00) ;// EQ
    // if(thhn==0xEA15FF00);// +
    // if(thhn==0xF807FF00) mPORTBToggleBits(BIT_5);// -
    // if(ch==0)ch=1; if(ch==33)ch=32;
    // if(thhn==0xE619FF00) ;// 100+
    // if(thhn==0xF20DFF00) mPORTBToggleBits(BIT_5);// 200+
    // if(thhn==0xE916FF00) hu=10;
    // if(thhn==0xF30CFF00) hu=1;
    // if(thhn==0xE718FF00) hu=2;
    // if(thhn==0xA15EFF00) hu=3;
    // if(thhn==0xF708FF00) hu=4;
    // if(thhn==0xE31CFF00) hu=5;
    // if(thhn==0xA55AFF00) hu=6;
    // if(thhn==0xBD42FF00) hu=7;
    // if(thhn==0xAD52FF00) hu=8;
    // if(thhn==0xB54AFF00) hu=9;
    t=0;
    // enable_interrupts (INT_EXT1);

    };
    }



    ///////////////////////////////////Nhay LED///////////////////////////////////////////////
    void nhayLED(unsigned int dr,unsigned int dg,unsigned int db, unsigned int nDots)
    {

    //SDO, SCLK is data andshiftoutput, bit variability ,nDots islight qty
    // this programis only suitable in GMODE=1,CMODE=1 situation.
    // first output 32 “0” start frame

    SCLK=0;
    SD0=0;
    for (i=0;i<32;i++){SCLK=1;SCLK=0;} ;

    // then output nDotsdata, here suppose each point colour are(dr,dg,db)
    //dr,db,dg is red, green andblue grey level0-31
    for(i=0;i<nDots;i++)

    {
    SD0=1;SCLK=1;SCLK=0; //firstoutput one “1”as start bit
    //output 5 bits red data
    Mask=0x10;

    for (j=0;j<5;j++)
    {
    if (Mask&dr) SD0=1;
    else SD0=0;

    SCLK=1; SCLK=0;
    Mask>>=1;
    }
    // output 5bits greendata

    Mask=0x10;
    for(j=0;j<5;j++)
    {
    if(Mask &dg)SD0=1;
    else SD0=0;
    SCLK=1;SCLK=0;
    Mask>>=1;
    }
    //output 5bits blue data

    Mask=0x10;
    for(j=0;j<5;j++)
    {
    if(Mask &db) SD0=1;
    else SD0=0;
    SCLK=1;SCLK=0;
    Mask>>=1;
    }

    }
    // after output all nDotsdata, need addnDots pulse
    SD0=0;

    for(i=0;i<nDots;i++){SCLK=1;SCLK=0;}
    //transport data finish
    delayMS(1);

    //here add some delay , or transfer to otherdealings, after sometime(say 1/30
    //second), then fresh again.
    }
    ĐT : 01676455880

  • #2
    Có bác nào giúp em với!!!
    ĐT : 01676455880

    Comment

    Về tác giả

    Collapse

    quanghuy_125 Tìm hiểu thêm về quanghuy_125

    Bài viết mới nhất

    Collapse

    Đang tải...
    X