void readCNAndClrIsr()
{
    int portStatus = 0;
    portStatus = mPORTDRead(); //clear mismatch
    portStatus = mPORTBRead(); //clear mismatch
    mCNClearIntFlag();
}
Ejemplo n.º 2
0
Archivo: Override.c Proyecto: ddeo/sdp
/**********************************************************************
 * Function: Override_init
 * @return None
 * @remark Initializes the override board and the interrupt to detect
 *  when the receiver comes online.
 * @author Darrel Deo
 * @date 2013.04.01  
 **********************************************************************/
void Override_init() {

    // Initialize override board pins to give Micro control
    ENABLE_OUT_TRIS = OUTPUT;  // Set pin to be an output (fed to the AND gates)
    ENABLE_OUT_LAT = MICRO_HAS_CONTROL;  // Initialize control for Microcontroller

    // Let override timer expire to disable override
    Timer_new(TIMER_OVERRIDE, 1);
    while (Override_isTriggered()) {
        asm("nop");
    }

    //Enable the interrupt for the override feature
    mPORTBSetPinsDigitalIn(BIT_0); // CN2

    mCNOpen(CN_ON | CN_IDLE_CON , CN2_ENABLE , CN_PULLUP_DISABLE_ALL);
    uint16_t value = mPORTDRead(); //?
    ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_2);

    //CN2 J5-15

    //DBPRINT("Override Function has been Initialized\n");

    Override_giveMicroControl();

    INTEnable(INT_CN,1);
}
Ejemplo n.º 3
0
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();
}
Ejemplo n.º 4
0
void __ISR(_CHANGE_NOTICE_VECTOR, ipl6)IntCN(void)
{
    mPORTDRead();
    //mPORTDRead(); //Vaciar
    if (BUTTON_1) {
        PUSH_BUTTON_pressed = TRUE;
    }
    mCNClearIntFlag();
}
Ejemplo n.º 5
0
Archivo: Override.c Proyecto: ddeo/sdp
/**********************************************************************
 * Function: Interrupt Service Routine for Override board
 * @return None
 * @remark ISR that is called when CH3 pings external interrupt
 * @author Darrel Deo
 * @date 2013.04.01 
 **********************************************************************/
void __ISR(_CHANGE_NOTICE_VECTOR, ipl2) ChangeNotice_Handler(void){
    mPORTDRead(); //?

    Timer_new(TIMER_OVERRIDE, OVERRIDE_TIMEOUT_DELAY);

    //Clear the interrupt flag that was risen for the external interrupt
    //might want to set a timer in here

    mCNClearIntFlag();
    //INTEnable(INT_CN,0);
}
Ejemplo n.º 6
0
void __ISR(_CHANGE_NOTICE_VECTOR, ipl1) ChangeNotice_Handler(void)
{      

    BYTE temp;
   
    temp = mPORTDRead();

    sendCard = 1;

    mCNClearIntFlag();   // clear the interrupt flag
}
Ejemplo n.º 7
0
/**
 * Function: Interrupt Service Routine
 * @return None
 * @remark ISR that is called when CH3 pings external interrupt
 * @author Darrel Deo
 * @date 2013.04.01  */
void __ISR(_CHANGE_NOTICE_VECTOR, ipl2) ChangeNotice_Handler(void){
    mPORTDRead();
    Serial_putChar('Y');

    //Change top-level state machine so it is in state Reciever
    CONTROL_MASTER = RECIEVER_CONTROL;
    OVERRIDE_TRIGGERED = TRUE;
    //Clear the interrupt flag that was risen for the external interrupt
    //might want to set a timer in here

    mCNClearIntFlag();
    INTEnable(INT_CN,0);
}
Ejemplo n.º 8
0
/**
 * Function: Override_init()
 * @return None
 * @remark Initializes interrupt for Override functionality
 * @author Darrel Deo
 * @date 2013.04.01  */
void Override_init(){
    //Enable the interrupt for the override feature

    mPORTBSetPinsDigitalIn(BIT_0); // CN2

    mCNOpen(CN_ON | CN_IDLE_CON , CN2_ENABLE , CN_PULLUP_DISABLE_ALL);
    uint16_t value = mPORTDRead();
    ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_2);
    //CN2 J5-15
    INTEnableSystemMultiVectoredInt();
    printf("Override Function has been Initialized\n\n");
    //INTEnableInterrupts();
    INTEnable(INT_CN,1);
}
void atras(unsigned int pasos4, int tiempo, unsigned int motornum){
	motornum--;
	unsigned int lectura = mPORTDRead() & (~(0xF << motornum * 4));
	do{
	mPORTDWrite(lectura | (0x6 << motornum*4)); //0110
	Delay(tiempo);
	mPORTDWrite(lectura | (0xC << motornum*4)); //1100
	Delay(tiempo);
	mPORTDWrite(lectura | (0x9 << motornum*4)); //1001
	Delay(tiempo); 
	mPORTDWrite(lectura | (0x3 << motornum*4)); //0011
	Delay(tiempo); 
	//Delay(140000*4 - tiempo*4);//lo restante para que el total sea constante
	pasos4--;
	
	}while (pasos4>0);
}
void __ISR(_CHANGE_NOTICE_VECTOR, ipl2) ChangeNotice_Handler(void)
{
	unsigned int temp;

    // clear the mismatch condition
    temp = mPORTDRead();

    // clear the interrupt flag
    mCNClearIntFlag();

    // .. things to do .. 
    if ( !(temp & (1<<13)) ) { //button on RD13 is pressed
		if (Robo_State == 0)
			Robo_State = 1;
		else {
			Robo_State = 0;
		}
	}
	
}
/************ PROCEDURES ********************/
void step (unsigned char data, unsigned char motorNumber) {
	data = data & 0x0F; // make sure only the LS-nibble will be overwritten
	unsigned int lectura;

	switch (motorNumber) {
		case 1:
			lectura = mPORTDRead() & (~(0xF << 4));
			mPORTDWrite(lectura | (data << 4));
			break;
		case 2:
			lectura = mPORTCRead() & (~(0xF << 1));
			mPORTCWrite(lectura | (data << 1));
			break;
		case 3:
		case 4:
			motorNumber -= 3;
			lectura = mPORTERead() & (~(0xF << motorNumber*4));
			mPORTEWrite(lectura | (data << motorNumber*4));
			break;
	}
}
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;
}
Ejemplo n.º 13
0
// main() ---------------------------------------------------------------------
//
int main(void)
{
    int pbClk;         // Peripheral bus clock

 	// 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.
   	pbClk = SYSTEMConfig(CPU_HZ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // The pic32 has 2 programming i/f's: ICD/ICSP and JTAG. The
    // starter kit uses JTAG, whose pins are muxed w/ RA0,1,4 & 5.
    // If we wanted to disable JTAG to use those pins, we'd need:
#if defined M4557_DUINOMITE
    DDPCONbits.JTAGEN = 0;  // Disable the JTAG port.
#endif

    // Pins that share ANx functions (analog inputs) will default to
    // analog mode (AD1PCFG = 0x0000) on reset.  To enable digital I/O
    // Set all ones in the ADC Port Config register. (I think it's always
    // PORTB that is shared with the ADC inputs). PORT B is NOT 5v tolerant!
    // Also, RB6 & 7 are the ICD/ICSP lines.
    AD1PCFG = 0xffff;  // Port B as digital i/o.

 	//Initialize the DB_UTILS IO channel
	DBINIT();
	
#if defined ST7565_M4557_PROTOTYPE_STARTERKIT
    // Enable pullup resistors for Starter Kit's 3 switches.
    //CNPUE = (CN15_PULLUP_ENABLE | CN16_PULLUP_ENABLE | CN19_PULLUP_ENABLE);
    mCNOpen(CN_ON, CN15_ENABLE | CN16_ENABLE,
            CN15_PULLUP_ENABLE | CN16_PULLUP_ENABLE | CN19_PULLUP_ENABLE);
    // Read the port to clear any mismatch on change notice pins
    dummy = mPORTDRead();
    // Clear change notice interrupt flag
    ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_2);
    // Ok now to enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();

    // Display the introduction
    DBPUTS("SW1:Set contrast; SW2:Halt display\n");
    //DBPRINTF("DBPRINTF: The build date and time is (" __DATE__ "," __TIME__ ")\n");

    // Init ports for ST7565 parallel prototype setup (using pic32 starter kit
    // and i/o expansion board): TODO: This belongs somewhere else!!
    //
    //    PIC32       NHD Display
    //    RB10        /CS1, /RES, A0, /WR, /RD
    //    RE0..7      D0..7
    LATBSET  = 0x7C00;   // Set the control bits high (inactive)
    TRISBCLR = 0x7C00;   // Set the control bits as outputs.

    // Init ESI touch-screen controller's (TSC2046) CS line as output
    LATFSET  = BIT_5;     // Set CSn inactive...
    TRISFCLR = BIT_5;     //    and set as output

    // Data is on port E.  The LCD controller (ST7565) uses its parallel
    // mode on port E, RE0..7. The touch screen controller shares port
    // shares a serial interface on some of these bits.
    TRISECLR = 0x00ff;   // Set the cmd/data bits as outputs.

#elif defined M4557_DUINOMITE

    // Init ports for the M4557.
    //
    // Control bits are on port B:
    //
    //    bit    M4557 function
    //    ----   --------------
    //      3     /CS1   - LCD Chip sel (ST7565 controller)
    //      4     /RES   - Reset
    //      6     A0     - 
    //      7     /WR    - Write
    //      9     /RD    - Read
    //      10    /TPCS  - Touch panel Chip Sel (TSC2046)
    //
    // Set the control bits low, and enable as outputs.
    LATBSET  = (BIT_3|BIT_4|BIT_6|BIT_7|BIT_9|BIT_10);
    TRISBCLR = (BIT_3|BIT_4|BIT_6|BIT_7|BIT_9|BIT_10);

    // Data is on port E.  The LCD controller (ST7565) uses its parallel
    // mode on port E, RE0..7. The touch screen controller shares port
    // shares a serial interface on some of these bits.
    TRISECLR = 0x00ff;   // Set the cmd/data bits as outputs.

#else
    #error Need product defined
#endif

    Nop();
    lcdInit(5,35);        // Init lcd controller
	Nop();

    // Output Compare (PWM) pins
    //
    //   OCn  64pin  100pin/port  SKII-J11-       Duinomite
    //   ---  -----  -----------  --------------  ----------
    //   OC1   46     72/RD0       19 (LED1,Red)   SOUND
    //   OC2   49     76/RD1       20 (LED2,Yel)   D13,SD_CLK
    //   OC3   50     77/RD2       17 (LED3,Grn)   D12,SD_MISO
    //   OC4   51     78/RD3       18              D11,SD_MOSI
    //   OC5   52     81           15              vga_hsync
    //

	// Init Timer 2 for use by the OC (PWM) module(s).
    // This will set the PWM frequency, f = pbClk/reloadValue.
    // Examples (pcClk = 40MHz):  
    //     f = pbClk/100000 = 400Hz
    //     f = pbClk/10000  = 4kHz
    //     f = pbClk/2500 = 20kHz
    // 
    //OpenTimer2(T2_ON | T2_32BIT_MODE_ON, 100000);  // f = pbClk/100000 = 400Hz
    //OpenTimer2(T2_ON | T2_32BIT_MODE_ON, 10000);  // f = pbClk/10000 = 4kHz
    OpenTimer2(T2_ON | T2_32BIT_MODE_ON, 2500);  // f = pbClk/2500 = 16kHz


	// I tried the uChip example using "OpenOC2", and as is ofter the
	// case, I can't get it to work, the documentation is lacking, and
	// it's easier to just read the datasheet and program the bloody
	// OCxCON register directly.
    //OpenOC2( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , 40000, 30000 );
    SetDCOC2PWM(0);
    OC2CON = 0x8026;  // OC on; 32bit-mode; PWM mode.

    SetDCOC3PWM(0);
    OC3CON = 0x8026;  // OC on; 32bit-mode; PWM mode.

/*
    int testOnly = 1;
    uint32_t loadVal = 0;
	int i;
    while(testOnly)
    {
		// Try the dimmer's 16 levels (0..15)
		for(i=0; i<16; i++)
		{
			loadVal = pwmTable1[i];
        	SetDCOC2PWM(loadVal);
        	delay_ms(300);
		}
    }
    CloseOC2();
*/

    // Do ESI M4557 demo application (dimmer-control type demo w/ touchscreen)
    esi_M4557();
    
    return 0;
}
Ejemplo n.º 14
0
int main(void)
{
	DBINIT();
	DBPRINTF("MAIN.... \n");
   	unsigned int temp;
	int POWER;
	//bool RELAY;

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //STEP 1. Configure cache, wait states and peripheral bus clock
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // STEP 2. configure the port registers
    mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_9);
	mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // STEP 3. initialize the port pin states = outputs low
    mPORTDClearBits(BIT_0 | BIT_1 | BIT_2);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // STEP 4. enable change notice, enable discrete pins and weak pullups
    mCNOpen(CONFIG, PINS, PULLUPS);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // STEP 5. read port(s) to clear mismatch on change notice pins
    temp = mPORTDRead();

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // STEP 6. clear change notice interrupt flag
    ConfigIntCN(INTERRUPT);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // STEP 7. enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// STEP 8. configure SPI port and MCP3909
	OpenSPI1(FRAME_ENABLE_ON | ENABLE_SDO_PIN | SPI_MODE32_ON | SPI_CKE_ON | SLAVE_ENABLE_OFF | CLK_POL_ACTIVE_LOW | MASTER_ENABLE_ON , SPI_ENABLE | SPI_FRZ_CONTINUE | SPI_IDLE_STOP | SPI_RX_OVFLOW_CLR);
	CONFIG_3909();

	DBPRINTF("CONFIGURED.... \n");


   while(1)
   {
		// Toggle LED's to signify code is looping
		mPORTDToggleBits(BIT_0);     // toggle LED1 (same as LATDINV = 0x0002)

		SAMPLE();

		POWER = MEASURE_POWER();

		DBPRINTF("power measured! \n");

		DelayMs(1000);

		/************************
		*ETHERNET COMMUNICATIONS*
		************************/

   };

}
Ejemplo n.º 15
0
static void buttonTask()
{
    //Message to be sent to xCarMessageQueue
    struct CarMessage message;

    /*We are polling values as not all butons have CN support.
    INT_CN and buttondrv.h and .c are not longer needed.*/
    while(1)
    {

         //1st button - Left most 
         if(!(mPORTDRead() & 0x40))
         {
            vTaskDelay(10 / portTICK_PERIOD_MS);

            if( (mPORTDRead() & 0x40) == 0)
            {
                //Lockout until release
                while( (mPORTDRead() & 0x40) == 0)
                    vTaskDelay(10 / portTICK_PERIOD_MS);

                //P1 button message
                message.button = SW1;
                xQueueSendToBack(xCarMessageQueue, &message, 0);
            }
         }
         //2nd button
         else if(!(mPORTDRead() & 0x80))
         {
            vTaskDelay(10 / portTICK_PERIOD_MS);

            if( (mPORTDRead() & 0x80) == 0)
            {
                //Lockout until release
                while( (mPORTDRead() & 0x80) == 0)
                    vTaskDelay(10 / portTICK_PERIOD_MS);

                //P2 button message
                message.button = SW2;
                xQueueSendToBack(xCarMessageQueue, &message, 0);
            }
         }
         //3rd button
         else if(!(mPORTARead() & 0x80))
         {
            vTaskDelay(10 / portTICK_PERIOD_MS);

            if( (mPORTARead() & 0x80) == 0)
            {
                //Lockout until release
                while( (mPORTARead() & 0x80) == 0)
                     vTaskDelay(10 / portTICK_PERIOD_MS);

                //Door open message
                message.button = SW3;
                xQueueSendToBack(xCarMessageQueue, &message, 0);
            }
        }
         //4th button - right most
        else if(!(mPORTDRead() & 0x2000))
        {
            vTaskDelay(10 / portTICK_PERIOD_MS);

            if( (mPORTDRead() & 0x2000) == 0)
            {
                //Lockout until release
                while( (mPORTDRead() & 0x2000) == 0)
                    vTaskDelay(10 / portTICK_PERIOD_MS);

                //Door close message
                message.button = SW4;
                xQueueSendToBack(xCarMessageQueue, &message, 0);
            }
        }
        //small delay to prevent button from triggering
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}
Ejemplo n.º 16
0
int main( void )
{
    DWORD   dataLength;
    BYTE    temp,fileName[FILE_NAME_SIZE];
    BYTE    fileDescription[FILE_DESCRIPTION_SIZE];
    DWORD   i;
    WORD    result;

    UARTxInit();

    _TRISD6 = 1;
    _RD6 = 0;
    mCNOpen(CONFIG, PINS, PULLUPS);
    temp = mPORTDRead();
    ConfigIntCN(INTERRUPT);

     SYSTEMConfigPerformance(GetSystemClock());
     INTEnableSystemMultiVectoredInt();

    UARTxPrintString( "\r\nOBEX PDA.\r\n" );
    while (1)
    {
        UARTxPutChar( '-' );

        if (sendCard)
        {
            sendCard = 0;

            UARTxPrintString( "\r\nSending vCard...\r\n" );

            #ifdef USE_MY_READ
                result = IrDA_SendOBEX( "Microchip", "PIC24.vcf", myDataRead, NULL, sizeof( vcf_info ) );
            #else
                result = IrDA_SendOBEX( "Microchip", "PIC24.vcf", NULL, vcf_info, sizeof( vcf_info ) );
            #endif

            if (result)
            {
                UARTxPutHex(result);
                UARTxPrintString( " Error sending.\r\n\r\n" );
            }
            else
            {
                UARTxPrintString( "vCard sent.\r\n\r\n" );
            }
        }

        UARTxPutChar( '|' );

        if (!IrDA_InitServerOBEX())
        {
            #ifdef USE_MY_STORE
                result = IrDA_ReceiveOBEX( fileDescription, fileName, myDataStore, NULL, DATA_ARRAY_SIZE, &dataLength, 1000 );
            #else
                result = IrDA_ReceiveOBEX( fileDescription, fileName, NULL, dataArray, DATA_ARRAY_SIZE, &dataLength, 1000 );
            #endif
            if (!result)
            {
                UARTxPrintString( "\r\nReceived:\r\n" );
                for (i=0; i<dataLength; i++)
                {
                    UARTxPutChar( dataArray[i] );
                }
                UARTxPrintString( "\r\n\r\n" );
            }

            IrDA_TerminateOBEX();
        }
    }
}