void BOARD_Init() { unsigned int dma_status; unsigned int int_status; //DEVCFG1bits.FCKSM = 0; //mSYSTEMUnlock(int_status, dma_status); //DEVCFG1bits.FCKSM = 0; //DEVCFG2bits.FPLLIDIV=1; SYSKEY = 0; SYSKEY = 0xAA996655; SYSKEY = 0x556699AA; CFGCONbits.IOLOCK = 0; SYSKEY = 0; SYSTEMConfig(SYSTEM_CLOCK, SYS_CFG_ALL); ANSELA=0; ANSELB=0; ANSELC=0; //OSCConfig(OSC_FRC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); //OSCCONbits.NOSC=0x1; OSCSetPBDIV(OSC_PB_DIV_1); //mSYSTEMLock(int_status, dma_status); // AD1PCFG = 0xffff; INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableInterrupts(); SERIAL_Init(); //printf("This code compiled at %s on %s\r\n", __TIME__, __DATE__); }
/* main function */ int main() { OSCSetPBDIV (OSC_PB_DIV_1); //configure PBDIV so PBCLK = SYSCLK initIntGlobal(); initPort(); initCN(); initPWM(); while (1); }
void systemInit(void) { //Enable optimal performance SYSTEMConfigPerformance(80000000); //Configures peripheral bus divisor OSCSetPBDIV(OSC_PB_DIV_2); //Enable multi-vectored mode INTEnableSystemMultiVectoredInt(); }
int main(void) { // 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); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(SYS_FREQ); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4 the CPU frequency OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); INTEnableInterrupts(); // Set up the UART peripheral so we can send serial data. UARTConfigure(UART_USED, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_USED, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_USED, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_USED, F_PB, UART_BAUD_RATE); UARTEnable(UART_USED, UART_ENABLE | UART_TX); // And configure printf/scanf to use the correct UART. if (UART_USED == UART1) { __XC_UART = 1; } /*************************************************************************************************** * Your code goes in between this comment and the following one with asterisks. **************************************************************************************************/ /*************************************************************************************************** * Your code goes in between this comment and the preceding one with asterisks. **************************************************************************************************/ // Returning from main() is bad form in embedded environments. So we sit and spin. while (1); }
int main(void) { // 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); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(SYS_FREQ); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4 the CPU frequency OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); // Set up the UART peripheral so we can send serial data. UARTConfigure(UART_USED, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_USED, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_USED, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_USED, F_PB, UART_BAUD_RATE); UARTEnable(UART_USED, UART_ENABLE | UART_TX); // And configure printf/scanf to use the correct UART. if (UART_USED == UART1) { __XC_UART = 1; } extern void T1Setup(); extern void T1Stop(); extern void T1Start(); // Enable LED outputs 0-7 by setting TRISE register TRISECLR = 0x00FF; // Initialize the PORTE to 0 PORTECLR = 0x00FF; // Set the lowest bit PORTESET = 1; OledInit(); OledDisplayOn(); printf("Starting Timer Set-up\n"); T1Setup(); int stopped = 0, reset = 0; extern volatile int milliseconds; int temp = 0; int count = 0; printTime(0, 0); while(1) { // Display the least significant part of the time for debugging int x = PORTD & 0xfff; if(x == 272) { if(stopped) { stopped = 0; T1Start(); } if(reset) reset = 0; if(milliseconds - temp >= 1000) { count++; int minutes = count/60; int seconds = count % 60; printTime(minutes,seconds); temp = milliseconds; } } else if(x == 784 || x == 528) { if(reset != 1) { printTime(0, 0); T1Stop(); stopped = 1; reset = 1; count = 0; } } else { stopped = 1; continue; } } }
int main(void) { // 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); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(SYS_FREQ); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4 the CPU frequency OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); INTEnableInterrupts(); // Set up the UART peripheral so we can send serial data. UARTConfigure(UART_USED, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_USED, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_USED, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_USED, F_PB, UART_BAUD_RATE); UARTEnable(UART_USED, UART_ENABLE | UART_TX); // And configure printf/scanf to use the correct UART. if (UART_USED == UART1) { __XC_UART = 1; } // Enable LED outputs 0-7 by setting TRISE register TRISECLR = 0x00FF; // Initialize the PORTE to 0 PORTECLR = 0x00FF; // Set the lowest bit int mask = 1; PORTESET = mask; int delay = 0xA0000; // Loop forever, it is bad to exit in an embedded processor. int count=0; // move this into the delay function while (1) { // Move this printf into your getDelay function! printf("Hello, world! %d\n",count++); // Replace this with the getDelay function call! //int delay = getDelay(); // do nothing for a lot of cycles int i=0; for(i=0;i<delay;i++) ; // shift left by 1 mask = mask << 1; // rotate around if more than 8 bits if (mask & 0x0100) mask = 1; // Set the output to the new mask PORTE=mask; delay = 0xA0000 + 0x40000*sin((double)count/10) + 0x20000*cos((double)count/5); if(delay < 0) delay *= -1; } }
int main(void) { int i, spi_timeout; unsigned long counter; /* Disable JTAG port so we get our I/O pins back */ DDPCONbits.JTAGEN = 0; /* Enable optimal performance */ SYSTEMConfigPerformance(GetSystemClock()); /* Use 1:1 CPU Core:Peripheral clocks */ OSCSetPBDIV(OSC_PB_DIV_1); /* configure the core timer roll-over rate */ OpenCoreTimer(CORE_TICK_RATE); /* set up the core timer interrupt */ mConfigIntCoreTimer((CT_INT_ON | CT_INT_PRIOR_6 | CT_INT_SUB_PRIOR_0)); /* enable multi vector interrupts */ INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableInterrupts(); map_peripherals(); init_io_ports(); configure_pwm(); init_spi(); init_dma(); /* wait until tx buffer is filled up */ while (!SPI2STATbits.SPITBF); reset_board(); spi_data_ready = 0; spi_timeout = 0; counter = 0; /* enable watchdog */ WDTCONSET = 0x8000; /* main loop */ while (1) { if (spi_data_ready) { spi_data_ready = 0; /* the first element received is a command string */ switch (rxBuf[0]) { case 0x5453523E: /* >RST */ reset_board(); break; case 0x314D433E: /* >CM1 */ stepgen_update_input((const void *)&rxBuf[1]); stepgen_get_position((void *)&txBuf[1]); break; case 0x324D433E: /* >CM2 */ update_outputs(rxBuf[1]); update_pwm_duty((uint32_t *)&rxBuf[2]); txBuf[1] = read_inputs(); break; case 0x4746433E: /* >CFG */ stepgen_update_stepwidth(rxBuf[1]); update_pwm_period(rxBuf[2]); stepgen_reset(); break; case 0x5453543E: /* >TST */ for (i=0; i<BUFSIZE; i++) txBuf[i] = rxBuf[i] ^ ~0; break; } } /* if rx buffer is half-full, update the integrity check. There isn't enough time if we wait for complete transfer */ if (DCH0INTbits.CHDHIF) { DCH0INTCLR = 1<<4; /* clear flag */ txBuf[0] = rxBuf[0] ^ ~0; } /* if rx buffer is full, data from spi bus is ready */ if (DCH0INTbits.CHBCIF) { DCH0INTCLR = 1<<3; /* clear flag */ spi_data_ready = 1; spi_timeout = SPI_TIMEOUT; } /* reset the board if there is no SPI activity */ if (spi_timeout) spi_timeout--; if (spi_timeout == 1) { DCH0ECONSET=BIT_6; /* abort DMA transfers */ DCH1ECONSET=BIT_6; init_spi(); init_dma(); reset_board(); /* wait until tx buffer is filled up */ while (!SPI2STATbits.SPITBF); } /* blink onboard led */ if (!(counter++ % (spi_timeout ? 0x10000 : 0x40000))) { LED_TOGGLE; } /* keep alive */ WDTCONSET = 0x01; } return 0; }
int main(void) { // 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); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(SYS_FREQ); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4 the CPU frequency OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); INTEnableInterrupts(); // Set up the UART peripheral so we can send serial data. UARTConfigure(UART_USED, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART_USED, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_USED, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_USED, F_PB, UART_BAUD_RATE); UARTEnable(UART_USED, UART_ENABLE | UART_TX); // And configure printf/scanf to use the correct UART. if (UART_USED == UART1) { __XC_UART = 1; } // Initialize the array char seed1[] = __TIME__; unsigned int seed2 = (((unsigned int)(seed1[7] ^ seed1[2])) << 8) | ((unsigned int)(seed1[4] ^ seed1[6])); srand(seed2); unsigned char vals[] = { rand(), rand(), rand(), rand(), rand() }; int valsToBeSorted[] = {vals[0], vals[1], vals[2], vals[3], vals[4]}; // Sort the array in place. int i, j; for (i = 0; i < 5; ++i) { int aTemp = valsToBeSorted[i]; for (j = i - 1; j >= 0; j--) { if (valsToBeSorted[j] <= aTemp) break; valsToBeSorted[j + 1] = valsToBeSorted[j]; } valsToBeSorted [j+1] = aTemp; } // Print out the array printf("["); for (i=0;i<4;++i) { printf("%d, ", valsToBeSorted[i]); } printf("%d]\n", valsToBeSorted[i]); /* * Returning from main() is bad form in embedded environments. So we * sit and spin. */ while (1); }
int main() { // 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(F_SYS, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); // Auto-configure the PIC32 for optimum performance at the specified operating frequency. SYSTEMConfigPerformance(F_SYS); // osc source, PLL multipler value, PLL postscaler , RC divisor OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1); // Configure the PB bus to run at 1/4th the CPU frequency, so 20MHz. OSCSetPBDIV(OSC_PB_DIV_4); // Enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); INTEnableInterrupts(); // Configure Timer 2 using PBCLK as input. We configure it using a 1:16 prescalar, so each timer // tick is actually at F_PB / 16 Hz, so setting PR2 to F_PB / 16 / 100 yields a .01s timer. OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_16, F_PB / 16 / 100); // Set up the timer interrupt with a medium priority of 4. INTClearFlag(INT_T2); INTSetVectorPriority(INT_TIMER_2_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_TIMER_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_T2, INT_ENABLED); /******************************** Your custom code goes below here ********************************/ int check; OledInit(); AdcInit(); LEDS_INIT(); check = GameInit(); if(check == STANDARD_ERROR) { FATAL_ERROR(); } float currPage; float binSize; float titleSize; float descSize; float numPages; uint8_t roomExit; uint16_t adcValue = 0; while(1) { roomExit = GameGetCurrentRoomExits(); LEDS_SET(roomExit); while(buttonEvents == 0) { descSize = GameGetCurrentRoomDescription(roomData.description); titleSize = GameGetCurrentRoomTitle(roomData.title); numPages = ((titleSize + descSize) / MAX_OLED_PIXELS); binSize = (ADC_MAX_VALUE / numPages); if(AdcChanged()) { adcValue = AdcRead(); } currPage = (adcValue / binSize); if(currPage < 1) { char titleArray[TITLE_OLED_SPACE] = {0}; char descriptionBuffer[FIRST_PG_DESCRIPTION_OLED_SPACE] = {0}; strncpy(descriptionBuffer, roomData.description, DESCRIPTION_COPY); sprintf(titleArray, "%s\n%s", roomData.title, descriptionBuffer); OledClear(OLED_COLOR_BLACK); OledDrawString(titleArray); } else { char buffer[MAX_OLED_PIXELS] = {0}; int buffIndex; buffIndex = (int)currPage * MAX_OLED_PIXELS; strncpy(buffer, (roomData.description + buffIndex - OFFSET), MAX_OLED_PIXELS); OledClear(OLED_COLOR_BLACK); OledDrawString(buffer); } OledUpdate(); } if((buttonEvents & BUTTON_EVENT_4UP) && (roomExit & GAME_ROOM_EXIT_NORTH_EXISTS)) { GameGoNorth(); } else if((buttonEvents & BUTTON_EVENT_3UP) && (roomExit & GAME_ROOM_EXIT_EAST_EXISTS)) { GameGoEast(); } else if((buttonEvents & BUTTON_EVENT_2UP) && (roomExit & GAME_ROOM_EXIT_SOUTH_EXISTS)) { GameGoSouth(); } else if((buttonEvents & BUTTON_EVENT_1UP) && (roomExit & GAME_ROOM_EXIT_WEST_EXISTS)) { GameGoWest(); } buttonEvents = BUTTON_EVENT_NONE; } /**************************************************************************************************/ while (1); }