Exemple #1
0
void delay(int t){
    _CP0_SET_COUNT(0); // set core timer counter to 0
        time = 0;
        while (time < t) { // 0.5ms = 12000 ticks
            time = _CP0_GET_COUNT();
        }
}
int main()
{
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    
    TRISAbits.TRISA4 = 0;       // RA4 is output
    TRISBbits.TRISB4 = 1;       // RB4 is input
    LATAbits.LATA4 = 1;			// LED is on
    
    __builtin_enable_interrupts();
    
    while(1)
    {
	    // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
        _CP0_SET_COUNT(0);
        LATAINV = 0b10000;
        for(;_CP0_GET_COUNT()<(WAIT_TIME+1);)
        {
            while(!PORTBbits.RB4){;}
        }
    }   
}
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    
    __builtin_enable_interrupts();
    TRISAbits.TRISA4 = 0;     // set B4-B7 as digital outputs, 0-3 as digital inputs
    TRISBbits.TRISB4 = 1;
   
    
    while(1) {
        _CP0_SET_COUNT(0);
        LATAbits.LATA4=0;
        while(_CP0_GET_COUNT()<12000){
            ;
        }
        _CP0_SET_COUNT(0);
        LATAbits.LATA4=1;
        while(_CP0_GET_COUNT()<12000){
            ;
        }
        while (PORTBbits.RB4==0){
            ;
        }     
	    // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
		// remember the core timer runs at half the CPU speed
    }
    
    
}
Exemple #4
0
int main()
{
    char            whoamiCheck = 0;
    unsigned char   data[14];
    short           output[7];
    int             oc1New      = 0;
    int             oc2New      = 0;
    
    // PIC32 Setup
    __builtin_disable_interrupts();
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);
    BMXCONbits.BMXWSDRM = 0x0;      // 0 data RAM access wait states
    INTCONbits.MVEC     = 0x1;      // enable multi vector interrupts
    DDPCONbits.JTAGEN   = 0;        // disable JTAG to get pins back
    TRISAbits.TRISA4    = 0;        // RA4 output
    TRISBbits.TRISB4    = 1;        // RB4 input
    LATAbits.LATA4      = 0;		// LED off
    
    i2cMasterSetup();
    tim2Setup();
    oc1Setup();
    oc2Setup();
    
    __builtin_enable_interrupts();
    
    whoamiCheck = i2cMasterRead(IMU_ADDR,WHO_AM_I);
    if (whoamiCheck == WHOAMI_VAL)
    {
        LATAbits.LATA4 = 1;
    }
    
    while(1)
    {
        _CP0_SET_COUNT(0);
        i2cMasterReadAll(IMU_ADDR,OUT_TEMP_L,14,data);
        char2short(data,output,14);
        oc1New = (PER2+1)/2+(output[4]/20);
        oc2New = (PER2+1)/2+(output[5]/20);
        
        if(oc1New>(PER2+1)){
            oc1New=(PER2+1);
        }
        if(oc2New>(PER2+1)){
            oc2New=(PER2+1);
        }
        
        OC1RS = oc1New;
        OC2RS = oc2New;
        LATAbits.LATA4 = !LATAbits.LATA4;
        
        while(_CP0_GET_COUNT()<UPDATER){
            ;
        }
    }   
}
Exemple #5
0
int main ( void )
{
    /* Initialize all MPLAB Harmony modules, including application(s). */
    SYS_Initialize ( NULL );
    _CP0_SET_COUNT(0);
           // set up USER pin as input
        ANSELBbits.ANSB13=0;
	TRISBbits.TRISB13=1;
    // set up LED1 pin as a digital output
    //    TRISBbits.TRISB7=0;
    //   	LATBbits.LATB7=1;

    // set up LED2 as OC1 using Timer2 at 1kHz   PWM: B15   DIO: B9   left
        TRISBbits.TRISB9=0;
        LATBbits.LATB9=1;
        ANSELBbits.ANSB15=0;
	RPB15Rbits.RPB15R=0b0101;
	T2CONbits.TCKPS = 0;
	PR2=1999;
	TMR2=0;
	T2CONbits.ON=1;
        T2CONbits.TCS=0;
	OC1CONbits.OCTSEL=0;
	OC1CONbits.OCM = 0b110;  // PWM mode without fault pin; other OC1CON bits are defaults
	OC1CONbits.ON = 1;
	OC1RS=0;
	OC1R=0;
    //  OC2  PWM: B5    DIO: B3    right
        ANSELBbits.ANSB3=0;
        TRISBbits.TRISB3=0;
        LATBbits.LATB3=0;

        RPB5Rbits.RPB5R=0b0101;
        T3CONbits.TCKPS = 0;
	PR3=1999;
	TMR3=0;
	T3CONbits.ON=1;
        T3CONbits.TCS=0;
	OC2CONbits.OCTSEL=1;
	OC2CONbits.OCM = 0b110;  // PWM mode without fault pin; other OC1CON bits are defaults
	OC2CONbits.ON = 1;
	OC2RS=0;
	OC2R=0;
    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );

    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}
Exemple #6
0
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    //set port4A to digital out
    TRISAbits.TRISA4 = 0;
    LATAbits.LATA4 = 1;
    _CP0_SET_COUNT(0);
    
    __builtin_enable_interrupts();
    
    while(1) {
	    // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
		// remember the core timer runs at half the CPU speed
                
        if (_CP0_GET_COUNT()>2400000){
            //change LED state
            LATAbits.LATA4 = !LATAbits.LATA4;
            //LATAbits.LATA4 = 0;
            _CP0_SET_COUNT(0);
        //LATAbits.LATA4 =1;j
        }
                
    }
    
    
}
Exemple #7
0
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISAbits.TRISA4 = 0; // Set RA4 to output (green LED)
    TRISBbits.TRISB4 = 1; // Set RB4 to input (user button)
    LATAbits.LATA4 = 0; //Set green LED to 0 to start 


    
    __builtin_enable_interrupts();
    _CP0_SET_COUNT(0);

    while(1) {
        if(_CP0_GET_COUNT()>=2400000){      
        LATAbits.LATA4 = !LATAbits.LATA4;
        _CP0_SET_COUNT(0);
        }
	    
        while(PORTBbits.RB4 == 0){
            LATAbits.LATA4 = 0;

        }
    }
    
    
}
Exemple #8
0
int main(int argc, char** argv) {
    __builtin_disable_interrupts();
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);
    BMXCONbits.BMXWSDRM = 0x0;
    INTCONbits.MVEC = 0x1;
    DDPCONbits.JTAGEN = 0;
     // do your TRIS and LAT commands here
    //TRISAbits.TRISA4=1;
    TRISAbits.TRISA4 = 0;
    TRISBbits.TRISB4 = 1;
    
    __builtin_enable_interrupts();
    int t = 24000;
    int i = 0;
    while(1) {
        if (PORTBbits.RB4)
        {
            i = 22000;
        }else
        {
            i = 3000;
        }
        //if (PORTBbits.RB4)
        //__delay_ms(1000);
        _CP0_SET_COUNT(0);
        LATAbits.LATA4=1;
        //__delay_ms(1000);
        while (_CP0_GET_COUNT()<(t-i)); 
        LATAbits.LATA4=0;
	    _CP0_SET_COUNT(0);
        while (_CP0_GET_COUNT()<i); 
        //_CP0_SET_COUNT(0);
	    //_CP0_GET_COUNT();
        // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
		// remember the core timer runs at half the CPU speed
    }

    return (EXIT_SUCCESS);
}
Exemple #9
0
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISAbits.TRISA4=0;
    TRISBbits.TRISB4=1;
    //spi_init();
    i2c_master_setup();
   // readaddress();
    init_OC();
    setlsm(0x10,0b10000000);
    setlsm(0x11,0b10000000);
    setlsm(0x12,0b00000100);
   // i2c_readmulti();
    __builtin_enable_interrupts();
    
    LATAbits.LATA4=0;    
     while(1){
        _CP0_SET_COUNT(0);
        //LATAINV = 0x10; // make sure timer2 works
        /*
        if(PORTBbits.RB4==0)
        {
            if(read == 0x69)
            LATAbits.LATA4=1;
            else
            LATAbits.LATA4=0;
        }
        else
        LATAbits.LATA4=0;
         */
        while(_CP0_GET_COUNT() < 480000) { 
            ;
        }
i2c_readmulti();
    }   
}
void sys_cpu_en_timer(uint32_t counts, uint8_t ien)
{
    /* Disable Counter by setting DC bit to 1 in CP0.Cause */
    _CP0_BIS_CAUSE(_CP0_CAUSE_DC_MASK);

    _CP0_SET_COUNT(counts);
    if (ien) {
        jtvic_en_source(MEC14xx_GIRQ24_ID, 0, 0);
    } else {
        jtvic_dis_clr_source(MEC14xx_GIRQ24_ID, 0, 1);
    }

    /* Enable Counter */
    _CP0_BIC_CAUSE(_CP0_CAUSE_DC_MASK);

}
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISAbits.TRISA4 = 0;   //RA4 (PIN#12) for Green LED
    LATAbits.LATA4 = 1;
    
    TRISBbits.TRISB4 = 1;   //RB4 (PIN#11) for pushbutton
    
    
    __builtin_enable_interrupts();
    
    
    while(1) {
	    // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
		// remember the core timer runs at half the CPU speed
        
        _CP0_SET_COUNT(0);
        while(_CP0_GET_COUNT() < 12000) { 
            ;
        }
        if (PORTBbits.RB4 == 0) {
            LATAbits.LATA4 = 1; // push button will make LED not blink.
        }
        else {
            LATAINV = 0x10; // LED turn on/off for 0.5 ms, LED blinking.
        }
    }
}
Exemple #12
0
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here 
    TRISBbits.TRISB4    = 1;        // Set B4 (push-button) as input pin
    TRISAbits.TRISA4    = 0;        // Set A4 (LED) as output
    LATAbits.LATA4      = 1;        // Set A4 as high
    
    __builtin_enable_interrupts();
    
    while(1) {
        
        // Turn off LED while button is pressed
        while(PORTBbits.RB4 == 0) {
            LATAbits.LATA4 = 0;
        }
        
	    // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
		// remember the core timer runs at half the CPU speed
        // Toggle LED at 1000 Hz
        if (_CP0_GET_COUNT() > 24000){ 
            LATAbits.LATA4 = !LATAbits.LATA4;
            _CP0_SET_COUNT(0);
        }
    } 
}
Exemple #13
0
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISBbits.TRISB4 = 1; // RB4 is an input
    TRISAbits.TRISA4 = 0; // RA4 is an output
    LATAbits.LATA4 = 1; // RA4 is set high
            
    __builtin_enable_interrupts();
    
    while(1) {
	    // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
		// remember the core timer runs at half the CPU speed
        while(PORTBbits.RB4 == 0){} //wait while USER button is pressed
        _CP0_SET_COUNT(0);
        LATAbits.LATA4 = 1; // RA4 is set high
        while (_CP0_GET_COUNT() < 12000){} // wait for 0.5ms
        LATAbits.LATA4 = 0; // RA4 is set low
        while (_CP0_GET_COUNT() < 24000){} // wait for 0.5ms
        
    }
    
    
}
Exemple #14
0
int main() {

    // Startup code to run as fast as possible and get pins back from bad defaults

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // no cache on this chip!

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;

    __builtin_enable_interrupts();

    // set up USER pin (RB13) as a digital input
    ANSELBbits.ANSB13 = 0;
    TRISBbits.TRISB13 = 1;

    // set up LED1 pin (RB7) as a digital output
    RPB7Rbits.RPB7R = 0b0001;
    TRISBbits.TRISB7 = 0;
    LATBbits.LATB7 = 1;    
    
    // set up LED2 as OC1 using Timer2 at 1kHz
    RPB15Rbits.RPB15R = 0b0101;
    T2CONbits.TCKPS = 2; // Timer2 prescaler N=4 (1:4)
    PR2 = 9999; // period = (PR2+1) * N * 25 ns = 1 ms, 1 kHz
    TMR2 = 0; // initial TMR2 count is 0
    OC1CONbits.OCM = 0b110; // PWM mode without fault pin; other OC1CON bits are defaults
    OC1RS = 5000; // duty cycle = OC1RS/(PR2+1) = 50%
    OC1R = 5000; // initialize before turning OC1 on; afterward it is read-only
    T2CONbits.ON = 1; // turn on Timer2
    OC1CONbits.ON = 1; // turn on OC1    
   
    // set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;

    display_init();
    display_clear();
    char message[20];
    int num = 1337;
    sprintf(message, "Hello World %d!",num);
    display_now(message, 28, 32);
    
    while (1) {
    _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock
    LATBINV = 0x0080; // invert a pin

    // wait for half a second, setting LED brightness to pot angle while waiting
    while (_CP0_GET_COUNT() < 10000000) {
        
        OC1RS = readADC() * PR2/1024;

        if (PORTBbits.RB13 == 1) {
            // nothing
        } else {
            LATBINV = 0x0080;
        }
    }
}
}
int main() {

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;


    __builtin_enable_interrupts();

    initSPI1();
    initI2C2();
    i2c_master_setup();
    initExpander();

    //sine wave
    int sine[1000];
    int i;
    for(i = 0; i < 1000; i++){
      sine[i] = 128 + 127*sin(2*3.14*10*i/1000);
    }

    int triangle[1000];
    i = 0;
    for(i = 0; i < 1000; i++){
      triangle[i] = .256*i;
    }

    i = 0;

    while(1) {
      _CP0_SET_COUNT(0);

      if(_CP0_GET_COUNT() > 24000){
        i++;
        setVoltage(0, sine[i]);
        setVoltage(1, triangle[i]);
        _CP0_SET_COUNT(0);
      }

      if(i > 1000){
        i = 0;
      }

      char status = getExpander();          //read the expander
      char g7 = (status & 0x80) >> 7;       //get level of pin g7
      setExpander(0, g7);                   //set pin 0 to level of g7



    }


}
Exemple #16
0
int main() {
    // startup
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // no cache on this chip!

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;

    __builtin_enable_interrupts();

    // set up USER pin as input
    ANSELBbits.ANSB13 = 0;
    TRISBbits.TRISB13 = 1;
    // set up LED1 pin as a digital output
    TRISBbits.TRISB7 = 0;
    LATBbits.LATB7 = 1;
    // set up LED2 as OC1 using Timer2 at 1kHz
    ANSELBbits.ANSB15 = 0;
    RPB15Rbits.RPB15R = 0b0101;

   
    // set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;
    
    //pwm
    T2CONbits.TCKPS = 1;     // Timer2 prescaler N=2
    PR2 = 19999;              // period = (PR2+1) * N * 25 ns => 1000 Hz
    TMR2 = 0;                // initial TMR4 count is 0

    //OC1
    OC1CONbits.OCM = 0b110;  // PWM mode without fault pin; other OC1CON bits are defaults
    OC1CONbits.OCTSEL = 0;   // set timer select bit to 0 for using Timer2
    OC1RS = 20000;             // duty cycle = OC1RS/(PR3+1) = 75%
   // OC1R = 19999;              // initialize before turning OC1 on; afterward it is read-only

    T2CONbits.ON = 1;			//Turn Timer4 on
    OC1CONbits.ON = 1;       // turn on OC1

    while (1) {
        // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output %
        _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock
        LATBINV = 0b10000000; // invert a pin

    // wait for half a second, setting LED brightness to pot angle while waiting
        while (_CP0_GET_COUNT() < 10000000) {
            int val = readADC();
            OC1RS = val * 20000/1023;

            if (PORTBbits.RB13 == 1) {
                // nothing
                //EVERYTHING
            } else {
                LATBINV = 0b10000000;
            }
        }
    }
}
Exemple #17
0
int main() {
    // startup FROM HW1
    __builtin_disable_interrupts();
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);
    BMXCONbits.BMXWSDRM = 0x0;
    INTCONbits.MVEC = 0x1;
    DDPCONbits.JTAGEN = 0;
    __builtin_enable_interrupts();
    ANSELBbits.ANSB13 = 0; //Make B13 Digital
    TRISBbits.TRISB7 = 0;
    RPB15Rbits.RPB15R = 0b0101; //Set B15 as OC1
    T2CONbits.TCKPS = 0; //Prescalar = 1
    PR2 = 39999; //Max counter value
    TMR2 = 0; // Initialize timer 2
    OC1CONbits.OCM = 0b110; //Without failsafe
    OC1RS = 2000; //Duts cycle is 5%
    OC1R = 2000; //Initial duty cycle is 5%
    T2CONbits.ON = 1; //turns timer on
    OC1CONbits.ON = 1; //Turns output control on
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;

    display_init();

    while (1) {
        _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock
        LATBINV = 0b10000000; // invert a pin B7 (LED1)
        while (_CP0_GET_COUNT() < 10000000) {
            int val;
            val = readADC();
            OC1RS = val * 39; //comes out to about 39
            if (PORTBbits.RB13 == 1) {
            } else {
                LATBINV = 0b10000000; //Invert LED1
            }
        }
        //END STARTUP FROM HW1

        display_clear();

        char MessageLetters[17]; //= 'Hello world 1337!';
        sprintf(MessageLetters, "Hello world 1337!");
        int ii = 0;
        int currentVal;

        int message[17];
        while (MessageLetters[ii]) {
            currentVal = MessageLetters[ii];
            if (currentVal - 32 >= 0) {
                message[ii] = currentVal - 32;
            }
            ii++;
        }
        
        int StringCount;

        int colLCD; //Create column location on LCD
        int rowLCD; //Create row location on LCD

        for (StringCount = 0; StringCount < 20; StringCount++) {
            for (colLCD = 1; colLCD < 6; colLCD++) { //Cycle through column of array
                for (rowLCD = 1; rowLCD < 9; rowLCD++) { //Cycle through row of array
                    display_pixel_set(rowLCD + 32, colLCD + 6 * StringCount + 28, getBit(message[StringCount], rowLCD - 1, colLCD - 1)); //At this specific (row, col) of screen, turn LCD on (1) or off (0) based on array value

                }

            }
        }


        display_draw(); //Commit to LCD screen once all positions defined

    }
}
Exemple #18
0
int main() {

    // startup

	__builtin_disable_interrupts();

	// set the CP0 CONFIG register to indicate that
	// kseg0 is cacheable (0x3) or uncacheable (0x2)
	// see Chapter 2 "CPU for Devices with M4K Core"
	// of the PIC32 reference manual
	__builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

	// no cache on this chip!

	// 0 data RAM access wait states
	BMXCONbits.BMXWSDRM = 0x0;

	// enable multi vector interrupts
	INTCONbits.MVEC = 0x1;

	// disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
	DDPCONbits.JTAGEN = 0;

	__builtin_enable_interrupts();

	ANSELAbits.ANSA0 = 0;

    // set up USER pin as input
	ANSELBbits.ANSB13 = 0;
	TRISBbits.TRISB13 = 1;

    // set up LED1 pin as a digital output
	RPB7Rbits.RPB7R = 0b0001;
        TRISBbits.TRISB7 = 0;
	LATBbits.LATB7 = 1;

    // set up LED2 as OC1 using Timer2 at 1kHz
        ANSELBbits.ANSB15 = 0; // 0 for digital
        RPB15Rbits.RPB15R = 0b0101; //set B15 as output compare 1

//    __builtin_disable_interrupts();
	T2CONbits.TCKPS = 0;     // Timer2 prescaler N=1 (1:4)
	PR2 = 39999;             // period = (PR2+1) * N * 12.5 ns = 1 ms, 1 kHz
	TMR2 = 0;                // initial TMR2 count is 0

 	OC1CONbits.OCM = 0b110;  // PWM mode without fault pin; other OC1CON bits are defaults
	T2CONbits.ON = 1;        // turn on Timer2
	OC1CONbits.ON = 1;       // turn on OC1

        OC1RS = 0;
        OC1R = 0;

    // set up A0 as AN0
        ANSELAbits.ANSA0 = 1;
        AD1CON3bits.ADCS = 3;
        AD1CHSbits.CH0SA = 0;
        AD1CON1bits.ADON = 1;

	while (1) {
            // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output
            //Use the core timer to double check your CPU clock settings
            _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock
            LATBINV = 0x0080; // invert a pin

            // wait for half a second, setting LED brightness to pot angle while waiting


            while (_CP0_GET_COUNT() < 10000000) {
                int val = readADC();
                OC1RS = (val * PR2)/1024;

                if (PORTBbits.RB13 == 1) {
                    // nothing
                }
                else {
                    LATBINV = 0x0080;
                }
            }
        }
}
Exemple #19
0
int main() {

    // startup
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // no cache on this chip!

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;

    __builtin_enable_interrupts();

    // set up USER pin as input

    ANSELBbits.ANSB13 = 0;
    TRISBbits.TRISB13 = 1;
    INT4Rbits.INT4R = 0b0100;

    // set up LED1 pin as a digital output

    ANSELBbits.ANSB14 = 0;
    TRISBbits.TRISB14 = 0;
    //RPB14Rbits.RPB14R = 0b0001;
    LATBbits.LATB14 = 1;


    // set up LED2 as OC1 using Timer2 at 1kHz
    ANSELBbits.ANSB15 = 0;
    TRISBbits.TRISB15 = 0;
    RPB15Rbits.RPB15R = 0b0101;

    T2CONbits.T32 = 0;
    T2CONbits.TCS = 0;
    T2CONbits.TGATE = 0;
    T2CONbits.TCKPS = 0b100;
    PR2 = 4999;
    TMR2 = 0;
    T2CONbits.ON = 1;
    OC1CONbits.OCM = 0b110;
    OC1RS = 2500; // 50% duty cycle
    OC1R = 2500;
    OC1CONbits.ON = 1;

    // set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;

    while (1) {
    _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock
    LATBINV = 0b100000000000000; // invert a pin
    
    // wait for half a second, setting LED brightness to pot angle while waiting
    while (_CP0_GET_COUNT() < 10000000) {
        int val = readADC();
        OC1RS = val*(5000/1024);

        if (PORTBbits.RB13 == 1) {
            // nothing
        }
        else {
            LATBINV = 1<<14;
        }
    }
    }
}
Exemple #20
0
int main(void) {
    
//Startup
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // no cache on this chip!

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;

    __builtin_enable_interrupts();

ANSELBbits.ANSB13 = 0; // 0 for digital, 1 for analog
ANSELBbits.ANSB15 = 0; // 0 for digital, 1 for analog

    T2CONbits.TCKPS = 0;	//Setting prescaler to 1 (0 corresponds to 1)
    PR2 = 39999;            //Setting PR for timer 2 to 39999
    TMR2 = 0;				//Setting Timer 2 to 0
    OC1CONbits.OCTSEL = 0;	//Telling OC1 to use timer 2
    OC1CONbits.OCM = 0b110;	//Telling OC1 to use PWM without the fault
    OC1RS = 20000;			//Setting initial duty cycle to 20000/(39999+1)*100% = 50%
    OC1R = 20000;			//Updating duty cycles to 20000/(39999+1)*100% = 50%
    T2CONbits.ON = 1;		//turn on timer
    OC1CONbits.ON = 1;		//turn on OC code

// set up USER pin as input
    TRISBbits.TRISB13 = 1; // set pin B13 to be digital INPUT
    // U1RXRbits.U1RXR = 0b0011; // set U1RX to pin B13 (Input pin from User button)

// set up LED1 pin as a digital output
    TRISBbits.TRISB7 = 0; // set pin B7 to be digital OUTPUT
    // LATBbits.LATB7 = 1;
    // RPB7Rbits.RPB7R = 0b0001; //set B7 to U1TX (Output pin for LED1)
    
// set up LED2 as OC1 using Timer2 at 1kHz
    // TRISBbits.TRISB15 = 0; // set B15 to digital OUTPUT
    RPB15Rbits.RPB15R = 0b0101; // set B15 to U1TX (Output pin for OC1)

// set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;
    
// Accelerometer
    acc_setup();
    short accels[3]; // accelerations for the 3 axes
    short mags[3]; // magnetometer readings for the 3 axes
    short temp;

// Display
    display_init();
    
    /*int number = 1337;
    sprintf(buffer,"Hello World %d!", number);
    display_write(28,32,buffer);
    */
    /*                           
    for (ii = 0; ii < 128; ii++){       // Draw a line from position (x,y) = (0,15) to (127,15)
    display_pixel_set(15,ii,1);
    display_draw();
    }*/
    
    
    while (1){
     // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output %
        
        _CP0_SET_COUNT(0);
        LATBINV = 0b10000000;
        
            while(_CP0_GET_COUNT()<10000000){
                OC1RS = readADC()*(PR2+1)/1023; // delay for 10M core ticks, 0.5s
                if (PORTBbits.RB13 == 1){
                    ;// nothing
                }
                else{
                    LATBINV = 0b10000000;
                }
            }
        // read the accelerometer from all three axes
        // the accelerometer and the pic32 are both little endian by default (the lowest address has the LSB)
        // the accelerations are 16-bit twos compliment numbers, the same as a short
        acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6);
        // need to read all 6 bytes in one transaction to get an update.
        acc_read_register(OUT_X_L_M, (unsigned char *) mags, 6);
        // read the temperature data. Its a right justified 12 bit two's compliment number
        acc_read_register(TEMP_OUT_L, (unsigned char *) &temp, 2);
        
        display_clear();
        
        sprintf(buffer1, "(ax, ay, az)");
        display_write(0, 0, buffer1);
        sprintf(buffer2, "(%d, %d, %d)", accels[0], accels[1], accels[2]);
        display_write(0, 48, buffer2);
        sprintf(buffer, "Derek Oung");
        display_write(0, 56, buffer);
        
        
        
        if (accels[0]>0 && accels[1]>0){
            x_line_point = (float)((float)accels[0]/16000*64) + 64;
            y_line_point = (float)((float)accels[1]/16000*32) + 32;
            /*
            while (i<x_line_point){
                display_pixel_set(32,i,1);
                i++;
            }
            */
                
                for (i=64;i<x_line_point;i++){
                    display_pixel_set(31, i, 1);
                    display_pixel_set(32, i, 1);
                    display_pixel_set(33, i, 1);
                    }
                for (j=32;j<y_line_point;j++){
                    display_pixel_set(j, 63, 1);
                    display_pixel_set(j, 64, 1);
                    display_pixel_set(j, 65, 1);
                }
        }
        else if (accels[0]>0 && accels[1]<0){
            x_line_point = (float)((float)accels[0]/16000*64) + 64;
            y_line_point = (float)((float)accels[1]/16000*32) + 32;
                for (i=64;i<x_line_point;i++){
                    display_pixel_set(31, i, 1);
                    display_pixel_set(32, i, 1);
                    display_pixel_set(33, i, 1);
                    }

                for (j=32;j>y_line_point;j--){
                    display_pixel_set(j, 63, 1);
                    display_pixel_set(j, 64, 1);
                    display_pixel_set(j, 65, 1);
                }
        }
        else if (accels[0]<0 && accels[1]>0){
            x_line_point = (float)((float)accels[0]/16000*64) + 64;
            y_line_point = (float)((float)accels[1]/16000*32) + 32;
                for (i=64;i>x_line_point;i--){
                    display_pixel_set(31, i, 1);
                    display_pixel_set(32, i, 1);
                    display_pixel_set(33, i, 1);
                    }

                for (j=32;j<y_line_point;j++){
                    display_pixel_set(j, 63, 1);
                    display_pixel_set(j, 64, 1);
                    display_pixel_set(j, 65, 1);
                }
        }
        else if (accels[0]<0 && accels[1]<0){
            x_line_point = (float)((float)accels[0]/16000*64) + 64;
            y_line_point = (float)((float)accels[1]/16000*32) + 32;
                for (i=64;i>x_line_point;i--){
                    display_pixel_set(31, i, 1);
                    display_pixel_set(32, i, 1);
                    display_pixel_set(33, i, 1);
                    }

                for (j=32;j>y_line_point;j--){
                    display_pixel_set(j, 63, 1);
                    display_pixel_set(j, 64, 1);
                    display_pixel_set(j, 65, 1);
                }
        }
        display_pixel_set(31,63,1);
        display_pixel_set(31,64,1);
        display_pixel_set(31,65,1);
        display_pixel_set(32,63,1);
        display_pixel_set(32,64,1);
        display_pixel_set(32,65,1);
        display_pixel_set(33,63,1);
        display_pixel_set(33,64,1);
        display_pixel_set(33,65,1);
        display_draw();
        
        
        }

}
Exemple #21
0
int main() {
    //int value = 0;
    char r;
    makewave();
    int count1 = 0;
    int count2 = 0;
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISAbits.TRISA4 = 0;     // ouput
    TRISBbits.TRISB4 = 1;     // input

    LATAbits.LATA4 = 1;       // intialize LED on
    initSPI1();
    initI2C2();
    init_ctrl1();
    init_ctrl2();
    init_ctrl3();
    init_OC();
    __builtin_enable_interrupts();
    
    while(1) {
	    // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing
		// remember the core timer runs at half the CPU speed
        _CP0_SET_COUNT(0);                   // set core timer to 0
        while(_CP0_GET_COUNT() < 480000){     // wait 1ms / 0.001s
            ;
        }
        while(_CP0_GET_COUNT() < 480000){     // wait 1ms / 0.001s
            ;
        }
        while(_CP0_GET_COUNT() < 480000){     // wait 1ms / 0.001s
            ;
        }
        while(_CP0_GET_COUNT() < 480000){     // wait 1ms / 0.001s
            ;
        }
        //setVoltage(0,sinewave[count1]);
        //setVoltage(1,triangle_wave[count2]);
        //count1++;
        //count2++;
        //if(count1 == 100){
          //count1 = 0;
        //}
        //if(count2 == 200){
          //count2 = 0;
        //}
        I2C_read_multiple(0x6B, 0x20, data_array, 14);

    }// while loop
        //CS = 0;                                 // listen to me
        //SPI1_IO(0x38); // most significant byte of address
        //SPI1_IO(0x00);         // the least significant address byte
        //CS = 1;   
    
}
Exemple #22
0
void APP_Tasks (void )
{
    char message[25];
    char inputfrompc[25];
    int i;
    short accels[3]; // accelerations for the 3 axes
    short accelsMAF;
    short accelsFIR;
    int buf1=0;
    int buf2=0;
    int buf3=0;
    int buf4=0;
    int buf5=0;
    //bn*1000 for FIR
    float b1=.0088;
    float b2=.0479;
    float b3=.1640;
    float b4=.2793;
    float b5=.2793;
    float b6=.1640;
    float b7=.0479;
    float b8=.0088;

    int FIRbuf1=0;
    int FIRbuf2=0;
    int FIRbuf3=0;
    int FIRbuf4=0;
    int FIRbuf5=0;
    int FIRbuf6=0;
    int FIRbuf7=0;
    int FIRbuf8=0;


    //sprintf(message,"Hello!");
    //use_display(20,20,message);
    //display_draw();
    /* Check if device is configured.  See if it is configured with correct
     * configuration value  */

    switch(appData.state)
    {
        case APP_STATE_INIT:

            /* Open the device layer */
            appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE );

            if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID)
            {
                /* Register a callback with device layer to get event notification (for end point 0) */
                USB_DEVICE_EventHandlerSet(appData.usbDevHandle, APP_USBDeviceEventHandler, 0);

                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            else
            {
                /* The Device Layer is not ready to be opened. We should try
                 * again later. */
            }

            break;

        case APP_STATE_WAIT_FOR_CONFIGURATION:

            if(appData.deviceConfigured == true)
            {
                /* Device is ready to run the main task */
                appData.hidDataReceived = false;
                appData.hidDataTransmitted = true;
                appData.state = APP_STATE_MAIN_TASK;

                /* Place a new read request. */
                USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0,
                        &appData.rxTransferHandle, appData.receiveDataBuffer, 64);
            }
            break;

        case APP_STATE_MAIN_TASK:

            if(!appData.deviceConfigured)
            {
                /* Device is not configured */
                appData.state = APP_STATE_WAIT_FOR_CONFIGURATION;
            }
            else if( appData.hidDataReceived )
            {
                /* Look at the data the host sent, to see what
                 * kind of application specific command it sent. */

                switch(appData.receiveDataBuffer[0])
                {
                    case 0x80:
                        /* Toggle on board LED1 to LED2. */
                        BSP_LEDToggle( APP_USB_LED_1 );
                        BSP_LEDToggle( APP_USB_LED_2 );
                        
                        for (i=0; i<8; i++){
                            inputfrompc[i] = appData.receiveDataBuffer[i+1];
                        }
                        use_display(20,20,inputfrompc);
                        display_draw();
                        appData.hidDataReceived = false;

                        /* Place a new read request. */
                        USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0,
                                &appData.rxTransferHandle, appData.receiveDataBuffer, 64 );

                        break;

                    case 0x81:

                        if(appData.hidDataTransmitted)
                        {
                            /* Echo back to the host PC the command we are fulfilling in
                             * the first byte.  In this case, the Get Push-button State
                             * command. */

                            appData.transmitDataBuffer[0] = 0x81;

                            acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6);
                            sprintf(message,"Z: %d",accels[0]);
                            use_display(10,10,message);
                            display_draw();
                            if(_CP0_GET_COUNT()>800000){
                                appData.transmitDataBuffer[1] = 1;
                                appData.transmitDataBuffer[2] = accels[0]>>8;
                                appData.transmitDataBuffer[3] = accels[0]&0xFF;

                                //use MAF buffer values to calculate accelsMAF
                                accelsMAF = ((buf1+buf2+buf3+buf4+buf5+accels[0])/6);

                                //change MAF buffer values
                                buf5=buf4;
                                buf4=buf3;
                                buf3=buf2;
                                buf2=buf1;
                                buf1=accels[0];



                                //change FIR buffer values
                                FIRbuf8=FIRbuf7;
                                FIRbuf7=FIRbuf6;
                                FIRbuf6=FIRbuf5;
                                FIRbuf5=FIRbuf4;
                                FIRbuf4=FIRbuf3;
                                FIRbuf3=FIRbuf2;
                                FIRbuf2=FIRbuf1;
                                FIRbuf1=accels[0];
                                //FIR Filtering calculations
                                accelsFIR = (b1*FIRbuf1)+(b2*FIRbuf2)+(b3*FIRbuf3)+(b4*FIRbuf4)+(b5*FIRbuf5)+(b6*FIRbuf6)+(b7*FIRbuf7)+(b8*FIRbuf8);

                                appData.transmitDataBuffer[4] = accelsMAF>>8;
                                appData.transmitDataBuffer[5] = accelsMAF&0xFF;
                                appData.transmitDataBuffer[6] = accelsFIR>>8;
                                appData.transmitDataBuffer[7] = accelsFIR&0xFF;
                                _CP0_SET_COUNT(0);

                            }
                            else{appData.transmitDataBuffer[1]=0;}


                            

                            appData.hidDataTransmitted = false;

                            /* Prepare the USB module to send the data packet to the host */
                            USB_DEVICE_HID_ReportSend (USB_DEVICE_HID_INDEX_0,
                                    &appData.txTransferHandle, appData.transmitDataBuffer, 64 );

                            appData.hidDataReceived = false;

                            /* Place a new read request. */
                            USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0,
                                    &appData.rxTransferHandle, appData.receiveDataBuffer, 64 );
                        }
                        break;

                    default:

                        appData.hidDataReceived = false;

                        /* Place a new read request. */
                        USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0,
                                &appData.rxTransferHandle, appData.receiveDataBuffer, 64 );
                        break;
                }
Exemple #23
0
int main() {
    // Startup code to run as fast as possible and get pins back from bad defaults
    __builtin_disable_interrupts();
    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);
    // no cache on this chip!
    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;
    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;
    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;
    __builtin_enable_interrupts();
    
    // set up USER pin as digital input
    ANSELBbits.ANSB13 = 0; // 0 = digital
    TRISBbits.TRISB13 = 1; // 1 = input

    // set up LED1 pin as a digital output
    TRISBbits.TRISB7 = 0; //digital output
    LATBbits.LATB7 = 1; //output on=0 initially
    
    // set up LED2 as OC1 using Timer2 at 1kHz
    ANSELBbits.ANSB15 = 0; // 0 = digital
    RPB15Rbits.RPB15R = 0b0101; //Set B15 as OC1

    // pwm
    OC1CONbits.OCTSEL = 0; 	// Timer 2 is used for output compare
    OC1CONbits.OCM = 0b110;     // PWM mode without fault pin; other OC1CON bits are defaults
    OC1R = 20000; 		// duty cycle = OC1RS/(PR2+1) = 100
				// initialize before turning OC1 on; afterward it is read-only
    PR2 = 19999;                // period = (PR2+1)*N=2*25ns = 1 ms, or 1 kHz
    TMR2 = 0;			// initalize counter to 0
    T2CONbits.TCKPS = 1; 	// set prescaler to 1:2

    T2CONbits.ON = 1; 		// turn on Timer2
    OC1CONbits.ON = 1;          // turn on OC1
    
    // set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;

    while (1) {
        // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output %
        _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock
        LATBINV = 0b10000000;// invert pin RB7

        // wait for half a second, setting LED brightness to pot angle while waiting
        while (_CP0_GET_COUNT() < 10000000) {
            int val = readADC(); //max = 1023
            OC1RS = val * 20000/1023;

            if (PORTBbits.RB13 == 1) {
                //nothing
            } else {
                LATBINV = 0b10000000;
            }
        }
    }
}
int main ( void )
{
    /* Initialize all MPLAB Harmony modules, including application(s). */
    SYS_Initialize ( NULL );

    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // initialize I2C
    initI2C2();
    
    // initialize IMU
    initIMU();
    
    // initialize SPI, LCD
//    SPI1_init();
//    LCD_init();
    
    __builtin_enable_interrupts();
    
//    LCD_clearScreen(BLACK);   
//    char message[MAX_LENGTH];  
    unsigned char data[14];
//    float accX, accY;
    _CP0_SET_COUNT(0);
    while ( true )
    {
        if (_CP0_GET_COUNT() > 480000) {    // 50 Hz     
            i2c_read_multiple(IMU_ADDRESS, OUT_TEMP_L, data, 14);
            temp_raw = data[1] << 8 | data[0];
            gyroX_raw = data[3] << 8 | data[2];
            gyroY_raw = data[5] << 8 | data[4];
            gyroZ_raw = data[7] << 8 | data[6];
            accX_raw = data[9] << 8 | data[8];
            accY_raw = data[11] << 8 | data[10];
            accZ_raw = data[13] << 8 | data[12];
            
//            accX = accX_raw * 2.0 / 32768; // accel in g
//            accY = accY_raw * 2.0 / 32768; // accel in g
//            accZ = accZ_raw * 2.0 / 32768; // accel in g
            
//            sprintf(message, "temp raw: %x    ", temp_raw);
//            LCD_drawString(10, 10, message, WHITE);
//            
//            sprintf(message, "accX: %f g    ", accX);
//            LCD_drawString(10, 20, message, WHITE);
//            
//            sprintf(message, "accY: %f g    ", accY);
//            LCD_drawString(10, 30, message, WHITE);
//            
//            sprintf(message, "accZ: %f g    ", accZ);
//            LCD_drawString(10, 40, message, WHITE);
//            
//            sprintf(message, "gyroX raw: %i    ", gyroX_raw);
//            LCD_drawString(10, 50, message, WHITE);
//            
//            sprintf(message, "gyroY raw: %i    ", gyroY_raw);
//            LCD_drawString(10, 60, message, WHITE);
//            
//            sprintf(message, "gyroZ raw: %i    ", gyroZ_raw);
//            LCD_drawString(10, 70, message, WHITE);
            
            _CP0_SET_COUNT(0);
        }
        
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );

    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}
Exemple #25
0
void setup(){
    // DEVCFGs here

// DEVCFG0
#pragma config DEBUG = OFF // no debugging
#pragma config JTAGEN = OFF // no jtag
#pragma config ICESEL = ICS_PGx1 // use PGED1 and PGEC1
#pragma config PWP = OFF // no write protect
#pragma config BWP = OFF // not boot write protect
#pragma config CP = OFF // no code protect

// DEVCFG1
#pragma config FNOSC = PRIPLL // use primary oscillator with pll
#pragma config FSOSCEN = OFF // turn off secondary oscillator
#pragma config IESO = OFF // no switching clocks
#pragma config POSCMOD = HS // high speed crystal mode
#pragma config OSCIOFNC = OFF // free up secondary osc pins
#pragma config FPBDIV = DIV_1 // divide CPU freq by 1 for peripheral bus clock
#pragma config FCKSM = CSDCMD // do not enable clock switch
#pragma config WDTPS = PS1 // slowest wdt
#pragma config WINDIS = OFF // no wdt window
#pragma config FWDTEN = OFF // wdt off by default
#pragma config FWDTWINSZ = WINSZ_25 // wdt window at 25%

// DEVCFG2 - get the CPU clock to 40MHz
#pragma config FPLLIDIV = DIV_2 // divide input clock to be in range 4-5MHz
#pragma config FPLLMUL = MUL_20 // multiply clock after FPLLIDIV
#pragma config UPLLIDIV = DIV_1 // divide clock after FPLLMUL
#pragma config UPLLEN = ON // USB clock on
#pragma config FPLLODIV = DIV_2 // divide clock by 1 to output on pin

// DEVCFG3
#pragma config USERID = 0 // some 16bit userid
#pragma config PMDL1WAY = ON // not multiple reconfiguration, check this
#pragma config IOL1WAY = ON // not multimple reconfiguration, check this
#pragma config FUSBIDIO = ON // USB pins controlled by USB module
#pragma config FVBUSONIO = ON // controlled by USB module
    
     // startup
    __builtin_disable_interrupts();
    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);
    // no cache on this chip!
    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;
    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;
    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;
    __builtin_enable_interrupts();

    // set up USER pin as input
    ANSELBbits.ANSB13 = 0;
    
    // set up LED1 pin as a digital output - 
    ANSELBbits.ANSB15 = 0;
    TRISBbits.TRISB15 = 0;
    _CP0_SET_COUNT(0);
    while(_CP0_GET_COUNT()<4000000);
    LATBbits.LATB15 = 1;


   
    // set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;
    
    
  
   
    
    
    display_init();
    acc_setup();
}
Exemple #26
0
void MIPS32 SetCP0Count(u32 count)
{
	//asm("di"); // Disable all interrupts
	_CP0_SET_COUNT(count);
	//asm("ei"); // Enable all interrupts
}
int main(void)
{
    //Startup code to run as fast as possible and get pins back from bad defaults
    __builtin_disable_interrupts(); //Disable interrupts for configuration

    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210582);

    //no cache on this chip!

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;
    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;
    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;

    // Pin B13 as USER Pin
    ANSELBbits.ANSB13 = 0;  // pin B13 as digital
    TRISBbits.TRISB13 = 1;  // pin B13 as input

    // Pin B7 as digital output (LED1)
    TRISBbits.TRISB7 = 0;  // pin B7 as output
    LATBbits.LATB7 = 1;    // LED1 ON

    // Pin B15 as output compare OC1, using Timer2 (1kHz)
    RPB15Rbits.RPB15R = 0b0101; // RB15 as OC1 (see Ouput Pin Selection TAble 11-2)
    T2CONbits.TCKPS = 0;   // Prescaler N=1
    PR2 = 39999;  // period = (PR2+1) * N * 25ns = 1ms, 10kHz --> period = 1ms
    TMR2 = 0; // initialize timer to 0
    OC1CONbits.OCM = 0b110; // PWM mode without fault pin;
    OC1RS = 10000; // 25% duty cycle (PR2+1)/2 = 10000
    OC1R = 20000; // initialize OC1 on; larter read-only
    T2CONbits.ON = 1; // turn on Timer2
    OC1CONbits.ON = 1; // turn on OC1

    // set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;

    // Set up timer1 for ISR
    T1CONbits.TCKPS = 0b01;         // Prescaler N=8
//    T1CONbits.TGATE = 0;            //(default)
//    T1CONbits.TCS = 0;              //(default)
    PR1 = 499;	// period = (PR1+1) * N * 25ns = 0.1ms, 10 kHz
    TMR1 = 0;                       // initialize timer to 0
    T1CONbits.ON = 1;               // turn on Timer1
    // Set Interrupt for Timer1
    IPC1bits.T1IP = 7;              // INT step 4: priority 7
    IPC1bits.T1IS = 0;              //             subpriority 0
    IFS0bits.T1IF = 0;              // INT step 5: clear interrupt flag
    IEC0bits.T1IE = 1;              // INT step 6: enable interrupt

    __builtin_enable_interrupts(); //re enable interrupts after configuration is complete


    // Main while loop
    while (1)
    {
        _CP0_SET_COUNT(0); // Reset core counter
        while(_CP0_GET_COUNT() < 10000000) //The CP0 timer runs at 20kHZ (Half the core frequency)
		{ if (PORTBbits.RB13 == 0){
                    break; } } //Skip the wait if USER Pin B13 is pushed

        if (PORTBbits.RB13 == 1){ LATBINV = 0x0080; } // toggle LED1
        else{ LATBbits.LATB7 = 1; } // LED1 ON

//*****This logic is better in an an ISR to guarantee a smoother dimmer*****//
//        unsigned int ADCval;
//        ADCval = readADC();            // read ADC (0-1024)
//        OC1RS =  40000* ADCval/1024;   //convert ADV calue to duty cycle
    }
}
Exemple #28
0
void NU32_WriteCoreTimer(unsigned int value) {
    // the core timer updates the cp0 COUNT register, so set that
    _CP0_SET_COUNT(value);
}
Exemple #29
0
int main ()
{
    
    __builtin_disable_interrupts();

// set the CP0 CONFIG register to indicate that
// kseg0 is cacheable (0x3) or uncacheable (0x2)
// see Chapter 2 "CPU for Devices with M4K Core"
// of the PIC32 reference manual
__builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

// no cache on this chip!

// 0 data RAM access wait states
BMXCONbits.BMXWSDRM = 0x0;

// enable multi vector interrupts
INTCONbits.MVEC = 0x1;

// disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
DDPCONbits.JTAGEN = 0;

__builtin_enable_interrupts();


TRISBbits.TRISB7 = 0;       // set up LED1 pin as a digital output
//    int i,a;
//    display_init();
//    for (i = 0; i<10; ++i)
//    {
//        a = 30+i;
//        display_pixel_set(15,a,1);
//        display_draw();
//    }
    char input[100]; 
    int i=0;
    short accels[3]; // accelerations for the 3 axes
    short mags[3]; // magnetometer readings for the 3 axes
    short temp; 
    
    acc_setup();    //initialize accelerometer
	display_init(); //initialize LED screen
    

    
    float xg, yg, zg;

    
    while(1)
    {
    start_position[0] = 0;
    start_position[1] = 0;
    center_position[0] = 32;
    center_position[1] = 64;
    acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6);
    acc_read_register(OUT_X_L_M, (unsigned char *) mags, 6);
    acc_read_register(TEMP_OUT_L, (unsigned char *) &temp, 2);
    
    xg = (float) accels[0]/16000;
    yg = (float) accels[1]/16000;
    zg = (float) accels[2]/16000;
    
    sprintf(input,"x: %.2f y: %.2f z: %.2f ", xg,yg,zg);
    display_reset();
    display_draw();
    display_ggraph(xg,yg);
    while(input[i])
    {

        display_message(input[i]);
        i++;
        start_position[1] = start_position[1]+5;
//        if(start_position[1]+5>128)
//        {start_position[0]+1;}
//        else
//        {start_position[1] = start_position[1]+5;}
    }
    i = 0;
    display_draw();
    _CP0_SET_COUNT(0);
    while(_CP0_GET_COUNT()<5000000)
    {;}
    
    }
	return (0);
}
Exemple #30
0
int main(void) {
    
//Startup
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that
    // kseg0 is cacheable (0x3) or uncacheable (0x2)
    // see Chapter 2 "CPU for Devices with M4K Core"
    // of the PIC32 reference manual
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // no cache on this chip!

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital
    DDPCONbits.JTAGEN = 0;

    __builtin_enable_interrupts();

ANSELBbits.ANSB13 = 0; // 0 for digital, 1 for analog
ANSELBbits.ANSB15 = 0; // 0 for digital, 1 for analog

    T2CONbits.TCKPS = 0;	//Setting prescaler to 1 (0 corresponds to 1)
    PR2 = 39999;            //Setting PR for timer 2 to 39999
    TMR2 = 0;				//Setting Timer 2 to 0
    OC1CONbits.OCTSEL = 0;	//Telling OC1 to use timer 2
    OC1CONbits.OCM = 0b110;	//Telling OC1 to use PWM without the fault
    OC1RS = 20000;			//Setting initial duty cycle to 20000/(39999+1)*100% = 50%
    OC1R = 20000;			//Updating duty cycles to 20000/(39999+1)*100% = 50%
    T2CONbits.ON = 1;		//turn on timer
    OC1CONbits.ON = 1;		//turn on OC code

// set up USER pin as input
    TRISBbits.TRISB13 = 1; // set pin B13 to be digital INPUT
    // U1RXRbits.U1RXR = 0b0011; // set U1RX to pin B13 (Input pin from User button)

// set up LED1 pin as a digital output
    TRISBbits.TRISB7 = 0; // set pin B7 to be digital OUTPUT
    // LATBbits.LATB7 = 1;
    // RPB7Rbits.RPB7R = 0b0001; //set B7 to U1TX (Output pin for LED1)
    
// set up LED2 as OC1 using Timer2 at 1kHz
    // TRISBbits.TRISB15 = 0; // set B15 to digital OUTPUT
    RPB15Rbits.RPB15R = 0b0101; // set B15 to U1TX (Output pin for OC1)

// set up A0 as AN0
    ANSELAbits.ANSA0 = 1;
    AD1CON3bits.ADCS = 3;
    AD1CHSbits.CH0SA = 0;
    AD1CON1bits.ADON = 1;
    
    display_init();
    int number = 1337;
    sprintf(buffer,"Hello World %d!", number);
    display_write(28,32,buffer);
    /*int ii;                           // Draw a line from position (x,y) = (0,15) to (127,15)
    for (ii = 0; ii < 128; ii++){
    display_pixel_set(15,ii,1);
    display_draw();
    }*/
    
    while (1){
     // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output %
        
        _CP0_SET_COUNT(0);
        LATBINV = 0b10000000;
        
            while(_CP0_GET_COUNT()<10000000){
                OC1RS = readADC()*(PR2+1)/1023; // delay for 10M core ticks, 0.5s
                if (PORTBbits.RB13 == 1){
                    ;// nothing
                }
                else{
                    LATBINV = 0b10000000;
                }
            }
        
    }
}