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; }
int main() { // Setup GPIO wiringOliSetup(); printf("LCD Init...\n"); // LCD at address 0x27, with 4 rows and 20 columns LCDInit(0x27, 4, 20); LCDPrintString(0, 0, "Test"); LCDPrintString(1, 0, "This is second line"); return(0); }
void LCDPrintFInt(int val,int deximal) { if(deximal == 0) return LCDPrintInt(val); int rval = 0,dval = 0; if(val < 0) { LCDPrintString("-"); val = -val; } rval = val; int i; for(i = 0;i<deximal;i++) rval /= 10; LCDPrintInt(rval); dval = rval; for(i = 0;i<deximal;i++) dval *= 10; dval = val-dval; if(dval > 0) { LCDPrintString("."); if(deximal == 2) { if(dval < 10) LCDPrintString("0"); } if(deximal == 3) { if(dval < 100) LCDPrintString("0"); if(dval < 10) LCDPrintString("0"); } LCDPrintInt(dval); } else { LCDPrintString(" "); for(i = 0;i<deximal;i++) LCDPrintString(" "); } }
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; }
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; }
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; }
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; }
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; }
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 } }