void ui_usb_sof_event(void) { static uint16_t counter_sof = 0; if (ui_enum_status == UHC_ENUM_SUCCESS) { /* Display device enumerated and in active mode */ if (++counter_sof > 2000) { counter_sof = 0; } if (counter_sof % 1000 == 0) { ui_adc_read(); } if (read_complete_flag != AOA_READ_ONGOING) { if (read_complete_flag == AOA_READ_SUCCESS) { ui_usb_message_reception(); } read_complete_flag = AOA_READ_ONGOING; uhi_aoa_read(ui_msg, sizeof(ui_msg), read_complete); } /* Buttons */ if (ui_button_state_changed) { ui_button_state[ui_button_state_changed - 1] = !ui_button_state[ui_button_state_changed - 1]; ui_msg[0] = MESSAGE_ATD_SIMPLE_SWITCH; ui_msg[1] = ui_button_state_changed - 1; ui_msg[2] = ui_button_state[ui_button_state_changed - 1]; uhi_aoa_write(ui_msg, 3, NULL); ui_button_state_changed = 0x00; } } }
/******************************************************************************* * MAIN FUNCTION * *******************************************************************************/ int main(void) { unsigned char distance = 0; // declare a variable to store distance // ensure all the hardware port in zero initially PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; // Initialize the I/O port direction, this must be configured according to circuit // please refer to PTK40A schematic for details // TRISX control pin direction, output pin must be configure as '0' // while input must be configure as '1' TRISA = 0b00010001; TRISB = 0b00001111; TRISC = 0b10010011; TRISD = 0; TRISE = 0; // Initialize ADC. adc_initialize(); //Ensure pin share with analog is being configured to digital correctly // Initialize LCD lcd_initialize(); //Initialize LCD before use beep(2); //buzzer sound for twice // PTK40A come with an ADC input (RA0), 3 different source can be connected to ADC in // There are LM35 temperature sensor, Potentiometer and external sensor // This example will use external ADC to read information from SHARP ANALOG DISTANCE SENSOR // Connect GND of the sensor to GND of PTK40A (ADC external port) // Connect Vcc of sensor to 5V of PTK40A (ADC external port) // Connect Vo of senosr to IN of PTK40A (ADC external port) // Please refer to PTK40A schematic for the connection // Please move JP14 to EXT (under ADC) // This example is based on GP2Y0A21 (10 to 80 cm) adc_on(); //activate ADC module in PIC LCD_BACKLIGHT = 1; //activate LCD Backlight lcd_putstr("Cytron PTK40A"); //display message on LCD lcd_2ndline(); //move cursor to 2nd line lcd_putstr("DISTANCE:"); lcd_goto(0X4E); lcd_putstr("cm"); // display symbol of cm while(1) // create an infinite loop { //refer datasheet graph for the convertion calculation distance = ui_adc_read()>>2; // read from Sharp distance sensor and convert to cm distance = 256 - distance; distance = ((distance*10)+100)/36; //calculation is calculater according to the data sheet graph lcd_goto(0x4B); //move cursor to after DISTANCE: on LCD lcd_bcd(3,distance); // display distance in cm delay_ms(50); } while(1) continue; // infinite loop to prevent PIC from reset if there is no more program }
/******************************************************************************* * MAIN FUNCTION * *******************************************************************************/ int main(void) { unsigned char step = 0; // declare a variable to store stepper step sequence unsigned int delay = 0; // declare a variable for stepper delay, need to be 16-bit for > 255 // ensure all the hardware port in zero initially PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; // Initialize the I/O port direction, this must be configured according to circuit // please refer to PTK40A schematic for details // TRISX control pin direction, output pin must be configure as '0' // while input must be configure as '1' TRISA = 0b00010001; TRISB = 0b00001111; TRISC = 0b10010011; TRISD = 0; TRISE = 0; // Initialize ADC. adc_initialize(); //Ensure pin share with analog is being configured to digital correctly // PTK40A come with an ADC input (RA0), 3 different source can be connected to ADC in // There are LM35 temperature sensor, Potentiometer and external sensor // There is also a stepper motor // Please refer to PTK40A schematic for the connection // Turning the potentiometer clock wise will change the speed of stepper rotation // Please move JP10 to PWM, JP14 to POT, JP20&21 to STEPPER, JP23&24 to UNIPOLAR adc_on(); //activate ADC module in PIC step = 1; //initial step is 1 RC2 = 1; //RC2 pin is the PWM pin, in stepper case, the PWM is always 1 while(1) // create an infinite loop { if(step >= 5) step = 1; // reset the step sequance to 1 delay = ui_adc_read(); if (delay > 10) // if delay is larger than 10, enter stepping { switch (step) { case 1: X = 1; // step 1 Y = 0; XN = 0; YN = 0; break; case 2: X = 0; // step 2 Y = 1; XN = 0; YN = 0; break; case 3: X = 0; // step 3 Y = 0; XN = 1; YN = 0; break; case 4: X = 0; // step 4 Y = 0; XN = 0; YN = 1; break; }//switch (step) delay_ms(delay); step ++; // increase step }//if(delay > 10) } // while (1) while(1) continue; // infinite loop to prevent PIC from reset if there is no more program }