void enableCNModule(int userIsWatchingTV)
{
    if(userIsWatchingTV)
    {
        //do not enable PIR input during TV operation it is useless
        mCNOpen(CN_ON | CN_IDLE_CON, CN16_ENABLE, CN16_PULLUP_ENABLE | CN4_PULLUP_ENABLE);
    }
    else
    {
        mCNOpen(CN_ON | CN_IDLE_CON, CN16_ENABLE | CN4_ENABLE, CN16_PULLUP_ENABLE | CN4_PULLUP_ENABLE);
    }
    mCNIntEnable(1);
}
示例#2
0
文件: Override.c 项目: 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);
}
示例#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();
}
示例#4
0
void setup_spi() {
    // TODO move it out of here
    INTDisableInterrupts();
    mCNOpen(CN_ON | CN_IDLE_CON, CN1_ENABLE, CN1_PULLUP_ENABLE);
    mPORTCRead();
    mCNSetIntPriority(6); // same as below
    mCNClearIntFlag();
    mCNIntEnable(1);
    INTEnableInterrupts();
}
示例#5
0
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();
}
void setupCNModuleAndPIRInput()
{
    //PIR Input and notification A1
    mPORTBSetPinsDigitalIn(BIT_2); //A1 PIR Input (CN4 module)
    mCNOpen(CN_OFF | CN_IDLE_CON | CN_IDLE_CON, CN4_ENABLE, CN4_PULLUP_ENABLE);
    // read port(s) to clear mismatch
    mPORTBReadBits(BIT_2);
    // configure interrupts and clear change notice interrupt flag
    ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_3);
    mCNClearIntFlag(); // Clear interrupt flag
}
示例#7
0
文件: Drive.c 项目: SamChenzx/sdp
/**
 * 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);
}
示例#8
0
void buttonInit(void) {
    TRISBbits.TRISB2 = 1;
    TRISBbits.TRISB0 = 1;
    TRISBbits.TRISB3 = 1;
    TRISBbits.TRISB4 = 1;
    TRISBbits.TRISB5 = 1;
    AD1PCFG = BIT_0 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
    //AD1PCFG = 0xFF;
    mCNOpen(CONFIG, PINS, PULLUPS);
    ConfigCNPullups(CN2_PULLUP_ENABLE);
    ConfigCNPullups(CN3_PULLUP_ENABLE);
    ConfigCNPullups(CN5_PULLUP_ENABLE);
    ConfigCNPullups(CN6_PULLUP_ENABLE);
    ConfigCNPullups(CN7_PULLUP_ENABLE);
}
示例#9
0
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;
    }
}
void setupCNModuleAnd_IR_PIR_Input()
{
    //T2 used to sample IR Input signals
    setupTimer2();

    //IR INPUT
    mPORTDSetPinsDigitalIn(BIT_7); //D5 IR Input (CN16 module, RD7)
    //PIR input
    mPORTBSetPinsDigitalIn(BIT_2); //A1 PIR Input (CN4 module, RB2)

    // setup the change notice options
    //(ensure that CN continues working in sleep mode)
    mCNOpen(CN_OFF | CN_IDLE_CON, CN16_ENABLE | CN4_ENABLE, CN16_PULLUP_ENABLE | CN4_PULLUP_ENABLE);

    // read port(s) to clear mismatch
    mPORTDReadBits(BIT_7);
    mPORTBReadBits(BIT_2);
    mPORTBReadBits(BIT_4);

    // configure interrupts and clear change notice interrupt flag
    ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_3);
    mCNClearIntFlag(); // Clear interrupt flag
}
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;
}
示例#12
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;
}
示例#13
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*
		************************/

   };

}
示例#14
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();
        }
    }
}
int main(void)
{
	unsigned int temp;

	// 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);

/*PORT CONFIGURATION for UART*/
	// Blinky LED for Uart activity
	//mPORTAClearBits(BIT_7);
	//mPORTASetPinsDigitalOut(BIT_7);

// PINS used for the buttons
    PORTSetPinsDigitalIn(IOPORT_B, BIT_2 | BIT_3 | BIT_4);
	#define CONFIG          (CN_ON | CN_IDLE_CON)
	#define INTERRUPT       (CHANGE_INT_ON | CHANGE_INT_PRI_2)
	mCNOpen(CONFIG, CN4_ENABLE | CN5_ENABLE | CN6_ENABLE,
			CN4_PULLUP_ENABLE | CN5_PULLUP_ENABLE | CN6_PULLUP_ENABLE);
	temp = mPORTBRead();

//Analog input
	CloseADC10();
	#define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG32 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
	#define PARAM2 ADC_VREF_AVDD_AVSS | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_2 | ADC_BUF_16 | ADC_ALT_INPUT_OFF
	#define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_5
	#define PARAM4	ENABLE_AN0_ANA | ENABLE_AN1_ANA
	#define PARAM5	SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | SKIP_SCAN_AN4 | SKIP_SCAN_AN5 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15
	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0);
	OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 );
	EnableADC10();

//PORT D for motors
	mPORTDClearBits(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); 		// Turn off PORTD on startup.
	mPORTDSetPinsDigitalOut(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);	// Make PORTD output.


	// Explorer-16 uses 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(), DESIRED_BAUDRATE);
    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);


	// Congifure Change/Notice Interrupt Flag
	ConfigIntCN(INTERRUPT);

    // configure for multi-vectored mode
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

    // enable interrupts
    INTEnableInterrupts();


	WriteString("*** UART Interrupt-driven Example ***\r\n");

	unsigned int channel1, channel2;
	unsigned int motor1Time, motor2Time;
	// Let interrupt handler do the work
	while (1) {
		
		while ( ! mAD1GetIntFlag() );

		channel1 = ReadADC10(0);
		channel2 = ReadADC10(1);
		motor1Time = (channel1*(60000)/(1023) + 80000);
		motor2Time = (channel2*(60000)/(1023) + 80000);
		//maximo valor de motorTime = 140000

		//use motor time for stepping delay		
		if (MotorsON) {
			if (M1forward) {
				adelante(1, motor1Time, 1);
			} else {
				atras(1, motor1Time, 1);
			}

			if (M2forward) {
				adelante(1, motor2Time, 3);
			} else {
				atras(1, motor2Time, 3);
			}

		}
	
			mAD1ClearIntFlag();
		}
			
		return 0;
}