void sendData(void) { Xper = (ADCXval*3/4095*100/3); //calculate percentage outputs Yper = (ADCYval*3/4095*100/3); int i; //Send packet via rs-232 UART_putchar(0x55); //send header //Use character pointers to send one byte of float X and Y value at a time char *xpointer=(char *)&Xper; char *ypointer=(char *)&Yper; //Send x percentage float one byte at a time for(i=0; i<4; i++) { UART_putchar(xpointer[i]); } //Send y percentage float one byte at a time for(i=0; i<4; i++) { UART_putchar(ypointer[i]); } }
int main(void) { DDRB = 0xFF; PORTB = 0x20; // use CLK/1024 prescale value, clear timer/counter on compareA match TCCR1B = (1<<CS10) | (1<<CS12) | (1<<WGM12); // preset timer1 high/low byte OCR1A = ((F_CPU/1024) - 1 ); // enable Output Compare 1 overflow interrupt //TIMSK = (1<<OCIE1A); // Enable interrupts sei(); UART_init(4800); UART_putchar('W'); UART_putchar('\n'); UART_putchar('\r'); UART_puts("Hello World!\n\r"); UART_puts("This is a longer message!\n\rLet's hope it works well enough...\n\r"); UART_puts("Hello World! 1\n\r"); UART_puts("Hello World! 2\n\r"); UART_puts("Hello World! 3\n\r"); unsigned int i = 0; while (1) { i++; PORTB ^= 0x20; char charbuf[100]; sprintf(charbuf, "Loop number %d\n\r",i); UART_puts(charbuf); } }
void mtk_send_command(char* command){ uint8_t checksum = 0; int i; /* Send what we have first, and then calculate checksums, etc while its sending */ UART_printf("%s", command); for(i=0; command[i] != '$'; i++) ; /* Get to the character after the $ */ i++; /* Calculate the checksum */ for(; (command[i] != '*') && (command[i] != '\0'); i++) checksum ^= command[i]; if(command[i] != '*') UART_putchar('*'); UART_putchar(checksum); UART_printf("\r\n"); }
void UART_puts_no_newline( const char * str ) { while( *str != '\0' ) { if( *str == '\n' ) // if current character is '\n', insert and output '\r' UART_putchar( '\r' ); UART_putchar( *str++ ); } }
/* ****************************************************************************************************************** * *Function Name : UART_printf * *Description : This function is to formatedly output through UART, similar to ANSI C function printf(). * This function can support and only support the following Conversion Specifiers: * %d Signed decimal integer. * %u Unsigned decimal integer. * %x Unsigned hexadecimal integer, using hex digits 0f. * %X Unsigned hexadecimal integer, using hex digits 0F. * %c Single character. * %s Character string. * %p A pointer. * *Input : refer to ANSI C function printf(). * *Output : void, different form ANSI C function printf(). * *call for : void int_to_string_dec( __s32 input, char * str ), defined in format_transformed.c. * void int_to_string_hex( __s32 input, char * str ); defined in format_transformed.c. * void Uint_to_string_dec( __u32 input, char * str ); defined in format_transformed.c. * void UART_putchar( __s32 ch); defined in boot loader. * void UART_puts( const char * string ); defined in boot loader. * *Others : None at present. * ******************************************************************************************************************* */ void UART_printf2( const char * str, ...) { char string[13]; char *p; __s32 hex_flag ; va_list argp; va_start( argp, str ); while( *str ) { if( *str == '%' ) { ++str; p = string; hex_flag = HEX_X; switch( *str ) { case 'd': int_to_string_dec( va_arg( argp, __s32 ), string ); UART_puts_no_newline( p ); ++str; break; case 'x': hex_flag = HEX_x; // jump to " case 'X' " case 'p': case 'X': int_to_string_hex( va_arg( argp, __s32 ), string, hex_flag ); UART_puts_no_newline( p ); ++str; break; case 'u': Uint_to_string_dec( va_arg( argp, __s32 ), string ); UART_puts_no_newline( p ); ++str; break; case 'c': UART_putchar( va_arg( argp, __s32 ) ); ++str; break; case 's': UART_puts_no_newline( va_arg( argp, char * ) ); ++str; break; default : UART_putchar( '%' ); // if current character is not Conversion Specifiers 'dxpXucs', UART_putchar( *str ); // output directly '%' and current character, and then ++str; // let 'str' point to next character. } } else { if( *str == '\n' ) // if current character is '\n', insert and output '\r' UART_putchar( '\r' ); UART_putchar( *str++ ); } } va_end( argp ); }
void PGcode() { for(;;) { /* Get a character */ while(UART_getchar_present() == 0); (*p_buffer) = UART_getchar(); UART_putchar((*p_buffer)); (*(p_buffer+sizeof(char))) = 0; if((*p_buffer) == BS) p_buffer -= sizeof(char); /* Check for end of command */ if((*p_buffer) == CR) { GCODE_STATUS st; LINEFEED; st = ProcessCommand(); if(st == GCODE_DONE) break; p_buffer = &buffer[0]-(sizeof(char)); } /* Go for next char */ if(p_buffer < buffer+BUF_SIZE*sizeof(char)) p_buffer += sizeof(char); else { LINEFEED; PRINTLN("Buffer overflow"); PRINTLN("Returning to terminal"); break; } } }
int main(void) { char c; // odebrany znak UART_init(); // inicjalizacja portu szeregowego LCD_init(); // inicjalizacja wy�wietlacza LCD LCD_PL_chars_init(); // polskie znaki na wy�wietlaczu LCD KBD_init(); // inicjalizacja klawiatury LED7SEG_init(); // inicjalizacja wy�wietlacza sei(); // w��cz obs�ug� przerwa� while(1) // p�tla niesko�czona { PCF8583_get_time(&godz,&min,&sek,&ssek); LED7SEG_putU08(sek); // wy�wietlaj warto�� if (UART_rxlen()>0) // je�li odebrano znak { c=UART_getchar(); // tu mo�na wstawi� reakcje na komendy steruj�ce np. typu ESC[ LCD_putchar(c); // wy�wietl go na LCD } if (KBD_read()) // je�li naci�ni�to klawisz { if ((KBD_ascii>='A')&&(KBD_ascii<='D')) UART_putstr_P(CURSOR); // sterowanie kursorem UART_putchar(KBD_ascii); // wy�lij go na port szeregowy KBD_wait(); // czekaj na zwolnienie klawisza } } }
static void UART_printchar(char **str, int c) { if (str) { **str = c; ++(*str); } else { (void)UART_putchar(c); } }
u8 UART_putstring(char *chaine) { char car; int i=0; while ( (car=chaine[i]) != '\0' ) { while (UART_putchar(car) == ERROR); i++; } }
/* * Terminal loop application. */ void terminal() { char c = 48; for(;;) { LINEFEED; UART_putchar('>'); while(UART_getchar_present() == 0); c = UART_getchar(); switch(c) { case 'w': PRINTLN("Moving UP"); MoveY(-MOVE_DIST); break; case 's': PRINTLN("Moving DOWN"); MoveY(MOVE_DIST); break; case 'a': PRINTLN("Moving LEFT"); MoveX(-MOVE_DIST); break; case 'd': PRINTLN("Moving RIGHT"); MoveX(MOVE_DIST); break; case 'h': PRINTLN("Going Home..."); MoveHome(); break; case 'z': PRINTLN("Zeroed at current position."); MoveSetZero(); break; case 'g': PRINTLN("Entering Pseudo G-Code mode."); PGcode(); break; case 'o': PRINTLN("Draw a circle?"); DrawCircle(50); break; case ' ': PRINTLN("Pen action"); penp = 100 - penp; PWM_setPosition(penp); break; } } }
void UART_putchar(char c, FILE *stream) { if (c == '\n') UART_putchar('\r', stream); // Make \n to \r\n. loop_until_bit_is_set(UCSR0A, UDRE0); UDR0 = c; }
void DEBUG_hextable(u08 *buffer, u16 length) { u08 i; u16 j; u08 s; // print the low order address indicies and ASCII header UART_putstr_P(PSTR("\r\n ")); for(i=0; i<0x10; i++) { UART_puthexU08(i); UART_putchar(' '); } UART_putchar(' '); for(i=0; i<0x0a; i++) UART_putchar(i+'0'); for(i=0; i<6; i++) UART_putchar(i+'A'); UART_putstr_P(PSTR("\r\n ")); for(i=0; i<47; i++) UART_putchar('-'); UART_putstr_P(PSTR(" ---- ASCII -----\r\n")); // print the data for(j=0; j<((length+15)>>4); j++) { // print the high order address index for this line UART_puthexU16(j<<4); UART_putchar(' '); // print the hex data for(i=0; i<0x10; i++) { // be nice and print only up to the exact end of the data if( ((j<<4)+i) < length) { // print hex byte UART_puthexU08(buffer[(j<<4)+i]); UART_putchar(' '); } else { // we're past the end of the data's length // print spaces UART_putstr_P(PSTR(" ")); } } // leave some space UART_putchar(' '); // print the ascii data for(i=0; i<0x10; i++) { // be nice and print only up to the exact end of the data if( ((j<<4)+i) < length) { // get the character s = buffer[(j<<4)+i]; // make sure character is printable if(s >= 0x20) UART_putchar(s); else UART_putchar('.'); } else { // we're past the end of the data's length // print a space UART_putchar(' '); } } UART_putstr_P(PSTR("\r\n")); } UART_putstr_P(PSTR("\r\n")); }
void main(void) { int i = 0; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer UART_Initialize(); ADC12CTL0 = SHT0_8 + REFON + ADC12ON; ADC12CTL1 = SHP; // enable sample timer ADC12MCTL0 = 0x01A; ADC12IE = 0x001; rx_flag = 0; // rx default state "empty" _EINT(); // enable global interrupts Wait: //send a greeting message for(i = 0; i < 67; i++) { thr_char = gm1[i]; UART_putchar(thr_char); } while(!(rx_flag&0x01)); // wait until receive the character from HyperTerminal rx_flag = 0; // clear rx_flag UART_putchar(thr_char); UART_putchar('\n'); // newline UART_putchar('\r'); // carriage return // character input validation if ((thr_char != 'y') && (thr_char != 'n') && (thr_char !='Y') && (thr_char !='N')) { for(i = 0; i < 15; i++) { thr_char = gm3[i]; UART_putchar(thr_char); } UART_putchar('\n'); // newline UART_putchar('\r'); // carriage return goto Wait; } if ((thr_char == 'y') || (thr_char == 'Y')) { ADC12CTL0 |= ENC + ADC12SC; // Sampling and conversion start _BIS_SR(CPUOFF + GIE); // LPM0 with interrupts enabled // oF = ((x/4096)*1500mV)-923mV)*1/1.97mV = x*761/4096 - 468 // IntDegF = (ADC12MEM0 - 2519)* 761/4096 IntDegF = (temp - 2519) * 761; IntDegF = IntDegF / 4096; // oC = ((x/4096)*1500mV)-986mV)*1/3.55mV = x*423/4096 - 278 // IntDegC = (ADC12MEM0 - 2692)* 423/4096 IntDegC = (temp - 2692) * 423; IntDegC = IntDegC / 4096; //printing the temperature on hyperterminal sprintf(NewKey, "T(F)=%ld\tT(C)=%ld\n", IntDegF, IntDegC); for(i = 0; i < 25; i++) { thr_char = NewKey[i]; UART_putchar(thr_char); } UART_putchar('\n'); // newline UART_putchar('\r'); // carriage return goto Wait; } if ((thr_char == 'n') || (thr_char == 'N')) { for(i = 0; i < 9; i++) { thr_char = gm2[i]; UART_putchar(thr_char); } UART_putchar('\n'); // newline UART_putchar('\r'); // carriage return } }