yaoyutaoTom 发表于 2013-5-28 16:12:31

lpc1768 操作LCD12864 不成功 请指教

#include "stdint.h"
#include "lpc17xx_gpio.h"
#include "lcd12864.h"
#include "mytimer.h"

#define LCD_RST        (1<<18)//P1.18
#define LCD_CS1        (1<<25)//P1.25
#define LCD_CS2        (1<<26)//P1.26
#define LCD_RS        (1<<27)//P1.27
#define LCD_RW        (1<<28)//P1.28
#define LCD_EN        (1<<29)//P1.29
#define LCD_CTR_PORT        1
#define LCD_BUS_PORT        0
#define LCD_BUS        (0XFF)

volatile timer timer_12864;

static void write_cmd( uint8_t cmd );
static void write_data( uint8_t dat );


/*
static void delay( uint16_t ms )
{
        while( ms--);
}
*/
void delay( uint16_t ms )
{
        timer_12864.cnt = 0;
        timer_12864.dly = ms;
        timer_12864.stat = MY_ENABLE;
        timer_12864.mode = MY_ONE;
        timer_12864.alarm = NOALARM;
        while( timer_12864.alarm == NOALARM );
}

void lcd12864_init( void )
{       
        delay( 100 );       
        LPC_GPIO0->FIODIR0 |= 0XFF;       
        LPC_GPIO1->FIODIR |= (LCD_RST | LCD_CS1 | LCD_RS | LCD_RW | LCD_RS);       
       
        LPC_GPIO1->FIOCLR = LCD_RST;
        delay( 50 );       
        LPC_GPIO1->FIOSET = LCD_RST;       
        delay( 50 );       
        LPC_GPIO1->FIOSET = LCD_CS1;               
       
        write_cmd( 0x30 );
        delay( 50 );       
        write_cmd( 0x30 );
        delay( 50 );       
        write_cmd( 0x30 );
        delay( 50 );       
       
        write_cmd( 0x30 );       
        delay( 50 );       
        write_cmd( 0x08 );
        delay( 50 );       
        write_cmd( 0x01 );
        delay( 50 );       
        write_cmd( 0x06 );
        delay( 50 );       
        write_cmd( 0x0f );//显示开       
        delay( 50 );       
}

void lcd12864_clr()
{
        write_cmd( 0x01 );       
        write_cmd( 0x34 );       
        write_cmd( 0x30 );       
}

void set_cursor( uint8_t x, uint8_t y )
{
    unsigned char i;
    switch(x)//确定行号
    {
      case 0x00: i=0x80; break;//第一行
      case 0x01: i=0x90; break;//第二行
      case 0x02: i=0x88; break;//第三行
      case 0x03: i=0x98; break;//第四行
      default : break;
    }
    i = y+i;//确定列号
        write_cmd( 0x30 );
    write_cmd( i );//写地址
}



/***********************************************
val 为所要发送的数据
ctrl 取值范围为 CMD 表示发送命令 或 DATA 表示发送数据
************************************************/
static void write_cmd( uint8_t cmd )
{
        delay( 10 );
        GPIO_ClearValue( LCD_CTR_PORT, LCD_RW );
        GPIO_ClearValue( LCD_CTR_PORT, LCD_RS );                       
       
        LPC_GPIO0->FIOMASK0 = 0X00;
        LPC_GPIO0->FIOPIN0 = cmd;
        LPC_GPIO0->FIOMASK0 = 0XFF;
       
        delay( 1 );
        GPIO_SetValue( LCD_CTR_PORT, LCD_EN );
        delay( 1 );       
        GPIO_ClearValue( LCD_CTR_PORT, LCD_EN );       
        delay( 1 );       
}

static void write_data( uint8_t dat )
{
        delay( 10 );
        GPIO_ClearValue( LCD_CTR_PORT, LCD_RW );       
        GPIO_SetValue( LCD_CTR_PORT, LCD_RS );       
       
        LPC_GPIO0->FIOMASK0 = 0X00;
        LPC_GPIO0->FIOPIN0 = dat;
        LPC_GPIO0->FIOMASK0 = 0XFF;
        delay( 1 );
        GPIO_SetValue( LCD_CTR_PORT, LCD_EN );
        delay( 1 );       
        GPIO_ClearValue( LCD_CTR_PORT, LCD_EN );       
        delay( 1 );       
}

void lcd_show_string( uint8_t x, uint8_t y, uint8_t *str )
{
        set_cursor( x, y );
        while( *str != '\0' ) {
                write_data( *str );               
                str++;
        }       
}

/*
void lcd_show_chinese( uint8_t x, uint8_t y, uint8_t *chinese )
{
        set_cursor( x, y );
        while( *str != '\0' ) {
                write_data( *str );               
                str++;
        }       
}
*/

richyhuang 发表于 2013-5-28 17:22:42

1768是3.3v的,模块是5v的,电平转换了没啊

yaoyutaoTom 发表于 2013-6-9 08:53:20

12864 VO 需要直接从 VCC串电阻引入即可,无须再接地

穷折腾 发表于 2013-6-9 17:08:27

这片子初始化比较麻烦,又是时钟又是功率配置什么的,看看有没有初始化正常。
页: [1]
查看完整版本: lpc1768 操作LCD12864 不成功 请指教