/* exchanges a byte (full duplex) with the controller */ static byte send_and_receive_byte(byte send_data) { byte received_data = 0x00; unsigned char i; /* send bit by bit */ for(i = 0; i < 8; i++) { /* send our bit by setting the command pin with our send_data "i" bit */ if(CHECK_BIT(send_data, i) == 0) { DS2MSP_PORTOUT &= ~DS2MSP_COMMAND_PIN; } else { DS2MSP_PORTOUT |= DS2MSP_COMMAND_PIN; } /* sinalize that our data is there and ask for the controller data by setting the clock pin to low */ DS2MSP_PORTOUT &= ~DS2MSP_CLOCK_PIN; bit_delay(); /* receive the data sent by the controller */ if((DS2MSP_PORTIN & DS2MSP_DATA_PIN) != 0) { received_data |= (1 << i); } /* set the clock to high */ DS2MSP_PORTOUT |= DS2MSP_CLOCK_PIN; bit_delay(); } return received_data; }
static void l3_write_byte (unsigned char data, bool address_mode) { int bit; L3PORT |= L3CLOCK; if (address_mode) L3PORT &= ~L3MODE; else L3PORT |= L3MODE; bit_delay(); for (bit=0; bit < 8; bit++) { if (data & 1) { L3PORT |= L3DATA; } else { L3PORT &= ~L3DATA; } L3PORT &= ~L3CLOCK; bit_delay(); L3PORT |= L3CLOCK; bit_delay(); data >>= 1; } if (address_mode) L3PORT |= L3MODE; else L3PORT &= ~L3MODE; bit_delay(); }
static void tm1640_start(void) { TM1640_PORT &= ~TM1640_DIN; bit_delay(); TM1640_PORT &= ~TM1640_SCLK; bit_delay(); }
void tm1640_end(void) { TM1640_PORT &= ~TM1640_DIN; bit_delay(); TM1640_PORT |= TM1640_SCLK; bit_delay(); TM1640_PORT |= TM1640_DIN; bit_delay(); }
/* uart_putc() -- blocking 8N1 transmit */ void uart_putc(uint8_t ch) { uint8_t bit; tx_pin(STARTBIT); bit_delay(); for (bit = 0; bit < 8; bit++) { tx_pin(ch & 0x1); ch = ch >> 1; bit_delay(); } tx_pin(STOPBIT); bit_delay(); }
void tm1640_send_byte(uint8_t v) { uint8_t b=0x01; /* start at LSB */ while(b) { if(v & b) TM1640_PORT |= TM1640_DIN; else TM1640_PORT &= ~TM1640_DIN; bit_delay(); TM1640_PORT |= TM1640_SCLK; bit_delay(); TM1640_PORT &= ~ TM1640_SCLK; b <<= 1; /* move from LSB to MSB */ } }
static int __getc(void) { uint8_t bit; uint8_t ch; half_bit_delay(); if (rx_pin() != STARTBIT) return -1; /* Glitch */ bit_delay(); for (bit = 0; bit < 8; bit++) { ch = ch >> 1; if (rx_pin()) ch |= 1 << 7; bit_delay(); } if (rx_pin() != STOPBIT) return -2; /* Frame error */ return (int) ch; }
void put_char(volatile unsigned char *port, unsigned char pin, char txchar) { // // send character in txchar on port pin // assumes line driver (inverts bits) // // start bit // clear(*port,pin); bit_delay(); // // unrolled loop to write data bits // if bit_test(txchar,0) set(*port,pin); else
void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) { // // read character into rxbyte on pins pin // assumes line driver (inverts bits) // *rxbyte = 0; while (pin_test(*pins,pin)) // // wait for start bit // ; // // delay to middle of first data bit // half_bit_delay(); bit_delay(); // // unrolled loop to read data bits // if pin_test(*pins,pin) *rxbyte |= (1 << 0); else