__attribute__((always_inline)) __INLINE void lcd_wrcmd8(uint32_t cmd) { GPIO_CLRPIN(LCD_PORT, RS_PIN); //cmd GPIO_CLRPIN(LCD_PORT, WR_PIN); DATA_WR(cmd); GPIO_SETPIN(LCD_PORT, WR_PIN); GPIO_SETPIN(LCD_PORT, RS_PIN); //data return; }
__attribute__((always_inline)) __INLINE void lcd_wrdata16(uint32_t data) { //GPIO_SETPIN(LCD_PORT, RS_PIN); //data GPIO_CLRPIN(LCD_PORT, WR_PIN); DATA_WR(data>>8); GPIO_SETPIN(LCD_PORT, WR_PIN); //NOP(); GPIO_CLRPIN(LCD_PORT, WR_PIN); DATA_WR(data); GPIO_SETPIN(LCD_PORT, WR_PIN); return; }
__attribute__((always_inline)) __INLINE void lcd_wrdata8(uint_least8_t data) { //GPIO_SETPIN(LCD_PORT, RS_PIN); //data GPIO_CLRPIN(LCD_PORT, WR_PIN); DATA_WR(data); GPIO_SETPIN(LCD_PORT, WR_PIN); return; }
void ldr_init(void) { //set AD4 to output and low IOCON_SETRPIN(ADC_PORT, ADC_4, IOCON_R_PIO | IOCON_NOPULL | IOCON_DIGITAL); GPIO_CLRPIN(ADC_PORT, ADC_4); GPIO_PORT(ADC_PORT)->DIR |= (1<<ADC_4); //set AD5 to analog IOCON_SETPIN(ADC_PORT, ADC_5, IOCON_ADC | IOCON_NOPULL | IOCON_ANALOG); return; }
void io_clr(uint_least8_t source) { status &= ~source; if(features & FEATURE_IRQ) { if(io_config == 0) { GPIO_SETPIN(IO_PORT, IO_PIN); } else { GPIO_CLRPIN(IO_PORT, IO_PIN); } } return; }
uint_least8_t ldr_service(uint_least8_t power_max) { uint_least16_t i, j; static uint_least8_t last=0; //set AD4 to high GPIO_SETPIN(ADC_PORT, ADC_4); //read AD5 //IOCON_SETPIN(ADC_PORT, ADC_5, IOCON_ADC | IOCON_NOPULL | IOCON_ANALOG); ADC_READ(5, i); ADC_READ(5, j); //IOCON_SETPIN(ADC_PORT, ADC_5, IOCON_PIO | IOCON_NOPULL | IOCON_DIGITAL); //set AD4 to low GPIO_CLRPIN(ADC_PORT, ADC_4); if(i == j) { if(i > 800) i = 100; else if(i > 100) i /= 8; else i = 10; last += i; //add to last value last /= 2; //div by 2 (do average) if(last > power_max) { last = power_max; } set_pwm(last); } else if(last == 0) { last = power_max; set_pwm(last); } return last; }
uint_least16_t adc_read(uint_least8_t chn) { uint_least16_t i=0; switch(chn) { case 4: IOCON_SETRPIN(ADC_PORT, ADC_4, IOCON_R_ADC | IOCON_NOPULL | IOCON_ANALOG); ADC_READ(4, i); IOCON_SETRPIN(ADC_PORT, ADC_4, IOCON_R_PIO | IOCON_PULLUP | IOCON_DIGITAL); break; case 5: IOCON_SETPIN(ADC_PORT, ADC_5, IOCON_ADC | IOCON_NOPULL | IOCON_ANALOG); ADC_READ(5, i); IOCON_SETPIN(ADC_PORT, ADC_5, IOCON_PIO | IOCON_PULLUP | IOCON_DIGITAL); break; case 7: IOCON_SETPIN(ADC_PORT, ADC_7, IOCON_ADC | IOCON_NOPULL | IOCON_ANALOG); ADC_READ(7, i); IOCON_SETPIN(ADC_PORT, ADC_7, IOCON_PIO | IOCON_PULLUP | IOCON_DIGITAL); break; case 255: //set AD4 to output and high IOCON_SETRPIN(ADC_PORT, ADC_4, IOCON_R_PIO | IOCON_NOPULL | IOCON_DIGITAL); GPIO_SETPIN(ADC_PORT, ADC_4); GPIO_PORT(ADC_PORT)->DIR |= (1<<ADC_4); //read AD5 IOCON_SETPIN(ADC_PORT, ADC_5, IOCON_ADC | IOCON_NOPULL | IOCON_ANALOG); ADC_READ(5, i); IOCON_SETPIN(ADC_PORT, ADC_5, IOCON_PIO | IOCON_NOPULL | IOCON_DIGITAL); //set AD4 to low GPIO_CLRPIN(ADC_PORT, ADC_4); break; } return i; }
__attribute__((always_inline)) __INLINE void lcd_enable(void) { GPIO_CLRPIN(LCD_PORT, CS_PIN); return; }
void lcd_reset(void) { uint32_t c, i, j; uint8_t initdata[] = { //0x40| 1, LCD_CMD_RESET, //0xC0|60, //0xC0|60, 0x40| 1, LCD_CMD_DISPLAY_OFF, 0xC0|20, 0x40| 1, LCD_CMD_POWER_CTRLB, 0x80| 3, 0x00, 0x83, 0x30, //0x83 0x81 0xAA 0x40| 1, LCD_CMD_POWERON_SEQ_CTRL, 0x80| 4, 0x64, 0x03, 0x12, 0x81, //0x64 0x67 0x40| 1, LCD_CMD_DRV_TIMING_CTRLA, 0x80| 3, 0x85, 0x01, 0x79, //0x79 0x78 0x40| 1, LCD_CMD_POWER_CTRLA, 0x80| 5, 0x39, 0X2C, 0x00, 0x34, 0x02, 0x40| 1, LCD_CMD_PUMP_RATIO_CTRL, 0x80| 1, 0x20, 0x40| 1, LCD_CMD_DRV_TIMING_CTRLB, 0x80| 2, 0x00, 0x00, 0x40| 1, LCD_CMD_POWER_CTRL1, 0x80| 1, 0x26, //0x26 0x25 0x40| 1, LCD_CMD_POWER_CTRL2, 0x80| 1, 0x11, 0x40| 1, LCD_CMD_VCOM_CTRL1, 0x80| 2, 0x35, 0x3E, 0x40| 1, LCD_CMD_VCOM_CTRL2, 0x80| 1, 0xBE, //0xBE 0x94 0x40| 1, LCD_CMD_FRAME_CTRL, 0x80| 2, 0x00, 0x1B, //0x1B 0x70 0x40| 1, LCD_CMD_ENABLE_3G, 0x80| 1, 0x08, //0x08 0x00 0x40| 1, LCD_CMD_GAMMA, 0x80| 1, 0x01, //G2.2 0x40| 1, LCD_CMD_POS_GAMMA, 0x80|15, 0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0x87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00, //0x80|15, 0x0F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0x87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00, 0x40| 1, LCD_CMD_NEG_GAMMA, 0x80|15, 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F, //0x80|15, 0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x0F, 0x40| 1, LCD_CMD_DISPLAY_CTRL, 0x80| 4, 0x0A, 0x82, 0x27, 0x00, 0x40| 1, LCD_CMD_ENTRY_MODE, 0x80| 1, 0x07, 0x40| 1, LCD_CMD_PIXEL_FORMAT, 0x80| 1, 0x55, //16bit 0x40| 1, LCD_CMD_MEMACCESS_CTRL, 0x80| 1, (1<<MEM_BGR) | (1<<MEM_X) | (1<<MEM_Y), 0x40| 1, LCD_CMD_COLUMN, 0x80| 2, 0x00, 0x00, 0x80| 2, ((LCD_HEIGHT-1)>>8)&0xFF, (LCD_HEIGHT-1)&0xFF, 0x40| 1, LCD_CMD_PAGE, 0x80| 2, 0x00, 0x00, 0x80| 2, ((LCD_WIDTH-1)>>8)&0xFF, (LCD_WIDTH-1)&0xFF, 0x40| 1, LCD_CMD_SLEEPOUT, 0xC0|60, 0xC0|60, 0x40| 1, LCD_CMD_DISPLAY_ON, 0xC0|20, }; //init pins INIT_PINS(); //hardware reset GPIO_CLRPIN(LCD_PORT, RST_PIN); delay_ms(20); GPIO_SETPIN(LCD_PORT, RST_PIN); delay_ms(120); lcd_enable(); //send init commands and data for(i=0; i<sizeof(initdata);) { c = initdata[i++]; switch(c&0xC0) { case 0x40: //command for(j=c&0x3F; j!=0; j--) { c = initdata[i++]; lcd_wrcmd8(c); } break; case 0x80: //data for(j=c&0x3F; j!=0; j--) { c = initdata[i++]; lcd_wrdata8(c); } break; case 0xC0: //delay delay_ms(c&0x3F); break; } } //clear display buffer lcd_drawstart(); for(i=(LCD_WIDTH*LCD_HEIGHT); i!=0; i--) { lcd_draw(0); } lcd_drawstop(); lcd_disable(); return; }
void cmd_lcd_test(uint_least16_t fgcolor, uint_least16_t bgcolor) { uint_least8_t c=1, f_save=features; char tmp[32]; #ifdef TP_SUPPORT uint_least16_t x, y, z, last_x=0, last_y=0; uint_least32_t ms=0; tp_init(); ldr_init(); features = FEATURE_TP | FEATURE_LDR; //FEATURE_TP | FEATURE_LDR #else uint_least8_t sw; int_least8_t pos=0, hpos=0, vpos=0; enc_init(); features = FEATURE_ENC; #endif lcd_fillrect(0, 0, (LCD_WIDTH-1)/3, LCD_HEIGHT-1, RGB(255,0,0)); lcd_fillrect((LCD_WIDTH-1)/3, 0, ((LCD_WIDTH-1)/3)*2, LCD_HEIGHT-1, RGB(0,255,0)); lcd_fillrect(((LCD_WIDTH-1)/3)*2, 0, LCD_WIDTH-1, LCD_HEIGHT-1, RGB(0,0,255)); /* delay_ms(1500); lcd_clear(bgcolor); lcd_setorientation( 0); lcd_drawrect(10, 20, 40, 40, RGB(200, 0, 0)); lcd_drawtext(15, 25, "0 ", 0, RGB(200, 0, 0), 0, 0); lcd_setorientation( 90); lcd_drawrect(10, 20, 40, 40, RGB( 0,200, 0)); lcd_drawtext(15, 25, "90 ", 0, RGB( 0,200, 0), 0, 0); lcd_setorientation(180); lcd_drawrect(10, 20, 40, 40, RGB( 0, 0,200)); lcd_drawtext(15, 25, "180", 0, RGB( 0, 0,200), 0, 0); lcd_setorientation(270); lcd_drawrect(10, 20, 40, 40, RGB(200, 0,200)); lcd_drawtext(15, 25, "270", 0, RGB(200, 0,200), 0, 0); lcd_setorientation(0); lcd_drawline(0, LCD_WIDTH/4*1, LCD_WIDTH-1, LCD_WIDTH/4*1, RGB(120,120,120)); lcd_drawline(0, LCD_WIDTH/4*2, LCD_WIDTH-1, LCD_WIDTH/4*2, RGB(120,120,120)); lcd_drawline(0, LCD_WIDTH/4*3, LCD_WIDTH-1, LCD_WIDTH/4*3, RGB(120,120,120)); lcd_drawline(LCD_WIDTH/4*1, 0, LCD_WIDTH/4*1, LCD_HEIGHT-1, RGB(120,120,120)); lcd_drawline(LCD_WIDTH/4*2, 0, LCD_WIDTH/4*2, LCD_HEIGHT-1, RGB(120,120,120)); lcd_drawline(LCD_WIDTH/4*3, 0, LCD_WIDTH/4*3, LCD_HEIGHT-1, RGB(120,120,120)); lcd_drawcircle(LCD_WIDTH/2, LCD_HEIGHT/2, 40, RGB(120,120,120)); */ lcd_drawtext(LCD_CENTER, LCD_HEIGHT/2-5, "v"VERSION, 0, 0, 0, 0); lcd_drawtext(LCD_CENTER, LCD_HEIGHT/2+5, "("__DATE__")", 0, 0, 0, 0); lcd_drawtext(LCD_CENTER, LCD_HEIGHT-10, "watterott.com", 1, 0, 0, 0); do { #ifdef TP_SUPPORT tp_read(); z = tp_getz(); if(z) { x = tp_getx(); y = tp_gety(); if((x!=last_x) || (y!=last_y)) { last_x = x; last_y = y; lcd_fillcircle(x, y, 4, fgcolor); //lcd_drawpixel(x, y); sprintf(tmp, "X%03i Y%03i Z%03i", x, x, z); lcd_drawtext(5, 5, tmp, 0, fgcolor, bgcolor, 1); } GPIO_SETPIN(LED_PORT, LED_PIN); //LED on } else { GPIO_CLRPIN(LED_PORT, LED_PIN); //LED off } if(features & FEATURE_LDR) { if((get_ms() - ms) >= 100) { ms = get_ms(); x = ldr_service(100); sprintf(tmp, "LDR %03i", x); lcd_drawtext(5, 15, tmp, 0, fgcolor, bgcolor, 1); } } #else //TP_SUPPORT pos += enc_getdelta(); hpos += nav_gethdelta(); vpos += nav_getvdelta(); sprintf(tmp, "P%03i H%03i V%03i", pos, hpos, vpos); lcd_drawtext(5, 5, tmp, 0, fgcolor, bgcolor, 1); sw = enc_getsw(); sw |= nav_getsw(); if(sw) { GPIO_SETPIN(LED_PORT, LED_PIN); //LED on if(sw & 0x02) { if(features == FEATURE_ENC) { sprintf(tmp, "NAV"); nav_init(); features = FEATURE_NAV; } else //if(features == FEATURE_NAV) { sprintf(tmp, "ENC"); enc_init(); features = FEATURE_ENC; } lcd_drawtext(5, 25, tmp, 0, fgcolor, bgcolor, 1); delay_ms(500); while(enc_getsw() || nav_getsw()); } else if(sw & 0x01) { lcd_drawtext(5, 15, "press", 0, fgcolor, bgcolor, 1); } } else { GPIO_CLRPIN(LED_PORT, LED_PIN); //LED off } #endif if(if_available()) { c = if_read8(); } }while(c != 0); GPIO_CLRPIN(LED_PORT, LED_PIN); //LED off lcd_clear(bgcolor); features = f_save; return; }
void init(void) { //disable interrupts DISABLE_IRQ(); //init BOD LPC_SYSCON->BODCTRL = (2<<0)|(1<<4); //2.35V - 2.43V //set power down config LPC_SYSCON->PDRUNCFG &= ~(1<<4); //enable ADC //init system AHB LPC_SYSCON->SYSAHBCLKDIV = 0x00000001; //clock divider #define AHB_WDT (1<<15) #define AHB_CT16B0 (1<< 7) #define AHB_CT16B1 (1<< 8) #define AHB_CT32B0 (1<< 9) #define AHB_CT32B1 (1<<10) #define AHB_GPIO (1<< 6) #define AHB_IOCON (1<<16) #define AHB_SSP0 (1<<11) #define AHB_SSP1 (1<<18) #define AHB_I2C (1<< 5) #define AHB_UART (1<<12) #define AHB_ADC (1<<13) LPC_SYSCON->SYSAHBCLKCTRL = 0x1F | AHB_CT16B1 | AHB_GPIO | AHB_IOCON | AHB_ADC; //CT16 = PWM for backlight //init pins GPIO_PORT(SS_PORT)->DIR &= (1<<SS_PIN); IOCON_SETPIN(SS_PORT, SS_PIN, IOCON_PULLUP); IOCON_SETPIN(UART_PORT, RX_PIN, IOCON_PULLUP); IOCON_SETPIN(SPI_PORT, MOSI_PIN, IOCON_PULLUP); GPIO_PORT(IO_PORT)->DIR &= (1<<IO_PIN); IOCON_SETPIN(IO_PORT, IO_PIN, IOCON_PULLUP); GPIO_PORT(LED_PORT)->DIR |= (1<<LED_PIN); GPIO_CLRPIN(LED_PORT, LED_PIN); GPIO_PORT(PWM_PORT)->DIR |= (1<<PWM_PIN); GPIO_CLRPIN(PWM_PORT, PWM_PIN); //init PWM for backlight // LPC_IOCON->PIO1_9 = (0x1<<0); //PIO1_9/CT16B1MAT0 -> PWM (set in set_pwm()) LPC_TMR16B1->TC = 0; LPC_TMR16B1->PR = 0; //no prescale LPC_TMR16B1->PC = 0; LPC_TMR16B1->CTCR = 0; LPC_TMR16B1->MCR = 0; LPC_TMR16B1->MR0 = ~((0xFFFF*0)/100); //0% LPC_TMR16B1->PWMC = (1<<0); //PWM chn 0 on LPC_TMR16B1->EMR = (1<<0)|(2<<4); //enable PIO1_9/CT16B1MAT0 LPC_TMR16B1->TCR = (1<<0); //enable timer //init adc LPC_ADC->CR = (((sysclock(0)/4000000UL)-1)<< 8) | //4MHz (0<<16) | //burst off (0x0<<17) | //10bit (0x0<<24); //stop //init systick timer ms_ticks = 0; SysTick_Config(sysclock(0) / 1000); //1000 Hz //enable interrupts ENABLE_IRQ(); return; }