Table of Contents

 

USB

Required knowledge: [HW] Kontrollermoodul, [LIB] USB,
[LIB] Graafiline LCD

Theory

Practice

HomeLab III generation controllers have a USB connection directly to the microcontroller, which enables a direct connection between the microcontroller and a computer.

To test the example program, you need a terminal-type application, e.g., HyperTerminal (Windows XP). On Linux you can use minicom. Characters typed in the window are shown on the LCD screen. Pressing Enter changes the line on the LCD screen. When the '?' symbol is sent, the ADC reading is returned.

// HomeLab USB interface example program
// Text sent from a computer or another device is displayed on the LCD screen
#include <homelab/module/lcd_gfx.h>
#include <homelab/usb/usb.h>
#include <homelab/adc.h>
 
// 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("Ootan teadet");
	lcd_gfx_goto_char_xy(0, row);
 
	// Start ADC
	adc_init(ADC_REF_AVCC,ADC_PRESCALE_128);
 
	// 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 == '\r') // Is it a newline?
			{
				// 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,row);
			}
			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,"\n\r ADC val: %4d. \n\r",adc_get_average_value(15,5));
                usb_send_string(buf);
            }
		}
		usb_task();
	}
}
en/examples/communication/usb.txt · Last modified: by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0