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; }
Xuint32 Decode_display_bitmap(XTft * InstancePtr, Xuint32 FRAME_ADDR) { Xuint16 Flash_Data; Xuint8 Pixel_R; Xuint8 Pixel_G; Xuint8 Pixel_B; Xuint32 Pixel_Data0, Pixel_Data1, Pixel_Data2; Xuint32 temp; Xuint32 Pixel_Data; Xuint32 Bitmap_File_Size; Xuint32 Bitmap_Data_Offset; Xuint32 Bitmap_Width_Pixels; Xuint32 Bitmap_Height_Pixels; Xuint16 Bitmap_Bits_Per_Pixel; Xuint32 Bitmap_Image_Size; Xuint32 Bytes_Per_Row; Xuint32 MEM_ADDR; Xuint32 MEM_HIGHADDR; Xuint32 i, j, k; if (verbose) xil_printf ("\r\nDecoding bitmap"); //reset FLASH to read mode XIo_Out16 (XPAR_FLASH_MEM0_BASEADDR, 0xFF); Flash_Data = XIo_In16(XPAR_FLASH_MEM0_BASEADDR+FLASH_START_ADDR); if (verbose) xil_printf("\r\nFile Type is 0x%X", Flash_Data); //check if the bitmap is present in the FLASH memory by checking for the 'B' and 'M' characters //at the beginnig of the file location if (Flash_Data != 0x424d) { if (verbose) xil_printf("\r\nBitmap was not found in the FLASH memory, data found in the FLASH memory is 0x%X", Flash_Data); return 1; } //determine bitmap parameters Bitmap_File_Size = Read_Bitmap_Header_Bytes(FLASH_BASEADDR+FLASH_START_ADDR, BITMAP_FILE_SIZE_OFFSET); //Bitmap_File_Size = XIo_In32(XPAR_FLASH_MEM0_BASEADDR+FLASH_START_ADDR+BITMAP_FILE_SIZE_OFFSET); if (verbose) xil_printf("\r\nFile Size is 0x%X", Bitmap_File_Size); Bitmap_Data_Offset = Read_Bitmap_Header_Bytes(FLASH_BASEADDR+FLASH_START_ADDR, BITMAP_DATA_OFFSET); if (verbose) xil_printf("\r\nBitmap Data Offset is 0x%X", Bitmap_Data_Offset); Bitmap_Width_Pixels = Read_Bitmap_Header_Bytes(FLASH_BASEADDR+FLASH_START_ADDR, BITMAP_WIDTH_OFFSET); if (verbose) xil_printf("\r\nBitmap Width in pixels is %d", Bitmap_Width_Pixels); Bitmap_Height_Pixels = Read_Bitmap_Header_Bytes(FLASH_BASEADDR+FLASH_START_ADDR, BITMAP_HEIGHT_OFFSET); if (verbose) xil_printf("\r\nBitmap Height in pixels is %d", Bitmap_Height_Pixels); Bitmap_Bits_Per_Pixel = XIo_In16(FLASH_BASEADDR + FLASH_START_ADDR + BITMAP_BITS_PER_PIXEL_OFFSET); Bitmap_Bits_Per_Pixel = Bitmap_Bits_Per_Pixel >> 8; if (verbose) xil_printf("\r\nBitmap Bits per pixel is %d", Bitmap_Bits_Per_Pixel); Bitmap_Image_Size = Read_Bitmap_Header_Bytes(FLASH_BASEADDR+FLASH_START_ADDR, BITMAP_IMAGE_SIZE_OFFSET); if (verbose) xil_printf("\r\nBitmap image size is %d", Bitmap_Image_Size); Bytes_Per_Row = Bitmap_Image_Size / Bitmap_Height_Pixels; if (verbose) xil_printf("\r\nBytes per row = %d", Bytes_Per_Row); MEM_HIGHADDR = (MEM_ROW_WIDTH * MEM_DISPLAYED_HEIGHT) * 4; //start loading the image in reverse order, because the bitmap data is stored in reverse order //therefore set the current address to the beginning of the highest line in the frame and //increment at each pixel in the line, then decrement with a line at the end of each line MEM_ADDR = MEM_HIGHADDR - (MEM_ROW_WIDTH * 4); if (verbose) xil_printf("\r\nImage Memory High address = 0x%X, Current Address = 0x%X", MEM_HIGHADDR, MEM_ADDR); LCDSetLine(2); for (i = 0; i<(Bitmap_Height_Pixels * Bytes_Per_Row); i = i + Bytes_Per_Row) { //read three bytes from the FLASH memory that represent B, G and R data for a pixel for (j = 0; j <Bytes_Per_Row; j = j + 3) { Pixel_B = XIo_In8(FLASH_BASEADDR + FLASH_START_ADDR + Bitmap_Data_Offset + i + j); Pixel_G = XIo_In8(FLASH_BASEADDR + FLASH_START_ADDR + Bitmap_Data_Offset + i + (j+1)); Pixel_R = XIo_In8(FLASH_BASEADDR + FLASH_START_ADDR + Bitmap_Data_Offset + i + (j+2)); Pixel_Data = ((0x00000000 | Pixel_R) << 16) | ((0x00000000 | Pixel_G) << 8) | (0x00000000 | Pixel_B); XIo_Out32(FRAME_ADDR + MEM_ADDR, Pixel_Data); //increment one pixel MEM_ADDR = MEM_ADDR + 4; } //if (verbose) xil_printf("\r\nCurrent MEM_ADDR value is 0x%X", MEM_ADDR); //decrement with one line MEM_ADDR = MEM_ADDR - ((MEM_DISPLAYED_ROW_WIDTH + MEM_ROW_WIDTH) * 4); //if (verbose) xil_printf("\r\nMEM_ADDR value is 0x%X", MEM_ADDR); if (!(i&0xFFF)) { LCDPrintChar('*'); xil_printf("."); } } //set the base address of the TFT device to the beginning of the bitmap image XTft_SetFrameBaseAddr(InstancePtr, FRAME_ADDR); //XIo_Out32 (XPAR_XPS_TFT_0_SPLB_BASEADDR, XPAR_DDR2_SDRAM_MPMC_BASEADDR + MEM_START_ADDR); if (verbose) xil_printf("\r\nMEM_ADDR value is 0x%X", MEM_ADDR); 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) { 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; }