打印
[活动专区]

【AT-START-F405测评】5.SPI驱动LCD+RTC实现电子时钟显示屏

[复制链接]
1242|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 yuyy1989 于 2024-4-29 20:52 编辑

要驱动的LCD是这块

用到的IO口有5个,CS A0 RST MOSI SCK,其中CS A0 RST使用GPIO_output模式就行,使用AT32 Work Bench配置SPI

使用的GPIO

spi发送方法和LCD初始化方法
YUYY_HS12864G18B_DEV_Type lcd_dev;
void lcd_spiwritebyte(spi_type *spix,uint8_t byte)
{
    while(spi_i2s_flag_get(spix, SPI_I2S_TDBE_FLAG) == RESET);
    spi_i2s_data_transmit(spix,byte);
}
void lcd_init()
{
    lcd_dev.cs.gpio = LCD_CS_GPIO_PORT;
    lcd_dev.cs.pin = LCD_CS_PIN;
    lcd_dev.a0.gpio = LCD_A0_GPIO_PORT;
    lcd_dev.a0.pin = LCD_A0_PIN;
    lcd_dev.rst.gpio = LCD_RST_GPIO_PORT;
    lcd_dev.rst.pin = LCD_RST_PIN;
    lcd_dev.spix = SPI1;
    lcd_dev.spi_sendbyte_func = (YUYY_HS12864G18B_SpiWriteByteFunc_Type)lcd_spiwritebyte;
   
    YUYY_HS12864G18B_Init(&lcd_dev);
    YUYY_HS12864G18B_ClearScreen(&lcd_dev);
    YUYY_HS12864G18B_DisplayString8x16(&lcd_dev,0,0,0," AT32F405 TEST");
    YUYY_HS12864G18B_DisplayString8x16(&lcd_dev,0,2,0,"   LCD use SPI");
    YUYY_HS12864G18B_DisplayString8x16(&lcd_dev,0,4,0,"  bbs.21ic.com");
    YUYY_HS12864G18B_DisplayString8x16(&lcd_dev,0,6,0,"Code by yuyy1989");
    YUYY_HS12864G18B_DisplayFinish(&lcd_dev);
}
LCD驱动代码
void YUYY_HS12864G18B_writebyte(YUYY_HS12864G18B_DEV_Type *hs12864_dev,uint8_t dat)
{
    if(hs12864_dev->spix)
        hs12864_dev->spi_sendbyte_func(hs12864_dev->spix, dat);
}

void YUYY_HS12864G18B_cs(YUYY_HS12864G18B_DEV_Type *hs12864_dev,YUYY_GPIO_LEV_TYPE lev)
{
    hs12864_dev->delayus_func(5);
    hs12864_dev->cs_setlev_func(&(hs12864_dev->cs),lev);
    hs12864_dev->delayus_func(5);
}
void YUYY_HS12864G18B_rst(YUYY_HS12864G18B_DEV_Type *hs12864_dev,YUYY_GPIO_LEV_TYPE lev)
{
    hs12864_dev->rst_setlev_func(&(hs12864_dev->rst),lev);
}
void YUYY_HS12864G18B_a0(YUYY_HS12864G18B_DEV_Type *hs12864_dev,YUYY_GPIO_LEV_TYPE lev)
{
    hs12864_dev->a0_setlev_func(&(hs12864_dev->a0),lev);
}

/*
* com 0指令 1数据
*/
void YUYY_HS12864G18B_writedata(YUYY_HS12864G18B_DEV_Type *hs12864_dev,YUYY_GPIO_LEV_TYPE com,uint8_t dat)
{
    YUYY_HS12864G18B_cs(hs12864_dev,YUYY_GPIO_LEV0);
    YUYY_HS12864G18B_a0(hs12864_dev,com);
    YUYY_HS12864G18B_writebyte(hs12864_dev,dat);
}

void YUYY_HS12864G18B_Init(YUYY_HS12864G18B_DEV_Type *hs12864_dev)
{
    if(!hs12864_dev->a0_setlev_func)
        hs12864_dev->a0_setlev_func = YUYY_GPIO_SetLev;
    if(!hs12864_dev->cs_setlev_func)
        hs12864_dev->cs_setlev_func = YUYY_GPIO_SetLev;
    if(!hs12864_dev->mo_setlev_func)
        hs12864_dev->mo_setlev_func = YUYY_GPIO_SetLev;
    if(!hs12864_dev->rst_setlev_func)
        hs12864_dev->rst_setlev_func = YUYY_GPIO_SetLev;
    if(!hs12864_dev->sck_setlev_func)
        hs12864_dev->sck_setlev_func = YUYY_GPIO_SetLev;
    if(!hs12864_dev->delayus_func)
        hs12864_dev->delayus_func = (YUYY_DELAY_Func_Type)YUYY_DelayUs;
        
    YUYY_HS12864G18B_cs(hs12864_dev,YUYY_GPIO_LEV0);
    YUYY_HS12864G18B_rst(hs12864_dev,YUYY_GPIO_LEV0);/*低电平复位*/
    hs12864_dev->delayus_func(20);
    YUYY_HS12864G18B_rst(hs12864_dev,YUYY_GPIO_LEV1);/*复位完毕*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0xE2);/*软复位*/
    hs12864_dev->delayus_func(50);
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0x2C); /*升压步聚 1*/
    hs12864_dev->delayus_func(50);
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0x2E); /*升压步聚 2*/
    hs12864_dev->delayus_func(50);
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0x2F); /*升压步聚 3*/
    hs12864_dev->delayus_func(50);
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0x24); /*0x24粗调对比度,可设置范围0x20~0x27*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0x81); /*微调对比度*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0x08); /*0x28,微调对比度的值,可设置范围0x00~0x3f  1f*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0xA2); /*1/9偏压比(bias)*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0xC8); /*行扫描顺序:从上到下*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0xA0); /*列扫描顺序:从左到右*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0x40); /*起始行:第一行开始*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0xAF); /*开显示*/
    YUYY_HS12864G18B_cs(hs12864_dev,YUYY_GPIO_LEV1);
}

void YUYY_HS12864G18B_address(YUYY_HS12864G18B_DEV_Type *hs12864_dev,uint8_t page,uint8_t column)
{
    YUYY_HS12864G18B_cs(hs12864_dev,YUYY_GPIO_LEV0);
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,0xB0+page); //设置页地址。每页是 8行。一个画面的 64行被分成 8个页*/
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,((column>>4)&0x0F)+0x10); //设置列地址的高4位
    YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV0,column&0x0F); //设置列地址的低4位
}

/*全屏清屏*/
void YUYY_HS12864G18B_ClearScreen(YUYY_HS12864G18B_DEV_Type *hs12864_dev)
{
    uint8_t i,j;
    for(i=0;i<8;i+=1)
    {
        YUYY_HS12864G18B_address(hs12864_dev,i,0);
        for(j=0;j<132;j+=1)
        {
            YUYY_HS12864G18B_writedata(hs12864_dev,YUYY_GPIO_LEV1,0x00);
        }
    }
}

/*显示8x16ASCII字符串*/
void YUYY_HS12864G18B_DisplayString8x16(YUYY_HS12864G18B_DEV_Type *hs12864_dev,uint8_t option,uint8_t page,uint8_t column,char *text)
{
    uint8_t i=0,j;
    option &= 0x01;
    while(text[i]>0x00)
    {
        if((text[i] > 0x1F)&&(text[i] < 0x7F))
        {
            j=text[i]-0x20;
            YUYY_HS12864G18B_DisplayDatas(hs12864_dev,option,page,column,(uint8_t *)(YUYY_FONT_ASCII_TABLE_8x16+j),8,16);
            column+=8;
        }
        i+=1;
    }
}
运行效果

配置RTC,设置外部低速晶振为RTC时钟源

查看用户手册

配置RTC


开启唤醒中断

在RTC初始化方法中手动使能中断
void wk_ertc_init(void)
{
  /* add user code begin ertc_init 0 */
  /* add user code end ertc_init 0 */

  exint_init_type exint_init_struct;

  /* add user code begin ertc_init 1 */

  /* add user code end ertc_init 1 */

  pwc_battery_powered_domain_access(TRUE);

  crm_ertc_clock_select(CRM_ERTC_CLOCK_LEXT);
  crm_ertc_clock_enable(TRUE);
  ertc_reset();
  ertc_wait_update();
  ertc_divider_set(127, 255);
  ertc_hour_mode_set(ERTC_HOUR_MODE_24);

  ertc_time_set(22, 10, 0, ERTC_24H);

  ertc_date_set(24, 4, 28, 7);

  /* configure Wakeup timer */
  ertc_wakeup_clock_set(ERTC_WAT_CLK_CK_B_16BITS);
  ertc_wakeup_counter_set(0);

  exint_default_para_init(&exint_init_struct);
  exint_init_struct.line_enable   = TRUE;
  exint_init_struct.line_mode     = EXINT_LINE_INTERRUPUT;
  exint_init_struct.line_select   = EXINT_LINE_22;
  exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
  exint_init(&exint_init_struct);
  /**
   * Users need to configure this interrupt functions according to the actual application.
   * 1. Call the below function to enable this  interrupt.
   *      --ertc_interrupt_enable(ERTC_WAT_INT, TRUE)
   * 2. Add the user's interrupt handler code into the below function in the at32f402_405_int.c file.
   *      --void ERTC_WKUP_IRQHandler (void)
   */

  ertc_wakeup_enable(TRUE);  

  /* add user code begin ertc_init 2 */
    ertc_interrupt_enable(ERTC_WAT_INT,TRUE);
  /* add user code end ertc_init 2 */
}



中断处理
char *WeekdayStr[7]= {"SUN","MON","TUE","WED","THU","FRI","SAT"};
void rtc_int()
{
    ertc_time_type time;
    char out[20];

    ertc_calendar_get(&time);
    sprintf(out," 20%02d-%02d-%02d %s ",time.year, time.month, time.day,WeekdayStr[time.week%7]);
    YUYY_HS12864G18B_DisplayString8x16(&lcd_dev,0,0,0,out);
    sprintf(out,"    %02d:%02d:%02d    ",time.hour, time.min, time.sec);
    YUYY_HS12864G18B_DisplayString8x16(&lcd_dev,0,2,0,out);
}
void ERTC_WKUP_IRQHandler(void)
{
  /* add user code begin ERTC_WKUP_IRQ 0 */
    if(ertc_flag_get(ERTC_WATF_FLAG) != RESET)
    {
        ertc_flag_clear(ERTC_WATF_FLAG);
        rtc_int();
    }
  /* add user code end ERTC_WKUP_IRQ 0 */
  /* add user code begin ERTC_WKUP_IRQ 1 */

  /* add user code end ERTC_WKUP_IRQ 1 */
}
运行效果



使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

146

主题

698

帖子

6

粉丝