BOOL ValidateAndDisplayDeviceFwVersion ( void ) { if ( USBHostGenericGetRxLength(deviceAddress) == 4 && DataPacket.CMD == READ_VERSION && DataPacket._byte[3] >= DEMO_FW_MAJOR_VERSION ) { // Display Device FW version on LCD LcdData[0][13] = '0' + DataPacket._byte[3]; LcdData[0][15] = '0' + DataPacket._byte[4]; LCDWriteLine(1, (char *)&LcdData[0][0] ); // Display device FW version on terminal UART2PrintString( "Device firmware version " ); UART2PutDec( DataPacket._byte[3] ); UART2PrintString( "." ); UART2PutDec( DataPacket._byte[4] ); UART2PrintString( "\r\n" ); return TRUE; } else { UART2PrintString( "Device Firmware Version Error!\r\n" ); LCDWriteLine( 2, "Dev FW Ver Error" ); return FALSE; } } // ValidateAndDisplayDeviceFwVersion
int main(){ LCD_Init(); char* dutyString = "Duty: "; char* rpmString = "RPM: "; ////////////// //duty ////////////////////////////////////////////// DDRB = 0xFF; //ouput hbridge PWMSCLA = 0x0C; // 0x0C = 12 PWMCLK = 0x33; PWMPOL = 0x01; PWMCTL = 0x00; PWMPER1 = 100; // 120 //PWMDTY1 = dutyValue; PWME = 0x01; PORTB = 0x08; ////////////////////// //read ////////////////////////////////////////// DDRT &= 0x00; // Enable the Timer System TSCR1 = 0x80; // Make IC channel TIOS = 0x00; //rising edge only TCTL4 = 0x40;//01 //Enable IC3 interrupts TIE = 0x08;//01 UserTimerCh3 = (unsigned short)&IC3_ISR; asm("cli"); // Spin forever while (1){ //dutyValue = dutyCycle(); //PWMDTY1 = dutyValue; _uitoa(dutyValue,&dutyString[6],10); _uitoa(rpmValue,&rpmString[5],10); LCDWriteLine(0x1,dutyString); LCDWriteLine(0x2,rpmString); //delay(100); } return 0; }
void InitLcdMessage ( void ) { // Init LCD message FILL_LCD_LINE(1, &LcdInitData[0][0]); FILL_LCD_LINE(2, &LcdInitData[1][0]); LcdData[0][5] = '0' + USB_HOST_FW_MAJOR_VER; LcdData[0][7] = '0' + USB_HOST_FW_MINOR_VER; // display message LCDWriteLine(1, (char *)&LcdData[0][0] ); LCDWriteLine(2, (char *)&LcdData[1][0] ); } // InitLcdMessage
void DisplayTemperature ( void ) { DWORD Temperature; BYTE Digit; // Calculate the temperature in degrees C Temperature = DataPacket._byte[1]; Temperature |= (DataPacket._byte[2] << 8); Temperature += 6400; Temperature /= 41; // Translate to ASCII Digit = Temperature % 10; Temperature /= 10; LcdData[1][3] = '0' + Digit; Digit = Temperature % 10; Temperature /= 10; LcdData[1][1] = '0' + Digit; Digit = Temperature % 10; Temperature /= 10; LcdData[1][0] = '0' + Digit; // Display on LCD LCDWriteLine(2, (char *)&LcdData[1][0] ); } // DisplayTemperature
int print(int statement,int value){ //subfunction for uitoa if(statement == 1){ char* scoreString = "Score: "; _uitoa(value,&scoreString[7],10); LCDWriteLine(0x1,scoreString); } else if(statement == 2){ //DEBUG char* debugString = "Debug: "; _uitoa(value,&debugString[7],10); LCDWriteLine(0x1,debugString); } else{ char* ahhhString = "ahhhhhh"; LCDWriteLine(0x1,ahhhString); } return; }
void main(void) { OSCTUNEbits.PLLEN = 1; LCDInit(); LCDClear(); InitPins(); ConfigPeriph(); ConfigInterrupts(); while (1) { received = ReadSerial(); LCDClear(); switch(received) { case 'a': received2 = ReadSerial(); sprintf(output,"Received char:%c",received2); /* * FIXME : PIC18 doesn't transmit back to RPi * python script hangs on serial.read(1) * Same behavior with cat -A /dev/ttyACM0 * * * _delay(2); // 2 cycle delay *while(!PIR1bits.TX1IF); *sprintf(output,"TRANSMIT"); *LCDWriteLine(output,0); *TXREG1=received2; * */ break ; case 'b': potentiometer = ReadPot(); sprintf(output,"Pot value: %d", potentiometer); LATD =~ LATD; break; case 'c': received2=ReadSerial(); sprintf(output,"Set LED to 0x%X",received2); LATD = received2; break; default: sprintf(output, ">>UNKNOWN %c",received); break; } LCDWriteLine(output,0); } }
void DisplayPot ( void ) { DWORD POT_Val; BYTE Digit; int i; // Calculate actual POT value POT_Val = DataPacket._byte[1]; POT_Val |= (DataPacket._byte[2] << 8); POT_Val *= POT_FULL_VALUE+10; POT_Val /= 1024; // Translate to ASCII for (i=0; i < 5; i++) { Digit = POT_Val % 10; POT_Val /= 10; LcdData[1][10-i] = '0' + Digit; } // Display on LCD LCDWriteLine(2, (char *)&LcdData[1][0] ); } // DisplayPot
/************************************************************************* * Function: ManageDemoState * * Preconditions: The DemoState global variable must be initialized to * DEMO_STATE_IDLE (0). (This occurs on reset.) * * Input: DemoState (global) * Actions selected based value of DemoState on function * entry. * * deviceAddress (global) * May use device address to access device, depending on * state. * * DataPacket (global) * May read data from packet buffer, depending on state. * * Output: DemoState (global) * Updates demo state as appropriate. * * DataPacket (global) * May cause data in the packet buffer to be updated, * depending on state. * * Returns: None * * Side Effects: Depend on state transition * * Overview: This routine maintains the state of the application, * updateing global data and taking actions as necessary * to maintain the custom demo operations. *************************************************************************/ void ManageDemoState ( void ) { BYTE RetVal; BlinkStatus(); // Watch for device timeouts // if (MSTimerGetTime() > DEMO_TIMEOUT_LIMIT) // { // if (DemoState == DEMO_STATE_IDLE) // { // LCDWriteLine( 2, "Awaiting Device" ); // } // else // { // UART2PrintString( "Device Time-out Error!\r\n" ); // LCDWriteLine( 2, "Dev Time-out Err" ); // DemoState = DEMO_STATE_ERROR; // } // } // Watch for device detaching if (USBHostGenericDeviceDetached(deviceAddress) && deviceAddress != 0) { UART2PrintString( "Generic demo device detached - polled\r\n" ); DemoState = DEMO_INITIALIZE; deviceAddress = 0; } switch (DemoState) { case DEMO_INITIALIZE: InitLcdMessage(); DemoState = DEMO_STATE_IDLE; break; /** Idle State: Loops here until attach **/ case DEMO_STATE_IDLE: if (CheckForNewAttach()) { DemoState = DEMO_STATE_GET_DEV_VERSION; } break; /** Sequence: Read Dev FW Version **/ case DEMO_STATE_GET_DEV_VERSION: // Send the Read Version command DataPacket.CMD = READ_VERSION; DataPacket.len = 2; if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS ) { DemoState = DEMO_STATE_WAITING_VER_REQ; } else { UART2PrintString( "1 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_VER_REQ: if (!USBHostGenericTxIsBusy(deviceAddress) ) DemoState = DEMO_STATE_READ_DEV_VERSION; break; case DEMO_STATE_READ_DEV_VERSION: if (!USBHostGenericRxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 4)) == USB_SUCCESS ) { DemoState = DEMO_STATE_WAITING_READ_VER; } else { UART2PrintString( "1 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_READ_VER: if (!USBHostGenericRxIsBusy(deviceAddress)) DemoState = DEMO_STATE_VERIFY_DEV_FW_VER; break; case DEMO_STATE_VERIFY_DEV_FW_VER: if (ValidateAndDisplayDeviceFwVersion()) DemoState = DEMO_STATE_GET_TEMPERATURE; else DemoState = DEMO_STATE_ERROR; break; /** Sequence: Read Temperature Sensor Data **/ case DEMO_STATE_GET_TEMPERATURE: // Send the Read Temperature command DataPacket.CMD = RD_TEMP; DataPacket.len = 2; if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_GET_TEMP; } else { UART2PrintString( "2 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_GET_TEMP: if (!USBHostGenericTxIsBusy(deviceAddress) ) { DemoState = DEMO_STATE_READ_TEMPERATURE; } break; case DEMO_STATE_READ_TEMPERATURE: if (!USBHostGenericRxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 3)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_READ_TEMP; } else { UART2PrintString( "2 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_READ_TEMP: if (!USBHostGenericRxIsBusy(deviceAddress)) { DemoState = DEMO_STATE_DISPLAY_TEMPERATURE; } break; case DEMO_STATE_DISPLAY_TEMPERATURE: DisplayTemperature(); DemoState = DEMO_STATE_GET_POT; break; /** Sequence: Read POT Sensor Data **/ case DEMO_STATE_GET_POT: // Send the Read POT command DataPacket.CMD = RD_POT; DataPacket.len = 2; if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 2)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_GET_POT; } else { UART2PrintString( "3 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_GET_POT: if (!USBHostGenericTxIsBusy(deviceAddress) ) DemoState = DEMO_STATE_READ_POT; break; case DEMO_STATE_READ_POT: if (!USBHostGenericRxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 3)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_READ_POT; } else { UART2PrintString( "3 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_READ_POT: if (!USBHostGenericRxIsBusy(deviceAddress)) DemoState = DEMO_STATE_DISPLAY_POT; break; case DEMO_STATE_DISPLAY_POT: DisplayPot(); DemoState = DEMO_STATE_SEND_SET_LED; break; /** Sequence: Update LEDs **/ case DEMO_STATE_SEND_SET_LED: // Send the set-LED command DataPacket.CMD = UPDATE_LED; DataPacket.len = 3; if (Switch3WasPressed()) { DataPacket.led_num = 3; // LED 3 on original PIC18 FS USB board DataPacket.led_status = LEDState.bits.b3 ^ 1; } else if (Switch6WasPressed()) { DataPacket.led_num = 4; // LED 4 on original PIC18 FS USB board DataPacket.led_status = LEDState.bits.b4 ^ 1; } else { DemoState = DEMO_STATE_GET_TEMPERATURE; break; } if (!USBHostGenericTxIsBusy(deviceAddress)) { if ( (RetVal=USBHostGenericWrite(deviceAddress, &DataPacket, 3)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_SET_LED; } else { UART2PrintString( "4 Device Write Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Write Error " ); } } break; case DEMO_STATE_WAITING_SET_LED: if (!USBHostGenericTxIsBusy(deviceAddress) ) DemoState = DEMO_STATE_READ_SET_LED_RESP; break; case DEMO_STATE_READ_SET_LED_RESP: if (!USBHostGenericRxIsBusy(deviceAddress)) { DataPacket.CMD = CMD_INVALID; if ( (RetVal=USBHostGenericRead(deviceAddress, &DataPacket, 1)) == USB_SUCCESS) { DemoState = DEMO_STATE_WAITING_LED_RESP; } else { UART2PrintString( "4 Device Read Error 0x" ); UART2PutHex(RetVal); UART2PrintString( "\r\n" ); LCDWriteLine( 2, "Dev Read Error " ); } } break; case DEMO_STATE_WAITING_LED_RESP: if (!USBHostGenericRxIsBusy(deviceAddress)) DemoState = DEMO_STATE_UPDATE_LED_STATE; break; case DEMO_STATE_UPDATE_LED_STATE: if (DataPacket.CMD == UPDATE_LED) // CMD updated by read from device { if (DataPacket.led_num == 3) // led_num left-over from set-LED command send { LEDState.bits.b3 ^= 1; // mLED_10_Toggle(); } else if (DataPacket.led_num == 4) { LEDState.bits.b4 ^= 1; // mLED_9_Toggle(); } } DemoState = DEMO_STATE_GET_TEMPERATURE; break; /** Error state: Hold here until detached **/ case DEMO_STATE_ERROR: // To Do: Flash LEDs break; default: DemoState = DEMO_INITIALIZE; break; } DelayMs(1); // 1ms delay } // ManageDemoState
int main1(void) { word LED = 0; asm("sei"); // disable the global interrupts #ifdef GCC_TOOLCHAIN UserRTI = (unsigned short)&RealTimeInterrupt; // 3e70 UserTimerCh3 = (unsigned short)&Timer3Interrupt; UserTimerCh4 = (unsigned short)&Timer4Interrupt; UserTimerCh5 = (unsigned short)&Timer5Interrupt; // 3e64 UserTimerCh6 = (unsigned short)&Timer6Interrupt; // 3e62 #endif // Port set ups. DDRH = 0x0; // set porth pins to input DIP Switches dipswitch = PTH; // Read the DIP Switches. // LEDs row of LEDs and 7 segs share Port B // PJ1 select the row of LEDs // PTP[0:4] control the 7 segs. Set Lo to select. DDRB = 0xff; // Port B output to LEDS DDRJ |= 0x02; // PJ1 enables the row of LEDS, so make it output. DDRP |= 0xf; // PP0-PP3 Are digit enables for the 7 segs. Set to output. PERP |= 0xf; // Enable pull ups on PP0-PP3. PTP |= 0xf; // Turn off all the digits for now. PTJ &= ~0x2; // Set PJ1 to 0 to turn on LEDS. // At this point what ever you write to PORTB will // show up on the single row of LEDs // After we start running, then stuff will be muxed instead. PORTB = dipswitch; LCD_Init(); LCDWriteLine(1,"Ready..."); PUCR = PUCR | 1; // enable pullups on porta for Key Pad. // ATD block set up. ATD0CTL2 = 0x80; // Enable power up mode for ADC Block ATD0CTL3 = 0x40; // Set 8 bit conversions. // Set the enable digital input for the switches SW1-SW4 ATD0DIEN = 0xff; // LEDOut(LED); rtiCnt = 0; // roll over counter for forground loop. // Set the Real Time Clock Rate RTICTL = 0x10; // 1/(16Mz/2^10) = 64 us or 15.6 ticks per ms // 244us or 4 ticks/ms CRGINT |= 0x80; // enable the interrupt for RTI InitSPI0DAC(); // Set up timer 5 for the speaker. t5delay = 1000; t6delay = 3000; // 1ms t6acc = t6start; TSCR1 = 0x90; // Enable TCNT and fast clear TSCR2 = 0x03; // Set prescaler to 1:8 TIOS |= TIOS_IOS6_MASK; // Enable OC6 for song timer TIOS |= TIOS_IOS4_MASK; // Enable Timer 4 for muxing TIOS |= TIOS_IOS3_MASK; // Enable Timer 3 for DAC waveforms // TIOS |= TIOS_IOS5_MASK; // Disable the speaker until song starts. TCTL1 = 5; // Set toggle mode for OC5 and OC4 TC5 = TCNT + t5delay; // Init the comnpare registers TC6 = TCNT; // This also resets the inturrupt. TIE |= TIOS_IOS5_MASK ; // Enable the timer interrupt bits. TIE |= TIOS_IOS6_MASK; TIE |= TIOS_IOS4_MASK; TIE |= TIOS_IOS3_MASK; // LEDOut(2); // New code // TDBG12printf("\nTEST for miniDragon12+ Hello World!\n"); // Init the EEProm and Flash Burn clocks // #ifdef miniDragon FCLKDIV = ((16000/200/8)-1)+0x40; // Flash CLK = 200 kHz ECLKDIV = ((16000/200/8)-1)+0x40; // Eeprom CLK = 200 kHz #else FCLKDIV = ((4000/200)-1); // Flash CLK = 200 kHz ECLKDIV = ((4000/200)-1); // Eeprom CLK = 200 kHz #endif asm("cli"); // enable the global interrupts // TDBG12printf("\nTEST for Dragon12 Hello World!\n"); LCDWriteLine(1,"Ready..."); LCDSetCharDelay(2, 0x2000); LCDScrollLine(2,initMessage); LCDWriteLine(3,"ABCDEFGHIJKLMNOPQRST"); LCDWriteLine(4,"!@#$%^&*(){}[]:;?><"); for (;;) { // Main forground loop. // Do things at different intervals. LEDSvalue = PTH; // Read the dip switches LCDUpdateScroll(); if (rtiCnt >= LEDCountRate) { ++LED; seg7value = LED; // set the value in the 7 segs. LEDCountRate = LED_COUNT_RATE; if(ReadSwitch( SW5) ) LEDCountRate >>= 1; if(ReadSwitch( SW2) ) LEDCountRate >>= 1; if(ReadSwitch( SW3) ) LEDCountRate >>= 1; if(ReadSwitch( SW4) ) LEDCountRate >>= 1; rtiCnt = 0; } if(0 == rtiCnt % 10 ) { LEDDutyCycle = ReadADC(POT_CHANNEL_NUM); _uitoa(LEDDutyCycle,&LCDMessage[9],10); LCDWriteLine(1,&LCDMessage[0]); if( LEDDutyCycle > 80 ) { LCDSetCharDelay(2, LEDDutyCycle * 50); } } if( 0 == rtiCnt % 2 ) { ScanKeyPad(); } }