/** @brief Enable SPI hardware and reset the connected device. This function leaves the target in reset mode. Use SET_RST() to un-reset the target device. @param clockOption Clock option for spi clock speed @param maintainReset True to maintain target reset state @return actual set clock option: this may differ from the option passed in if the passed in value is invalid or if the slow clock jumper is connected */ uchar SPI_Connect(uchar clockOption, uchar maintainReset) { clockOption = SPI_SetSCKOption(clockOption); /** Turn on the SPI pins */ EnableSPI(); /** Reset the device, and maintain reset */ SET_RST(); CLR_RST(); clockWait(1); SET_RST(); /** Put the device back into reset mode if required */ if (maintainReset) { clockWait(1); CLR_RST(); } /** Enable the SPI Hardware (if needed) */ if (sw_delay_time == 0) spiHWenable(); return clockOption; }
uchar SPI_EnterProgrammingMode() { uchar check; uchar count = 3; // Try to connect 3 times for (; count > 0; count--) { SPI_Transmit(0xAC); SPI_Transmit(0x53); check = SPI_Transmit(0); SPI_Transmit(0); if (check == 0x53) { return 0; // Success! We're connected an in programming mode! } // If the connect command failed, // the communication may be out of sync. // Pulse reset (as per the datasheet) and // try again. SET_RST(); // Device into normal operation clockWait(1); CLR_RST(); // Device back into reset mode clockWait(63); // Wait 20 ms before sending another program enable command } return 1; /* error: device dosn't answer */ }
void ssd1306_init(void) { uint32_t i; // init necessary io // enable clocks for PORTA & PORTB SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK; // pin 13, B5 output for rst PORTB_PCR5 = PORT_PCR_MUX(1); // gpio GPIOB_PCOR = (1 << 5); // low initially GPIOB_PDDR |= (1 << 5); // output mode // pin 6, a6 output for dc PORTA_PCR6 = PORT_PCR_MUX(1); // gpio GPIOA_PCOR = (1 << 6); // low initially GPIOA_PDDR |= (1 << 6); // output mode // init spi0 // configure io pins for spi // PTA5, 7, PTB0 PORTA_PCR5 = PORT_PCR_MUX(3); // CS PORTA_PCR7 = PORT_PCR_MUX(3); // MOSI PORTB_PCR0 = PORT_PCR_MUX(3); // SCK // enable SPI0 module SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK; // configure as master, cs output driven automatically, CPOL=1 SPI0_C1 |= (SPI_C1_MSTR_MASK | SPI_C1_SSOE_MASK | SPI_C1_CPOL_MASK); SPI0_C2 |= SPI_C2_MODFEN_MASK; // select clock divider- SPPR = 0, SPR = 0 (2) SPI0_BR = SPI_BR_SPPR(0) | SPI_BR_SPR(3); // turn on spi SPI0_C1 |= SPI_C1_SPE_MASK; // do some setup on the oled display // wait a ms after 3.3v comes up on reset delay_ms(10); // reset low for 10ms CLR_RST(); // low delay_ms(10); SET_RST(); // now high // give it a few ms after reset goes high delay_ms(5); // now initialize display controller // Init sequence for 128x64 OLED module ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 ssd1306_command(0x80); // the suggested ratio 0x80 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 ssd1306_command(0x3F); ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 ssd1306_command(0x0); // no offset ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D ssd1306_command(0x14); // internall VCC ssd1306_command(SSD1306_MEMORYMODE); // 0x20 ssd1306_command(0x00); // 0x0 act like ks0108 ssd1306_command(SSD1306_SEGREMAP /*| 0x1*/); ssd1306_command(SSD1306_COMSCANINC); ssd1306_command(SSD1306_SETCOMPINS); // 0xDA ssd1306_command(0x12); ssd1306_command(SSD1306_SETCONTRAST); // 0x81 ssd1306_command(0xCF); // internal VCC ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 ssd1306_command(0xF1); // internal VCC ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB ssd1306_command(0x40); ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel // set display to noahs face ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0 ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0 ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 for(i=0; i<SSD1306_SIZEOF_SCREENBUF; i++){ ssd1306_data(noahs_face[i]); } }
void initLcd(void) { CLR_RST(); SET_CS(); SET_DC(); delay_ms(5); SET_RST(); delay_ms(5); CLR_RST(); delay_ms(20); SET_RST(); delay_ms(150); _width = ILI9341_TFTWIDTH; _height = ILI9341_TFTHEIGHT; writeCommand(0xEF); writeData(0x03); writeData(0x80); writeData(0x02); writeCommand(0xCF); writeData(0x00); writeData(0XC1); writeData(0X30); writeCommand(0xED); writeData(0x64); writeData(0x03); writeData(0X12); writeData(0X81); writeCommand(0xE8); writeData(0x85); writeData(0x00); writeData(0x78); writeCommand(0xCB); writeData(0x39); writeData(0x2C); writeData(0x00); writeData(0x34); writeData(0x02); writeCommand(0xF7); writeData(0x20); writeCommand(0xEA); writeData(0x00); writeData(0x00); writeCommand(ILI9341_PWCTR1); //Power control writeData(0x23); //VRH[5:0] writeCommand(ILI9341_PWCTR2); //Power control writeData(0x10); //SAP[2:0];BT[3:0] writeCommand(ILI9341_VMCTR1); //VCM control writeData(0x3e); writeData(0x28); writeCommand(ILI9341_VMCTR2); //VCM control2 writeData(0x86); //-- writeCommand(ILI9341_MADCTL); // Memory Access Control writeData(ILI9341_MADCTL_MX | ILI9341_MADCTL_BGR); writeCommand(ILI9341_PIXFMT); writeData(0x55); writeCommand(ILI9341_FRMCTR1); writeData(0x00); writeData(0x18); writeCommand(ILI9341_DFUNCTR); // Display Function Control writeData(0x08); writeData(0x82); writeData(0x27); writeCommand(0xF2); // 3Gamma Function Disable writeData(0x00); writeCommand(ILI9341_GAMMASET); //Gamma curve selected writeData(0x01); writeCommand(ILI9341_GMCTRP1); //Set Gamma writeData(0x0F); writeData(0x31); writeData(0x2B); writeData(0x0C); writeData(0x0E); writeData(0x08); writeData(0x4E); writeData(0xF1); writeData(0x37); writeData(0x07); writeData(0x10); writeData(0x03); writeData(0x0E); writeData(0x09); writeData(0x00); writeCommand(ILI9341_GMCTRN1); //Set Gamma writeData(0x00); writeData(0x0E); writeData(0x14); writeData(0x03); writeData(0x11); writeData(0x07); writeData(0x31); writeData(0xC1); writeData(0x48); writeData(0x08); writeData(0x0F); writeData(0x0C); writeData(0x31); writeData(0x36); writeData(0x0F); writeCommand(ILI9341_SLPOUT); //Exit Sleep delay_ms(120); writeCommand(ILI9341_DISPON); //Display on }