This shows you the differences between two versions of the page.
| et:examples:demo [2011/08/15 16:37] – tekitatud raivo.sell | et:examples:demo [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Demo näide ====== | ||
| + | <code c> | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | //#include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | #include " | ||
| + | |||
| + | #define ULTRASONIC_SPEED_OF_SOUND | ||
| + | #define ZIGBEE_MAX_NODES 4 // | ||
| + | |||
| + | usart port = USART(1); | ||
| + | zigbee_node_t nodelist[ZIGBEE_MAX_NODES]; | ||
| + | |||
| + | void SPI_init(void); | ||
| + | void SPI_write(unsigned char data); | ||
| + | |||
| + | |||
| + | |||
| + | ///////////////////////// | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | char adc0[16], | ||
| + | unsigned char new_value=0, | ||
| + | int servopos=1, dcpos=1, | ||
| + | |||
| + | /////////////////////// | ||
| + | |||
| + | // | ||
| + | lcd_gfx_init(); | ||
| + | lcd_gfx_clear(); | ||
| + | adc_init(ADC_REF_AVCC, | ||
| + | segment_display_init(); | ||
| + | pin leds[3] | ||
| + | pin buttons[3] = { PIN(C, 0), PIN(C, 1), PIN(C, 2) }; | ||
| + | pin led_red = PIN(C, 5); | ||
| + | pin sensors = PIN(G, 0); | ||
| + | pin buzzer = PIN(G, 5); | ||
| + | pin srf05 = PIN(F,2); | ||
| + | |||
| + | pin_setup_output(sensors); | ||
| + | pin_setup_output(buzzer); | ||
| + | |||
| + | for (i = 0; i < 3; i++) | ||
| + | { | ||
| + | pin_setup_output(leds[i]); | ||
| + | pin_setup_input(buttons[i]); | ||
| + | dcmotor_init(i); | ||
| + | } | ||
| + | usart_init_async(port, | ||
| + | |||
| + | dcmotor_init(3); | ||
| + | unipolar_init(0); | ||
| + | unipolar_init(1); | ||
| + | servomotor_init(0); | ||
| + | servomotor_init(1); | ||
| + | encoder_init(0); | ||
| + | encoder_init(1); | ||
| + | |||
| + | /////////////////////////// | ||
| + | while (true) | ||
| + | { | ||
| + | new_value0 = pin_get_value(buttons[0]); | ||
| + | if ((new_value0) && (!old_value0)) | ||
| + | { | ||
| + | //nupp = PINC & 0x07; | ||
| + | nupp++; | ||
| + | if (nupp>4) nupp=0; | ||
| + | pin_toggle(leds[0]); | ||
| + | lcd_gfx_clear(); | ||
| + | for (i = 0; i < 10; i++) | ||
| + | { | ||
| + | pin_set(buzzer); | ||
| + | hw_delay_ms(1); | ||
| + | pin_clear(buzzer); | ||
| + | hw_delay_ms(1); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | old_value0 = new_value0; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | |||
| + | if (nupp==0){ | ||
| + | sprintf(caption0, | ||
| + | sprintf(caption1, | ||
| + | sprintf(adc0, | ||
| + | sprintf(adc1, | ||
| + | sprintf(adc2, | ||
| + | } | ||
| + | ////////////////////////// | ||
| + | |||
| + | else if (nupp==1){ | ||
| + | new_value = pin_get_value(buttons[1]); | ||
| + | if ((new_value) && (!old_value)) | ||
| + | { | ||
| + | // | ||
| + | nupp1=1; | ||
| + | lcd_gfx_clear(); | ||
| + | } | ||
| + | old_value = new_value; | ||
| + | |||
| + | new_value2 = pin_get_value(buttons[2]); | ||
| + | if ((new_value2) && (!old_value2)) | ||
| + | { | ||
| + | // | ||
| + | lcd_gfx_clear(); | ||
| + | nupp1=2; | ||
| + | } | ||
| + | old_value2 = new_value2; | ||
| + | | ||
| + | if (nupp1==1) { | ||
| + | pin_clear(leds[1]); | ||
| + | pin_set(leds[2]); | ||
| + | lcd_taust=1; | ||
| + | sprintf(caption1, | ||
| + | } | ||
| + | else if (nupp1==2) { | ||
| + | pin_set(leds[1]); | ||
| + | pin_clear(leds[2]); | ||
| + | lcd_taust=0; | ||
| + | sprintf(caption1, | ||
| + | } | ||
| + | else { | ||
| + | pin_set(leds[1]); | ||
| + | pin_set(leds[2]); | ||
| + | sprintf(caption1, | ||
| + | nupp1=0; | ||
| + | } | ||
| + | // | ||
| + | | ||
| + | sprintf(adc0, | ||
| + | sprintf(adc1, | ||
| + | sprintf(adc2, | ||
| + | sprintf(caption0, | ||
| + | hw_delay_ms(100); | ||
| + | } | ||
| + | ////////////////////////// | ||
| + | |||
| + | else if (nupp==2){ | ||
| + | new_value = pin_get_value(buttons[1]); | ||
| + | if ((new_value) && (!old_value)) | ||
| + | { | ||
| + | bit_invert(nupp1, | ||
| + | lcd_gfx_clear(); | ||
| + | } | ||
| + | old_value = new_value; | ||
| + | | ||
| + | if (nupp1) { | ||
| + | pin_clear(sensors); | ||
| + | pin_clear(leds[0]); | ||
| + | sprintf(caption1, | ||
| + | sprintf(adc0, | ||
| + | sprintf(adc1, | ||
| + | sprintf(adc2, | ||
| + | } | ||
| + | else { | ||
| + | pin_set(sensors); | ||
| + | pin_set(leds[0]); | ||
| + | sprintf(caption1, | ||
| + | sprintf(adc0, | ||
| + | sprintf(adc1, | ||
| + | sprintf(adc2, | ||
| + | } | ||
| + | // | ||
| + | sprintf(caption0, | ||
| + | hw_delay_ms(500); | ||
| + | } | ||
| + | ///////////////////////// | ||
| + | else if (nupp==3){ | ||
| + | | ||
| + | sprintf(caption0, | ||
| + | sprintf(caption1, | ||
| + | sprintf(adc0, | ||
| + | sprintf(adc1, | ||
| + | sprintf(adc2, | ||
| + | | ||
| + | servopos++; | ||
| + | if (servopos> | ||
| + | servopos=-10; | ||
| + | dcpos=dcpos*-1; | ||
| + | pin_toggle(leds[0]); | ||
| + | } | ||
| + | |||
| + | dcmotor_drive(0, | ||
| + | dcmotor_drive(1, | ||
| + | dcmotor_drive(2, | ||
| + | dcmotor_drive(3, | ||
| + | |||
| + | dcpulse=encoder_get_pulses(0); | ||
| + | |||
| + | servomotor_position(0, | ||
| + | servomotor_position(1, | ||
| + | |||
| + | unipolar_halfstep(1, | ||
| + | unipolar_halfstep(0, | ||
| + | |||
| + | |||
| + | } | ||
| + | ////////////////////////////// | ||
| + | else if (nupp==4){ | ||
| + | |||
| + | new_value = pin_get_value(buttons[1]); | ||
| + | if ((new_value) && (!old_value)) | ||
| + | { | ||
| + | count = 0; | ||
| + | lcd_gfx_clear(); | ||
| + | } | ||
| + | old_value = new_value; | ||
| + | |||
| + | sprintf(caption0, | ||
| + | sprintf(caption1, | ||
| + | sprintf(adc0, | ||
| + | sprintf(adc1, | ||
| + | sprintf(adc2, | ||
| + | |||
| + | if (count == 0) | ||
| + | { | ||
| + | lcd_gfx_clear(); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(3, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(3, | ||
| + | lcd_gfx_write_string(" | ||
| + | |||
| + | zigbee_find_nodes(port, | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | zigbee_lcd_show_nodes(3, | ||
| + | hw_delay_ms(2000); | ||
| + | |||
| + | // Kuvab ekraanil ühendamise teate | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string((nodelist + adr)-> | ||
| + | |||
| + | zigbee_set_destination(port, | ||
| + | count = 1; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | sprintf(adc0, | ||
| + | sprintf(adc1, | ||
| + | | ||
| + | if (usart_has_data(port)) // | ||
| + | { | ||
| + | pin_toggle(leds[usart_read_char(port) - ' | ||
| + | } | ||
| + | else // | ||
| + | { | ||
| + | usart_send_char(port, | ||
| + | hw_delay_ms(1000); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | else { | ||
| + | pin_toggle(led_red); | ||
| + | hw_delay_ms(500); | ||
| + | } | ||
| + | //////////////// | ||
| + | |||
| + | // | ||
| + | // | ||
| + | segment_display_write(nupp); | ||
| + | lcd_gfx_backlight(lcd_taust); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(caption0); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(caption1); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(adc0); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(adc1); | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(adc2); | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void SPI_init(void) // | ||
| + | { | ||
| + | SPCR |= ((1<< | ||
| + | SPSR |= (1<< | ||
| + | } | ||
| + | |||
| + | void SPI_write(unsigned char data) | ||
| + | { | ||
| + | SPDR = data; | ||
| + | while (!(SPSR & 0x80)); // wait for data transfer to be completed | ||
| + | } | ||
| + | </ | ||
| + | |||