void setup_ports (void) { if( _UART_JE_ == 1 ) //UART connected to JE pins { // UART JE port pins - /* JE-01 CN20/U1CTS/RD14 RD14 JE-02 U1RTS/BCLK1/CN21/RD15 RD15 JE-03 U1RX/RF2 RF2 JE-04 U1TX/RF8 RF8 */ PORTSetPinsDigitalIn (IOPORT_F, BIT_2); PORTSetPinsDigitalOut (IOPORT_F, BIT_8); } if( _UART_JH_ == 1 ) //UART connected to JH pins { // UART JH port pins - /* JH-01 U2CTS/RF12 RF12 JH-02 U2RTS/BCLK2/RF13 RF13 JH-03 PMA9/U2RX/CN17/RF4 RF4 JH-04 PMA8/U2TX/CN18/RF5 RF5 */ PORTSetPinsDigitalIn (IOPORT_F, BIT_4); PORTSetPinsDigitalOut (IOPORT_F, BIT_5); } }
void PmodHB5_INIT(UART_MODULE uartID) { PORTSetPinsDigitalOut(IOPORT_D, BIT_7); //HB5 Direction PORTSetPinsDigitalOut(IOPORT_D, BIT_1); //HB5 Enable PORTSetPinsDigitalIn(IOPORT_D, BIT_9); //HB5 Sensor A PORTSetPinsDigitalIn(IOPORT_C, BIT_1); //HB5 Sensor B hBridge.sensorAport = IOPORT_D; hBridge.sensorAportBit = BIT_9; hBridge.sensorBport = IOPORT_C; hBridge.sensorBportBit = BIT_1; hBridge.directionPort = IOPORT_D; hBridge.directionPortBit = BIT_7; hBridge.currentDirection = PMOD_HB5_DIR_CW; hBridge.newDirection = PMOD_HB5_DIR_CW; hBridge.ocChannel = 2; OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); OpenTimer2(T2_ON | T2_PS_1_256, SYSTEM_CLOCK/PB_DIV/PRESCALE/(TOGGLES_PER_SEC/2)); OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK); ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); INTEnableSystemMultiVectoredInt(); UARTPutS("\r\nPmodHB5 init complete\r\n",uartID); }
/* * Intializes hbridges for the motors */ static void init_hbridge_data() { memset(&Motors[LEFT_MOTOR], 0, sizeof(struct hbridge_info)); memset(&Motors[RIGHT_MOTOR], 0, sizeof(struct hbridge_info)); // Enable reading value of sensor A PORTSetPinsDigitalIn(HBRIDGE_LEFT_SA_PORT, HBRIDGE_LEFT_SA_BIT); PORTSetPinsDigitalIn(HBRIDGE_LEFT_SB_PORT, HBRIDGE_LEFT_SB_BIT); PORTSetPinsDigitalIn(HBRIDGE_RIGHT_SA_PORT, HBRIDGE_RIGHT_SA_BIT); PORTSetPinsDigitalIn(HBRIDGE_RIGHT_SB_PORT, HBRIDGE_RIGHT_SB_BIT); // Enable writing direction pin PORTSetPinsDigitalOut(HBRIDGE_LEFT_DIR_PORT, HBRIDGE_LEFT_DIR_BIT); PORTSetPinsDigitalOut(HBRIDGE_RIGHT_DIR_PORT, HBRIDGE_RIGHT_DIR_BIT); // Setup left motor Motors[LEFT_MOTOR].state.hbridge_id = LEFT_MOTOR; Motors[LEFT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_A] = HBRIDGE_LEFT_SA_BIT; Motors[LEFT_MOTOR].sensor_port[PMOD_HB_SENSOR_A] = HBRIDGE_LEFT_SA_PORT; Motors[LEFT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_B] = HBRIDGE_LEFT_SB_BIT; Motors[LEFT_MOTOR].sensor_port[PMOD_HB_SENSOR_B] = HBRIDGE_LEFT_SB_PORT; Motors[LEFT_MOTOR].dir_port = HBRIDGE_LEFT_DIR_PORT; Motors[LEFT_MOTOR].dir_bit = HBRIDGE_LEFT_DIR_BIT; Motors[LEFT_MOTOR].ocn = HBRIDGE_LEFT_OC; Motors[LEFT_MOTOR].tmrn = HBRIDGE_LEFT_TMR; // Setup right motor Motors[RIGHT_MOTOR].state.hbridge_id = RIGHT_MOTOR; Motors[RIGHT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_A] = HBRIDGE_RIGHT_SA_BIT; Motors[RIGHT_MOTOR].sensor_port[PMOD_HB_SENSOR_A] = HBRIDGE_RIGHT_SA_PORT; Motors[RIGHT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_B] = HBRIDGE_RIGHT_SB_BIT; Motors[RIGHT_MOTOR].sensor_port[PMOD_HB_SENSOR_B] = HBRIDGE_RIGHT_SB_PORT; Motors[RIGHT_MOTOR].dir_port = HBRIDGE_RIGHT_DIR_PORT; Motors[RIGHT_MOTOR].dir_bit = HBRIDGE_RIGHT_DIR_BIT; Motors[RIGHT_MOTOR].ocn = HBRIDGE_RIGHT_OC; Motors[RIGHT_MOTOR].tmrn = HBRIDGE_RIGHT_TMR; // Setup function pointers Motors[LEFT_MOTOR].state.read_sensor_state = &read_sensor_state; Motors[RIGHT_MOTOR].state.read_sensor_state = &read_sensor_state; Motors[LEFT_MOTOR].state.set_direction = &set_direction; Motors[RIGHT_MOTOR].state.set_direction = &set_direction; Motors[LEFT_MOTOR].state.clear_direction = &clear_direction; Motors[RIGHT_MOTOR].state.clear_direction = &clear_direction; Motors[LEFT_MOTOR].state.set_speed = &set_speed; Motors[RIGHT_MOTOR].state.set_speed = &set_speed; Motors[LEFT_MOTOR].state.get_speed = &get_speed; Motors[RIGHT_MOTOR].state.get_speed = &get_speed; // Intialize direction set_hbridge_direction(&(Motors[LEFT_MOTOR].state), PMOD_HB_CCW); set_hbridge_direction(&(Motors[RIGHT_MOTOR].state), PMOD_HB_CW); // Intialize speed set_hbridge_speed(&(Motors[LEFT_MOTOR].state), 0); set_hbridge_speed(&(Motors[LEFT_MOTOR].state), 0); set_target_speed(LEFT_MOTOR, 0); set_target_speed(RIGHT_MOTOR, 0); }
int setup(void) { // Optimize system timings SYSTEMConfig(SYSCLK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); // Setup interrupts //INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); //INTEnableInterrupts(); // Setup LEDs, these pins need to be outputs TRISEbits.TRISE0 = 0; TRISEbits.TRISE1 = 0; TRISEbits.TRISE2 = 0; TRISEbits.TRISE3 = 0; // Set all analog pins to be digital I/O AD1PCFG = 0xFFFF; //Configure SPI SpiChnOpen(1, SPI_CON_MSTEN | SPICON_CKE | SPI_CON_MODE8 | SPI_CON_ON, 200); PORTSetPinsDigitalIn(IOPORT_E, BIT_4); //PORTSetPinsDigitalIn(IOPORT_D, BIT_10); //PORTSetPinsDigitalIn(IOPORT_D, BIT_0); //PORTSetPinsDigitalIn(IOPORT_C, BIT_4); int rData = SPI1BUF; //Clears receive buffer IFS0CLR = 0x03800000; //Clears any existing event (rx / tx/ fault interrupt) SPI1STATCLR = 0x40; //Clears overflow ad7466_cs = 1; //Make sure CS pin is high YLED = 1; //1 is off, 0 is on RLED = 1; WLED = 1; GLED = 1; setupSerial(); SendDataBuffer("Radiometer Configuration Complete.\n\r", sizeof("Radiometer Configuration Complete.\n\r")); return 0; }
void SERIAL_Init(void) { transmitBuffer = (struct CircBuffer*) &outgoingUart; //set up buffer for receive newCircBuffer(transmitBuffer); receiveBuffer = (struct CircBuffer*) &incomingUart; //set up buffer for transmit newCircBuffer(receiveBuffer); UARTConfigure(UART1, 0x00); UARTSetDataRate(UART1, F_PB, 115200); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY | UART_INTERRUPT_ON_TX_BUFFER_EMPTY); INTSetVectorPriority(INT_UART_1_VECTOR, INT_PRIORITY_LEVEL_4); //set the interrupt priority //RPC5R = 1; PPSOutput(1, RPC5, U1TX); PORTSetPinsDigitalIn(IOPORT_C, BIT_3); //U1RXRbits.U1RXR = 0b111; PPSInput(3, U1RX, RPC3); UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); INTEnable(INT_U1RX, INT_ENABLED); //INTSetFlag(INT_U1RX); //INTEnable(INT_U1TX, INT_ENABLED); //PutChar('z'); }
////////////////////////////////////////////////////////////////////////////////// /// name: initialization /// params: none /// return: void /// desc: contains the configuration of hardware and clock, as well as /// the initialization of variables void intialization(void) { // button input pin PORTSetPinsDigitalIn(IOPORT_E, BUTTON); // LED output pin PORTSetPinsDigitalOut(IOPORT_E, LED_1); // motor direction pin PORTSetPinsDigitalOut(IOPORT_B, DC_MOTOR_1); // configuring timer OpenTimer2(T2_ON | T2_PS_1_1, TERMINAL_COUNT); OpenOC4(OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0x80, 0x60); // start these values at 0 _risetime = 0; _falltime = 0; _hitime = 0; // motor starts stationary _motorState = STOP; _motorGo = 0; // start arm fully retracted _faketach = 0; // set these values to 1, as they should start out not // equal to _risetime and _falltime for initial pwm read _store = 1; _ftore = 1; }
void START_Init (void) { #ifdef _TARGET_440H #else PORTSetPinsDigitalIn(IO_START); #endif }
void InitCRModule(void){ WORD TiempoT4 = 1; //En mseg; WORD Prescaler = 32; //Selecciono el prescaler, si lo //cambio revisar opentimer. WORD CuentaT4 = (TiempoT4)*(CLOCK_FREQ/((1<<mOSCGetPBDIV())*Prescaler*1000)); OpenTimer4(T4_ON | T1_IDLE_CON | T4_GATE_OFF | T4_PS_1_32 | T4_32BIT_MODE_OFF | T4_SOURCE_INT, CuentaT4); /*Pongo la int de nivel 3 porque de nivel 4 fastidia las interrupciones del transceptor. Dentro de nivel 3 le doy maxima subprioridad.*/ ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_3 | T4_INT_SUB_PRIOR_3); mCNOpen(CN_ON | CN_IDLE_CON, CN14_ENABLE, CN_PULLUP_DISABLE_ALL); PORTSetPinsDigitalIn(IOPORT_D, BIT_5); mPORTDRead(); ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_3);//6); CRM_Optm_Init(); CRM_Poli_Init(); CRM_AccCtrl_Init(); //Creamos una entrada en la tabla de permisos. ACCCTRL_MSSG_RCVD PeticionCrearEntrada; PeticionCrearEntrada.Action = ActAddEntry; #if defined NODE_1 //BYTE EUI_Permiso[] = {0x01, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, EUI_7}; BYTE EUI_Permiso[] = {EUI_0, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, 0x22}; #elif defined NODE_2 //BYTE EUI_Permiso[] = {0x05, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, EUI_7}; BYTE EUI_Permiso[] = {EUI_0, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, 0x11}; #endif PeticionCrearEntrada.DirecOrigen = EUI_Permiso; CRM_Message(NMM, SubM_AccCtrl, &PeticionCrearEntrada); //Fin de la creacion de entrada de la tabal de permisos. NodoCerrado = FALSE; //Para que pueda aceptar peticiones de acciones de otros nodos. CRM_Repo_Init(); }
void COLOR_Init (void) { #ifdef _TARGET_440H #else PORTSetPinsDigitalIn(IO_COLOR); #endif }
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(); }
void Setup_ports(void) { //Setup ESC pins for PWM output PORTSetPinsDigitalOut(IOPORT_D, BIT_0); //PWM1-OC1 digital pin 3 PORTSetPinsDigitalOut(IOPORT_D, BIT_1); //PWM2-OC2 digital pin 5 PORTSetPinsDigitalOut(IOPORT_D, BIT_2); //PWM3-OC3 digital pin 6 PORTSetPinsDigitalOut(IOPORT_D, BIT_3); //PWM4-OC4 digital pin 9 //Setup Input Capture pins PORTSetPinsDigitalIn(IOPORT_D, BIT_8); //IC1 digital pin 2 (THRO) PORTSetPinsDigitalIn(IOPORT_D, BIT_9); //IC2 digital pin 7 (AILE) PORTSetPinsDigitalIn(IOPORT_D, BIT_10); //IC3 digital pin 8 (ELEV) PORTSetPinsDigitalIn(IOPORT_D, BIT_11); //IC4 digital pin 35 (RUDD) PORTSetPinsDigitalIn(IOPORT_D, BIT_12); //IC5 digital pin 10 (GEAR) //Toggle pin for Timer 5 //PORTSetPinsDigitalOut(IOPORT_F, BIT_1); //pin 4 }
void DeviceInit() { // Configure left motor direction pin and set default direction. trisMtrLeftDirSet = ( 1 << bnMtrLeftDir ); prtMtrLeftDirSet = ( 1 << bnMtrLeftDir ); // forward // Configure right motor diretion pin and set default direction. trisMtrRightDirClr = ( 1 << bnMtrRightDir ); //modify for JD prtMtrRightDirClr = ( 1 << bnMtrRightDir ); // forward // Configure Output Compare 2 to drive the left motor. OC2CON = ( 1 << 2 ) | ( 1 << 1 ); // pwm OC2R = dtcMtrStopped; OC2RS = dtcMtrStopped; // Configure Output Compare 3. OC3CON = ( 1 << 3 ) | ( 1 << 2 ) | ( 1 << 1 ); // pwm OC3R = dtcMtrStopped; OC3RS = dtcMtrStopped; // Configure Timer 2. TMR2 = 0; // clear timer 2 count PR2 = 9999; // Configure Timer 3. TMR3 = 0; PR3 = 9999; // Start timers and output compare units. T2CON = ( 1 << 15 ) | ( 1 << TCKPS20 )|(1 << TCKPS21); // timer 2 prescale = 8 OC2CONSET = ( 1 << 15 ); // enable output compare module 2 OC3CONSET = ( 1 << 15 ); // enable output compare module 3 T3CON = ( 1 << 15 ) | ( 1 << TCKPS31 ) | ( 1 << TCKPS30); //timer3 prescale = 8 // Configure Timer 5. TMR5 = 0; PR5 = 99; // period match every 100 us IPC5SET = ( 1 << 4 ) | ( 1 << 3 ) | ( 1 << 2 ) | ( 1 << 1 ) | ( 1 << 0 ); // interrupt priority level 7, sub 3 IFS0CLR = ( 1 << 20); IEC0SET = ( 1 << 20); // Start timers. T5CON = ( 1 << 15 ) | ( 1 << 5 ) | ( 1 << 4 ); // fTimer5 = fPb / 8 // Setup light sensor pins PORTSetPinsDigitalIn(IOPORT_B, BIT_0 | BIT_3); // Change notice configured for CN 2 through 5 for light sensors. mCNOpen(CN_ON, (CN2_ENABLE | CN3_ENABLE | CN4_ENABLE | CN5_ENABLE), CN_PULLUP_DISABLE_ALL); ConfigIntCN(CHANGE_INT_OFF | CHANGE_INT_PRI_2); // Enable multi-vector interrupts. INTEnableSystemMultiVectoredInt(); }
int main(void) { initPic32(); PORTSetPinsDigitalIn(IOPORT_A, BIT_0); PORTSetPinsDigitalIn(IOPORT_A, BIT_1); PORTSetPinsDigitalIn(IOPORT_B, BIT_14); PPSInput(1, SS1, RPA0); PPSInput(2, SDI1, RPA1); #ifndef NO_SDO PPSOutput(3, RPA2, SDO1); #endif SpiChnOpen(1, SPI_OPEN_MODE8|SPI_OPEN_SLVEN|SPI_OPEN_SSEN #ifdef NO_SDO |SPI_OPEN_DISSDO #endif , 2); init(); uint8_t lastByteReceived = 0; uint8_t cmd[8]; uint8_t length=0; uint8_t toRead = 1; while(1) { if(SpiChnTxBuffEmpty(1)) SpiChnPutC(1, lastByteReceived); lastByteReceived = 0; while(!SpiChnDataRdy(1)) loop(); uint8_t byte = SpiChnGetC(1); cmd[length++] = byte; toRead--; if(toRead==0) toRead = commandReceived(cmd, length); } }
void OledHostTerm() { /* Make the Data/Command select, Reset, and SPI CS pins be inputs. */ PORTSetBits(prtDataCmd, bitDataCmd); PORTSetPinsDigitalIn(prtDataCmd, bitDataCmd); //Data/Command# select PORTSetBits(prtReset, bitReset); PORTSetPinsDigitalIn(prtReset, bitReset); /* Make power control pins be inputs. The pullup resistors on the ** board will ensure that the power supplies stay off. */ PORTSetBits(prtVddCtrl, bitVddCtrl); PORTSetBits(prtVbatCtrl, bitVbatCtrl); PORTSetPinsDigitalIn(prtVddCtrl, bitVddCtrl); //VDD power control (1=off) PORTSetPinsDigitalIn(prtVbatCtrl, bitVbatCtrl); //VBAT power control (1=off) /* Turn SPI port 2 off. */ #if defined (_BOARD_UNO_) SPI2CON = 0; #elif defined (_BOARD_MEGA_) PORTSetBits(prtSck, bitSck); PORTSetBits(prtMosi, bitMosi); PORTSetPinsDigitalIn(prtSck, bitSck); PORTSetPinsDigitalIn(prtMosi, bitMosi); #endif }
/******************************************************************** Funciton: void JoyStickInitialization(void) ********************************************************************/ void JoyStickInitialize(void) { // configure the input pins PORTSetPinsDigitalIn(IOPORT_B, (BIT_0 | BIT_1 | BIT_3 | BIT_4 | BIT_15)); joystickCntrl.value = 0; // set the change notice pins // going to have to use the SFR names here so that I do not overide anything CNPUESET = _CNPUE_CNPUE2_MASK | _CNPUE_CNPUE3_MASK | _CNPUE_CNPUE5_MASK | _CNPUE_CNPUE6_MASK | _CNPUE_CNPUE12_MASK; CNENSET = _CNEN_CNEN2_MASK | _CNEN_CNEN3_MASK | _CNEN_CNEN5_MASK | _CNEN_CNEN6_MASK | _CNEN_CNEN12_MASK; CNCONSET = _CNCON_ON_MASK; }
void CLIC_Init (void) { #ifdef _TARGET_440H PORTSetPinsDigitalIn(IOPORT_B, BIT_3); PORTSetPinsDigitalIn(IOPORT_B, BIT_2); PORTSetPinsDigitalIn(IOPORT_B, BIT_4); #else PORTSetPinsDigitalIn(IO_CLIC_1); PORTSetPinsDigitalIn(IO_CLIC_2); PORTSetPinsDigitalIn(IO_CLIC_3); PORTSetPinsDigitalIn(IO_CLIC_4); #endif }
void SetupMenu() // Set up menu { uint8 i; while(1) { i = GetMenuItem("TERMINAL SETUP","WiFi Setup","ESPUSB Fudge","Scan WiFi","Test Wifi","Exit"); if(i==1) SetupWIFI(); if(i==2) // Open USB in CDC Mode passthough to ESP-01 { SerialPurge(WIFI_UART); CLS(); LCDWriteStrAt(0,4, " USB <-> ESP01 "); LCDWriteStrAt(0,6, " PROGRAMMIING "); LCDWriteStrAt(0,10," PRESS KEY TO "); LCDWriteStrAt(0,12," CONTINUE "); PORTSetPinsDigitalIn(IOPORT_C, BIT_2 ); PPSUnLock; // Unlock PPS (Peripheral Pin Select) to allow PIN Mapping PPSOutput(4,RPC2,NULL); PPSLock; // lock PPS ESP_PGM(0); WIFI_PWR(1); i = WaitEvent(9999); CLS(); WIFI_PWR(0); ESP_PGM(1); DelayMs(500); WIFI_PWR(1); LCDWriteStrAt(0,4, " USB <-> ESP01 "); LCDWriteStrAt(0,6, " PASSTHROUGH "); LCDWriteStrAt(0,10," POWER OFF TO "); LCDWriteStrAt(0,12," RESUME "); while(1); } if(i==4) { if(CheckWIFI()) LCDInform("SUCCESS","Connection OK"); } if(i==3) { LCDInform("WARNING","Not Implemented"); } if(i==0 ||i==5) return; } }
int main() { mJTAGPortEnable(DEBUG_JTAGPORT_OFF); mPORTFClearBits(BIT_0); // Make all lower 8-bits of PORTA as output mPORTFSetPinsDigitalOut( BIT_0 ); TRISE=0x0; PORTE = 0x0f; // Start timer1, Fpb/256, max period OpenTimer1(T1_ON | T1_PS_1_256 | T1_SOURCE_INT, 0xFFFF); // The main loop PORTSetPinsDigitalIn(IOPORT_D, BIT_5); mCNOpen(CN_ON, CN14_ENABLE, 0); // Read the port to clear any mismatch on change notice pins int dummy = PORTD; // Clear change notice interrupt flag ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_2); INTEnableSystemMultiVectoredInt(); uart1_init(115200); setbuf(stdin, NULL); //no input buffer (for scanf) setbuf(stdout, NULL); //no output buffer (for printf) printf ("Hello World!\r\n"); while( 1) { putchar(getchar()); WriteTimer1(0); while ( TMR1 < LONG_DELAY); PORTE+=1; } }
static void SetPic32MZIoPins(void) { PORTSetPinsDigitalOut(_ETH_MDC_PORT, _ETH_MDC_BIT); PORTSetPinsDigitalIn(_ETH_MDIO_PORT, _ETH_MDIO_BIT); PORTSetPinsDigitalOut(_ETH_TXEN_PORT, _ETH_TXEN_BIT); PORTSetPinsDigitalOut(_ETH_TXD0_PORT, _ETH_TXD0_BIT); PORTSetPinsDigitalOut(_ETH_TXD1_PORT, _ETH_TXD1_BIT); PORTSetPinsDigitalIn(_ETH_RXCLK_PORT, _ETH_RXCLK_BIT); PORTSetPinsDigitalIn(_ETH_RXDV_PORT, _ETH_RXDV_BIT); PORTSetPinsDigitalIn(_ETH_RXD0_PORT, _ETH_RXD0_BIT); PORTSetPinsDigitalIn(_ETH_RXD1_PORT, _ETH_RXD1_BIT); PORTSetPinsDigitalIn(_ETH_RXERR_PORT, _ETH_RXERR_BIT); }
void openSerial(unsigned char serialPortIndex, unsigned long baudRate) { // important to activate the RX for UART5. Information found on the net if (serialPortIndex == SERIAL_PORT_5) { PORTSetPinsDigitalIn(IOPORT_B, BIT_8); } UART_MODULE uart = getUartModule(serialPortIndex); UARTConfigure(uart, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(uart, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(uart, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(uart, GetPeripheralClock(), baudRate); UARTEnable(uart, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); INTEnable(INT_SOURCE_UART_RX(uart), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(uart), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(uart), INT_SUB_PRIORITY_LEVEL_0); // TODO : Move this code to Global Setup ! // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); }
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; }
/** * Initialize an SD flash memory card. * * \param[in] sckRateID SPI clock rate selector. See setSckRate(). * \param[in] chipSelectPin SD chip select pin number. * * \return The value one, true, is returned for success and * the value zero, false, is returned for failure. The reason for failure * can be determined by calling errorCode() and errorData(). */ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0; // 16-bit init start time allows over a minute uint16_t t0 = (uint16_t)millis(); uint32_t arg; SPI2CON = 0; DDPCONbits.JTAGEN = 0; AD1PCFG = 0xFFFF; chipSelectPin_ = chipSelectPin; pinMode(chipSelectPin_, OUTPUT); PORTSetPinsDigitalOut(prtSCK, bnSCK); PORTSetPinsDigitalOut(prtSDO, bnSDO); PORTSetPinsDigitalIn(prtSDI, bnSDI); // set pin modes chipSelectHigh(); // must supply min of 74 clock cycles with CS high. for (uint8_t i = 0; i < 10; i++) spiSend(0XFF); chipSelectLow(); // command to go idle in SPI mode while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { error(SD_CARD_ERROR_CMD0); goto fail; } } // check SD version if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { type(SD_CARD_TYPE_SD1); } else { // only need last byte of r7 response for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); if (status_ != 0XAA) { error(SD_CARD_ERROR_CMD8); goto fail; } type(SD_CARD_TYPE_SD2); } // initialize card and send host supports SDHC if SD2 arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0; while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { // check for timeout if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { error(SD_CARD_ERROR_ACMD41); goto fail; } } // if SD2 read OCR register to check for SDHC card if (type() == SD_CARD_TYPE_SD2) { if (cardCommand(CMD58, 0)) { error(SD_CARD_ERROR_CMD58); goto fail; } if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC); // discard rest of ocr - contains allowed voltage range for (uint8_t i = 0; i < 3; i++) spiRec(); } chipSelectHigh(); #ifndef SOFTWARE_SPI return setSckRate(sckRateID); #else // SOFTWARE_SPI return true; #endif // SOFTWARE_SPI fail: chipSelectHigh(); return false; }
void hardwareInit(void) { // Configure the device for maximum performance, but do not change the PBDIV clock divisor. // Given the options, this function will change the program 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); PORTSetPinsAnalogIn(IOPORT_A, BIT_1 | BIT_0); // Could also use mPORTDSetPinsDigitalOut(BIT_6 | BIT_7); //PORTSetPinsDigitalOut(IOPORT_A, BIT_3 | BIT_2); PORTSetPinsDigitalOut(IOPORT_A, BIT_10 | BIT_9 | BIT_8 | BIT_7 | BIT_6 | BIT_5 | BIT_4 ); PORTSetPinsAnalogIn(IOPORT_B, BIT_2 | BIT_1 | BIT_0); PORTSetPinsDigitalOut(IOPORT_B, BIT_15 | BIT_14 | BIT_13 | BIT_12 | BIT_11 | BIT_10 | BIT_9 | BIT_7 | BIT_4 | BIT_3); PORTSetPinsDigitalIn(IOPORT_B, BIT_8 | BIT_6 | BIT_5); PORTSetPinsDigitalOut(IOPORT_C, BIT_9 | BIT_7 | BIT_6 | BIT_3 | BIT_2 |BIT_1 | BIT_0); PORTSetPinsDigitalIn(IOPORT_C, BIT_8 | BIT_5 | BIT_4); // This is the waggle to set reprogrammable peripheral // Assume ints & DMA disa SYSKEY = 0xAA996655; SYSKEY = 0x556699AA; U1RXR = 0b0001; // U2.15 RPB6 as RxD1 (PICO) input RPB7R = 0b0001; // U2.16 RPB7 as TxD1 (POCI) output SDI1R = 0b0011; // U2.22 RPB11 is SDI1 RPB8R = 0b0011; // U2.17 RPB8 is SDO1 SYSKEY = 0x33333333; // Junk relocks it OSCCONbits.SOSCEN = 1; OSCCONbits.SOSCRDY = 1; // Serial port on UART1 // Note, Mode & Sta have atomic bit clr, set, & inv registers U1MODEbits.ON = 1; // Ena UART1, per UEN, with UTXEN U1MODEbits.SIDL = 0; // Continue oper in idle mode U1MODEbits.IREN = 0; // Disa IrDA U1MODEbits.RTSMD = 1; // U1RTS_n pin in simplex mode U1MODEbits.UEN = 0b00; // U1CTS_n & U1RTS_n unused U1MODEbits.WAKE = 0; // Disa wakeup U1MODEbits.LPBACK = 0; // Disa loopback U1MODEbits.ABAUD = 0; // Disa autobaud U1MODEbits.RXINV = 0; // U1Rx 0 for idle high U1MODEbits.BRGH = 0; // Std speed 16x, 1: high speed is 4x baud clk U1MODEbits.PDSEL = 0b00; // 8 bit data, no parity U1MODEbits.STSEL = 0; // 1 stop bit U1STAbits.ADM_EN = 0; // Disa automatic address mode detect U1STAbits.UTXISEL = 0b01; // Interrupt gen when all chars transmitted U1STAbits.UTXINV = 0; // U1Tx idle is 1. U1STAbits.URXEN = 1; // Ena U1Rx U1STAbits.UTXBRK = 0; // Disa Break (Start bit, then 12 0's, Stop) U1STAbits.UTXEN = 1; // Ena U1Tx U1STAbits.URXISEL = 0b01; // Interrupt flag asserted while buffer is 1/2 or more full U1STAbits.ADDEN = 0; // Disa address mode U1BRG = 162; // 80 for 19200 baud = PBCLK / (16 x (BRG + 1)), PBCLK = 25 MHz @ div / 1, Jitter 0.47% // 162 for 9600, 325 for 4800 RS485_DE = 0; RS485_RE_n = 0; AD1CON1bits.FORM = 0b000; // Integer 16-bit, 10 lsb's AD1CON1bits.SSRC = 0b111; // Internal ctr ends sampling and starte conver, auto. AD1CON1bits.ASAM = 1; // Sampling begins immediately after last conversion completes; SAMP bit is automatically set AD1CON1bits.SAMP = 1; // Ena sampling AD1CON2bits.VCFG = 0b001; // Vref+ has bandgap ref, use Vss AD1CON2bits.OFFCAL = 0; // Disa offset cal mode AD1CON2bits.CSCNA = 0; // Disa scan the inputs AD1CON2bits.BUFM = 0; // Buffer is one 16-bit word AD1CON2bits.ALTS = 0; // Always use sample A input mux settings AD1CON3bits.ADRC = 1; // ADC clk from FRC AD1CON3bits.SAMC = 0b11111; // 31 x TAD autosample time AD1CON3bits.ADCS = 0xFF; // 512 x TPB = TAD, TPB from PBCLK // These don't matter if we scan. AD1CHSbits.CH0NB = 0; // Sample B Ch 0 neg input is Vrefl AD1CHSbits.CH0SB = 0b00000; // Select B: AN0 AD1CHSbits.CH0NA = 0; // Sample A Ch 0 neg input is Vrefl AD1CHSbits.CH0SA = 0b00000; // Select B: AN0 AD1CON1bits.ON = 1; // This must be last step. // SPI1CONbits.MSTEN = 1; // Master mode. Shd be first command. // SPI1CONbits.FRMEN = 0; // Framed SPI disa // SPI1CONbits.MSSEN = 0; // Slave select SPI support disa // SPI1CONbits.MCLKSEL = 0; // Use PBCLK for BRG // SPI1CONbits.ENHBUF = 0; // Enhanced buffer disa // SPI1CONbits.ON = 0; // Turn it off for now // SPI1CONbits.SIDL = 0; // Continue in Idle // SPI1CONbits.DISSDO = 0; // Use SDOx // SPI1CONbits.MODE16 = 0; // 8 bit transfer // SPI1CONbits.MODE32 = 0; // 8 bit transfer // SPI1CONbits.SMP = 0; // Sample data in middle of phase // SPI1CONbits.CKE = 0; // Serial output data changes on transition from idle clock state to active clock state // SPI1CONbits.SSEN = 0; // Do not use /SS1 pin // SPI1CONbits.CKP = 0; // Idle clock is low // // SPI1CONbits.DISSDI = 0; // Use SDI1 pin // // SPI1CON2bits.AUDEN = 0; // Audio protocol disa // SPI1BRG = 0x100; // spiIoxConfig(); LED_GREEN = 0; }
int main(void) { PORTSetPinsDigitalOut(IOPORT_F,BIT_3); PORTSetPinsDigitalOut(IOPORT_C,BIT_14); PORTSetPinsDigitalOut(IOPORT_D,BIT_11); PORTSetPinsDigitalIn(IOPORT_B,BIT_2|BIT_3|BIT_4|BIT_5); while (1) { if ((PORTBbits.RB2)>0) { PORTSetBits(IOPORT_C, BIT_14); PORTClearBits(IOPORT_D, BIT_11); PORTClearBits(IOPORT_F,BIT_3); } else { PORTClearBits(IOPORT_C, BIT_14); PORTClearBits(IOPORT_D, BIT_11); PORTClearBits(IOPORT_F,BIT_3); } if ((PORTBbits.RB3)>0) { PORTClearBits(IOPORT_C, BIT_14); PORTSetBits(IOPORT_D, BIT_11); PORTClearBits(IOPORT_F,BIT_3); } else { PORTClearBits(IOPORT_C, BIT_14); PORTClearBits(IOPORT_D, BIT_11); PORTClearBits(IOPORT_F,BIT_3); } if ((PORTBbits.RB4)>0) { PORTClearBits(IOPORT_C, BIT_14); PORTClearBits(IOPORT_D, BIT_11); PORTSetBits(IOPORT_F,BIT_3); } else { PORTClearBits(IOPORT_C, BIT_14); PORTClearBits(IOPORT_D, BIT_11); PORTClearBits(IOPORT_F,BIT_3); } if ((PORTBbits.RB5)>0) { PORTSetBits(IOPORT_C, BIT_14); PORTSetBits(IOPORT_D, BIT_11); PORTSetBits(IOPORT_F,BIT_3); } else { PORTClearBits(IOPORT_C, BIT_14); PORTClearBits(IOPORT_D, BIT_11); PORTClearBits(IOPORT_F,BIT_3); } } return (0); }
void I2C1update(void) { if (I2C1STAT & 0x0400) { // bus collision i2c1Mode = I2C1_MODE_ERROR; i2c1BusState = I2C1_BUS_ERROR; I2CEnable(I2C1, FALSE); } if (i2c1Delay) { i2c1Delay--; return; } if (i2c1Mode == I2C1_MODE_IDLE) { if (i2c1QueueRd != i2c1QueueWr) { // show that I2C1 is busy i2c1Mode = I2C1_MODE_WRITE; // clear the write and read indices i2c1Queue[i2c1QueueRd].wi = 0; i2c1Queue[i2c1QueueRd].ri = 0; // start I2C1 transfer i2c1BusState = I2C1_BUS_WR_DATA; I2C1CONSET = _I2CCON_SEN_MASK; } } // I2C1_IDLE else if (i2c1Mode == I2C1_MODE_WRITE_COMPLETE) { I2C1process(); i2c1QueueRd++; if (i2c1QueueRd >= I2C1_QUEUE_SIZE) { i2c1QueueRd = 0; } i2c1Mode = I2C1_MODE_IDLE; } // I2C1_WRITE_COMPLETE else if (i2c1Mode == I2C1_MODE_READ_COMPLETE) { I2C1process(); i2c1QueueRd++; if (i2c1QueueRd >= I2C1_QUEUE_SIZE) { i2c1QueueRd = 0; } i2c1Mode = I2C1_MODE_IDLE; } // I2C1_READ_COMPLETE else if (i2c1Mode == I2C1_MODE_ERROR) { // usually caused is SDA is low before start if (!(PORTA & PA_SCL1)) { // is clock is low, raise it PORTSetPinsDigitalOut(IOPORT_A, PA_SCL1); PORTSetBits(IOPORT_A, PA_SCL1); } else { if (!(PORTA & PA_SDA1)) { // if SDA is still low, try another clock pulse PORTSetPinsDigitalOut(IOPORT_A, PA_SCL1); PORTClearBits(IOPORT_A, PA_SCL1); } else { // SDA is now high, try clearing the error PORTSetPinsDigitalOut(IOPORT_A, PA_SDA1); PORTClearBits(IOPORT_A, PA_SDA1); i2c1Mode = I2C1_MODE_ERR_START; } } } else if (i2c1Mode == I2C1_MODE_ERR_START) { PORTSetBits(IOPORT_A, PA_SDA1); i2c1Mode = I2C1_MODE_ERR_STOP; } else if (i2c1Mode == I2C1_MODE_ERR_STOP) { I2C1STAT &= ~0x400; IFS0 &= ~0x20000000; PORTSetPinsDigitalIn(IOPORT_A, PA_SCL1 | PA_SDA1); I2CEnable(I2C1, TRUE); i2c1Mode = I2C1_MODE_IDLE; } } // I2C1update()
void initButtons() { //BTN1 is connected to I/O Port G, bits PORTSetPinsDigitalIn(BTN1_PORT, BTN1_BIT); }
int main(void) { SpiOpenFlags spiFlags; AudioStereo test_sine[]={ 0 , 0 , 946234 , 946234 , 1877546 , 1877546 , 2779247 , 2779247 , 3637119 , 3637119 , 4437630 , 4437630 , 5168158 , 5168158 , 5817180 , 5817180 , 6374462 , 6374462 , 6831216 , 6831216 , 7180237 , 7180237 , 7416020 , 7416020 , 7534850 , 7534850 , 7534850 , 7534850 , 7416020 , 7416020 , 7180237 , 7180237 , 6831216 , 6831216 , 6374462 , 6374462 , 5817180 , 5817180 , 5168158 , 5168158 , 4437630 , 4437630 , 3637119 , 3637119 , 2779247 , 2779247 , 1877546 , 1877546 , 946234 , 946234 , 0 , 0 , -946234 , -946234 , -1877546 , -1877546 , -2779247 , -2779247 , -3637119 , -3637119 , -4437630 , -4437630 , -5168158 , -5168158 , -5817180 , -5817180 , -6374462 , -6374462 , -6831216 , -6831216 , -7180237 , -7180237 , -7416020 , -7416020 , -7534850 , -7534850 , -7534850 , -7534850 , -7416020 , -7416020 , -7180237 , -7180237 , -6831216 , -6831216 , -6374462 , -6374462 , -5817180 , -5817180 , -5168158 , -5168158 , -4437630 , -4437630 , -3637119 , -3637119 , -2779247 , -2779247 , }; // Initialize audio codec. WM8960CodecOpen(); WM8960CodecConfigVolume(0,0); WM8960CodecConfigSampleRate(SAMPLE_RATE_16000_HZ); WM8960CodecConfigVolume(volADC,volDAC); //Congigure MIPS, Prefetch Cache module. SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); INTEnableSystemMultiVectoredInt(); //Test tone vector sampled at 48kHz. txBuffer = test_sine; //Configure the direction of used pins and //configure as digital pins. PORTSetPinsDigitalIn(IOPORT_G, BIT_7); PORTSetPinsDigitalOut(IOPORT_G, BIT_6); PORTSetPinsDigitalOut(IOPORT_G, BIT_8); PORTSetPinsDigitalOut(IOPORT_G, BIT_9); // //Configure Reference Clock Output to 12.288MHz. // mOSCREFOTRIMSet(REFTRIM); // OSCREFConfig(OSC_REFOCON_USBPLL, //USB-PLL clock output used as REFCLKO source // OSC_REFOCON_OE | OSC_REFOCON_ON, //Enable and turn on the REFCLKO // RODIV); //Configure SPI in I2S mode with 24-bit stereo audio. spiFlags= SPI_OPEN_MSTEN | //Master mode enable SPI_OPEN_SSEN | //Enable slave select function SPI_OPEN_CKP_HIGH | //Clock polarity Idle High Actie Low SPI_OPEN_MODE32 | //Data mode: 32b SPI_OPEN_FRMEN | // Enable Framed SPI SPI_OPEN_FSP_IN | // Frame Sync Pulse is input SPI_OPEN_FSP_HIGH; //Frame Sync Pulse is active high //Configure and turn on the SPI1 module. SpiChnEnable(WM8960DRV_SPI_MODULE, FALSE); SpiChnConfigure(WM8960DRV_SPI_MODULE, spiFlags); SpiChnSetBitRate(WM8960DRV_SPI_MODULE, GetPeripheralClock(), 1024000); SpiChnEnable(WM8960DRV_SPI_MODULE, TRUE); //Enable SPI2 interrupt. INTSetVectorPriority(INT_SPI_2_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_SPI_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_SPI2, INT_ENABLED); SpiChnPutC(SPI_CHANNEL2, 0); //Dummy write to start the SPI //while (1); return 0; }
int main(void) { UINT32 menu_choice; menu_choice = 'y'; OpenUart(UART1,BAUDERATE); OpenUart(UART2,BAUDERATE); // OpenUart(UART3,BAUDERATE); // OpenUart(UART4,BAUDERATE); OpenUart(UART5,BAUDERATE); OpenUart(UART6,BAUDERATE); //IMPORTANT POUR ACTIVER LE RX UART5. INFO TROUVEE SUR LE NET PORTSetPinsDigitalIn(IOPORT_B,BIT_8); while (1) { if (menu_choice=='n') { SendDataBuffer(UARTTEST,goodbye); return(0); } if (menu_choice=='y') { menu_choice = 'n'; SendDataBuffer(UART1,myHelloStr1); SendDataBuffer(UART1,mainMenu); SendDataBuffer(UART2,myHelloStr2); SendDataBuffer(UART2,mainMenu); // SendDataBuffer(UART3,myHelloStr3); // SendDataBuffer(UART3,mainMenu); // SendDataBuffer(UART4,myHelloStr4); // SendDataBuffer(UART4,mainMenu); SendDataBuffer(UART5,myHelloStr5); SendDataBuffer(UART5,mainMenu); SendDataBuffer(UART6,myHelloStr6); SendDataBuffer(UART6,mainMenu); menu_choice = GetMenuChoice(); } } return (0); }
virtual void setup_digital_in() {PORTSetPinsDigitalIn(ltr(), num());}
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]); } }