Example #1
0
int main(int argc, char** argv) {
    PT_setup();
    
	// Enable multivector interrupts
    INTEnableSystemMultiVectoredInt();
	// Initialize threads
    PT_INIT(&pt_cap);
    
    
    PT_INIT(&pt_blink);
    PT_INIT(&pt_cap_read)
    
    //init the display
    tft_init_hw();
    tft_begin();
    tft_fillScreen(ILI9340_BLACK);
    
	// Set orientation of the display
    tft_setRotation(1);
	
	// Set up pins
    PerPinSetup();
    while(1){
        PT_SCHEDULE(protothread_blink(&pt_blink));
        PT_SCHEDULE(protothread_cap(&pt_cap));
        PT_SCHEDULE(protothread_cap_read(&pt_cap_read));
    }
  
}
Example #2
0
int main(void)
{

  // === config the uart, DMA, SPI ===========
  PT_setup();

  // == SPI ==
  //enable SPI at 10 MHz clock to meet digital potentiometer specifications
  SpiChnOpen(spiChn, SPI_OPEN_ON | SPI_OPEN_MODE16 | SPI_OPEN_MSTEN | SPI_OPEN_CKE_REV , spiClkDiv);
  
  // === setup system wide interrupts  ====================
  INTEnableSystemMultiVectoredInt();
    
  // === set up i/o port pin ===============================
  //Port B bits, 3,7,8, and 9 are used to select digital output
  //Port B bit 4 is used as chip select for treble digital potentiometer
  //Port B bit 13 is used as a select signal for output effect selection multiplexer
  //Additional functionality would use the TFT to display which output was being
  //selected
  mPORTBSetPinsDigitalOut(BIT_4 | BIT_3|BIT_7 | BIT_8 | BIT_9 |BIT_13);    //Set port as output
 
  //Port A Bits 0,2,and 3 are used to configure digital potentiometers (chip selects). Port A bit 4 is used
  //for multiplexing whether to have input from Digital effector chip, distortion,
  //or pure tonestack sound
  mPORTASetPinsDigitalOut(BIT_0 | BIT_2 | BIT_3 | BIT_4);
  // === now the threads ===================================
  
  // init the threads
  PT_INIT(&pt_cmd);
  PT_INIT(&pt_time);
  
  //==Digipot spi stuff
   // SCK2 is pin 26 
    // SDO1 (MOSI) is in PPS output group 1, could be connected to RB5 which is pin 14
    PPSOutput(2, RPB5, SDO1);
    // control CS for DAC
    //mPORTBSetPinsDigitalOut(BIT_4); //CS
    mPORTBSetBits(BIT_4 | BIT_6);
    //===
    
    mPORTASetBits(BIT_0 | BIT_2 | BIT_3 | BIT_4); //CS pins active high
    mPORTAClearBits(BIT_4);
    mPORTBClearBits(BIT_13);
    mPORTBSetBits(BIT_13);
    
    
  // schedule the threads
  while(1) {
      //cmd used as command center for all effects
    PT_SCHEDULE(protothread_cmd(&pt_cmd));
  }
} // main
int main(void)
{
  // === config the uart, DMA, vref, timer5 ISR =============
  PT_setup();

   // === setup system wide interrupts  ====================
  INTEnableSystemMultiVectoredInt();
    
  // === set up i/o port pin ===============
  mPORTASetBits(BIT_0 | BIT_1 );	//Clear bits to ensure light is off.
  mPORTASetPinsDigitalOut(BIT_0 | BIT_1 );    //Set port as output
  mPORTBSetBits(BIT_0 );	//Clear bits to ensure light is off.
  mPORTBSetPinsDigitalOut(BIT_0 );    //Set port as output

  // === now the threads ====================
  // init  the thread control semaphores
  PT_SEM_INIT(&control_t1, 0); // start blocked
  PT_SEM_INIT(&control_t2, 1); // start unblocked
  PT_SEM_INIT(&send_sem, 1); // start with ready to send

  // init the threads
  PT_INIT(&pt1);
  PT_INIT(&pt2);
  PT_INIT(&pt3);
  PT_INIT(&pt4);
  PT_INIT(&pt5);

  // init the optional rate scheduler
  PT_RATE_INIT();

  // schedule the threads
  while(1) {
    PT_RATE_LOOP(); // not necessary if you use PT_SCHEDULE
    PT_RATE_SCHEDULE(protothread1(&pt1), t1_rate);
    if (run_t4) PT_RATE_SCHEDULE(protothread4(&pt4), t4_rate); //run always
    PT_RATE_SCHEDULE(protothread2(&pt2), t1_rate);
    if (cmd[0] != 'k') PT_RATE_SCHEDULE(protothread3(&pt3),t3_rate);
    PT_SCHEDULE(protothread5(&pt5));

     /*
    // alternate scheme
    PT_SCHEDULE(protothread1(&pt1));
    if (run_t4) PT_SCHEDULE(protothread4(&pt4));
   PT_SCHEDULE(protothread2(&pt2));
    if (run_t4) PT_SCHEDULE(protothread4(&pt4));
    if (cmd[0] != 'k') PT_SCHEDULE(protothread3(&pt3));
     */
  }
} // main
Example #4
0
//===================== Main ======================= //
void main(void) {
    SYSTEMConfig( SYS_FREQ,  SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    ANSELA = 0; ANSELB = 0; CM1CON = 0; CM2CON = 0;

    PT_setup();
    INTEnableSystemMultiVectoredInt();

    
    // initialize the threads
    PT_INIT(&pt_blink);
    PT_INIT(&pt_capture);
    
    // initialize the display
    tft_init_hw();
    tft_begin();
    tft_fillScreen(ILI9340_BLACK);

    tft_setRotation(0); //240x320 vertical display

    // initialize the comparator
    CMP1Open(CMP_ENABLE | CMP_OUTPUT_ENABLE | CMP1_NEG_INPUT_IVREF);
    
    // initialize the timer2
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_64, 0xffffffff);

    // initialize the input capture, uses timer2
    OpenCapture1( IC_EVERY_RISE_EDGE | IC_FEDGE_RISE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON);
    ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_3 );
    INTClearFlag(INT_IC1);

    // initialize the input/output I/O
    mPORTBSetPinsDigitalOut(BIT_3);
    mPORTBClearBits(BIT_3);
    PPSOutput(4, RPB9, C1OUT);		//set up output of comparator for debugging
    PPSInput(3, IC1, RPB13);		//Either Pin 6 or Pin 24 idk
   
    //round-robin scheduler for threads
    while(1) {
	PT_SCHEDULE(protothread_blink(&pt_blink));
	PT_SCHEDULE(protothread_capture(&pt_capture));
    }
    
} //main
Example #5
0
//===================== Main ======================= //
void main(void) {
    SYSTEMConfigPerformance(PBCLK);

    ANSELA = 0; ANSELB = 0; CM1CON = 0; CM2CON = 0;

    PT_setup();
    refreshRate = 10; //msec
    
    //Temporary Random Ball generator
    head = Ball_create(50, 50, 2, 0,  0, NULL);
    int i = 1;
    for(i = 1; i < 15; i++) {
        signed char troll1 = rand() % 5;
        signed char troll2 = rand() % 5;
        struct Ball *temp = Ball_create(i*10,i*10,troll1,troll2,0,NULL);
        temp->b = head;
        head = temp;
    }
        
    // initialize the threads
    PT_INIT(&pt_calculate);
    PT_INIT(&pt_refresh);
    // initialize the display
    tft_init_hw();
    tft_begin();
    tft_fillScreen(ILI9340_BLACK);
    
    INTEnableSystemMultiVectoredInt();

    tft_setRotation(0); //240x320 vertical display
  
    //round-robin scheduler for threads
    while(1) {
        PT_SCHEDULE(protothread_calculate(&pt_calculate));
        PT_SCHEDULE(protothread_refresh(&pt_refresh));
    }
    
} //main
// === Main  ======================================================
void main(void) {
 SYSTEMConfigPerformance(PBCLK);
  
  ANSELA = 0; ANSELB = 0; CM1CON = 0; CM2CON = 0;

  // === config threads ==========
  // turns OFF UART support and debugger pin
  PT_setup();

  // === setup system wide interrupts  ========
  INTEnableSystemMultiVectoredInt();

  // init the threads
  PT_INIT(&pt_timer);
  PT_INIT(&pt_color);
  PT_INIT(&pt_anim);
  PT_INIT(&pt_key);

  // init the display
  tft_init_hw();
  tft_begin();
  tft_fillScreen(ILI9340_BLACK);
  //240x320 vertical display
  tft_setRotation(0); // Use tft_setRotation(1) for 320x240

  // seed random color
  srand(1);

  // round-robin scheduler for threads
  while (1){
      PT_SCHEDULE(protothread_timer(&pt_timer));
      PT_SCHEDULE(protothread_color(&pt_color));
      PT_SCHEDULE(protothread_anim(&pt_anim));
      PT_SCHEDULE(protothread_key(&pt_key));
      }
  } // main
void main(void) {
        // === enable system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();

//    // init the threads 
    PT_INIT(&pt_uart);

    PT_setup();
    ANSELA = 0; //make sure analog is cleared
    ANSELB = 0;
   
    OpenI2C2( I2C_EN, BRG_VAL ); 
    I2C2CON |= I2C_7BIT_ADD + I2C_SM_EN;
    I2C2CON |= I2C_RESTART_EN; //restart needed to read from ir temp
    unsigned char cmd = 0; //command line 
    unsigned char data = 0; //output data, digital value to be converted
    //unsigned char addr = 0x1C; 
    //Thermometer sensor: 5A
    
    //===========Configuring the IMU========================//
    unsigned int OPR_CODE = 0x3D;
    SendIMUData(OPR_CODE, 0x0C);        //sets IMU mode
            
    //========================================================//
            
    while(1){
//        temp = RcvIRTemp();
//        cels = temp / 50 - 273; //convert read value to celsius
//        delay_ms(10);
        PT_SCHEDULE(protothread_uart(&pt_uart));
        temp = RcvIMUData(0x20);
        delay_ms(10);

    }


//    // PuTTY
    clrscr();  //clear PuTTY screen
    home();
//    // By default, MPLAB XC32's libraries use UART2 for STDOUT.
//    // This means that formatted output functions such as printf()
//    // will send their output to UART2

//-----------------------------------------------//    
   
   
   
   


    // === configure threads ==========
    // turns OFF UART support and debugger pin, unless defines are set

    // === enable system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();

//    // init the threads 
    PT_INIT(&pt_uart);


//    // === set up input capture 
//  // based on timer3 (need to configure timer 3 seperately)
//  OpenCapture1(IC_EVERY_RISE_EDGE | IC_INT_1CAPTURE | IC_CAP_32BIT | IC_TIMER2_SRC | IC_ON);
//  // turn on the interrupt so that every capture can be recorded
//  ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_3);
//  INTClearFlag(INT_IC1);
//  // connect PIN 24 to IC1 capture unit
//  PPSInput(3, IC1, RPB13);
//  mPORTBSetPinsDigitalIn(BIT_13); //Set port as input
//
//    // round-robin scheduler for threads
    while (1) {
        PT_SCHEDULE(protothread_uart(&pt_uart));
    }
} // main
Example #8
0
// === Main  ======================================================
void main(void) {
    //SYSTEMConfigPerformance(PBCLK);
    ANSELA = 0; ANSELB = 0; 
    // === config threads ==========
    // turns OFF UART support and debugger pin, unless defines are set
    PT_setup();
    // === setup system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();
    CloseADC10();	// ensure the ADC is off before setting the configuration
    #define PARAM1  ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_OFF //
    // define setup parameters for OpenADC10
    // ADC ref external  | disable offset test | disable scan mode | do 1 sample | use single buf | alternate mode off
    #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    // Define setup parameters for OpenADC10
    // use peripherial bus clock | set sample time | set ADC clock divider
    // ADC_CONV_CLK_Tcy2 means divide CLK_PB by 2 (max speed)
    // ADC_SAMPLE_TIME_5 seems to work with a source resistance < 1kohm
    #define PARAM3 ADC_CONV_CLK_PB | ADC_SAMPLE_TIME_5 | ADC_CONV_CLK_Tcy2 //ADC_SAMPLE_TIME_15| ADC_CONV_CLK_Tcy2
    // define setup parameters for OpenADC10
    // set AN11 and  as analog inputs
    #define PARAM4	ENABLE_AN11_ANA // pin 24
    // define setup parameters for OpenADC10
    // do not assign channels to scan
    #define PARAM5	SKIP_SCAN_ALL
    // use ground as neg ref for A | use AN11 for input A     
    // configure to sample AN11 
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11 ); // configure to sample AN4 
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above
    EnableADC10(); // Enable the ADC      
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, 400); 
    mPORTAClearBits(BIT_0 );		//Clear bits to ensure light is off.
    mPORTASetPinsDigitalOut(BIT_0 );    //Set port as output
    int CVRCON_setup;
    // set up the Vref pin and use as a DAC
    // enable module| eanble output | use low range output | use internal reference | desired step
    CVREFOpen( CVREF_ENABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
    // And read back setup from CVRCON for speed later
    // 0x8060 is enabled with output enabled, Vdd ref, and 0-0.6(Vdd) range
    CVRCON_setup = CVRCON; //CVRCON = 0x8060
	// Open the desired DMA channel.
	// We enable the AUTO option, we'll keep repeating the same transfer over and over.
	DmaChnOpen(dmaChn, 0, DMA_OPEN_AUTO);
	// set the transfer parameters: source & destination address, source & destination size, number of bytes per event
    // Setting the last parameter to one makes the DMA output one byte/interrut
    //DmaChnSetTxfer(dmaChn, sine_table, (void*) &CVRCON, sizeof(sine_table), 1, 1);
	// set the transfer event control: what event is to start the DMA transfer
    // In this case, timer2 
	DmaChnSetEventControl(dmaChn,  DMA_EV_START_IRQ(_TIMER_2_IRQ));
	// once we configured the DMA channel we can enable it
	// now it's ready and waiting for an event to occur...
	//DmaChnEnable(dmaChn);
  
    int i;
    for(i=0; i <256; i++){
        ball_scored[i] = 0x60|((unsigned char)((float)255/2*(sin(6.2832*(((float)i)/(float)256))+1))) ;
        //ball_lost[i]
       // game_over
    }

        
    // init the threads
    PT_INIT(&pt_timer);
    PT_INIT(&pt_adc);
    PT_INIT(&pt_launch);
    PT_INIT(&pt_anim);
    // init the display
    tft_init_hw();
    tft_begin();
    tft_fillScreen(ILI9340_BLACK);
    tft_setRotation(1); // Use tft_setRotation(1) for 320x240
    while (1){
        PT_SCHEDULE(protothread_timer(&pt_timer));
        PT_SCHEDULE(protothread_adc(&pt_adc));
        PT_SCHEDULE(protothread_launch_balls(&pt_launch));
        PT_SCHEDULE(protothread_anim_balls(&pt_anim));
    }
  } // main
void main(void) {
    
    PT_setup();
    ANSELA = 0; //make sure analog is cleared
    ANSELB = 0;
   
    OpenI2C1( I2C_EN, BRG_VAL ); 
    unsigned char cmd = 0; //command line 
    unsigned char data = 0; //output data, digital value to be converted
    //unsigned char addr = 0x1C; 
    //Thermometer sensor: 5A
    
//    SendData(0x75, addr);
    temp = RcvData2(addr);
    
//    while(1);
//    delay_ms(5000);
//        while(1) // run the code over and over again
//    { 
//
//        // start the I2C communication
//        StartI2C1(); // Send the Start Bit (begin of data send) 
//        IdleI2C1(); // Wait to complete 
//
//        // write the address of the chip, defined by pins AD0 and AD1 on the MAX518
//        MasterWriteI2C1 (addr); // address 
//        IdleI2C1(); 
//        
//        
//        while( !I2C1STATbits.ACKSTAT==0 ){}
//        
//        // write the command to tell the MAX518 to change its output on output 0
//        MasterWriteI2C1 (cmd); // command line 
//        IdleI2C1(); 
//        while( !I2C1STATbits.ACKSTAT==0 ){}
//
//        // wite the value to put on the output
//        MasterWriteI2C1(data); // output 
//        IdleI2C1(); 
//        while( !I2C1STATbits.ACKSTAT==0 ){}
//
//        // end the I2C communication
//        StopI2C1(); // end of data send 
//        IdleI2C1(); // Wait to complete 
//
//        // the total write time is ~285us with an I2C clock of 100 kHz
//
//        data++; // increase the data, make a sawtooth wave as an example
//
//        //if(data>255) { // don't have to worry about this, data is 8 bit so it will roll over automatically
//        //    data=0;
//        //}
//
//    } // end while(1)
//    
    
    
    
//------- uncomment to init the uart2 -----------//
//    UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
//    UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
//    UARTSetDataRate(UART2, PB_FREQ, BAUDRATE);
//    UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
//    ConfigIntUART2(UART_RX_INT_EN | UART_TX_INT_EN | UART_ERR_INT_EN | UART_INT_PR0 | UART_INT_SUB_PR0);

     //rxchar = 0 ; // a received character
     //count = 0 ; // count the number of characters

//    // PuTTY
    clrscr();  //clear PuTTY screen
    home();
//    // By default, MPLAB XC32's libraries use UART2 for STDOUT.
//    // This means that formatted output functions such as printf()
//    // will send their output to UART2

//-----------------------------------------------//    
   
   
   
   


    // === configure threads ==========
    // turns OFF UART support and debugger pin, unless defines are set

    // === enable system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();

//    // init the threads 
    PT_INIT(&pt_uart);
//    PT_INIT(&pt_anim);
   
// ---------- uncomment to init the tft display -----------//
//    tft_init_hw();
//    tft_begin();
//    tft_fillScreen(ILI9340_BLACK); //240x320 vertical display
//    tft_setRotation(0); // Use tft_setRotation(1) for 320x240
//---------------------------------------------------------//
    

//    // === set up input capture 
//	// based on timer3 (need to configure timer 3 seperately)
//	OpenCapture1(IC_EVERY_RISE_EDGE | IC_INT_1CAPTURE | IC_CAP_32BIT | IC_TIMER2_SRC | IC_ON);
//	// turn on the interrupt so that every capture can be recorded
//	ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_3);
//	INTClearFlag(INT_IC1);
//	// connect PIN 24 to IC1 capture unit
//	PPSInput(3, IC1, RPB13);
//	mPORTBSetPinsDigitalIn(BIT_13); //Set port as input
//
//    // round-robin scheduler for threads
    while (1) {
        PT_SCHEDULE(protothread_uart(&pt_uart));
        //PT_SCHEDULE(protothread_anim(&pt_anim));
    }
} // main