Exemplo n.º 1
0
void vHardwareUseMultiVectoredInterrupts( void )
{
	/* Enable multi-vector interrupts. */
	_CP0_BIS_CAUSE( 0x00800000U );
	INTCONSET = _INTCON_MVEC_MASK;
	__builtin_enable_interrupts();
}
Exemplo n.º 2
0
int main()
{
    pic_init();
    logging_init();
    button_init();
    bumper_init();
    pwm_init();
    motor_timer_init();
    button_timer_init();
    sound_config_timer_init();

    //put_str_ln("Initialising DMA...");
    //init_DMA();

    //put_str_ln("Initialising ADC...");
    init_ADC();

    // setup des interrupts
    INTCONSET = _INTCON_MVEC_MASK;
    __builtin_enable_interrupts();

    if (VERBOSE_PIC_STATUS)
        put_str_ln("Ready.");

    while (1)
    {
        WDTCONbits.WDTCLR = 1;  // ecrire un 1 dans ce bit force la reinitialisation du watchdog
    }
}
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){;}
        }
    }   
}
Exemplo n.º 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){
            ;
        }
    }   
}
Exemplo n.º 5
0
// Perform startup routines:
//  Make NU32_LED1 and NU32_LED2 pins outputs (NU32_USER is by default an input)
//  Initialize the serial port - UART3 (no interrupt) 
//  Enable interrupts
void NU32_Startup() {
  // disable interrupts
  __builtin_disable_interrupts();

  // enable the cache 
  // This command sets the CP0 CONFIG register
  // the lower 4 bits can be either 0b0011 (0x3) or 0b0010 (0x2)
  // to indicate that kseg0 is cacheable (0x3) or uncacheable (0x2)
  // see Chapter 2 "CPU for Devices with M4K Core" of the PIC32 reference manual
  // most of the other bits have prescribed values
  // microchip does not provide a _CP0_SET_CONFIG macro, so we directly use
  // the compiler built-in command _mtc0
  // to disable cache, use 0xa4210582 
  __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); 
  
  // set the prefectch cache wait state to 2, as per the
  // electrical characteristics data sheet
  CHECONbits.PFMWS = 0x2;   

  //enable prefetch for cacheable and noncacheable memory
  CHECONbits.PREFEN = 0x3; 

  // 0 data RAM access wait states
  BMXCONbits.BMXWSDRM = 0x0;
  
  // enable multi vector interrupts
  INTCONbits.MVEC = 0x1;

  // disable JTAG to get B10, B11, B12 and B13 back
  DDPCONbits.JTAGEN = 0;

  TRISFCLR = 0x0003;  // Make F0 and F1 outputs (LED1 and LED2)
  NU32_LED1 = 1;      // LED1 is off
  NU32_LED2 = 0;      // LED2 is on

  // turn on UART3 without an interrupt
  U3MODEbits.BRGH = 0; // set baud to NU32_DESIRED_BAUD
  U3BRG = ((NU32_SYS_FREQ / NU32_DESIRED_BAUD) / 16) - 1;

  // 8 bit, no parity bit, and 2 stop bit (8N2 setup)
  U3MODEbits.PDSEL = 0;
  U3MODEbits.STSEL = 0;

  // configure TX & RX pins as output & input pins
  U3STAbits.UTXEN = 1;
  U3STAbits.URXEN = 1;
  // configure hardware flow control using RTS and CTS
  U3MODEbits.UEN = 0;

  // enable the uart
  U3MODEbits.ON = 1;

  __builtin_enable_interrupts();
}
Exemplo n.º 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
    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();
    }   
}
Exemplo n.º 7
0
void i2c_master_setup() {
  int ie = __builtin_disable_interrupts();
  I2C1BRG = 90;                       // I2CBRG = [1/(2*Fsck) - PGD]*Pblck - 2
                                      // Fsck is the frequency (usually 100khz or 400 khz), PGD = 104ns
                                      // this is 400 khz mode
                                      // enable the i2c interrupts
  IPC8bits.I2C1IP  = 1;            // master has interrupt priority 1
  IEC1bits.I2C1MIE = 1;            // master interrupt is enabled
  IFS1bits.I2C1MIF = 0;            // clear the interrupt flag
  I2C1CONbits.ON = 1;                 // turn on the I2C2 module
  
  if(ie & 1) {
    __builtin_enable_interrupts();
  }
}
Exemplo n.º 8
0
int main(void)
{
    //set high on LED pin as output value
    //LATFbits.LATF1 = 1;

    //set LED pin as output
    TRISFbits.TRISF1 = 0;

    //set BUT pin as input
    TRISDbits.TRISD8 = 1;

    T2CON = 0x0;
    TMR2 = 0x0;
    PR2 = 62500;                    // 8Mhz : 8 (peripheral clock divisor) : 16 (prescale)
                                    // = 62 500 timer ticks per sec
    
    T2CONbits.TCKPS = 0b100;        // 1:16 prescale value

    //setup INT1 to interrupt on falling edge
    INTCONbits.INT1EP = 0;

    //clear interrupt
    IFS0bits.T2IF = 0;
    IFS0bits.INT1IF = 0;

    //set priority
    IPC2bits.T2IP = 7;
    IPC1bits.INT1IP = 4;

    //starting interrupts  
    IEC0bits.T2IE = 1;
    IEC0bits.INT1IE = 1;

    INTCONSET = _INTCON_MVEC_MASK;
    __builtin_enable_interrupts();
    
    //start timer
    T2CONbits.ON = 1;

    //set timer 3 as already ended
    g_timer3_ended = 1;
    
    while (1)
    {
        WDTCONbits.WDTCLR = 1;
    }
    return (0);
}
Exemplo n.º 9
0
int main(void)
{
    //set high on LED pin as output value
    //LATFbits.LATF1 = 1;

    //set LED pin as output
    TRISFbits.TRISF1 = 0;

    //set BUT pin as input
    TRISDbits.TRISD8 = 1;

    //setup INT1 to interrupt on falling edge
    INTCONbits.INT1EP = 0;

    //clear interrupt
    IFS0bits.INT1IF = 0;

    //set priority
    IPC1bits.INT1IP = 4;

    //UART config
    U1BRG = 25;                                        //(8Mhz / 2) / (16 * 9600) - 1
    U1STA = 0;
    U1MODE = 0x8000;
    //U1MODEbits.PDSEL = 0x0;
    //U1MODEbits.STSEL = 0x0;

    //starting interrupt
    IPC6bits.U1IP = 0x7;

    U1STAbits.UTXISEL = 0x1;
    U1STAbits.UTXEN = 0x1;
    //U1MODEbits.UARTEN = 1;
    //U1MODEbits.ON = 1;

    //starting interrupts  
    IEC0bits.INT1IE = 1;
    IEC0bits.U1TXIE = 0x1;

    INTCONSET = _INTCON_MVEC_MASK;
    __builtin_enable_interrupts();
    
    while (1)
    {
        WDTCONbits.WDTCLR = 1;
    }
    return (0);
}
Exemplo n.º 10
0
int main() {

    char str[200];

    ANSELBbits.ANSB13 = 0; // make analog input digital
    U1RXRbits.U1RXR = 0b0000; // set U1RX to pin A2
    RPB15Rbits.RPB15R = 0b0101; // set B15 to U1TX

    __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
    TRISBbits.TRISB13 = 1;
    // set up LED1 pin as a digital output
    TRISBbits.TRISB7 = 0;

	//use the functions to print "Hello world 1337!" at 28,32
    int num = 1337;
    display_init();
    display_clear();
    sprintf(str, "Hello world %d!", num);

    display_oled(str,28,32);


    return 0;
}
Exemplo n.º 11
0
int main(int argc, char** argv) {
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);
    BMXCONbits.BMXWSDRM = 0x0;
    INTCONbits.MVEC = 0x1;
    DDPCONbits.JTAGEN = 0;
     // do your TRIS and LAT commands here
    ANSELA = 0; // A Analogic off
    ANSELB = 0; // B Analogic off

    TRISBbits.TRISB2 = 0; // A4 output
    TRISAbits.TRISA0 = 0; // A4 output

    TRISAbits.TRISA4 = 0; // A4 output
    TRISBbits.TRISB4 = 1; // B4 inuput
    
    __builtin_disable_interrupts();
    
    // starts the PINEX
    
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);
    BMXCONbits.BMXWSDRM = 0x0;
    INTCONbits.MVEC = 0x1;
    DDPCONbits.JTAGEN = 0;
    __builtin_enable_interrupts();
    uart_init();
    return 0;
    
    start_PWM();
    
    lcd_start();
    while(1)
    {
        LATAbits.LATA4 = 0;
        lcd_clearScreen(LCD_COLOR_BLACK);
        lcd_printf("hello\nworld!",LCD_COLOR_WHITE);
        //unsigned int t = timer_start();
        //while (!timer_timeout(t, 500 * TIMER_MILLISECOND));
        blink();
        LATAbits.LATA4 = 1;
        lcd_clearScreen(LCD_COLOR_WHITE);
        lcd_printf("hello!",LCD_COLOR_BLACK);
        //t = timer_start();
        //while (!timer_timeout(t, 500 * TIMER_MILLISECOND));
    }   
    return (EXIT_SUCCESS);
}
Exemplo n.º 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
    
    __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
    }
    
    
}
Exemplo n.º 13
0
int main() {
    unsigned char master_read  = 0x00; 

    __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 = 1; //set portA4 as input pin for button input
     TRISBbits.TRISB4 = 0; //set port B4 as output pin for LED
    
     
     i2c_init();
     i2c_expander_init();
     
    __builtin_enable_interrupts();
    
  
   
    
    while(1) {
        master_read = getExpander();
    if(master_read>>7 ==0x01)
   {
          setExpander(0,1);
   }else{
        
        setExpander(0,0);
   }
        
    }
Exemplo n.º 14
0
int main() {
    unsigned char master_read  = 0x00; 

    __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;   
     
     TRISAbits.TRISA4 = 1; //set port A4 (PIN12)as input pin for button input
     TRISBbits.TRISB4 = 0; //set port B4 (PIN11)as output pin for LED
    
     
     i2c_init();
     i2c_imu_init();
     
    __builtin_enable_interrupts();
    
    //WHO_AM_I (0Fh)
    i2c_master_start();
    i2c_master_send(0xD6);
    i2c_master_send(0x0F);
    i2c_master_restart();
    i2c_master_send(0xD7);
    unsigned char input=i2c_master_recv();
    i2c_master_ack(1);
    i2c_master_stop();
    
    
   
    
    
    
    
}
Exemplo n.º 15
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;   //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.
        }
    }
}
Exemplo n.º 16
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;

        }
    }
    
    
}
Exemplo n.º 17
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
        }
                
    }
    
    
}
Exemplo n.º 18
0
// Initializes the module and the peripherals it uses
void motor_init(void) {

	// initializing the current control loop ISR - 5 kHz - Timer2
	__builtin_disable_interrupts(); 	// INT step 2: disable interrupts at CPU

																		// INT step 3: 	setup TMR2 to call ISR at frequency of 5 kHz
	PR2 = 15999; 											// 							set period register to 16,000
	TMR2 = 0;													// 							initialize count to 0
	T2CONbits.TCKPS = 0; 							// 							set prescaler to 1:1
	T2CONbits.ON = 1; 								// 							turn on Timer2
	IPC2bits.T2IP = 5; 								// INT step 4: 	priority 5
	IPC2bits.T2IS = 0; 								// 							subpriority 0
	IFS0bits.T2IF = 0; 								// INT step 5: 	clear interrupt flag
	IEC0bits.T2IE = 1; 								// INT step 6: 	enable interrupt
	__builtin_enable_interrupts(); 		// INT step 7: 	enable interrupts at CPU


	// initializing the PWM output - 20 kHz - Timer3
	OC1CONbits.OCTSEL = 1;   // Select Timer3 for comparison

  T3CONbits.TCKPS = 0;     // Timer3 prescaler N=1 (1:1)
  PR3 = 3999;              // period = (PR3+1) * N * 12.5 ns = 20 kHz
  TMR3 = 0;                // initial TMR3 count is 0

  OC1CONbits.OCM = 0b110;  // PWM mode with no fault pin; other OC1CON bits are defaults

  OC1RS = 0;           		 // duty cycle = OC1RS/(PR3+1) = 0%
  OC1R = 0;            		 // initialize before turning OC1 on; afterward it is read-only

  T3CONbits.ON = 1;        // turn on Timer3
  OC1CONbits.ON = 1;       // turn on OC1


	// initializing the direction pin output - AN10/RB10 digital output
	AD1PCFGbits.PCFG10 = 1;   // pin AN10 is digital pin
	TRISBbits.TRISB10 = 0;		// RB10 is an output pin
	LATBbits.LATB10 = 1;

}
Exemplo n.º 19
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);
}
Exemplo n.º 20
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);
        }
    } 
}
Exemplo n.º 21
0
Arquivo: main.c Projeto: srshull/ME433
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
        
    }
    
    
}
Exemplo n.º 22
0
Arquivo: pic_code.c Projeto: pg8/ME433
void startup(void) {
 //    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();
}
int main(void) {

	// Cache on, min flash wait, interrupts on, LED/button init, UART init
  NU32_Startup();


	// =============================================================================================
	// USER Button Interrupt
	// =============================================================================================
  __builtin_disable_interrupts(); 	// step 2: disable interrupts
  INTCONCLR = 0x1;               	 	// step 3: INT0 triggers on falling edge
  IPC2CLR = 0x1F << 24;           	// step 4: clear the 5 pri and subpri bits
  IPC2 |= 9 << 24;               	 	// step 4: set priority to 2, subpriority to 1
  IFS0bits.INT2IF = 0;           	 	// step 5: clear the int flag, or IFS0CLR=1<<3
  IEC0SET = 1 << 11;              	// step 6: enable INT0 by setting IEC0<3>
  __builtin_enable_interrupts();  	// step 7: enable interrupts


	// =============================================================================================
	// PWM and digital output for piezoelectric droplet generator
	// =============================================================================================
  OC1CONbits.OCTSEL = 1;  			 		// Select Timer3 for comparison

  T3CONbits.TCKPS = 0;     					// Timer3 prescaler N=1 (1:1)
  PR3 = 3999;              					// period = (PR3+1) * N * 12.5 ns = 20 kHz
  TMR3 = 0;                					// initial TMR3 count is 0

  OC1CONbits.OCM = 0b110; 					// PWM mode with no fault pin; other OC1CON bits are defaults
  OC1RS = 0;           							// duty cycle = OC1RS/(PR3+1) = 75%
  OC1R = 0;             						// initialize before turning OC1 on; afterward it is read-only

  T3CONbits.ON = 1;        					// turn on Timer3
  OC1CONbits.ON = 1;       					// turn on OC1


	// Set A10/A2 to digital output pins
	TRISAbits.TRISA10 = 0;						// RA10 is an output pin
	TRISAbits.TRISA2 = 0;							// RA2 is an output pin

	//-Vcc: set L1 to HIGH, L2 to LOW, PWMA to >0
	OC1RS = 4000;
	LATAbits.LATA10 = 1;
	LATAbits.LATA2 = 0;

/*
	// =============================================================================================
	// TrackCam ExSync Trigger
	// =============================================================================================

	// Set A3 to digital output pin
	TRISAbits.TRISA3 = 0;						// RA3 is an output pin

	//Set A3 to HIGH
	LATAbits.LATA3 = 1;
*/


	// =============================================================================================
	// Keep program running to look for interrupts
	// =============================================================================================
  while(1) {
		;
	}

  return 0;
}
Exemplo n.º 24
0
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
    }
}
Exemplo n.º 25
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;
        }
    }
    }
}
Exemplo n.º 26
0
/* main ***********************************************************************/
int main (void){
    unsigned int temp_ui;
    CO_NMT_reset_cmd_t reset = CO_RESET_NOT;

    /* Configure system for maximum performance. plib is necessary for that.*/
    /* SYSTEMConfig(CO_FSYS*1000, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); */

    /* Enable system multi vectored interrupts */
    INTCONbits.MVEC = 1;
    __builtin_enable_interrupts();

    /* Disable JTAG and trace port */
    DDPCONbits.JTAGEN = 0;
    DDPCONbits.TROEN = 0;


    /* Verify, if OD structures have proper alignment of initial values */
    if(CO_OD_RAM.FirstWord != CO_OD_RAM.LastWord) while(1) CO_clearWDT();
    if(CO_OD_EEPROM.FirstWord != CO_OD_EEPROM.LastWord) while(1) CO_clearWDT();
    if(CO_OD_ROM.FirstWord != CO_OD_ROM.LastWord) while(1) CO_clearWDT();


    /* initialize EEPROM - part 1 */
#ifdef USE_EEPROM
    CO_ReturnError_t eeStatus = CO_EE_init_1(&CO_EEO, (uint8_t*) &CO_OD_EEPROM, sizeof(CO_OD_EEPROM),
                            (uint8_t*) &CO_OD_ROM, sizeof(CO_OD_ROM));
#endif


    programStart();


    /* increase variable each startup. Variable is stored in eeprom. */
    OD_powerOnCounter++;


    while(reset != CO_RESET_APP){
/* CANopen communication reset - initialize CANopen objects *******************/
        CO_ReturnError_t err;
        uint16_t timer1msPrevious;
        uint16_t TMR_TMR_PREV = 0;
        uint8_t nodeId;
        uint16_t CANBitRate;

        /* disable timer and CAN interrupts */
        CO_TMR_ISR_ENABLE = 0;
        CO_CAN_ISR_ENABLE = 0;
        CO_CAN_ISR2_ENABLE = 0;

        /* Read CANopen Node-ID and CAN bit-rate from object dictionary */
        nodeId = OD_CANNodeID;
        if(nodeId<1 || nodeId>127) nodeId = 0x10;
        CANBitRate = OD_CANBitRate;/* in kbps */

        /* initialize CANopen */
        err = CO_init(ADDR_CAN1, nodeId, CANBitRate);
        if(err != CO_ERROR_NO){
            while(1) CO_clearWDT();
            /* CO_errorReport(CO->em, CO_EM_MEMORY_ALLOCATION_ERROR, CO_EMC_SOFTWARE_INTERNAL, err); */
        }


        /* initialize eeprom - part 2 */
#ifdef USE_EEPROM
        CO_EE_init_2(&CO_EEO, eeStatus, CO->SDO, CO->em);
#endif


        /* initialize variables */
        timer1msPrevious = CO_timer1ms;
        OD_performance[ODA_performance_mainCycleMaxTime] = 0;
        OD_performance[ODA_performance_timerCycleMaxTime] = 0;
        reset = CO_RESET_NOT;



        /* Configure Timer interrupt function for execution every 1 millisecond */
        CO_TMR_CON = 0;
        CO_TMR_TMR = 0;
        #if CO_PBCLK > 65000
            #error wrong timer configuration
        #endif
        CO_TMR_PR = CO_PBCLK - 1;  /* Period register */
        CO_TMR_CON = 0x8000;       /* start timer (TON=1) */
        CO_TMR_ISR_FLAG = 0;       /* clear interrupt flag */
        CO_TMR_ISR_PRIORITY = 3;   /* interrupt - set lower priority than CAN (set the same value in interrupt) */

        /* Configure CAN1 Interrupt (Combined) */
        CO_CAN_ISR_FLAG = 0;       /* CAN1 Interrupt - Clear flag */
        CO_CAN_ISR_PRIORITY = 5;   /* CAN1 Interrupt - Set higher priority than timer (set the same value in '#define CO_CAN_ISR_PRIORITY') */
        CO_CAN_ISR2_FLAG = 0;      /* CAN2 Interrupt - Clear flag */
        CO_CAN_ISR2_PRIORITY = 5;  /* CAN Interrupt - Set higher priority than timer (set the same value in '#define CO_CAN_ISR_PRIORITY') */


        communicationReset();


        /* start CAN and enable interrupts */
        CO_CANsetNormalMode(ADDR_CAN1);
        CO_TMR_ISR_ENABLE = 1;
        CO_CAN_ISR_ENABLE = 1;

#if CO_NO_CAN_MODULES >= 2
        CO_CANsetNormalMode(ADDR_CAN2);
        CO_CAN_ISR2_ENABLE = 1;
#endif


        while(reset == CO_RESET_NOT){
/* loop for normal program execution ******************************************/
            uint16_t timer1msCopy, timer1msDiff;

            CO_clearWDT();


            /* calculate cycle time for performance measurement */
            timer1msCopy = CO_timer1ms;
            timer1msDiff = timer1msCopy - timer1msPrevious;
            timer1msPrevious = timer1msCopy;
            uint16_t t0 = CO_TMR_TMR;
            uint16_t t = t0;
            if(t >= TMR_TMR_PREV){
                t = t - TMR_TMR_PREV;
                t = (timer1msDiff * 100) + (t / (CO_PBCLK / 100));
            }
            else if(timer1msDiff){
                t = TMR_TMR_PREV - t;
                t = (timer1msDiff * 100) - (t / (CO_PBCLK / 100));
            }
            else t = 0;
            OD_performance[ODA_performance_mainCycleTime] = t;
            if(t > OD_performance[ODA_performance_mainCycleMaxTime])
                OD_performance[ODA_performance_mainCycleMaxTime] = t;
            TMR_TMR_PREV = t0;


            /* Application asynchronous program */
            programAsync(timer1msDiff);

            CO_clearWDT();


            /* CANopen process */
            reset = CO_process(CO, timer1msDiff, NULL);

            CO_clearWDT();


#ifdef USE_EEPROM
            CO_EE_process(&CO_EEO);
#endif
        }
    }


/* program exit ***************************************************************/
//    CO_DISABLE_INTERRUPTS();

    /* delete objects from memory */
    programEnd();
    CO_delete(ADDR_CAN1);

    /* reset */
    SYSKEY = 0x00000000;
    SYSKEY = 0xAA996655;
    SYSKEY = 0x556699AA;
    RSWRSTSET = 1;
    temp_ui = RSWRST;
    while(1);
}
Exemplo n.º 27
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;
            }
        }
    }
}
Exemplo n.º 28
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



    }


}
Exemplo n.º 30
0
void 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
    TRISA = 0xFFCF; 
    TRISB = 0b0001111001110011;
   i2c_master_setup();
    ANSELBbits.ANSB2 = 0; //SDA2 set to digital
    ANSELBbits.ANSB3 = 0; //SCL2 set to digital
 
    __builtin_enable_interrupts();
   // SYSTEMConfigPerformance(48000000);
    
    SPI1_init(); 
    LCD_init();
    LCD_clearScreen(0);
//    RPB13Rbits.RPB13R = 0b0011; //SDO
//    SDI1Rbits.SDI1R = 0b0000; //A1
    RPB7Rbits.RPB7R = 0b0101; //OC1
    RPB8Rbits.RPB8R = 0b0101; //OC2
    PORTAbits.RA4 = 1; //led init
    T2CONbits.TCKPS = 2; //timer 2 prescale = 1:4
    PR2 = 1999; //period = (PR2+1) * N * 12.5 ns = 100 us, 10 kHz
    TMR2 = 0;
    OC1RS = 1000;
    OC1R = 1000;
    OC2RS = 1000;
    OC2R = 1000;
    OC1CONbits.OCTSEL = 0; //select timer2
    OC2CONbits.OCTSEL = 0;
    OC1CONbits.OCM = 0b110; //set pwm mode
    OC2CONbits.OCM = 0b110;
    T2CONbits.ON = 1;
    OC1CONbits.ON = 1;
    OC2CONbits.ON = 1;
    
    unsigned char x = 0; //sine counter
    unsigned char y = 0; //triangle counter
    char pressed = 0; //for tracking button logic
    char counter = 0;
    char m = 100; //(triangle wave frequency is 1000/2m)
    unsigned char voltage = 0;
    unsigned char channel = 0;
    int bytes = 14;
    unsigned char i2cdata[bytes];
    unsigned char i2cdatatest;
    unsigned char i2cwhoami;
    short temp = 0;
    short accel_x = 0;
    short accel_y = 0;
    short accel_z = 0;
    short gyro_x = 0;
    short gyro_y = 0;
    short gyro_z = 0;
    char i2cdatacount = 0;
    char textbuffer[20];
    char length = 0;
    
    i2cwhoami = i2c_master_read(GYRO,WHOAMI,0,0);
    i2c_master_write(GYRO,CTRL1_XL,0b10000000,0);
    i2c_master_write(GYRO,CTRL2_G,0b10000000,0);
    i2c_master_write(GYRO,CTRL3_C,0b00000100);
    //i2cdatatest = i2c_master_read(GYRO,CTRL1_XL,0,0);
//    i2c_master_write(GYRO,CTRL1_XL,0b10000000,1);
//    i2c_master_send(0b10000000);
//    i2c_master_send(0b00000100);
//    i2c_master_stop();
  
    
   
    CS = 1;
    int leet = 1337;
    sprintf(textbuffer,"Hello world %d!",leet);
    char text[2] = {'H','6'};
 
    //int text[5] = {40,30,50,30,20};
     LCD_clearScreen(0);
     
    while(1) {
        length = sizeof(textbuffer);//size must be taken here otherwise pointer size is taken instead of 
       LCD_type(28,32,textbuffer,length,0b1111100000000000);
       //LCD_char(28,32,30,0b1111100000000000);
        
    
       i2c_master_multiread(GYRO,OUT_TEMP_L,bytes,i2cdata);
//       //i2cdatatest = i2c_master_read(GYRO,0x28,0,0);
       temp = i2cdata[1];
       temp = (temp<<8)|i2cdata[0];
       temp = (unsigned short)temp;
        
       gyro_x = i2cdata[3];
       gyro_x = (gyro_x<<8)|i2cdata[2];
       gyro_x = (unsigned short)gyro_x;
       
       gyro_y = i2cdata[5];
       gyro_y = (gyro_y<<8)|i2cdata[4];
       gyro_y = (unsigned short)gyro_y;
       
       gyro_z = i2cdata[7];
       gyro_z = (gyro_z<<8)|i2cdata[6];
       gyro_z = (unsigned short)gyro_z;
       
       accel_x = i2cdata[9];
       accel_x = (i2cdata[9]<<8)|i2cdata[8];
       accel_x = (unsigned short)accel_x;
       
      accel_y = i2cdata[11];
       accel_y = (i2cdata[11]<<8)|i2cdata[10];
       accel_y = (unsigned short)accel_y;
       
       accel_z = i2cdata[13];
       accel_z = (accel_z<<8)|i2cdata[12];
       accel_z = (unsigned short)accel_z;
       
        
       OC1RS = floor((accel_x/16.768));
       OC2RS = floor((accel_y/16.768));
       if(OC1RS>2999){
           OC1RS = 2000;
            }
       else if(OC1RS<1000){
           OC1RS = 0;
       }
       else{
           OC1RS = OC1RS - 1000;
       }
       
       
       if(OC2RS>2999){
           OC2RS = 2000;
            }
       else if(OC2RS<1000){
           OC2RS = 0;
       }
       else{
           OC2RS = OC2RS - 1000;
       }
//       OC1R = floor((gyro_x/3.2768 + 10000));
//       OC2R = floor((gyro_y/3.2768 + 10000));
//-------------SPI debugging for IMU------------------
//       if (!PORTBbits.RB4){
//       CS = 0;
//            channel = counter;
//            //voltage = floor(100*sin((x*2*pi)/100)+100);
//            voltage = i2cwhoami;
//            //char voltage = 0b10101001;
//            spi1_set(channel,voltage);
//            //delay(6000);
//            CS = 1;
//       }
//       else {
//            CS = 0;
//            channel = counter;
//            //voltage = floor(100*sin((x*2*pi)/100)+100);
//            voltage = i2cdata[0];
//            //char voltage = 0b10101001;
//            spi1_set(channel,voltage);
//            //delay(6000);
//            CS = 1;
//            //++i2cdatacount %14;
//       }
//-------------------------------------------------------
       delay(960000);
                 }
            }