void display_init(void) { SPI_set_sample_rising_edge(); /* Disable shutdown mode */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_SHUTDOWN); SPI_send(0x1); SPI_deselect(SPI_CS_MAX7221); /* Disable display test mode */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DISPLAY_TEST); SPI_send(0x0); SPI_deselect(SPI_CS_MAX7221); /* Set the decoding mode to Code-B font for all digits */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DECODE_MODE); SPI_send(0xFF); SPI_deselect(SPI_CS_MAX7221); /* Set the display intensity */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_INTENSITY); SPI_send(0xF); SPI_deselect(SPI_CS_MAX7221); /* Set the scan limit to include all 8 digits */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_SCAN_LIMIT); SPI_send(0x07); SPI_deselect(SPI_CS_MAX7221); }
/** Transforms the humidity value given in percent relative humidity into decimal digits and sends the information to the 7-segment display controller. The display is updated when the chip select signal is deactivated. \param humidity Integer that represents relative humidity in percent. */ void display_humidity(uint8_t humidity) { SPI_set_sample_rising_edge(); /* Values of each of the 3 digits */ uint8_t digit [3]; /* Determine the value of each digit */ int8_t tmp; tmp = humidity / 10; digit[2] = humidity - 10 * tmp; humidity = tmp; tmp = humidity / 10; digit[1] = humidity - 10 * tmp; humidity = tmp; tmp = humidity / 10; digit[0] = humidity - 10 * tmp; /* Eliminate leading zeros */ if (digit[0] == 0) { digit[0] = DISPLAY_CODE_B_BLANK; if (digit[1] == 0) { digit[1] = DISPLAY_CODE_B_BLANK; } } /* Send the SPI commands */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_5); SPI_send(digit[0]); SPI_deselect(SPI_CS_MAX7221); SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_6); SPI_send(digit[1]); SPI_deselect(SPI_CS_MAX7221); SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_7); SPI_send(digit[2]); SPI_deselect(SPI_CS_MAX7221); }
static BYTE send_cmd ( /* Returns command response (bit7==1:Send failed)*/ BYTE cmd, /* Command byte */ DWORD arg /* Argument */ ) { BYTE n, d, buf[6]; if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */ cmd &= 0x7F; n = send_cmd(CMD55, 0); if (n > 1) return n; } /* Select the card and wait for ready */ deselect(); if (!SPI_select()) return 0xFF; /* Send a command packet */ buf[0] = 0x40 | cmd; /* Start + Command index */ buf[1] = (BYTE)(arg >> 24); /* Argument[31..24] */ buf[2] = (BYTE)(arg >> 16); /* Argument[23..16] */ buf[3] = (BYTE)(arg >> 8); /* Argument[15..8] */ buf[4] = (BYTE)arg; /* Argument[7..0] */ n = 0x01; /* Dummy CRC + Stop */ if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */ if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */ buf[5] = n; xmit_mmc(buf, 6); /* Receive command response */ if (cmd == CMD12) rcvr_mmc(&d, 1); /* Skip a stuff byte when stop reading */ n = 10; /* Wait for a valid response in timeout of 10 attempts */ do rcvr_mmc(&d, 1); while ((d & 0x80) && --n); return d; /* Return with the response value */ }
// Initializes mem card char MEM_init() { char val; mem_bus_granted = 0; // acquire the SPI bus and other necesary signals val = MEM_acquireControl(); if(val) return(val); // reset to known idle state // assert reset, wait and deassert reset ClearPin(PORTC, MEM_RESET); delay(1); SetPin(PORTC, MEM_RESET); // get memory status // select memory SPI_select(MEMCS); // read status register SPI_send(READ_STATUS_REGISTER); // get return value val = SPI_receive(0x00); SPI_deselect(); if(((val >> 2) & 0x0f) == 0x0f) { if(!(val & 0x01)) { MEM.mem_size = 8650752; MEM.page_size = 1056; } else { blinkLED(10); } MEM.fbell_offset = MEM.page_size; MEM.rbell_offset = MEM.mem_size / 2; } else
/** Transforms the given temperature that is represented as degrees Celsius times 100 into decimal digits in preparation for display. Then it transfers the information to the 7-segment display controller. The display is updated as soon as the chip select signal is deactivated. \param temperature Integer that represents temperature in Celsius multiplied by the factor 100. */ void display_temperature(int16_t temperature) { SPI_set_sample_rising_edge(); /* Values of each of the 5 digits */ uint8_t digit [5]; /* Test for negativity */ if (temperature < 0) { temperature = -temperature; digit[0] = DISPLAY_CODE_B_MINUS; } else { digit[0] = DISPLAY_CODE_B_BLANK; } /* Determine the value of each digit */ int16_t tmp; tmp = temperature / 10; digit[4] = temperature - 10 * tmp; temperature = tmp; tmp = temperature / 10; digit[3] = temperature - 10 * tmp; temperature = tmp; tmp = temperature / 10; digit[2] = temperature - 10 * tmp; digit[2] |= DISPLAY_CODE_DECIMAL_POINT; temperature = tmp; tmp = temperature / 10; digit[1] = temperature - 10 * tmp; /* Eliminate leading zeros */ if (digit[1] == 0) { /* The 0th digit may contain a minus sign */ digit[1] = digit[0]; digit[0] = DISPLAY_CODE_B_BLANK; } /* Send the SPI commands for digit 0 */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_0); SPI_send(digit[0]); SPI_deselect(SPI_CS_MAX7221); /* Send the SPI commands for digit 1 */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_1); SPI_send(digit[1]); SPI_deselect(SPI_CS_MAX7221); /* Send the SPI commands for digit 2 */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_2); SPI_send(digit[2]); SPI_deselect(SPI_CS_MAX7221); /* Send the SPI commands for digit 3 */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_3); SPI_send(digit[3]); SPI_deselect(SPI_CS_MAX7221); /* Send the SPI commands for digit 4 */ SPI_select(SPI_CS_MAX7221); SPI_send(DISPLAY_REG_DIGIT_4); SPI_send(digit[4]); SPI_deselect(SPI_CS_MAX7221); }