int main() { mJTAGPortEnable(0); mPORTASetPinsDigitalOut(BIT_0|BIT_1|BIT_2|BIT_3|BIT_4|BIT_5|BIT_6|BIT_7); mPORTAClearBits(BIT_0|BIT_1|BIT_2|BIT_3|BIT_4|BIT_5|BIT_6|BIT_7); INTCONSET=0x1000; INTEnableSystemMultiVectoredInt(); IEC0 =0; IFS0 = 0; INTCONCLR = 0x00000018; IEC0SET = 0x00088000; IPC3 = 0x1F000000; IPC4 = 0X17000000; lcdconfig(); while(1) { if(IFS0 & 0x8000==1) { lcddata('3'); PORTA=0X0F; } if(IFS0 & 0x80000==1) { lcddata('4'); PORTA=0XFF; } } }
int main(void) { int i; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 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); // Explorer16 LEDs are on lower 8-bits of PORTA and to use all LEDs, JTAG port must be disabled. mJTAGPortEnable(DEBUG_JTAGPORT_OFF); // Make all lower 8-bits of PORTA as output. Turn them off before changing // direction so that we don't have unexpected flashes mPORTAClearBits(BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \ BIT_3 | BIT_2 | BIT_1 | BIT_0 ); mPORTASetPinsDigitalOut( BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \ BIT_3 | BIT_2 | BIT_1 | BIT_0 ); // Now blink all LEDs ON/OFF forever. while(1) { mPORTAToggleBits(BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \ BIT_3 | BIT_2 | BIT_1 | BIT_0 ); // Insert some delay i = 1024*1024*10; while(i--); } }
int main() { i=0; lcdini(); DDPCON=0x0000; mPORTDSetPinsDigitalIn(BIT_7|BIT_13|BIT_6); //Set Portd pins DigitalIn mPORTASetPinsDigitalOut(BIT_0|BIT_1|BIT_2|BIT_3|BIT_4|BIT_5|BIT_6); mPORTAClearBits(BIT_0|BIT_1|BIT_2|BIT_3|BIT_4|BIT_5|BIT_6); mPORTASetPinsDigitalIn(BIT_7); char x; while(1) { switch(i) //Switch case to select function { case 0: mode(); break; case 1: batteryv(); break; case 2: temps(); break; } if(i==3) { lcdcmd(0x01); break; } } }
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"); }
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 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(); }
// initialize hardware void Initialize() { // All of these items will affect the performance of your code and cause it to run significantly slower than you would expect. SYSTEMConfigPerformance(SYS_CLOCK); WriteCoreTimer(0); // Core timer ticks once every two clocks (verified) // set default digital port A for IO DDPCONbits.JTAGEN = 0; // turn off JTAG DDPCONbits.TROEN = 0; // ensure no tracing on //mPORTASetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); // todo - set this based on width of image. make rest inputs? mPORTBSetPinsDigitalOut(BIT_0|BIT_1|BIT_2|BIT_3|BIT_4|BIT_5|BIT_6|BIT_7|BIT_8|BIT_9|BIT_10|BIT_11|BIT_12|BIT_13|BIT_14|BIT_15); // 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.. int pbClk = SYSTEMConfig( SYS_CLOCK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); InitializeUART(pbClk); mPORTASetPinsDigitalOut(BIT_1); // set internals // SetUARTClockDivider(flashOptions.baudDivisor); // prepare 32 bit timer 45 to trigger interrupts //OpenTimer45(T45_ON | T45_SOURCE_INT | T45_PS_1_1, interruptTime); // set up the timer interrupt and priority //ConfigIntTimer45(T4_INT_ON | T4_INT_PRIOR_7); // enable multivectored interrupts //INTEnableSystemMultiVectoredInt(); // start watchdog timer //tickle in interrupt, turn off during reset of device, causes a reset //The next statement enables the Watchdog Timer: // WDTCONbits.ON = 1; //sprintf(text,"Wait states %d\r\n.",BMXCONbits.BMXWSDRM); //PrintSerial(text); //BMXCONbits.BMXWSDRM = 0; // set RAM access to zero wait states //sprintf(text,"TODO _ REMOVE:override %d\r\n",pinOverride); //PrintSerial(text); //sprintf(text,"TODO _ REMOVE:actual %d\r\n",PORTAbits.RA1); //PrintSerial(text); }
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(int argc, char** argv) { mPORTAClearBits(BIT_6); //Clear bits to ensure the LED is off. mPORTASetPinsDigitalOut(BIT_6); //Set port as output while(1) { //PORTAbits.RA2 = ~PORTAbits.RA2; mPORTAToggleBits(BIT_6); delay_millis(100); } return (EXIT_SUCCESS); }
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
// init the UART settings void InitializeUART(int clock) { clockDivider = clock/(4*DESIRED_BAUDRATE) - 1; // UART1 // U1RX on RPA4 // U1TX on RPA0 mPORTAClearBits(BIT_0 | BIT_4); mPORTASetPinsDigitalIn(BIT_4); mPORTASetPinsDigitalOut(BIT_0); U1RXR = 2; // RPB2 = U1RX RPA0R = 1; // PIN RPA0 = U1TX SetUARTClockDivider(clockDivider); }
void InitializeSystem() { SYSTEMConfigWaitStatesAndPB(CLOCK_FREQ); mOSCSetPBDIV(OSC_PB_DIV_4); // Set to get 20MHz PB clock //mOSCSetPBDIV(OSC_PB_DIV_2); CheKseg0CacheOn(); mJTAGPortEnable(0); // Initialize the pins to all digital output and driven to ground. // Exception is RE7 and RE6 which are switch inputs PORTSetPinsDigitalIn(IOPORT_E, BIT_6); PORTSetPinsDigitalIn(IOPORT_E, BIT_7); mPORTASetPinsDigitalOut(0xFFFF); mPORTBSetPinsDigitalOut(0xFFFF); mPORTCSetPinsDigitalOut(0xFFFF); mPORTDSetPinsDigitalOut(0xFFFF); mPORTESetPinsDigitalOut(0xFF3F); mPORTFSetPinsDigitalOut(0xFFFF); mPORTGSetPinsDigitalOut(0xFFFF); mPORTAClearBits(0xFFFF); mPORTBClearBits(0xFFFF); mPORTCClearBits(0xFFFF); mPORTDClearBits(0xFFFF); mPORTEClearBits(0xFF3F); mPORTESetBits(0x000F); // LED latches need to be set high for off mPORTFClearBits(0xFFFF); mPORTGClearBits(0xFFFF); INTEnableSystemMultiVectoredInt(); #ifdef SANITY_CHECK mLED_Green_On(); #endif //LCD_Initialize(); //WIFI_Initialize(); //SPRINKLER_Initialize(); //RTCC_Initialize(); //SERIALUSB_Initialize(); SDCARD_Initialize(); TCPIP_Initialize(); }
int main(void) { int dmaChn=0; // the DMA channel to use // first let us set the LED I/O ports as digital outputs mPORTASetPinsDigitalOut(0xff); mPORTAClearBits(0xff); // start with all LED's turned off // Open the desired DMA channel. // We enable the AUTO option, we'll keep repeating the sam 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 DmaChnSetTxfer(dmaChn, LED_pattern, (void*)&LATA, sizeof(LED_pattern), 1, 1); // set the transfer event control: what event is to start the DMA transfer DmaChnSetEventControl(dmaChn, DMA_EV_START_IRQ(_TIMER_3_IRQ)); // once we configured the DMA channel we can enable it // now it's ready and waiting for an event to occur... DmaChnEnable(dmaChn); // now use the 32 bit timer to generate an interrupt at the desired LED_BLINK_RATE { int pbFreq=SYS_FREQ/(1<<mOSCGetPBDIV()); // get the PB frequency the timer is running at // use 1:1 prescaler for max resolution, the PB clock OpenTimer23(T2_ON | T2_SOURCE_INT | T2_PS_1_1, (pbFreq/1000)*LED_BLINK_RATE); } while(1) { // do some other useful work } }
int main() { SYSTEMConfigPerformance(FCY); /* JTAG off */ DDPCONbits.JTAGEN = 0; /* RA0 = 0 */ mPORTAClearBits(BIT_0); /* RA0 O/P */ mPORTASetPinsDigitalOut(BIT_0); while(1) { /* F = 10 Hz */ delay_ms(50); /* RA0 = ! RA0 */ mPORTAToggleBits(BIT_0); } return 0; }
static void UART_Config (CPU_INT32U baud_rate) { CPU_INT32U ubrg; CPU_INT32U config1; CPU_INT32U config2; mPORTAClearBits(DEF_BIT_07); /* Turn off RA7 on startup. */ mPORTASetPinsDigitalOut(DEF_BIT_07); /* Make RA7 as output. */ ubrg = UART_CalcBRG(baud_rate); /* Calculate the correct multiplier */ config1 = UART_EN /* UART module enabled */ | UART_IDLE_CON /* UART works in IDLE mode */ | UART_RX_TX /* Communication is done through the normal pins */ | UART_DIS_WAKE /* Disable Wake-up on START bit detect during SLEEP */ | UART_DIS_LOOPBACK /* Disable loop back */ | UART_DIS_ABAUD /* Input to capture module from ICx pin */ | UART_NO_PAR_8BIT /* 8 bits no parity */ | UART_1STOPBIT /* 1 stop bit */ | UART_IRDA_DIS /* IrDA disabled */ | UART_MODE_FLOWCTRL /* UART pins in flow control mode */ | UART_DIS_BCLK_CTS_RTS /* Disable BCLK, CTS, and RTS pins */ | UART_NORMAL_RX /* UxRX idle stat is '1' */ | UART_BRGH_SIXTEEN; /* 16x baud clock */ config2 = UART_TX_PIN_LOW /* IrDA encoded UxTx idle stat is '0' */ | UART_RX_ENABLE /* Enable UxRx pin */ | UART_TX_ENABLE /* Enable UxTx pin */ | UART_INT_TX /* Interrupt on trasnfer of each character to TSR */ | UART_INT_RX_CHAR /* Interrupt on every char received */ | UART_ADR_DETECT_DIS /* Disable 9-bit address detect */ | UART_RX_OVERRUN_CLEAR; /* Rx buffer overrun status bit clear */ BSP_UART_START(config1, config2, ubrg); /* Configure the settings */ }
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; }
// // Main application entry point. // int main(void) { static TICK t = 0; static DWORD dwLastIP = 0; // Initialize application specific hardware InitializeBoard(); // Initialize stack-related hardware components that may be // required by the UART configuration routines TickInit(); MPFSInit(); // Initialize Stack and application related NV variables into AppConfig. InitAppConfig(); // Initialize core stack layers (MAC, ARP, TCP, UDP) and // application modules (HTTP, SNMP, etc.) StackInit(); // Initialize any application-specific modules or functions/ // For this demo application, this only includes the // UART 2 TCP Bridge //************** PIC32-WEB ******************** //***** Additional initializations Start: ***** // 1. UART1 USART_Init(115200,(GetPeripheralClock())); USART_Test_Menu_Begin(); // 2. Timer1 // configure Timer 1 using external clock(32768Hz), 1:1 prescale, // period 0x8000, thus set interrupt on every 1sec // Blink LED0 (right most one) with frequency 2Hz at every one Timer1 interrupt. OpenTimer1(T1_ON | T1_SOURCE_EXT | T1_PS_1_1, 0x8000); // set up the timer interrupt with a priority of 3 ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_3); //CloseTimer1(); // Switches off the Timer1 mPORTASetPinsDigitalOut(BIT_2); // Set RA2 like out -> LED1 // 3. Enable interrupts at the end of initialization // enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); //***** Additional initializations End: ***** // Now that all items are initialized, begin the co-operative // multitasking loop. This infinite loop will continuously // execute all stack-related tasks, as well as your own // application's functions. Custom functions should be added // at the end of this loop. // Note that this is a "co-operative mult-tasking" mechanism // where every task performs its tasks (whether all in one shot // or part of it) and returns so that other tasks can do their // job. // If a task needs very long time to do its job, it must be broken // down into smaller pieces so that other tasks can have CPU time. while(1){ // Run all enabled web demo applications: // This task performs normal stack task including checking // for incoming packet, type of packet and calling // appropriate stack entity to process it. StackTask(); // This tasks invokes each of the core stack application tasks StackApplications(); // Process application specific tasks here. // For this demo app, this will include the Generic TCP // client and servers, and the SNMP, Ping, and SNMP Trap // demos. Following that, we will process any IO from // the inputs on the board itself. // Any custom modules or processing you need to do should // go here. #if defined(STACK_USE_ICMP_CLIENT) PingDemo(); #endif #if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED) SNMPTrapDemo(); if(gSendTrapFlag) SNMPSendTrap(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleyTCPClientDemo(); BerkeleyTCPServerDemo(); BerkeleyUDPClientDemo(); #endif ProcessIO(); // If the local IP address has changed (ex: due to DHCP lease change) // write the new IP address to the LCD display, UART, and Announce // service if(dwLastIP != AppConfig.MyIPAddr.Val) { #if defined(STACK_USE_ANNOUNCE) AnnounceIP(); #endif } } }
main() { // Disable JTAG (on RA0 and RA1 ) mJTAGPortEnable( DEBUG_JTAGPORT_OFF ); // 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(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); initializeUART(); initializeADC(); initializeLCD(); initializeRPG(); /* Initialize SD card */ setup_SDSPI(); SD_setStart(); /* Fill tempBuffer[] with int 0 to 63 * Write it to the current block. * Empty tempBuffer[] to all 0. * Read from the current block to make sure that it returns the right value. */ fillTempBuffer(); testSDReadWrite(tempBuffer); curr_read_block = curr_block; ConfigTimer1(); // Enable Timer1 for second counts configureInterrupts(); // T2CON = 0x8030; // TMR1 on, prescale 1:256 PB mPORTASetPinsDigitalOut( LED_MASK ); // LEDs = output mPORTDSetPinsDigitalIn( PB_MASK_D ); // PBs on D = input curr_state = READY; // enable interrupts INTEnableInterrupts(); int i = 0; while( 1 ) { if (getPrintToUARTFlag() == 1){ LCDMenuControl(); //mPORTAToggleBits( LED_MASK ); convertAndPrintIntegerToString("i => ", i++); convertAndPrintIntegerToString("timeElapse => ", timeElapsed); convertAndPrintIntegerToString("timeElapsedLEDSample => ", timeElapsedLEDSample); convertAndPrintIntegerToString("timeElapsedLEDTurnedOff => ", timeElapsedLEDTurnedOff); convertAndPrintIntegerToString("sampleLEDNow => ", sampleLEDNow); convertAndPrintIntegerToString(" ADC Value => ", getChannel5Value()); printShadowDetect(); printLightLevel(); drawLightDetectedBar(); controlPowerRelay(); switch(curr_state) { case READY : WriteString("State => READY "); break; case SLEEP : WriteString("State => SLEEP "); break; case HIBERNATE : WriteString("State => HIBERNATE"); break; case BUSY : WriteString("State => BUSY "); break; } WriteString("\r"); setPrintToUARTFlag(0); } if (NEW_BYTE_RECEIVED == 1){ curr_state = READY; NEW_BYTE_RECEIVED = 0; //mPORTAToggleBits( LED_MASK ); char tempArray[] = "g"; tempArray[0] = characterByteReceived; WriteString(tempArray); if(curr_state = HIBERNATE) { addByteToBuffer(characterByteReceived); } else { PutCharacter(characterByteReceived); } } if(bufferIndex == 512) { SDWriteBlock(currBlock); currBlock++; bufferIndex = 0; } if((curr_state == READY) && (timeElapsed >= SLEEP_TIMEOUT) && (timeElapsed < HIBERNATE_TIMEOUT)) { curr_state = SLEEP; } else if((curr_state == SLEEP) && (timeElapsed >= HIBERNATE_TIMEOUT)) { curr_state = HIBERNATE; timeElapsed = 0; } if (transmitDataFromSDCard == 1) { transmitDataFromSDCard = 0; forwardDataToPrinter(); } } // main (while) loop return 0; } // main
//******************************** //******************************** //********** 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); }
// === 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