Tutoriales
Librería LCD Nokia 5110 para Raspberry Pi y otras plataformas - SPI por Bitbang - Raspberry Pi
- Detalles
- Categoría: Tutorial
- Publicado el Domingo, 01 Septiembre 2013 10:02
- Escrito por Santiago Villafuerte
- Visto: 44495
SPI por Bitbang - Raspberry Pi
Para la Raspberry Pi se usó la librería bcm2835.h de Mike McCauley. Básicamente se definen los pines como GPIO de salida.
C library for Broadcom BCM 2835 as used in Raspberry Pi
void Nokia_5110_Initialize_SPI(void)
{
if (!bcm2835_init())
{
return;
}
// set the direction of the pins
bcm2835_gpio_fsel(NOKIA_5110_CS , BCM2835_GPIO_FSEL_OUTP); // CE
bcm2835_gpio_fsel(NOKIA_5110_CLK , BCM2835_GPIO_FSEL_OUTP); // SCLK
bcm2835_gpio_fsel(NOKIA_5110_DATA, BCM2835_GPIO_FSEL_OUTP); // MOSI
bcm2835_gpio_fsel(NOKIA_5110_MODE, BCM2835_GPIO_FSEL_OUTP); // MODE
bcm2835_gpio_fsel(NOKIA_5110_RST , BCM2835_GPIO_FSEL_OUTP); // RST
// set the default state for the pins
bcm2835_gpio_write(NOKIA_5110_CS, HIGH);
bcm2835_gpio_write(NOKIA_5110_CLK, HIGH);
bcm2835_gpio_write(NOKIA_5110_DATA, HIGH);
bcm2835_gpio_write(NOKIA_5110_MODE, HIGH);
// reset the display
bcm2835_gpio_write(NOKIA_5110_RST, LOW);
bcm2835_delay(5);
bcm2835_gpio_write(NOKIA_5110_RST, HIGH);
}
void Nokia_5110_Write_Byte(uint8_t data, uint8_t lcd_mode)
{
uint8_t i;
// enable the lcd
bcm2835_gpio_write(NOKIA_5110_CS, LOW);
if (NOKIA_5110_MODE_CMD == lcd_mode)
bcm2835_gpio_write(NOKIA_5110_MODE, LOW);
else
bcm2835_gpio_write(NOKIA_5110_MODE, HIGH);
for (i = 0; i < 8; i++)
{
bcm2835_gpio_write(NOKIA_5110_DATA, data & 0x80);
data = data << 1;
// toggle the clock pin
bcm2835_gpio_write(NOKIA_5110_CLK, LOW);
bcm2835_gpio_write(NOKIA_5110_CLK, HIGH);
}
// disable the lcd
bcm2835_gpio_write(NOKIA_5110_CS, HIGH);
}
void Nokia_5110_Stop_SPI(void)
{
bcm2835_close();
}
SPI por hardware
Se usa el periférico SPI de la Pi directamente. Esto nos permite velocidades de transferencia más eficientes. Se estableció el reloj SPI en 2MHz (BCM2835_SPI_CLOCK_DIVIDER_128) ya que la pantalla permite velocidades de hasta 4MHz.
void Nokia_5110_Initialize_SPI(void)
{
if (!bcm2835_init())
{
return;
}
// set the direction of the pins
bcm2835_gpio_fsel(NOKIA_5110_MODE, BCM2835_GPIO_FSEL_OUTP); // MODE
bcm2835_gpio_fsel(NOKIA_5110_RST , BCM2835_GPIO_FSEL_OUTP); // RST
// set the default state for the pins
bcm2835_gpio_write(NOKIA_5110_MODE, HIGH);
// reset the display
bcm2835_gpio_write(NOKIA_5110_RST, LOW);
bcm2835_delay(5);
bcm2835_gpio_write(NOKIA_5110_RST, HIGH);
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); // 1.953125MHz
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
}
void Nokia_5110_Write_Byte(uint8_t data, uint8_t lcd_mode)
{
if (NOKIA_5110_MODE_CMD == lcd_mode)
bcm2835_gpio_write(NOKIA_5110_MODE, LOW);
else
bcm2835_gpio_write(NOKIA_5110_MODE, HIGH);
bcm2835_spi_transfer(data);
}
void Nokia_5110_Stop_SPI(void)
{
bcm2835_spi_end();
bcm2835_close();
}