nnn

Code Crtl & Display

17 May 2019

Beispiel für das LED Modul (nur einfach):

14b_SPI_MAX7219_led8x8 Simple demo to drive a 8x8-as LED matrix by a MAX7219 LED driver IC After initialisation two signs (X and o) are displayed alternatively. The MAX7219 IC is driven by hardware SPI: siehe auch https://os.mbed.com/teams/ST/wiki/SPI-output-clock-frequency

  1. include "mbed.h" DigitalIn button(USER_BUTTON); DigitalOut cs(PB_1); chip select of MCP3008 SPI spi(PB_15,PB_14,PB_13); mosi, miso, sclk

const unsigned char tabX[]= { 0b10000001, 0b01000010, 0b00100100, 0b00011000, 0b00011000, 0b00100100, 0b01000010, 0b10000001 }; X const unsigned char tabO[]= { 0b00000000, 0b00011000, 0b00100100, 0b01000010, 0b01000010, 0b00100100, 0b00011000, 0b00000000 }; o

/ Send two bytes to SPI bus void SPI_Write2(unsigned char MSB, unsigned char LSB) { cs = 0; Set CS Low spi.write(MSB); Send address spi.write(LSB); Send value cs = 1; Set CS High }

/ MAX7219 initialisation void Init_MAX7219(void) { SPI_Write2(0x09, 0x00); Decoding off SPI_Write2(0x0A, 0x08); Brightness to intermediate SPI_Write2(0x0B, 0x07); Scan limit = 7 SPI_Write2(0x0C, 0x01); Normal operation mode SPI_Write2(0x0F, 0x0F); Enable display test wait_ms(500); 500 ms delay SPI_Write2(0x01, 0x00); Clear row 0. SPI_Write2(0x02, 0x00); Clear row 1. SPI_Write2(0x03, 0x00); Clear row 2. SPI_Write2(0x04, 0x00); Clear row 3. SPI_Write2(0x05, 0x00); Clear row 4. SPI_Write2(0x06, 0x00); Clear row 5. SPI_Write2(0x07, 0x00); Clear row 6. SPI_Write2(0x08, 0x00); Clear row 7. SPI_Write2(0x0F, 0x00); Disable display test wait_ms(500); 500 ms delay }

int main() { cs = 1; CS initially High spi.format(8,0); 8-bit format, mode 0,0 spi.frequency(1000000); SCLK = 1 MHz Init_MAX7219(); Initialize the LED controller while (1) { if(button) { for(int adr=1; adr<9; adr++) Write first character (8 rows) SPI_Write2(adr,tabX[adr-1]); } else { for(int adr=1; adr<9; adr++) Write second character SPI_Write2(adr,tabO[adr-1]); } } }

Beispiel für Joystick Test

  1. include "mbed.h"

DigitalOut myLed(LED1); Serial pc(SERIAL_TX,SERIAL_RX); AnalogIn adcX(A0); AnalogIn adcY(A1);! DigitalIn ba(D2);! DigitalIn bb(D3); DigitalIn bc(D4);! DigitalIn bd(D5); DigitalIn be(D6); DigitalIn bf(D7);

int main() { while(1) { float x = adcX.read(); float y = adcY.read(); bool a = ba.read(); bool b = bb.read(); bool c = bc.read(); bool d = bd.read(); bool e = be.read(); bool f = bf.read(); pc.printf("%d ; %d ; %d ; %d ; %d ; %d ; %4.3f ; %4.3f\n",a,b,c,d,e,f,x,y); myLed = !myLed; wait(1.0); 1 sec } }