This shows you the differences between two versions of the page.
| — | en:examples:communication:usb [2026/02/19 11:30] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== USB ====== | ||
| + | |||
| + | //Required knowledge: | ||
| + | [HW] [[et: | ||
| + | [LIB] [[et: | ||
| + | [LIB] [[et: | ||
| + | |||
| + | ===== Theory ===== | ||
| + | |||
| + | ===== Practice ===== | ||
| + | HomeLab III generation controllers have a USB connection directly to the microcontroller, | ||
| + | |||
| + | To test the example program, you need a terminal-type application, | ||
| + | |||
| + | <code c> | ||
| + | // HomeLab USB interface example program | ||
| + | // Text sent from a computer or another device is displayed on the LCD screen | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Main program | ||
| + | int main(void) | ||
| + | { | ||
| + | char c; | ||
| + | unsigned char row = 1; | ||
| + | char buf[20]; | ||
| + | |||
| + | // LCD initialization and greeting message | ||
| + | lcd_gfx_init(); | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | |||
| + | // Start ADC | ||
| + | adc_init(ADC_REF_AVCC, | ||
| + | |||
| + | // USB interface setup | ||
| + | usb_init(); | ||
| + | |||
| + | // Infinite loop | ||
| + | while (1) | ||
| + | { | ||
| + | // Read a character from USB and check if one was received | ||
| + | c = usb_read_byte(); | ||
| + | if (c >= 0) | ||
| + | { | ||
| + | // Send the character back via USB | ||
| + | usb_send_char(c); | ||
| + | if (c == ' | ||
| + | { | ||
| + | // Switch line | ||
| + | if (row < 12) row++; | ||
| + | else row = 0; | ||
| + | // Clear previous message from the line | ||
| + | lcd_gfx_clear_line(row); | ||
| + | // Cursor to the beginning of the new line | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | // Print character directly to the screen | ||
| + | lcd_gfx_write_char(c); | ||
| + | } | ||
| + | // If question mark was sent, also return ADC reading | ||
| + | if(c == '?' | ||
| + | { | ||
| + | sprintf(buf," | ||
| + | usb_send_string(buf); | ||
| + | } | ||
| + | } | ||
| + | usb_task(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||