杰理AC692X---LCD1602显示屏驱动

闲话就不说了,直接上代码:

void LCD1602_init               (void);
void LCD_write_cmd          (unsigned char cmd);
void LCD_write_data         (unsigned char w_data);
void LCD_Write_half_byte    (unsigned char half_byte);
void LCD_set_xy             (unsigned char x, unsigned char y);
void LCD_write_string       (unsigned char X,unsigned char Y,unsigned char *s);

#define SET_RS()            PORTR_OUT(PORTR1,1);
#define CLR_RS()            PORTR_OUT(PORTR1,0);

#define SET_RW()            PORTR_OUT(PORTR2,1);
#define CLR_RW()            PORTR_OUT(PORTR2,0);

#define SET_EN()            PORTR_OUT(PORTR3,1);
#define CLR_EN()            PORTR_OUT(PORTR3,0);

#define DEUSER_JL_PORTA    JL_PORTA
#define DEUSER_JL_PORTC    JL_PORTC
#define L602_D4   BIT(13)
#define L602_D5   BIT(3)
#define L602_D6   BIT(4)
#define L602_D7   BIT(5)
#define L602_D4_HI()            DEUSER_JL_PORTA->OUT |= L602_D4;
#define L602_D4_LO()            DEUSER_JL_PORTA->OUT &= ~L602_D4;
#define L602_D5_HI()            DEUSER_JL_PORTC->OUT |= L602_D5;
#define L602_D5_LO()            DEUSER_JL_PORTC->OUT &= ~L602_D5;
#define L602_D6_HI()            DEUSER_JL_PORTC->OUT |= L602_D6;
#define L602_D6_LO()            DEUSER_JL_PORTC->OUT &= ~L602_D6;
#define L602_D7_HI()            DEUSER_JL_PORTC->OUT |= L602_D7;
#define L602_D7_LO()            DEUSER_JL_PORTC->OUT &= ~L602_D7;

#define DELAY_2N     0


static void delay_ms()
{
    //Timer2 for delay
    JL_TIMER2->CON = BIT(14);
    JL_TIMER2->PRD = 375;
    JL_TIMER2->CNT = 0;
    SFR(JL_TIMER2->CON, 2, 2, 2); //use osc
    SFR(JL_TIMER2->CON, 4, 4, 3); //div64
    SFR(JL_TIMER2->CON, 14, 1, 1); //clr pending
    SFR(JL_TIMER2->CON, 0, 2, 1); //work mode
    while (!(JL_TIMER2->CON & BIT(15)));
    JL_TIMER2->CON = BIT(14);
}
static void lcd_delay(u32 cnt)
{
	while(cnt--)
		delay_ms();
}

void LCD1602_init(void)
{
    PORTR_PU(PORTR1,1);PORTR_PD(PORTR1,0);PORTR_DIR(PORTR1,0);
    PORTR_PU(PORTR2,1);PORTR_PD(PORTR2,0);PORTR_DIR(PORTR2,0);
    PORTR_PU(PORTR3,1);PORTR_PD(PORTR3,0);PORTR_DIR(PORTR3,0);

    DEUSER_JL_PORTA->PU|= L602_D4;
    DEUSER_JL_PORTA->PD&= ~L602_D4;
    DEUSER_JL_PORTA->DIR &= ~L602_D4;
    DEUSER_JL_PORTC->PU|= L602_D5;
    DEUSER_JL_PORTC->PD&= ~L602_D5;
    DEUSER_JL_PORTC->DIR &= ~L602_D5;
    DEUSER_JL_PORTC->PU|= L602_D6;
    DEUSER_JL_PORTC->PD&= ~L602_D6;
    DEUSER_JL_PORTC->DIR &= ~L602_D6;
    DEUSER_JL_PORTC->PU|= L602_D7;
    DEUSER_JL_PORTC->PD&= ~L602_D7;
    DEUSER_JL_PORTC->DIR &= ~L602_D7;

    CLR_RW();

    lcd_delay (10);



    CLR_RS();
    LCD_Write_half_byte(0x03);
    lcd_delay (10);
    LCD_Write_half_byte(0x03);
    lcd_delay (10);
    LCD_Write_half_byte(0x03);
    LCD_Write_half_byte(0x02);


    LCD_write_cmd(0x28);
    lcd_delay (20);
    LCD_write_cmd(0x08);
    lcd_delay (20);
    LCD_write_cmd(0x01);
    lcd_delay (20);
    LCD_write_cmd(0x06);
    lcd_delay (20);
    LCD_write_cmd(0x0C);
   // LCD_write_cmd(0x0F);
    lcd_delay (20);
	LCD_write_cmd(0x01);
}

void LCD_write_cmd(unsigned char cmd)
{
    CLR_RS();
    LCD_Write_half_byte(cmd >> 4);
    LCD_Write_half_byte(cmd);
    lcd_delay (1);
}

void LCD_write_data(unsigned char w_data)
{
    SET_RS();
    LCD_Write_half_byte(w_data >> 4);
    LCD_Write_half_byte(w_data);
    lcd_delay (1);
}

void LCD_Write_half_byte(unsigned char half_byte)
{

	if (half_byte&0x01) {L602_D4_HI();}
	else {L602_D4_LO();}

	if (half_byte&0x02){L602_D5_HI();}
	else {L602_D5_LO();}

	if (half_byte&0x04){L602_D6_HI();}
	else {L602_D6_LO();}

	if (half_byte&0x08){L602_D7_HI();}
	else {L602_D7_LO();}

    SET_EN();
    lcd_delay(1);
    CLR_EN();
    //lcd_delay(1);
}


void LCD_set_xy( unsigned char x, unsigned char y )
{
    unsigned char address;
    if (y==1)
    {
        address=0x80-1+x;
    }
    else
    {
        address=0xc0-1+x;
    }
    LCD_write_cmd(address);
}

void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
    LCD_set_xy( X, Y );
    while (*s)
    {
        LCD_write_data(*s);
        s++;
    }
}

LCD1602一般数据线是8根线,由于项目只需要写数据,所以这里采用了4线驱动方式。

使用方法:

在board.c 的 board_main函数中调用 LCD1602_init 函数,然后再在你想显示数据的地方调用 LCD_write_string 函数显示数据。

注意传入的显示内容是u8*类型。

然后如果要做FM频率显示,播放时长之类的显示,可以直接套用LED7数码管的那一套,并且在 led7_drv.c中显示的数据都是已经经过u8*转换处理的了。可以直接使用。

下面是显示FM频率的demo :

void led7_show_fm_main(void)
{
    /*FM - Frequency*/
#if 1
    FM_MODE_VAR *fm_var;

    if (!UI_var.ui_buf_adr) {
        return;
    }

    fm_var = *(FM_MODE_VAR **)UI_var.ui_buf_adr;

    if (fm_var) {
        itoa4(fm_var->wFreq);

        if (fm_var->wFreq <= 999) {
            bcd_number[0] = '0';
        }
        LCD_write_string(1,0,(u8*)" Fre: ");
        LCD_write_string(7,0,(u8*)bcd_number);
        LCD_write_string(11,0,(u8*)" MHZ  ");
       // led7_show_string((u8 *)bcd_number);
       // LED_STATUS |= LED_MHZ;

#if  0 //REC_EN
        RECORD_OP_API *rec_var_p;
        REC_CTL *rec_ctl_var;

        if ((fm_var->fm_rec_op) && (*(RECORD_OP_API **)(fm_var->fm_rec_op))) {
            rec_var_p = *(RECORD_OP_API **)fm_var->fm_rec_op;
            rec_ctl_var = rec_var_p->rec_ctl;

            if ((rec_ctl_var) && (ENC_STOP != rec_ctl_var->enable)) {
                LED_STATUS &= ~(LED_PLAY | LED_PAUSE);
                LED_STATUS &= ~(LED_SD | LED_USB);

                /* led7_show_dev(); */
                led7_show_dev(rec_ctl_var->curr_device);
                if (ENC_STAR == rec_ctl_var->enable) {
                    LED_STATUS |= LED_PLAY;
                } else if (ENC_PAUS == rec_ctl_var->enable) {
                    LED_STATUS |= LED_PAUSE;
                }
            }

        }
#endif
    }

#endif
}

也可以自行做其他显示。

你可能感兴趣的:(AC692N,杰理AC692X学习记录)