Exemplo n.º 1
0
int main(void)
{

    int ADC_value;      // variable to store the binary value in the ADC buffer
    char value[8];      //  character array to store the values to be printed to the LCD
    double AD_value;    // variable to store the calculated value of the voltage

    LCDInitialize();  // initialize the LCD display

    AD1PCFG &= 0xFFDF; // Pin 7, AN5, where the POT is connected, IO6, is set to analog mode, AD module samples pin voltage
    AD1CON2 = 0x0;       // Always uses MUX A input multiplexer settings, configured as one 16-word buffer, interrupts at the completion of conversion for each sample/convert sequence, use the channel selected by the CH0SA bits as the MUX A input
    AD1CON3 = 0x0101;      //set the A/D conversion clock period to be 2*Tcy, set the Auto-Sample Time bits to be 1 T_AD, A/D conversion clock derived from system clock
    AD1CON1 = 0x20E4;   // A/D sample auto-start mode set for sampling begins immediately after last conversion completes, SAMP bit is automatically set, Conversion trigger source set to internal counter (auto-convert), data output format is integer, stop in idle mode set to discontinue module operation when device enters idle mode
    AD1CHS = 5;         // positive input is AN5
    AD1CSSL = 0;        // low reference set to 0

    AD1CON1bits.ADON = 1; // A/D operating mode set to A/D converter module is operating
    IFS0bits.AD1IF = 0;   // clear the A/D 1 interrupt flag

    while(1)
    {
        while(!IFS0bits.AD1IF);  // wait while the A/D 1 interrupt flag is low
        IFS0bits.AD1IF = 0;     // clear the A/D 1 interrupt flag
        ADC_value = ADC1BUF0;   // stores the current value in the A/D 1 buffer in the ADC_value variable
        sprintf(value, "%6d", ADC_value); // formats value in ADC_value as a 6 character string and stores in in the value character array
        LCDMoveCursor(0,0);                 // moves the cursor on the LCD to the home position
        LCDPrintString(value);              // sends value to the LCD print function to display it on the LCD screen
        AD_value = (ADC_value * 3.3)/1024;  // converts the binary value of the voltage to the analog value by multiplying by the maximum voltage and dividing by 2^n = 2^10, then stores it in AD_value
        sprintf(value, "%6.2f", AD_value);  // formats the value in AD_value to 2 decimals places and stores it in the value variable
        LCDMoveCursor(1,0);                 // moves the cursor on the LCD to the second line
        LCDPrintString(value);              // sends value to the LCD print function to display it on the LCD screen
    }
return 0;
}
Exemplo n.º 2
0
void initializeEverything()
{
	int i, j, k;
	uint32_t increment;
	LCDInitialize();
	SPI_Initialize();
	//* setup oscs
	initializeOscillators();
	// Setup the keyboard usb
	SynthStationUsbInitialize();
	//calculate the note frequencies
		WaveformTablesInitialize();
		
	/** Calculate frequency increments */
	for(i = 0; i < 120; i++)
	{
		increment = noteTable[i];
		noteTable[i] = getIncrement(increment);
	}
	
	/** Initialize sequencer layerw */
	
	for(i = 0; i < 8; i++)
	{
		for(j = 0; j < SEQUENCER_STEPS; j++)
		{
				synthLayers[i].sequenceNotes[j] = 0xFFFF;
		}
		synthLayers[i].waveType = currentSelectedWaveForm;
	}
	
	synthLayers[0].layerFlags |= LAYER_FLAGS_ENABLED;
	synthLayers[1].layerFlags |= LAYER_FLAGS_ENABLED;
	
	noteListHead = (noteListItem *)malloc(sizeof(noteListItem));
	noteListHead->pNextItem = NULL;
	noteListHead->pNote = NULL;
	noteListTail = noteListHead;
		
	currentSelectedMode = SYNTH_RECORDING;
	setBPM(DEFAULT_BPM);
	
	BEAT_LED_START_DD = BEAT_LED_DD = 1;
	BEAT_LED_START = BEAT_LED = 0;
	
	//listTest();
	
}
Exemplo n.º 3
0
int main(void)
{
	// ****************************************************************************** //

	// Set the switch to be digital
        AD1PCFGbits.PCFG4 = 1;  //Set the switch to be digital

	//Configures the LEDs to be outputs
        TRISAbits.TRISA0 = 0;   //Right (green/run)
        TRISAbits.TRISA1 = 0;   //Left (red/stop)


	//Set the green light to be on first and the red one to be off
        LATAbits.LATA0 = 1;     //Turn Right (green/run) one off
        LATAbits.LATA1 = 0;     //Turn Left (red/stop) one on


        //Giving the LEDs ODCs
        ODCAbits.ODA0 = 1;
        ODCAbits.ODA1 = 1;

        //Configuring switches to be inputs
        TRISBbits.TRISB2 = 1;   //switch not on board to be input connected to IO5. START/STOP BUTTON
        TRISBbits.TRISB5 = 1;   //switch on board to be input. RESET BUTTON

        //Enables the change notification for the switch on the board
        CNEN2bits.CN27IE = 1;       //Enables change notification for switch on board
        CNEN1bits.CN6IE = 1;        //Enables change notification for switch not on board
        IFS1bits.CNIF = 0;
        IEC1bits.CNIE = 1;

	// Gives the switch not on the board a pull up resistor
        CNPU1bits.CN6PUE = 1;

        //Timer 1 used for ISR to increment
        T1CONbits.TCS=0;
        T1CONbits.TCKPS0=1;
        T1CONbits.TCKPS1=1;
        T1CONbits.TON = 0;
        IFS0bits.T1IF = 0;
        IEC0bits.T1IE = 1;
        TMR1 = 0;
        PR1 = 575;  //timer 1 period value for 10ms
        

        LCDInitialize();
	while(1)
	{
//            mm = counter
		// TODO: For each distinct button press, alternate which
		// LED is illuminated (on).
            switch(state) {

                //Stopped and waiting for button to be pressed
                case 0:
                    LCDMoveCursor(0,0);
                    LCDPrintString("Stopped");
                    LATAbits.LATA0 = 1;     //Turn Right (green/run) one off
                    LATAbits.LATA1 = 0;     //Turn Left (red/stop) one on
                    break;

                //Running and updating time
                case 1:
                    LCDMoveCursor(0,0);
                    LCDPrintString("Running:");
                    LCDMoveCursor(1,0);
                    LATAbits.LATA0 = 0;     //Turn Right (green/run) one on
                    LATAbits.LATA1 = 1;     //Turn Left (red/stop) one off

                    //Prints the time
                    LCDMoveCursor(1,7);
                    LCDPrintChar(f1+'0');
                    LCDMoveCursor(1,6);
                    LCDPrintChar(f2+'0');

                    LCDMoveCursor(1,4);
                    LCDPrintChar(s1+'0');

                    LCDMoveCursor(1,3);
                    LCDPrintChar(s2+'0');

                    LCDMoveCursor(1,1);
                    LCDPrintChar(m1+'0');

                    LCDMoveCursor(1,0);
                    LCDPrintChar(m2+'0');
                    break;

                //Reset button is pressed and timer goes to 0
                case 2:

                    //Sets row 2 to be 0 on the LCD and resets all the values
                    LCDMoveCursor(1,0);
                    LCDPrintString("00:00.00");
                    f1 = 0;
                    f2 = 0;
                    s1 = 0;
                    s2 = 0;
                    m1 = 0;
                    m2 = 0;
                    state = 0;
                    break;


            }



	}
	return 0;
}
Exemplo n.º 4
0
int main(void)
{
	char value[8];
	Forward_Backward_State;
	
	// Set the directional pins
    TRIS_MOTOR_C = 0;  // RB2
    TRIS_MOTOR_D = 0;  // RB3
    LAT_MOTOR_C = 1;   // idle mode
    LAT_MOTOR_D = 1;   // idle mode

	// Motor related variables	
	int duty_cycle1 = 0;
    int duty_cycle2 = 0;

    // Configure Change notification of RB5 (SW1 momentary switch)
	TRISBbits.TRISB5 = 1;
	CNPU2bits.CN27PUE=1;	
    CNEN2bits.CN27IE=1;
    IFS1bits.CNIF=0;
    IEC1bits.CNIE=1;

	// Initialize LCD
    LCDInitialize();

	// Configure PWM
    PWMInitialize();

	// Configure A/D unit
    ADCInitialize(); 	

	while(1) 
      {		
	  if(done)
		{
		 sprintf(value, "%6d", ADC_value);
    	 LCDMoveCursor(0,0);
         LCDPrintString(value);
		

		 if((ADC_value > 462 || ADC_value == 462) && (ADC_value <562 || ADC_value == 562)) 
		  {
		   duty_cycle1 = 100;
		   duty_cycle2 = 100;
		  }
			
		 else if((ADC_value > 563 || ADC_value == 563) && (ADC_value <690 || ADC_value == 690)) 
		  {
		   duty_cycle1 = 75;
		   duty_cycle2 = 100;
		  }

		 else if((ADC_value > 691 || ADC_value == 691) && (ADC_value <818 || ADC_value == 818)) 	
		 {
 		   duty_cycle1 = 50;
		   duty_cycle2 = 100;
		 }

		else if((ADC_value > 819 || ADC_value == 819) && (ADC_value <975 || ADC_value == 975)) 	
		 {
 		   duty_cycle1 = 25;
		   duty_cycle2 = 100;
		 }

		else if (ADC_value > 975) 	
		 {
 		   duty_cycle1 = 0;
		   duty_cycle2 = 100;
		 }	

	 	else if((ADC_value < 461 || ADC_value == 461) && (ADC_value >334 || ADC_value == 334)) 
		  {
		   duty_cycle1 = 100;
		   duty_cycle2 = 75;
		  }
			
		else if((ADC_value < 333 || ADC_value == 333) && (ADC_value >206 || ADC_value == 206)) 
		  {
		   duty_cycle1 = 100;
		   duty_cycle2 = 50;
		  }

		else if((ADC_value < 205 || ADC_value == 205) && (ADC_value >40 || ADC_value == 40)) 
		  {
		   duty_cycle1 = 100;
		   duty_cycle2 = 25;
		  }
		
	   else if (ADC_value < 40) 
		  {
		   duty_cycle1 = 100;
		   duty_cycle2 = 0;
		  }

		
			OC1RS = (int)(PR_VALUE*duty_cycle1); //modifying PWM duty cycle
			OC1RS = OC1RS / 100;
			OC2RS = (int)(PR_VALUE*duty_cycle2); //modifying PWM duty cycle
			OC2RS = OC2RS / 100;
			
			sprintf(value, "%3d", duty_cycle1);
			LCDMoveCursor(1,0);
			LCDPrintString(value);
			sprintf(value, "%3d", duty_cycle2);
			LCDMoveCursor(1,5);
			LCDPrintString(value);  
			
			done = 0;
		} 
	  }
	return 0;
	}
Exemplo n.º 5
0
int main(void) {


    double percent1 = 0;
    double percent2 = 0;
    int duty1 = 0;
    int duty2 = 0;

    int ADC_value;      // variable to store the binary value in the ADC buffer
    char value[8];      //  character array to store the values to be printed to the LCD
    double AD_value;    // variable to store the calculated value of the voltage

/**********************************************/
    T3CONbits.TCS = 0; // sets up to use internal clock
    T3CONbits.TGATE = 0;
    T3CONbits.TON = 0;  // Turn timer 3 off
    IFS0bits.T3IF = 0;  // reset timer 3 interrupt flag
    TMR3 = 0;           // resets timer 3 to 0

    T3CONbits.TCKPS = 11; // set a prescaler of 8 for timer 2
    PR3 = 287;
    IEC0bits.T3IE = 1;

/*****************************************************/


    OC1CONbits.OCM0 = 1; // Initialize OCx pin low, compare event forces OCx pin high,
    OC1CONbits.OCTSEL = 1; // using timer 3

    OC1R = OC1RS = PR3/2;

    OC2CONbits.OCM0 = 1; // Initialize OCx pin low, compare event forces OCx pin high,
    OC2CONbits.OCTSEL = 1; // using timer 3

    OC2R = OC2RS = PR3/2;

/*****************************************************/
    LCDInitialize();  // initialize the LCD display
/*****************************************************/
    //initialize board outputs
    //RP18 tied to output compare 1
    //RP19 tied to output compare 2
/*****************************************************/
    AD1PCFG &= 0xFFDF; // Pin 7, AN5, where the POT is connected, IO6, is set to analog mode, AD module samples pin voltage
    AD1CON2 = 0x0;       // Always uses MUX A input multiplexer settings, configured as one 16-word buffer, interrupts at the completion of conversion for each sample/convert sequence, use the channel selected by the CH0SA bits as the MUX A input
    AD1CON3 = 0x0101;      //set the A/D conversion clock period to be 2*Tcy, set the Auto-Sample Time bits to be 1 T_AD, A/D conversion clock derived from system clock
    AD1CON1 = 0x20E4;   // A/D sample auto-start mode set for sampling begins immediately after last conversion completes, SAMP bit is automatically set, Conversion trigger source set to internal counter (auto-convert), data output format is integer, stop in idle mode set to discontinue module operation when device enters idle mode
    AD1CHS = 5;         // positive input is AN5
    AD1CSSL = 0;        // low reference set to 0

    AD1CON1bits.ADON = 1; // A/D operating mode set to A/D converter module is operating
    IFS0bits.AD1IF = 0;   // clear the A/D 1 interrupt flag
/*****************************************************/
    while(1)
    {
        while(!IFS0bits.AD1IF);  // wait while the A/D 1 interrupt flag is low
        IFS0bits.AD1IF = 0;     // clear the A/D 1 interrupt flag
        LCDClear();
        ADC_value = ADC1BUF0;   // stores the current value in the A/D 1 buffer in the ADC_value variable
        sprintf(value, "%6d", ADC_value); // formats value in ADC_value as a 6 character string and stores in in the value character array
        LCDMoveCursor(0,0);                 // moves the cursor on the LCD to the home position
        LCDPrintString(value);              // sends value to the LCD print function to display it on the LCD screen
        AD_value = (ADC_value * 3.3)/1024;  // converts the binary value of the voltage to the analog value by multiplying by the maximum voltage and dividing by 2^n = 2^10, then stores it in AD_value
        if (AD_value<1.65){
            OC1RS = PR3;
            percent1=100;
            percent2=AD_value/1.65;
            OC2RS =PR3*percent2;
            percent2=percent2*100;
        }
        else if (AD_value>1.65) {
            OC2RS = PR3;
            percent2=100;
            percent1=(AD_value-1.65)/1.65;
            percent1=1-percent1;
            OC1RS =PR3*(percent1);
            percent1=percent1*100;
        }
        else {
            OC1RS=PR3;
            OC2RS=PR3;
            percent1=100;
            percent2=100;
        }
        duty1=OC1RS;
        duty2=OC2RS;
        sprintf(value, "%3.0f", percent1); // formats value in ADC_value as a 6 character string and stores in in the value character array
        LCDMoveCursor(1,0);                 // moves the cursor on the LCD to the second line
        LCDPrintString(value);              // sends value to the LCD print function to display it on the LCD screen
        sprintf(value, " %3.0f", percent2); // formats value in ADC_value as a 6 character string and stores in in the value character array
        LCDPrintString(value);              // sends value to the LCD print function to display it on the LCD screen
    }
return 0;
}
Exemplo n.º 6
0
int main(void)
{

	// The following provides a demo configuration of Timer 1 in which
	// the Timer 1 interrupt service routine will be executed every 1 second
	PR1 = 57599;
	TMR1 = 0;
	IFS0bits.T1IF = 0;
	IEC0bits.T1IE = 1;
	T1CONbits.TCKPS = 3;
	T1CONbits.TON = 1;

	// printf by default is mapped to serial communication using UART1.
	// NOTES:
	//        1. You must specify a heap size for printf. This is required
	//           becuase printf needs to allocate its own memory, which is
	//           allocated on the heap. This can be set in MPLAB by:
	//           a.) Selecting Build Options...->Project from the Project menu.
	//           b.) Selecting the MPLABLINK30 Tab.
	//           c.) Entering the size of heap, e.g. 512, under Heap Size
	//        2. printf function is advanced and using printf may require
	//           significant code size (6KB-10KB).
	printf("Lab 2: Debugging Statements\n\r");

	// The following code will not work until you have implemented the
	// the required LCD functions defined within lcd.c
	LCDInitialize();
/*******************************/
////	//below is for testing MoceCursor command
////	LCDMoveCursor(0,2);
////	LCDPrintString("Hello");
////	LCDMoveCursor(1,2);
////	LCDPrintString("Test");
////	command = 0xC;
/*******************************/

	LCDPrintString("Running:");
	LCDMoveCursor(1,0);
	LCDPrintString("00:00.00");
//	LCDPrintChar('0');
//	LCDPrintChar('0');
//	LCDPrintChar(':');
//	LCDPrintChar('0');
//	LCDPrintChar('0');
//	LCDPrintChar('.');
//	LCDPrintChar('0');
//	LCDPrintChar('0');

	while(1)
	{
//			  LCDMoveCursor(1,0);
//            LCDPrintChar(cnt+'0');
//            LCDMoveCursor(1,1);
//            LCDPrintChar(cnt+'0');
//            LCDMoveCursor(1,3);
//            LCDPrintChar(cnt+'0');
//            //given
			  LCDMoveCursor(1,4);
			  LCDPrintChar(cnt+'0');
//            //given above
//            LCDMoveCursor(1,6);
//            LCDPrintChar(cnt+'0');
//            LCDMoveCursor(1,7);
//            LCDPrintChar(cnt+'0');
//
	}
	return 0;
}
Exemplo n.º 7
0
int main(void)
{
    int delay = 0;
    int barcode_state = 0;
    int barcode_count = 0;
    char barcode[4] = "0000";
    int test = 0;
    int remote_state = 0;
    int i = 0;
    int total  = 0;
    int count = -1;
    int start_flag = 0;
    int black_flag = 0;
    int red_flag = 0;
    int white_flag = 0;
    unsigned int ADC_value_left, ADC_value_middle, ADC_value_right;
    unsigned int ADC_value_barcode, ADC_value_remote;
    char value_remote[8];
    char value_barcode[8];
    char value_left[8]; //left phototransistor
    char value_right[8]; //right phototransistor
    char value_middle[8]; //middle phototransistorw




    RPOR4bits.RP8R = 18;    //right wheel
    RPOR4bits.RP9R = 19;    //left wheel
    RPOR0bits.RP0R = 21;    //right wheel ground
    RPOR1bits.RP2R = 20;    //left wheel ground


    /********************Settings for the wheels, starts off*******************/
    OC1R = 0;
    OC1RS = 0;        

    OC2R = 0;
    OC2RS = 0;        

    OC3R = 0;
    OC3RS = 0;

    OC4R = 0;
    OC4RS = 0;

    OC1CON = 0x000E;    //select timer 3
    OC2CON = 0x000E;    //select timer 3
    OC3CON = 0x000E;
    OC4CON = 0x000E;
    PR3 = 1023;         //Set the sampling to be around 15kHz
    TMR3 = 0;
    T3CON = 0x0800;
    T3CONbits.TON = 1;
    /**************************************************************************/

    /*******************Timer settings for the laser point*********************/
    T1CONbits.TCS = 0;
    T1CONbits.TCKPS0 = 1;
    T1CONbits.TCKPS1 = 1;
    T1CONbits.TON = 0;
    IFS0bits.T1IF = 0;
    IEC0bits.T1IE = 1;
    TMR1 = 0;
    PR1 = 57599;
    /**************************************************************************/


    /********************Timer setting for the barcode*************************/
    T4CONbits.TCS = 0;
    T4CONbits.TCKPS0 = 1;
    T4CONbits.TCKPS1 = 1;
    T4CONbits.TON = 0;
    IFS1bits.T4IF = 0;
    IEC1bits.T4IE = 1;
    TMR4 = 0;
    PR4 = 57599;
    /**************************************************************************/

    //input pins for sensor
    TRISAbits.TRISA0 = 1;
    TRISAbits.TRISA1 = 1;
    TRISBbits.TRISB3 = 1;
    TRISBbits.TRISB15 = 1;
    TRISBbits.TRISB14 = 1;



    LCDInitialize();


    
    //Analog input pins being used
    AD1PCFGbits.PCFG0 = 0;
    AD1PCFGbits.PCFG1 = 0;
    AD1PCFGbits.PCFG5 = 0;
    AD1PCFGbits.PCFG9 = 0;


    AD1CON2 = 0x0000;
    AD1CON3 = 0x0101;
    AD1CON1 = 0x00E0;


    AD1CSSL = 0;            //Channel scanning, want to use manaul scanning


    AD1CON1bits.ADON = 1;   //Turn on A/D converter
    IFS0bits.AD1IF = 0;     //Turn on interrupt flag
    while(1)
    {
/************Left ADC value******************/
        //LCDMoveCursor(1,4);
        AD1CHS = 0;
        AD1CON1bits.SAMP = 1;           //Start sample
        DelayUs(2000);
        while(IFS0bits.AD1IF == 0);
        AD1CON1bits.SAMP = 0;
        ADC_value_left = ADC1BUF0;
        sprintf(value_left, "%4d", ADC_value_left);
        //LCDPrintString(value_left);

/************Middle ADC value******************/
        AD1CHS = 1;
        AD1CON1bits.SAMP = 1;           //Start sample
        DelayUs(2000);
        while(IFS0bits.AD1IF == 0);
        AD1CON1bits.SAMP = 0;
        ADC_value_middle = ADC1BUF0;
        sprintf(value_middle, "%4d", ADC_value_middle);
        //LCDPrintString(value_middle);

/************Right ADC value******************/
        AD1CHS = 5;
        AD1CON1bits.SAMP = 1;           //Start sample
        DelayUs(2000);
        while(IFS0bits.AD1IF == 0);
        AD1CON1bits.SAMP = 0;
        ADC_value_right = ADC1BUF0;
        sprintf(value_right, "%4d", ADC_value_right);
        //LCDPrintString(value_right);

/************Barcode value******************/
        //LCDMoveCursor(1,4);
        AD1CHS = 9;
        AD1CON1bits.SAMP = 1;           //Start sample
        DelayUs(2000);
        while(IFS0bits.AD1IF == 0);
        AD1CON1bits.SAMP = 0;
        ADC_value_barcode = ADC1BUF0;
        sprintf(value_barcode, "%4d", ADC_value_barcode);
        //LCDPrintString(value_barcode);

/************Remote value******************/
	//LCDMoveCursor(1,4);
        AD1CHS = 10;
        AD1CON1bits.SAMP = 1;           //Start sample
        DelayUs(2000);
        while(IFS0bits.AD1IF == 0);
        AD1CON1bits.SAMP = 0;
        ADC_value_remote = ADC1BUF0;
        sprintf(value_remote, "%4d", ADC_value_remote);
	//LCDPrintString(value_remote);


        //Default ADC_value for the remote it 1023, only changes if light/laser
        //is shined on the phototransistor.
        //Timer is put in so that it takes 2 seconds before it is allowed to
        //change between on/off again.
    	if(ADC_value_remote < 950) {
            switch(remote_state) {
                //turn timer on and change to state 2
                case 0:
                    TMR1 = 0;
                    T1CONbits.TON = 1;
                    remote_state = 2;
                    break;
                //waits 3 seconds before allowing another change of state
		case 1:
                    if(sec > 3) {
                    remote_state = 0;
                    sec = 0;
                    T1CONbits.TON = 0;
                    }
                    break;
                //picks the state to change to based on the current state
		case 2:
                    if(state == 0) {
                        OC1RS = MAX_SPEED;
                        OC2RS = MAX_SPEED;
                        state = 1;
                    }
                    else if(state == 1) { 
                        state = 0;
                    }
                    remote_state = 1;
                    break;
		}
	}

        //State = 0 is off
        //State = 1 is running with the barcode working
        switch(state) {

            case 0:
                OC1RS = 0;
                OC2RS = 0;
                break;
                
            //Fast Mode: basically goes straight as long as the middle sensor
            //is reading black( which is above 600 for us)
            case 1:
                if(ADC_value_middle >= 600) {//on the line
                    OC1RS = MAX_SPEED;
                    OC2RS = MAX_SPEED;
                }
                else {

                    //hard left turn if left sensor is picked up
                    if(ADC_value_left > 300) {
                        OC1RS = MAX_SPEED;
                        OC2RS = 0;
                    }

                    //hard right turn ifi right sensor is picked up
                    else if(ADC_value_right > 300) {
                        OC1RS = 0;
                        OC2RS = MAX_SPEED;
                    }
                }

                //Does the barcode reading
                switch(barcode_state) {

                    //start state: waits for black to be detected
                    case 0:
                        if(ADC_value_barcode > 700) {
                            barcode_state = 4; //state where we wait for new line
                            start_flag = 1;
                        }
                        break;

                     //white state: basically if it doesn't leave this state
                     //after a second, then we reset the barcode reading and reset
                     //all the variables back to start
                    case 1:
                        if(barcode_timer > 2) {
                                if(total == 1) { //total keeps track of how many barcodes we've read
                                    LCDClear();
                                    LCDPrintChar(barcode[0]);
                                    LCDPrintChar(barcode[1]);
                                    LCDPrintChar(barcode[2]);
                                    LCDPrintChar(barcode[3]);
                                    LCDMoveCursor(1,0);
                                }
                                else
                                    LCDClear();
                                T4CONbits.TON = 0;
                                TMR4 = 0;
                                barcode_timer = 0;
                                start_flag = 0;
                                white_flag = 0;
                                barcode_state = 0;
                                count = -1;
                                barcode_count = 0;
                        }
                        else if(ADC_value_barcode > 600 && barcode_timer < 2) {
                            barcode_state = 5;
                            T4CONbits.TON = 0;
                            TMR4 = 0;
                        }
                        break;

                    //black state: prints to the LCD and stores the value
                    case 2:
                        barcode[barcode_count]='0';
                        LCDPrintChar('0');
                        barcode_count++;
                        barcode_state = 4;  //waits for new value
                        break;

                    //red state: prints to the LCD and stores the value
                    case 3:
                        barcode[barcode_count]='1';
                        LCDPrintChar('1');
                        barcode_count++;
                        barcode_state = 4;
                        break;

                    //wait state: waits for another color other than white
                    case 4:
                        //the start flag is not necessary but it's there from old code
                        if(start_flag == 1) {

                            //less than 650 means it is white and count keeps track of
                            //how many barcode values are read
                            if(ADC_value_barcode < 650 && count < 3) {
                                    count++;
                                    barcode_state = 1;
                                    white_flag = 1;
                                    TMR4 = 0;
                                    barcode_timer = 0;
                                    T4CONbits.TON = 1; //timer for seeing how long we are reading white
                                }

                            //if we read a full barcode, we'll go to the next line
                            else if(count == 3) {
                                    total = 1;
                                    count = -1;
                                    start_flag = 0;
                                    LCDMoveCursor(1,0);
                                    barcode_state = 0;
                             }
                        }
                        break;

                    //pick black or red state
                    case 5:
                        //Puts a minor delay to avoid the transition between colors and then polls again
                        DelayUs(10000);
                            AD1CHS = 9;
                            AD1CON1bits.SAMP = 1;           //Start sample
                            DelayUs(2000);
                            while(IFS0bits.AD1IF == 0);
                            AD1CON1bits.SAMP = 0;
                            ADC_value_barcode = ADC1BUF0;

                            //go black state
                            if(ADC_value_barcode > 800) {
                                barcode_state = 2;
                            }

                            //go red state
                            else if(ADC_value_barcode > 700 && ADC_value_barcode < 800)
                            {
                                barcode_state = 3;
                            }

                        break;
                }
                
                break;

        }
   }
    return 0;
}
Exemplo n.º 8
0
int main(void)
{

    TRISBbits.TRISB5 = 1; // setting up the push button
    CNEN2bits.CN27IE = 1;
    IFS1bits.CNIF = 0; // set flag low
    IEC1bits.CNIE = 1; // enable interrupt

        // UART1 Setup
        RPINR18bits.U1RXR = 9;
	RPOR4bits.RP8R = 3;
        U1BRG  = BRGVAL;
	U1MODE = 0x8000;
        U1STA  = 0x0440;
	IFS0bits.U1RXIF = 0;

        IFS1bits.CNIF = 0;
        IEC1bits.CNIE = 1;


        PR3 = 1474; // 0.1 second delay
	TMR3 = 0;
        T3CON = 0x8000;
	IFS0bits.T3IF = 0;
//	IEC0bits.T3IE = 1;

        /// ADC

        int ADC_value;
        char value[8];
        double AD_value;

        AD1PCFG &= 0xFFDF;
        AD1CON2 = 0; // reference voltage
        AD1CON3 = 0x0101;  // sample conversion
        AD1CON1 = 0x20E4;  // sample conversion
        AD1CHS = 5; // AN5 pin for reference
        AD1CSSL = 0;

        IFS0bits.AD1IF = 0; // set flag low
        AD1CON1bits.ADON = 1; // turn on ADC

        /// PWM
        int PWM_Period = 1024;
        OC1CON = 0x000E;
        OC1CONbits.OCTSEL = 1;
        OC1R = PWM_Period;
        OC1RS = PWM_Period/2;
        RPOR1bits.RP2R = 18;

        OC2CON = 0x000E;
        OC2CONbits.OCTSEL = 1;
        OC2R = PWM_Period;
        OC2RS = PWM_Period/2;
        RPOR4bits.RP8R = 19;

        // Forward and reverse pins on the PIC
        // RB9 and RB10
        TRISBbits.TRISB9 = 0; // output for H bridge
        TRISBbits.TRISB10 = 0; // output for H bridge
        LATBbits.LATB9 = 0;  // goes to input 1 and input 4 on H bridge
        LATBbits.LATB10 = 1; // goes to input 2 and input 3 on H bridge



        
	LCDInitialize();

        float num_temp = 0;
        
        printf("Working\n");

	while(1)
	{
            while(IFS0bits.AD1IF == 0);
            IFS0bits.AD1IF = 0;
            ADC_value = ADC1BUF0; // digital value

            sprintf(value, "%6d", ADC_value); // convert digital value to string for LCD
            LCDMoveCursor(0,0);
            LCDPrintString(value);
            printf("%d\n", ADC_value);
            
///////////////////////////////////////////////////////////////////////////////
            switch(state){
                case 0: // forward state
                    if(ADC_value <= PWM_Period/2){
                        OC1RS = PWM_Period;
                     }
                    else if(ADC_value > PWM_Period/2){
                        OC1RS = PWM_Period - ((ADC_value * 2) - PWM_Period);
                    }

                    if(ADC_value >= PWM_Period/2){
                        OC2RS = PWM_Period;
                    }
                    else if(ADC_value < PWM_Period/2){
                        OC2RS = (ADC_value * 2);
                    }
                    LATBbits.LATB9 = 0;
                    LATBbits.LATB10 = 1;
                    break;                    
                case 1:  // IDLE state
                    OC1RS = 0;
                    OC2RS = 0;
                    break;
                case 2: // reverse state
                   if(ADC_value <= PWM_Period/2){
                        OC1RS = PWM_Period;
                     }
                    else if(ADC_value > PWM_Period/2){
                        OC1RS = PWM_Period - ((ADC_value * 2) - PWM_Period);
                    }

                    if(ADC_value >= PWM_Period/2){
                        OC2RS = PWM_Period;
                    }
                    else if(ADC_value < PWM_Period/2){
                        OC2RS = (ADC_value * 2);
                    }
                    LATBbits.LATB9 = 1;
                    LATBbits.LATB10 = 0;
                    break;
                case 3:  // IDLE state
                    OC1RS = 0;
                    OC2RS = 0;
                    break;
            }
///////////////////////////////////////////////////////////////////////////////

            // TO DO  print the duty cycle of both PWM channels onto the LCD   convert a float to a string...
            LCDMoveCursor(1,0);
            num_temp = ((ADC_value / PWM_Period) * 100);
            sprintf(value, "%2f", num_temp);
            LCDPrintString(value);


//            AD_value = (ADC_value*3.3)/1024;
//            sprintf(value, "%6.2f", AD_value); // convert to string
//            LCDMoveCursor(1,0);
//            LCDPrintString(value);


        }
		
	return 0;
}
Exemplo n.º 9
0
int main(void)
{
    TRISBbits.TRISB5=1;//enables the SW1 as input
    CNEN2bits.CN27IE = 1;//change notification for SW1
    IFS1bits.CNIF = 0;//enables the change notification interrupt
    IEC1bits.CNIE = 1;//sets flag down


    int i=0;//used for printing delay
    state=0;//intializes in iddle state before going forward

   
   int value=0;//variable used to store the ADC value obtained from potentiometer
    double convert=0;//variable used to convert the duty cycle percentage
    
    char ADV[4];//used to print the ADC value
    char ADV2[4];//used to print the duty cycles
   
    
//initializes LCD, the ADC and the PWM
     LCDInitialize();
    InADC();
    InPWM();
 
   
    while(1){

        //Clears the LCD screen, and displays the ADC value on the 1st line
         LCDClear();
         value = AnalogtoDigital();
         LCDMoveCursor(0,0);
         sprintf(ADV,"%4d", value);
         LCDPrintString(ADV);

         //gets the OC values to calculate the duty cycles
        OC1RS = 1023- value;
         OC2RS = value;

        if((state==0 ) || (state==2)){
        //for both of the Idle states,
        //print a Duty cycle of 0 since the motors arent running
          LCDMoveCursor(1,0);
          LCDPrintString("0");
         LCDMoveCursor(1,4);
         LCDPrintString("0");

        }
       
        else if((state==1) ||(state==3) ){
        //if in the forward or backward states

            if(OC1RS>513 && OC2RS<511){
            //for the right motor working at full speed

            convert=(OC2RS*100)/511;//calculate duty cycle for left motor
            sprintf(ADV2,"%3.0f",convert);
            LCDMoveCursor(1,0);
            LCDPrintString(ADV2);//print left motor duty cycle percentage
             LCDMoveCursor(1,4);
            LCDPrintString("100");//print right motor duty cycle percentage which is at 100%
            }

            else if(OC1RS<510 && OC2RS>513){
            //for left motor working at full speed
            LCDMoveCursor(1,0);
            LCDPrintString("100");//print 100% for the left motor
            convert=(OC1RS*100)/511;//calculate duty cycle for right motor
            sprintf(ADV2,"%3.0f",convert);
            LCDMoveCursor(1,4);
            LCDPrintString(ADV2);//print duty cycle for left motor
            }

            else{// if both motors working at full speed,
                //print 100% for both duty cycles
            LCDMoveCursor(1,0);
            LCDPrintString("100");
            LCDMoveCursor(1,4);
            LCDPrintString("100");
            }
       }
     
       for(i=0;i<31000; i++);//small delay for LCD to print
    

      
    }
}