void LED_On (CPU_INT08U led) { switch (led) { case 0: mPORTASetBits((IOPORT_BIT_0 | IOPORT_BIT_1 | IOPORT_BIT_2 | IOPORT_BIT_3 | IOPORT_BIT_4 | IOPORT_BIT_5 | IOPORT_BIT_6 | IOPORT_BIT_7)); break; case 1: mPORTASetBits(IOPORT_BIT_0); break; case 2: mPORTASetBits(IOPORT_BIT_1); break; case 3: mPORTASetBits(IOPORT_BIT_2); break; case 4: mPORTASetBits(IOPORT_BIT_3); break; case 5: mPORTASetBits(IOPORT_BIT_4); break; case 6: mPORTASetBits(IOPORT_BIT_5); break; case 7: mPORTASetBits(IOPORT_BIT_6); break; case 8: mPORTASetBits(IOPORT_BIT_7); break; default: break; } }
static PT_THREAD (protothread_key(struct pt *pt)) { PT_BEGIN(pt); static int keypad, i, pattern; // order is 0 thru 9 then * ==10 and # ==11 // no press = -1 // table is decoded to natural digit order (except for * and #) // 0x80 for col 1 ; 0x100 for col 2 ; 0x200 for col 3 // 0x01 for row 1 ; 0x02 for row 2; etc static int keytable[12]={0x108, 0x81, 0x101, 0x201, 0x82, 0x102, 0x202, 0x84, 0x104, 0x204, 0x88, 0x208}; // init the keypad pins A0-A3 and B7-B9 // PortA ports as digital outputs mPORTASetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3); //Set port as output // PortB as inputs mPORTBSetPinsDigitalIn(BIT_7 | BIT_8 | BIT_9); //Set port as input while(1) { // read each row sequentially mPORTAClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3); pattern = 1; mPORTASetBits(pattern); // yield time PT_YIELD_TIME_msec(30); //mPORTAClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3); //pattern = 1; mPORTASetBits(pattern); for (i=0; i<4; i++) { keypad = mPORTBReadBits(BIT_7 | BIT_8 | BIT_9); if(keypad!=0) {keypad |= pattern ; break;} mPORTAClearBits(pattern); pattern <<= 1; mPORTASetBits(pattern); } // search for keycode if (keypad > 0){ // then button is pushed for (i=0; i<12; i++){ if (keytable[i]==keypad) break; } } else i = -1; // no button pushed // draw key number tft_fillRoundRect(30,200, 100, 28, 1, ILI9340_BLACK);// x,y,w,h,radius,color tft_setCursor(30, 200); tft_setTextColor(ILI9340_YELLOW); tft_setTextSize(4); sprintf(buffer,"%d", i); if (i==10)sprintf(buffer,"*"); if (i==11)sprintf(buffer,"#"); tft_writeString(buffer); // NEVER exit while } // END WHILE(1) PT_END(pt); } // keypad thread
void Drive_leds(void) { if (LEDS.out_bits.b0) { mPORTAClearBits(BIT_0); } else { mPORTASetBits(BIT_0); // LED OFF } if (LEDS.out_bits.b1) { mPORTAClearBits(BIT_1); } else { mPORTASetBits(BIT_1); // LED OFF } }
void InitBluetooth(void) { #if DESIRED_BAUDRATE == 9600 mPORTBSetPinsDigitalOut(BIT_9); mPORTBSetBits(BIT_9);// BT high level, the reset polarity is active low mPORTASetPinsDigitalOut(BIT_3); mPORTASetBits(BIT_3);// Set Baud rate (high = force 9,600, low = 115 K or firmware setting) mPORTASetPinsDigitalOut(BIT_2); mPORTASetBits(BIT_2);// Set BT master (high = auto-master mode) #elif DESIRED_BAUDRATE == 115200 mPORTBSetPinsDigitalOut(BIT_9); mPORTBSetBits(BIT_9);// BT high level, the reset polarity is active low mPORTASetPinsDigitalOut(BIT_3); mPORTAClearBits(BIT_3);// Set Baud rate (high = force 9,600, low = 115 K or firmware setting) mPORTASetPinsDigitalOut(BIT_2); mPORTASetBits(BIT_2);// Set BT master (high = auto-master mode) #elif DESIRED_BAUDRATE ==921600 //nothing #endif UARTConfigure(UART_MODULE_ID2, UART_ENABLE_PINS_TX_RX_ONLY|UART_ENABLE_PINS_CTS_RTS); UARTConfigure(UART_MODULE_ID2, UART_ENABLE_HIGH_SPEED); UARTSetFifoMode(UART_MODULE_ID2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_MODULE_ID2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_MODULE_ID2, GetPeripheralClock(), DESIRED_BAUDRATE); UARTEnable(UART_MODULE_ID2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART_MODULE_ID2), INT_SUB_PRIORITY_LEVEL_1); // Enable multi-vector interrupts //WriteStringXbee("*** UART Bluetooth demarre ***\r\n"); }
void initLCD() { //No commands for 40ms OpenTimer2( T2_PS_1_256 | T2_ON, delay( 40 ) ); //Start Reset mPORTAClearBits( 1 << 3 ); mPORTASetPinsDigitalOut( 1 << 3 ); //Prep mPMPOpen( PMP_ON | PMP_TTL | PMP_READ_WRITE_EN | PMP_WRITE_POL_LO | PMP_READ_POL_LO, PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 | PMP_WAIT_END_4, PMP_PEN_0, PMP_INT_OFF ); //Wake Up! while( !mT2GetIntFlag() ); CloseTimer2(); mT2ClearIntFlag(); //End Reset mPORTASetBits( 1 << 3 ); //No Commands for 39us OpenTimer2( T2_PS_1_256 | T2_ON, delay( 40 ) ); //Wake Up! while( !mT2GetIntFlag() ); CloseTimer2(); mT2ClearIntFlag(); lcdCommand(LCD_ON); //Best boot 5-1-05 lcdCommand(0xA3); //LCD Bias (A2 A3) - First Choice lcdCommand(0x2F); //Turn on internal power control lcdCommand(0x26); //Pseudo-Contrast 7 = dark 6 = OK - First Choice lcdCommand(0x81); //Set contrast lcdCommand(40);//The Contrast (30 to 54 - Recommeneded) lcdCommand(0xC8); //Flip screen on the horizontal to match with the vertical software invertion lcdClear(); }
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
int main(void) { //LOCALS unsigned int temp; unsigned int channel1, channel2; M1_stepPeriod = M2_stepPeriod = M3_stepPeriod = M4_stepPeriod = 50; // in tens of u-seconds unsigned char M1_state = 0, M2_state = 0, M3_state = 0, M4_state = 0; SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); /* TIMER1 - now configured to interrupt at 10 khz (every 100us) */ OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_TICK); ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); /* TIMER2 - 100 khz interrupt for distance measure*/ OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, T2_TICK); ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_3); //It is off until trigger /* PORTA b2 and b3 for servo-PWM */ mPORTAClearBits(BIT_2 | BIT_3); mPORTASetPinsDigitalOut(BIT_2 | BIT_3); /* ULTRASONICS: some bits of PORTB for ultrasonic sensors */ PORTResetPins(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11 ); PORTSetPinsDigitalOut(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11); //trigger /* Input Capture pins for echo signals */ //interrupt on every risging/falling edge starting with a rising edge PORTSetPinsDigitalIn(IOPORT_D, BIT_8| BIT_9| BIT_10| BIT_11); //INC1, INC2, INC3, INC4 Pin mIC1ClearIntFlag(); OpenCapture1( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//front ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture2( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//back ConfigIntCapture2(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture3( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//left ConfigIntCapture3(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture4( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//right ConfigIntCapture4(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); /* PINS used for the START (RD13) BUTTON */ PORTSetPinsDigitalIn(IOPORT_D, BIT_13); #define CONFIG (CN_ON | CN_IDLE_CON) #define INTERRUPT (CHANGE_INT_ON | CHANGE_INT_PRI_2) mCNOpen(CONFIG, CN19_ENABLE, CN19_PULLUP_ENABLE); temp = mPORTDRead(); /* PORT D and E for motors */ //motor 1 mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); // Turn on PORTD on startup. mPORTDSetPinsDigitalOut(BIT_4 | BIT_5 | BIT_6 | BIT_7); // Make PORTD output. //motor 2 mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); // Turn on PORTC on startup. mPORTCSetPinsDigitalOut(BIT_1 | BIT_2 | BIT_3 | BIT_4); // Make PORTC output. //motor 3 and 4 mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); // Turn on PORTE on startup. mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); // Make PORTE output. // UART2 to connect to the PC. // This initialization assumes 36MHz Fpb clock. If it changes, // you will have to modify baud rate initializer. UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, GetPeripheralClock(), BAUD); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART2 RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0); /* PORTD for LEDs - DEBUGGING */ mPORTDClearBits(BIT_0 | BIT_1 | BIT_2); mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2); // Congifure Change/Notice Interrupt Flag ConfigIntCN(INTERRUPT); // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); counterDistanceMeasure=600; //measure ULTRASONICS distance each 60 ms while (1) { /***************** Robot MAIN state machine *****************/ unsigned char ret = 0; switch (Robo_State) { case 0: MotorsON = 0; Robo_State = 0; InvInitialOrientation(RESET); TestDog(RESET); GoToRoom4short(RESET); BackToStart(RESET); InitialOrientation(RESET); GoToCenter(RESET); GoToRoom4long(RESET); break; case 1: ret = InvInitialOrientation(GO); if (ret == 1) { Robo_State = 2; } break; case 2: ret = TestDog(GO); if (ret == 1) { Robo_State = 3; //DOG not found } else if (ret == 2) { Robo_State = 4; //DOG found } break; case 3: ret = GoToRoom4short(GO); if (ret == 1) { Robo_State = 0; } break; case 4: ret = BackToStart(GO); if (ret == 1) { Robo_State = 5; } break; case 5: ret = GoToCenter(GO); if (ret == 1) { Robo_State = 6; } break; case 6: ret = GoToRoom4long(GO); if (ret == 1) { Robo_State = 0; } break; } if (frontDistance < 30 || backDistance < 30 || leftDistance < 30 || rightDistance < 30) mPORTDSetBits(BIT_0); else mPORTDClearBits(BIT_0); /***************************************************************/ /***************** Motors State Machine ************************/ if (MotorsON) { /**************************** MOTOR MAP M1 O-------------O M2 ON EVEN MOTORS, STEPS MUST BE INVERTED | /\ | i.e. FORWARD IS BACKWARD | / \ | | || | | || | M3 O-------------O M4 *****************************/ if (M1_counter == 0) { switch (M1_state) { case 0: // set 0011 step (0x3 , 1); if (M1forward) M1_state = 1; else M1_state = 3; break; case 1: // set 1001 step (0x9 , 1); if (M1forward) M1_state = 2; else M1_state = 0; break; case 2: // set 1100 step (0xC , 1); if (M1forward) M1_state = 3; else M1_state = 1; break; case 3: // set 0110 default: step (0x6 , 1); if (M1forward) M1_state = 0; else M1_state = 2; break; } M1_counter = M1_stepPeriod; step_counter[0]--; if (directionNow == countingDirection) step_counter[1]--; } if (M2_counter == 0) { switch (M2_state) { case 0: // set 0011 step (0x3 , 2); if (M2forward) M2_state = 1; else M2_state = 3; break; case 1: // set 0110 step (0x6 , 2); if (M2forward) M2_state = 2; else M2_state = 0; break; case 2: // set 1100 step (0xC , 2); if (M2forward) M2_state = 3; else M2_state = 1; break; case 3: // set 1001 default: step (0x9 , 2); if (M2forward) M2_state = 0; else M2_state = 2; break; } M2_counter = M2_stepPeriod; } if (M3_counter == 0) { switch (M3_state) { case 0: // set 0011 step (0x3 , 3); if (M3forward) M3_state = 1; else M3_state = 3; break; case 1: // set 1001 step (0x9 , 3); if (M3forward) M3_state = 2; else M3_state = 0; break; case 2: // set 1100 step (0xC , 3); if (M3forward) M3_state = 3; else M3_state = 1; break; case 3: // set 0110 default: step (0x6 , 3); if (M3forward) M3_state = 0; else M3_state = 2; break; } M3_counter = M3_stepPeriod; } if (M4_counter == 0) { switch (M4_state) { case 0: // set 0011 step (0x3 , 4); if (M4forward) M4_state = 1; else M4_state = 3; break; case 1: // set 0110 step (0x6 , 4); if (M4forward) M4_state = 2; else M4_state = 0; break; case 2: // set 1100 step (0xC , 4); if (M4forward) M4_state = 3; else M4_state = 1; break; case 3: // set 1001 default: step (0x9 , 4); if (M4forward) M4_state = 0; else M4_state = 2; break; } M4_counter = M4_stepPeriod; } } else { //motors off mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); } /************************************************************/ /******* TEST CODE, toggles the servos (from 90 deg. to -90 deg.) every 1 s. ********/ /* if (auxcounter == 0) { servo1_angle = 0; if (servo2_angle == 90) servo2_angle = -90; else servo2_angle = 90; auxcounter = 20000; // toggle angle every 2 s. } */ servo1_angle = 0; servo2_angle = -90; /* if (frontDistance > 13 && frontDistance < 17) { servo2_angle = 90; } else servo2_angle = -90; */ /*******************************************************************/ /****************** SERVO CONTROL ******************/ /* Changing the global servoX_angle at any point in the code will move the servo to the desired angle. */ servo1_counter = (servo1_angle + 90)*(18)/180 + 6; // between 600 and 2400 us if (servo1_period == 0) { mPORTASetBits(BIT_2); servo1_period = SERVOMAXPERIOD; /* 200 * 100us = 20000us period */ } servo2_counter = (servo2_angle + 90)*(18)/180 + 6; // between 600 and 2400 us if (servo2_period == 0) { mPORTASetBits(BIT_3); servo2_period = SERVOMAXPERIOD; /* 200 * 100us = 20000us period */ } /*****************************************************/ } /* end of while(1) */ return 0; }
//******************************** //******************************** //********** INITIALISE ********** //******************************** //******************************** void initialise (void) { BYTE data; //##### GENERAL NOTE ABOUT PIC32'S ##### //Try and use the peripheral libraries instead of special function registers for everything (literally everything!) to avoid //bugs that can be caused by the pipeline and interrupts. //--------------------------------- //----- CONFIGURE PERFORMANCE ----- //--------------------------------- //----- SETUP EVERYTHING FOR OPTIMUM PERFORMANCE ----- SYSTEMConfigPerformance(80000000ul); //Note this sets peripheral bus to '1' max speed (regardless of configuration bit setting) //Use PBCLK divider of 1:1 to calculate UART baud, timer tick etc //----- SET PERIPHERAL BUS DIVISOR ----- //To minimize dynamic power the PB divisor should be chosen to run the peripherals at the lowest frequency that provides acceptable system performance mOSCSetPBDIV(OSC_PB_DIV_2); //OSC_PB_DIV_1, OSC_PB_DIV_2, OSC_PB_DIV_4, OSC_PB_DIV_8, //----- SETUP INTERRUPTS ----- INTEnableSystemMultiVectoredInt(); //------------------------- //----- SETUP IO PINS ----- //------------------------- //(Device will powerup with all IO pins as inputs) //----- TURN OFF THE JTAG PORT ----- //(JTAG is on by default) //mJTAGPortEnable(0); //Must be on for Microchip Multimedia Development board #define PORTA_IO 0xc2ff //Setup the IO pin type (0 = output, 1 = input) mPORTAWrite(0xc033); //Set initial ouput pin states mPORTASetPinsDigitalIn(PORTA_IO); //(Sets high bits as input) mPORTASetPinsDigitalOut(~PORTA_IO); //(Sets high bits as output) #define PORTB_IO 0xfbff //Setup the IO pin type (0 = output, 1 = input) mPORTBWrite(0x6d13); //Set initial ouput pin states mPORTBSetPinsDigitalIn(PORTB_IO); //(Sets high bits as input) mPORTBSetPinsDigitalOut(~PORTB_IO); //(Sets high bits as output) mPORTBSetPinsDigitalIn(BIT_0 | BIT_1 | BIT_3 | BIT_4 | BIT_15); //Joystick inputs #define PORTC_IO 0xf01e //Setup the IO pin type (0 = output, 1 = input) mPORTCWrite(0x3018); //Set initial ouput pin states mPORTCSetPinsDigitalIn(PORTC_IO); //(Sets high bits as input) mPORTCSetPinsDigitalOut(~PORTC_IO); //(Sets high bits as output) #define PORTD_IO 0x7bfe //Setup the IO pin type (0 = output, 1 = input) mPORTDWrite(0xbdaf); //Set initial ouput pin states mPORTDSetPinsDigitalIn(PORTD_IO); //(Sets high bits as input) mPORTDSetPinsDigitalOut(~PORTD_IO); //(Sets high bits as output) mPORTDSetPinsDigitalOut(BIT_2 | BIT_1); //LED's 2 and 3 mPORTDSetPinsDigitalIn(BIT_9); #define PORTE_IO 0x03ff //Setup the IO pin type (0 = output, 1 = input) mPORTEWrite(0x02a2); //Set initial ouput pin states mPORTESetPinsDigitalIn(PORTE_IO); //(Sets high bits as input) mPORTESetPinsDigitalOut(~PORTE_IO); //(Sets high bits as output) #define PORTF_IO 0x111f //Setup the IO pin type (0 = output, 1 = input) mPORTFWrite(0x0039); //Set initial ouput pin states mPORTFSetPinsDigitalIn(PORTF_IO); //(Sets high bits as input) mPORTFSetPinsDigitalOut(~PORTF_IO); //(Sets high bits as output) #define PORTG_IO 0xd3cf //Setup the IO pin type (0 = output, 1 = input) mPORTGWrite(0xf203); //Set initial ouput pin states mPORTGSetPinsDigitalIn(PORTG_IO); //(Sets high bits as input) mPORTGSetPinsDigitalOut(~PORTG_IO); //(Sets high bits as output) //Read pins using: // mPORTAReadBits(BIT_0); //Write pins using: // mPORTAClearBits(BIT_0); // mPORTASetBits(BIT_0); // mPORTAToggleBits(BIT_0); //----- INPUT CHANGE NOTIFICATION CONFIGURATION ----- //EnableCN0(); ConfigCNPullups(CN2_PULLUP_ENABLE | CN3_PULLUP_ENABLE | CN5_PULLUP_ENABLE | CN6_PULLUP_ENABLE | CN12_PULLUP_ENABLE); //Joystick pins //----- SETUP THE A TO D PINS ----- ENABLE_ALL_DIG; //--------------------- //----- SETUP USB ----- //--------------------- //The USB specifications require that USB peripheral devices must never source current onto the Vbus pin. Additionally, USB peripherals should not source //current on D+ or D- when the host/hub is not actively powering the Vbus line. When designing a self powered (as opposed to bus powered) USB peripheral //device, the firmware should make sure not to turn on the USB module and D+ or D- pull up resistor unless Vbus is actively powered. Therefore, the //firmware needs some means to detect when Vbus is being powered by the host. A 5V tolerant I/O pin can be connected to Vbus (through a resistor), and //can be used to detect when Vbus is high (host actively powering), or low (host is shut down or otherwise not supplying power). The USB firmware //can then periodically poll this I/O pin to know when it is okay to turn on the USB module/D+/D- pull up resistor. When designing a purely bus powered //peripheral device, it is not possible to source current on D+ or D- when the host is not actively providing power on Vbus. Therefore, implementing this //bus sense feature is optional. This firmware can be made to use this bus sense feature by making sure "USE_USB_BUS_SENSE_IO" has been defined in the //HardwareProfile.h file. // #if defined(USE_USB_BUS_SENSE_IO) // tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h // #endif //If the host PC sends a GetStatus (device) request, the firmware must respond and let the host know if the USB peripheral device is currently bus powered //or self powered. See chapter 9 in the official USB specifications for details regarding this request. If the peripheral device is capable of being both //self and bus powered, it should not return a hard coded value for this request. Instead, firmware should check if it is currently self or bus powered, and //respond accordingly. If the hardware has been configured like demonstrated on the PICDEM FS USB Demo Board, an I/O pin can be polled to determine the //currently selected power source. On the PICDEM FS USB Demo Board, "RA2" is used for this purpose. If using this feature, make sure "USE_SELF_POWER_SENSE_IO" //has been defined in HardwareProfile.h, and that an appropriate I/O pin has been mapped to it in HardwareProfile.h. // #if defined(USE_SELF_POWER_SENSE_IO) // tris_self_power = INPUT_PIN; // See HardwareProfile.h // #endif //Enable the USB port now - we will check to see if Vbus is powered at the end of init and disable it if not. //USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware variables to known states. //------------------------ //----- SETUP TIMERS ----- //------------------------ //(INCLUDE THE USAGE OF ALL TIMERS HERE EVEN IF NOT SETUP HERE SO THIS IS THE ONE POINT OF //REFERENCE TO KNOW WHICH TIMERS ARE IN USE AND FOR WHAT). //----- SETUP TIMER 1 ----- //Used for: Available //OpenTimer1((T1_ON | T1_IDLE_CON | T1_GATE_OFF | T1_PS_1_4 | T1_SOURCE_INT), 20000); //----- SETUP TIMER 2 ----- //Used for: //OpenTimer2((T2_ON | T2_IDLE_CON | T2_GATE_OFF | T2_PS_1_1 | T2_SOURCE_INT), 0xffff); //0xffff = 305Hz //----- SETUP TIMER 3 ----- //Used for: //OpenTimer3((T3_ON | T3_IDLE_CON | T3_GATE_OFF | T3_PS_1_1 | T3_SOURCE_INT), PIEZO_TIMER_PERIOD); //----- SETUP TIMER 4 ----- //Used for: //OpenTimer4((T4_ON | T4_IDLE_CON | T4_GATE_OFF | T4_PS_1_1 | T4_SOURCE_INT), 20000); //----- SETUP TIMER 5 ----- //Used for: Heartbeat OpenTimer5((T5_ON | T5_IDLE_CON | T5_GATE_OFF | T5_PS_1_1 | T5_SOURCE_INT), 40000); //1mS with 80MHz osc and PB_DIV_2 ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_7); //1=lowest priority to 7=highest priority. ISR function must specify same value //--------------------------------- //----- SETUP EVAL BOARD CPLD ----- //--------------------------------- //Graphics bus width = 16 mPORTGSetPinsDigitalOut(BIT_14); mPORTGSetBits(BIT_14); //SPI source select = SPI3 (not used) mPORTGSetPinsDigitalOut(BIT_12); mPORTGClearBits(BIT_12); //SPI peripheral destination select = Expansion Slot (not used) mPORTASetPinsDigitalOut(BIT_7 | BIT_6); mPORTASetBits(BIT_7); mPORTAClearBits(BIT_6); //-------------------------------------- //----- PARALLEL MASTER PORT SETUP ----- //-------------------------------------- PMMODE = 0; PMAEN = 0; PMCON = 0; PMMODE = 0x0610; PMCONbits.PTRDEN = 1; //Enable RD line PMCONbits.PTWREN = 1; //Enable WR line PMCONbits.PMPEN = 1; //Enable PMP //------------------------------ //----- INITIALISE DISPLAY ----- //------------------------------ display_initialise(); display_test(); //LOAD OUR GLOBAL HTML STYLES FILE READY FOR DISPLAY HTML PAGES BYTE dummy_styles_count; DWORD file_size; if (display_html_setup_read_file(global_css, 0, &file_size)) { dummy_styles_count = 0; display_html_read_styles(&file_size, &dummy_styles_count, 1); //1 = this is global styles file } }
int main(int argc, char** argv) { uchar status = 0; mPORTAClearBits(BIT_6); //Clear bits to ensure the LED is off. //mPORTASetPinsDigitalOut(BIT_6); //Set port as output // lets set the digital IO bits for the ethernet module mPORTASetPinsDigitalOut(BIT_7|BIT_6); mPORTGSetPinsDigitalOut(BIT_8 | BIT_9 | BIT_14 | BIT_0); mPORTBSetPinsDigitalOut(0x0000FFFF); mPORTESetPinsDigitalOut(0x000000FF); // set the control bits high mPORTASetBits(BIT_7); mPORTGSetBits(BIT_8 | BIT_9 | BIT_14); mPORTGClearBits(BIT_0); //mPORTGClearBits(BIT_0); //mPORTGClearBits(0x0000FFFF); //mPORTEClearBits(0x0000FFFF); //mPORTAClearBits(0x0000FFFF); //mPORTBClearBits(0x0000FFFF); //while(1){} // lets init the W5100 struct W5100Context_t context; context.Mode = MR_RST; context.InterruptMask = 0xFF; context.RetryTimeValue = 0x0FA0; // 400 ms context.RetryCount = 1; context.GateAdd[0] = 10; context.GateAdd[1] = 1; context.GateAdd[2] = 1; context.GateAdd[3] = 3; context.SHardAdd[0] = 0; context.SHardAdd[1] = 1; context.SHardAdd[2] = 2; context.SHardAdd[3] = 3; context.SHardAdd[4] = 4; context.SHardAdd[5] = 5; context.Subnet[0] = 255; context.Subnet[1] = 255; context.Subnet[2] = 255; context.Subnet[3] = 0; context.SourceIP[0] = 10; context.SourceIP[1] = 1; context.SourceIP[2] = 1; context.SourceIP[3] = 2; context.sockets[0].Mem = MEM_2K; context.sockets[1].Mem = MEM_2K; context.sockets[2].Mem = MEM_2K; context.sockets[3].Mem = MEM_2K; ShortDelay_ms(100); W5100Init( &context ); W5100ReadData(SHAR0, &status, 1); W5100ReadData(SHAR1, &status, 1); W5100ReadData(SHAR2, &status, 1); W5100ReadData(SHAR3, &status, 1); W5100ReadData(SHAR4, &status, 1); W5100ReadData(SHAR5, &status, 1); EstablishServer(&context); // lets try a random write //W5100WriteData(0x001A, 0x07, 1); //mPORTEWrite(0x07); while(1) { status = W5100PollStatus(&context, 0); if( status == SOCK_ESTABLISH) mPORTGSetBits(BIT_0); else mPORTGClearBits(BIT_0); if( status == SOCK_CLOSE_WAIT) EstablishServer(&context); //PORTAbits.RA2 = ~PORTAbits.RA2; mPORTAToggleBits(BIT_6); ShortDelay(US_TO_CT_TICKS*1000000); //delay_millis(1000); } return (EXIT_SUCCESS); }
int main(void) { int a = 0; unsigned char data_mix = 0, data_mix_old = 0; SDEV_TYPE1 S1 = {0}, *S1_p = &S1; char comm_buffer[MAXSTRLEN]; int update_rate = 4; int eresult = 0, records = 0, file_errors = 0; // Init remote device data S1.obits.out_byte = 0xff; // Enable multi-vectored interrupts INTEnableSystemMultiVectoredInt(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //STEP 1. Configure cache, wait states and peripheral bus clock // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); srand(ReadCoreTimer()); // Seed the pseudo random generator // SCLK1 pin 25 - SD pin 5 PORTSetPinsDigitalOut(IOPORT_B, BIT_14); // Clock output // SCLK2 pin 26 - PIC pin 18 PORTSetPinsDigitalOut(IOPORT_B, BIT_15); // Clock output PPSInput(2, SDI1, RPB8); //Assign SDI1 to pin 17 - SD pin 7 PORTSetPinsDigitalIn(IOPORT_B, BIT_8); // Input for SDI1 PPSInput(3, SDI2, RPB13); //Assign SDI2 to pin 24 PORTSetPinsDigitalIn(IOPORT_B, BIT_13); // Input for SDI2 PPSOutput(2, RPB11, SDO1); // Set 22 pin as output for SDO1 - SD pin 2 PPSOutput(2, RPB5, SDO2); // Set 14 pin as output for SDO2 PORTSetPinsDigitalOut(IOPORT_B, BIT_2); // CS line pin 6 - SD pin 1 mPORTBSetBits(BIT_2); // deselect SDCARD PORTSetPinsDigitalOut(IOPORT_B, BIT_3); // select pin enable for SPI slaves bit 2 to 74hc138 mPORTBSetBits(BIT_3); // deselect Slave1 bit2 PORTSetPinsDigitalOut(IOPORT_A, BIT_3); // spare digital output mPORTASetBits(BIT_3); // //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Diag Led pins PORTSetPinsDigitalOut(IOPORT_A, BIT_0 | BIT_1); // bi-color LED PORTSetPinsDigitalOut(IOPORT_B, BIT_0 | BIT_1); // programming inputs /device select outputs address for SPI slaves bits [0..1] 74hc138 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 3. initialize the port pin states = outputs low mPORTASetBits(BIT_0 | BIT_1); mPORTBClearBits(BIT_0 | BIT_1); RTC_Init(); // Start Timer5 for background processes and 1ms soft timers Blink_Init(); // Start Timer4 background blink driver blink_led(RED_LED, LED_ON, FALSE); init_spi_ports(); ps_select(0); // select the pic18 slave1 on spi port 2, select 0 // send some text to clean the buffers SpiStringWrite("\r\n \n\r"); eresult = f_mount(&FatFs, "0:", 1); if (eresult) { sprintf(comm_buffer, "\r\n Mount error %i ", eresult); SpiStringWrite(comm_buffer); /* Register work area to the logical drive 1 */ blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } else { Show_MMC_Info(); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); } /* Create destination file on the drive 1 */ eresult = f_open(&File[0], "0:logfile.txt", FA_CREATE_ALWAYS | FA_WRITE); while (1) { // loop and move data V.Timer1 = update_rate; while (V.Timer1); /* loop back testing */ data_mix_old = data_mix; // save the old data data_mix = rand(); // make new data if (S1_p->char_ready || !S1_p->ibits.in_bits.eject) { S1_p->rec_data = S1_p->rec_tmp; // led1_green(); } else { // led1_off(); } S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) { S1_p->obits.out_bits.eject_led = OFF; SD_NOTRDY = STA_NOINIT; } else { S1_p->obits.out_bits.eject_led = ~S1_p->obits.out_bits.eject_led; } for (S1_p->chan_no = 0; S1_p->chan_no < 4; S1_p->chan_no++) { S1_p->adc_data[S1_p->chan_no] = SpiADCRead(S1_p->chan_no); } a = 0; if (((records % 100)) == 0 && !eresult) { if (S1_p->char_ready) { snprintf(comm_buffer, 64, "\r\n %x , %i, %x , %i , %i , %i , %i ", S1_p->rec_data, records, S1_p->ibits.in_byte, S1_p->adc_data[0], S1_p->adc_data[1], S1_p->adc_data[2], S1_p->adc_data[3]); } else { snprintf(comm_buffer, 64, "\r\n %x , %i , %i , %i , %i , %i ", S1_p->ibits.in_byte, records, S1_p->adc_data[0], S1_p->adc_data[1], S1_p->adc_data[2], S1_p->adc_data[3]); } S1_p->rec_tmp = SpiStringWrite(comm_buffer); } if (SpiSerialReadOk() || !S1_p->ibits.in_bits.eject) { S1_p->char_ready = TRUE; if ((S1_p->rec_tmp == 'f') || !S1_p->ibits.in_bits.eject) { if (S1_p->ibits.in_bits.card_detect) { // no disk blink_led(0, LED_ON, FALSE); S1_p->obits.out_bits.eject_led = OFF; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); SpiStringWrite("\r\n Insert Disk! y/n "); while (!SpiSerialReadReady() && S1_p->ibits.in_bits.card_detect) S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); V.Timer1 = update_rate; while (V.Timer1); if (!S1_p->ibits.in_bits.card_detect) { SpiStringWrite("/\r\n Mounting Disk "); S1_p->obits.out_bits.eject_led = ON; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); eresult = f_mount(&FatFs, "0:", 1); if (eresult) { sprintf(comm_buffer, "\r\n Mount error %i ", eresult); SpiStringWrite(comm_buffer); /* Register work area to the logical drive 1 */ blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } else { Show_MMC_Info(); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); } /* Create destination file on the drive 1 */ eresult = f_open(&File[0], "0:logfile.txt", FA_CREATE_ALWAYS | FA_WRITE); SpiStringWrite("\r\n Mount Complete "); S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) SD_NOTRDY = STA_NOINIT; } } else { // Eject current disk blink_led(0, LED_ON, FALSE); S1_p->obits.out_bits.eject_led = OFF; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); f_close(&File[0]); SpiStringWrite("\r\n Eject Disk? y/n "); while (!SpiSerialReadReady() && !S1_p->ibits.in_bits.card_detect) S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); V.Timer1 = update_rate; while (V.Timer1); if (S1_p->ibits.in_bits.card_detect) { SpiStringWrite("\r\n Ejecting Disk "); S1_p->obits.out_bits.eject_led = ON; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (f_mount(0, "0:", 0)) SpiStringWrite("\r\n UMount error "); /* Unregister work area from the logical drive 1 */ SpiStringWrite("\r\n Ejection Complete "); eresult = 1; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) SD_NOTRDY = STA_NOINIT; } } } } else { S1_p->char_ready = FALSE; } V.Timer1 = update_rate; while (V.Timer1); // only GREEN if the data sent and received match or eject button is pressed if (S1_p->ibits.in_bits.eject && (S1_p->rec_data != data_mix_old)) { // blink_led(0, LED_ON, TRUE); } else { // blink_led(0, LED_ON, FALSE); } if (!eresult) { a = f_puts(comm_buffer, &File[0]); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); records++; } else { file_errors++; if ((file_errors % 100) == 0) { SpiStringWrite("\r\n not logged "); blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } } if (a == (-1)) { if ((file_errors % 100) == 0) { sprintf(comm_buffer, "\r\n File Write error %i ", a); SpiStringWrite(comm_buffer); blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } } f_sync(&File[0]); } }
static PT_THREAD (protothread_cmd(struct pt *pt)) { PT_BEGIN(pt); while(1) { // send the prompt via DMA to serial sprintf(PT_send_buffer,"cmd>"); // by spawning a print thread PT_SPAWN(pt, &pt_DMA_output, PT_DMA_PutSerialBuffer(&pt_DMA_output) ); //spawn a thread to handle terminal input // the input thread waits for input // -- BUT does NOT block other threads // string is returned in "PT_term_buffer" PT_SPAWN(pt, &pt_input, PT_GetSerialBuffer(&pt_input) ); // returns when the thread dies // in this case, when <enter> is pushed // now parse the string sscanf(PT_term_buffer, "%s %d", cmd, &value); if (cmd[0]=='d' ) { //set mux to DSP chip mPORTAClearBits(BIT_4); mPORTBClearBits(BIT_13); mPORTASetBits(BIT_4); mPORTBClearBits(BIT_3 | BIT_7 | BIT_8 | BIT_9); if(((value << 6) & (0x0040))){ mPORTBSetBits(BIT_3); } //select sound effect mPORTBSetBits(value << 6); } if (cmd[0] == 's'){ //distortion //set mux to Distortion mPORTAClearBits(BIT_4); mPORTBClearBits(BIT_13); } if (cmd[0] == 'n'){ //normal: only tone stack //set mux to channel 0 mPORTAClearBits(BIT_4); mPORTBClearBits(BIT_13); mPORTBSetBits(BIT_13); } //pots if (cmd[0] == 't'){ //treble // CS low to start transaction mPORTBClearBits(BIT_4); // start transaction deliverSPICh1Datum(value); // CS high mPORTBSetBits(BIT_4); // end transaction } if (cmd[0] == 'b'){ //bass // CS low to start transaction mPORTAClearBits(BIT_0); // start transaction deliverSPICh1Datum(value); // CS high mPORTASetBits(BIT_0); // end transaction } if (cmd[0] == 'v'){ //level // CS low to start transaction mPORTAClearBits(BIT_2); // start transaction deliverSPICh1Datum(value); // CS high mPORTASetBits(BIT_2); // end transaction } if (cmd[0] == 'm'){ //mid // CS low to start transaction mPORTAClearBits(BIT_3); // start transaction deliverSPICh1Datum(value); // CS high mPORTASetBits(BIT_3); // end transaction } // never exit while } // END WHILE(1) PT_END(pt); } // thread 3