| 1 | #include <avr/io.h> // this contains all the IO port definitions |
| 2 | #include <avr/interrupt.h> // definitions for interrupts |
| 3 | #include "util.h" |
| 4 | |
| 5 | #define TXPORT PORTB |
| 6 | #define TX 1 |
| 7 | |
| 8 | |
| 9 | |
| 10 | #define SERIALDELAY 66 |
| 11 | void serialdelay(void) { |
| 12 | uint16_t timer; |
| 13 | for (timer=0; timer <= SERIALDELAY; timer++) { |
| 14 | NOP; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | |
| 19 | void uart_putchar(char d) { |
| 20 | int i; |
| 21 | cli(); // turn off interrupts, make it nice & kleen |
| 22 | |
| 23 | TXPORT &= ~_BV(TX); |
| 24 | serialdelay(); |
| 25 | for (i=0; i< 8; i++) { |
| 26 | if (d & 0x1) { |
| 27 | TXPORT |= _BV(TX); |
| 28 | } else { |
| 29 | TXPORT &= ~_BV(TX); |
| 30 | } |
| 31 | serialdelay(); |
| 32 | d >>= 1; |
| 33 | } |
| 34 | TXPORT |= _BV(TX); |
| 35 | sei(); // turn on interrupts |
| 36 | serialdelay(); |
| 37 | } |
| 38 | |
| 39 | void printhex(uint8_t hex) { |
| 40 | hex &= 0xF; |
| 41 | if (hex < 10) |
| 42 | uart_putchar(hex + '0'); |
| 43 | else |
| 44 | uart_putchar(hex + 'A' - 10); |
| 45 | } |
| 46 | |
| 47 | void putnum_uh(uint16_t n) { |
| 48 | if (n >> 12) |
| 49 | printhex(n>>12); |
| 50 | if (n >> 8) |
| 51 | printhex(n >> 8); |
| 52 | if (n >> 4) |
| 53 | printhex(n >> 4); |
| 54 | printhex(n); |
| 55 | |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void putnum_ud(uint16_t n) { |
| 61 | uint8_t cnt=0, flag=0; |
| 62 | |
| 63 | while (n >= 10000UL) { flag = 1; cnt++; n -= 10000UL; } |
| 64 | if (flag) uart_putchar('0'+cnt); |
| 65 | cnt = 0; |
| 66 | while (n >= 1000UL) { flag = 1; cnt++; n -= 1000UL; } |
| 67 | if (flag) uart_putchar('0'+cnt); |
| 68 | cnt = 0; |
| 69 | while (n >= 100UL) { flag = 1; cnt++; n -= 100UL; } |
| 70 | if (flag) uart_putchar('0'+cnt); |
| 71 | cnt = 0; |
| 72 | while (n >= 10UL) { flag = 1; cnt++; n -= 10UL; } |
| 73 | if (flag) uart_putchar('0'+cnt); |
| 74 | cnt = 0; |
| 75 | uart_putchar('0'+n); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | void ROM_putstring(const char *str, uint8_t nl) { |
| 80 | uint8_t i; |
| 81 | |
| 82 | for (i=0; pgm_read_byte(&str[i]); i++) { |
| 83 | uart_putchar(pgm_read_byte(&str[i])); |
| 84 | } |
| 85 | if (nl) { |
| 86 | uart_putchar('\n'); uart_putchar('\r'); |
| 87 | } |
| 88 | } |