【MM32F5270开发板试用】+ TFTLCD IO口方式驱动

本篇文章来自极术社区与灵动组织的MM32F5270开发板评测活动,更多开发板试用活动请关注极术社区网站。作者:小会

测试下使用开发板IO口方式驱动16位总线TFTLCD。

一、电路图

开发板上LCD接口部分电路图

转接板
硬件接口设计中间转接板,将开发板的接口定义转换成显示屏模块的接口。!

二、代码

st7789.c

#include "board_init.h"
#include "hal_gpio.h"
#include "hal_rcc.h"
#include "st7789s.h"
#include "delay.h"
#include "font.h"

uint16_t POINT_COLOR=0x0000;    //»­±ÊÑÕÉ«
uint16_t BACK_COLOR=0xFFFF;  //±³¾°É« 
_lcd_dev lcddev;

static void ST7789_Reset(void)
{
    LCD_SetRst();    
    delay_ms(1); 
    LCD_ClrRst();  
    delay_ms(10); // Delayms 10ms               // This delay time is necessary 
    LCD_SetRst();    
    delay_ms(120); // Delayms 120 ms         
}

void ST7789_Gpio_Writedat(uint16_t dat)
{
    uint16_t temp_rpd, rpd;
    uint16_t temp_rpe, rpe;
    temp_rpd=GPIO_ReadOutData(GPIOD);
    rpd=((dat>>5)&0x700) | ((dat<<14)&0xc000) | ((dat>>2)&0x03);
    temp_rpd= (temp_rpd&(~0xc703))|rpd;
    GPIO_WriteBits(GPIOD,temp_rpd);
    
    //printf("rpd=%04x\r\n",rpd);
    temp_rpe=temp_rpd=GPIO_ReadOutData(GPIOE);
    rpe=(dat<<3)&0xff80; //1111 1111 1000 0000
    temp_rpe=(temp_rpe&(~0xff80))|rpe;
    GPIO_WriteBits(GPIOE,temp_rpe);
    //printf("rpe=%04x\r\n",rpe);
    
/*    temp_rpd=GPIO_ReadOutData(GPIOD)&0xc703;//1100 0111 0000 0011
    
    printf("dat =%04x  ,   ",dat);
    printf("temp_rpd=%04x  ,   ",temp_rpd);
    
    temp_rpe=GPIO_ReadOutData(GPIOE)&0xff80; //1111 1111 1000 0000
    printf("temp_rpe=%04x\r\n",temp_rpe);*/
    
}

void ST7789_WriteCommand(uint16_t cmd)
{
    LCD_ClrCs();  //cs=0
  LCD_ClrRs();  //rs=0
    LCD_SetRd();  //rd=1
  LCD_ClrWr();  //wr=0
    //GPIO_Write(GPIOE, cmd);
    ST7789_Gpio_Writedat(cmd);
  LCD_SetWr();   //wr=1
  LCD_SetCs();   //cs=1
    LCD_SetRs();   //rs=1
}

void ST7789_WriteData(uint16_t dat)
{
    LCD_SetRs();  //rs=1
  LCD_ClrCs();  //cs=0 
  LCD_SetRd();  //rd=1    
    LCD_ClrWr();  //wr=0
    ST7789_Gpio_Writedat(dat);
    LCD_SetWr();   //wr=1
  LCD_SetCs();   //cs=1
}

//ÉèÖÃLCDÏÔʾ·½Ïò
//dir:0,ÊúÆÁ£»1,ºáÆÁ
void LCD_Display_Dir(uint8_t dir)
{
    if(dir==0)            //ÊúÆÁ
    {
        lcddev.dir=0;    //ÊúÆÁ
        lcddev.width=240;
        lcddev.height=320;
    }else                 //ºáÆÁ
    {                      
        lcddev.dir=1;    //ºáÆÁ
        lcddev.width=320;
        lcddev.height=240;
    } 
    //LCD_Scan_Dir(DFT_SCAN_DIR);    //ĬÈÏɨÃè·½Ïò
}


void init_st7789s(void)
{
    
        GPIO_Init_Type gpio_init;
    
    /* GPIOD. */
    RCC_EnableAHB1Periphs(RCC_AHB1_PERIPH_GPIOD, true);
    RCC_ResetAHB1Periphs(RCC_AHB1_PERIPH_GPIOD);

    /* GPIOE. */
    RCC_EnableAHB1Periphs(RCC_AHB1_PERIPH_GPIOE, true);
    RCC_ResetAHB1Periphs(RCC_AHB1_PERIPH_GPIOE);
    
        /* GPIOG. */
    RCC_EnableAHB1Periphs(RCC_AHB1_PERIPH_GPIOG, true);
    RCC_ResetAHB1Periphs(RCC_AHB1_PERIPH_GPIOG);
    
    gpio_init.Pins  = GPIO_PIN_15|GPIO_PIN_14|GPIO_PIN_10|GPIO_PIN_9|GPIO_PIN_8|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_1|GPIO_PIN_0;
    gpio_init.PinMode  = GPIO_PinMode_Out_PushPull;
    gpio_init.Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &gpio_init);

    
    gpio_init.Pins  = GPIO_PIN_15|GPIO_PIN_14|GPIO_PIN_13|GPIO_PIN_12|GPIO_PIN_11|GPIO_PIN_10|GPIO_PIN_9|GPIO_PIN_8|GPIO_PIN_7|GPIO_PIN_3|GPIO_PIN_2;
    gpio_init.PinMode  = GPIO_PinMode_Out_PushPull;
    gpio_init.Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &gpio_init);
        
        gpio_init.Pins  = GPIO_PIN_11|GPIO_PIN_12;
    gpio_init.PinMode  = GPIO_PinMode_Out_PushPull;
    gpio_init.Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOG, &gpio_init);
        
        ST7789_Reset();
        ST7789_WriteCommand(0x0011);        //exit SLEEP mode
        delay_ms(120);
        
        ST7789_WriteCommand(0x0036);
        ST7789_WriteData(0x0060);            //MADCTL: memory data access control
        ST7789_WriteCommand(0x003A);
        ST7789_WriteData(0x0005);            //COLMOD: Interface Pixel format *** I use 262K-colors in 18bit/pixel format when using 8-bit interface to allow 3-bytes per pixel
    //    ST7789_WriteCommand(0x003A);ST7789_WriteData(0x0055);//COLMOD: Interface Pixel format  *** I use 65K-colors in 16bit/pixel (5-6-5) format when using 16-bit interface to allow 1-byte per pixel
        ST7789_WriteCommand(0x00B2);
        ST7789_WriteData(0x000C);
        ST7789_WriteData(0x000C);
        ST7789_WriteData(0x0000);
        ST7789_WriteData(0x0033);
        ST7789_WriteData(0x0033);            //PORCTRK: Porch setting
        ST7789_WriteCommand(0x00B7);
        ST7789_WriteData(0x0035);            //GCTRL: Gate Control
        ST7789_WriteCommand(0x00BB);
        ST7789_WriteData(0x001c);            //VCOMS: VCOM setting
        ST7789_WriteCommand(0x00C0);
        ST7789_WriteData(0x002C);            //LCMCTRL: LCM Control
        ST7789_WriteCommand(0x00C2);
        ST7789_WriteData(0x0001);
        ST7789_WriteData(0x00FF);            //VDVVRHEN: VDV and VRH Command Enable
        ST7789_WriteCommand(0x00C3);
        ST7789_WriteData(0x000B);            //VRHS: VRH Set
        ST7789_WriteCommand(0x00C4);
        ST7789_WriteData(0x0020);            //VDVS: VDV Set
        ST7789_WriteCommand(0x00C6);
        ST7789_WriteData(0x000F);            //FRCTRL2: Frame Rate control in normal mode
        ST7789_WriteCommand(0x00D0);
        ST7789_WriteData(0x00A4);
        ST7789_WriteData(0x00A1);            //PWCTRL1: Power Control 1
        ST7789_WriteCommand(0x00E0);
        ST7789_WriteData(0x00D0);
        ST7789_WriteData(0x0000);
        ST7789_WriteData(0x0003);
        ST7789_WriteData(0x0009);
        ST7789_WriteData(0x0013);
        ST7789_WriteData(0x001c);
        ST7789_WriteData(0x003a);
        ST7789_WriteData(0x0055);
        ST7789_WriteData(0x0048);
        ST7789_WriteData(0x0018);
        ST7789_WriteData(0x0012);
        ST7789_WriteData(0x000e);
        ST7789_WriteData(0x0019);
        ST7789_WriteData(0x001e);            //PVGAMCTRL: Positive Voltage Gamma control
        ST7789_WriteCommand(0x00E1);
        ST7789_WriteData(0x00D0);
        ST7789_WriteData(0x0000);
        ST7789_WriteData(0x0003);
        ST7789_WriteData(0x0009);
        ST7789_WriteData(0x0005);
        ST7789_WriteData(0x0025);
        ST7789_WriteData(0x003a);
        ST7789_WriteData(0x0055);
        ST7789_WriteData(0x0050);
        ST7789_WriteData(0x003d);
        ST7789_WriteData(0x001c);
        ST7789_WriteData(0x001d);
        ST7789_WriteData(0x001d);
        ST7789_WriteData(0x001e);            
        ST7789_WriteCommand(0x0029);                //display ON
        ST7789_WriteCommand(0x002c);    

        printf("\n\r st7789 init ok! \r  ");
        LCD_Display_Dir(1);

        LCD_BLA_ON();    
}

void Lcd_SetBox(uint16_t xStart, uint16_t yStart, uint16_t xlong, uint16_t ylong)
{
    uint16_t xEnd=0, yEnd=0;
    xEnd=xStart+xlong-1;
    yEnd=yStart+ylong-1;
    
    ST7789_WriteCommand(0x2a);   
    ST7789_WriteData(xStart>>8);
    ST7789_WriteData(xStart);
    ST7789_WriteData(xEnd>>8);
    ST7789_WriteData(xEnd);

    ST7789_WriteCommand(0x2b);   
    ST7789_WriteData(yStart>>8);
    ST7789_WriteData(yStart);
    ST7789_WriteData(yEnd>>8);
    ST7789_WriteData(yEnd);

    ST7789_WriteCommand(0x2c);            
}

void Lcd_SetBox1(uint16_t xStart, uint16_t xEnd, uint16_t yStart, uint16_t yEnd)
{
    ST7789_WriteCommand(0x2a);   
    ST7789_WriteData(xStart>>8);
    ST7789_WriteData(xStart);
    ST7789_WriteData(xEnd>>8);
    ST7789_WriteData(xEnd);

    ST7789_WriteCommand(0x2b);   
    ST7789_WriteData(yStart>>8);
    ST7789_WriteData(yStart);
    ST7789_WriteData(yEnd>>8);
    ST7789_WriteData(yEnd);

    ST7789_WriteCommand(0x2c);            
}

void LCD_WriteOneDot(uint16_t color)
{ 
    ST7789_WriteData(color);
}

void LCD_Clear(uint16_t Color)
{
   uint32_t i;  
   Lcd_SetBox(0,0,320,240);  
   for(i=0;i<78900;i++){       
       LCD_WriteOneDot(Color); 
  }
}

void ST7789_SetPoint(uint16_t x, uint16_t y, uint16_t color)
{
    Lcd_SetBox1(x,x+1,y,y+1);
    LCD_WriteOneDot(color); 
}

//ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û
//x,y:Æðʼ×ø±ê
//num:ÒªÏÔʾµÄ×Ö·û:" "--->"~"
//size:×ÖÌå´óС 12/16/24
//mode:µþ¼Ó·½Ê½(1)»¹ÊǷǵþ¼Ó·½Ê½(0)
void LCD_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mode)
{                                
    uint8_t temp,t1,t;
    uint8_t y0=y;
    uint8_t csize=(size/8+((size%8)?1:0))*(size/2);        //µÃµ½×ÖÌåÒ»¸ö×Ö·û¶ÔÓ¦µãÕó¼¯ËùÕ¼µÄ×Ö½ÚÊý    
     num=num-' ';//µÃµ½Æ«ÒƺóµÄÖµ£¨ASCII×Ö¿âÊÇ´Ó¿Õ¸ñ¿ªÊ¼È¡Ä££¬ËùÒÔ-' '¾ÍÊǶÔÓ¦×Ö·ûµÄ×ֿ⣩
    for(t=0;t<csize;t++)
    {   
        if(size==12)temp=asc2_1206[num][t];          //µ÷ÓÃ1206×ÖÌå
        else if(size==16)temp=asc2_1608[num][t];    //µ÷ÓÃ1608×ÖÌå
        else if(size==24)temp=asc2_2412[num][t];    //µ÷ÓÃ2412×ÖÌå
        else return;                                //ûÓеÄ×Ö¿â
        for(t1=0;t1<8;t1++)
        {                
            if(temp&0x80)ST7789_SetPoint(x,y,POINT_COLOR);
            else if(mode==0)ST7789_SetPoint(x,y,BACK_COLOR);
            temp<<=1;
            y++;
            if(y>=lcddev.height)return;        //³¬ÇøÓòÁË
            if((y-y0)==size)
            {
                y=y0;
                x++;
                if(x>=lcddev.width)return;    //³¬ÇøÓòÁË
                break;
            }
        }       
    }                            
} 

//ÏÔʾ×Ö·û´®
//x,y:Æðµã×ø±ê
//width,height:ÇøÓò´óС  
//size:×ÖÌå´óС
//*p:×Ö·û´®ÆðʼµØÖ·          
void LCD_ShowString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, uint8_t *p)
{         
    uint8_t x0=x;
    width+=x;
    height+=y;
    while((*p<='~')&&(*p>=' '))//ÅжÏÊDz»ÊÇ·Ç·¨×Ö·û!
    {       
        if(x>=width){x=x0;y+=size;}
        if(y>=height)break;//Í˳ö
        LCD_ShowChar(x,y,*p,size,0);
        x+=size/2;
        p++;
    }  
}

st7789.h

#ifndef __ST7789S_H__
#define __ST7789S_H__

#include "board_init.h"

typedef struct  
{                                            
    uint16_t width;            
    uint16_t height;            
    uint16_t id;                
    uint8_t  dir;            
    uint16_t    wramcmd;        
    uint16_t  setxcmd;        
    uint16_t  setycmd;        
}_lcd_dev; 

extern _lcd_dev lcddev;       
extern uint16_t  POINT_COLOR;  
extern uint16_t  BACK_COLOR; 


#define    BLACK    0x0000
#define    BLUE    0x001F
#define    RED     0xF800
#define    GREEN     0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW    0xFFE0
#define WHITE    0xFFFF    
#define BACKCOLOR 0xFFFF    
#define    WORDCOLOR 0x0000


/* Ƭѡ */
#define LCD_SetCs()     {GPIO_WriteBit(GPIOG, GPIO_PIN_12, 1u);}
#define LCD_ClrCs()     {GPIO_WriteBit(GPIOG, GPIO_PIN_12, 0u);}
/* ¸´Î» */
#define LCD_SetRst()     {GPIO_WriteBit(GPIOG, GPIO_PIN_11, 1);}
#define LCD_ClrRst()     {GPIO_WriteBit(GPIOG, GPIO_PIN_11, 0);}
/* /RD */
#define LCD_SetRd()     {GPIO_WriteBit(GPIOD, GPIO_PIN_4, 1);}
#define LCD_ClrRd()        {GPIO_WriteBit(GPIOD, GPIO_PIN_4, 0);}
/* /WR */
#define LCD_SetWr()     {GPIO_WriteBit(GPIOD, GPIO_PIN_5, 1);}
#define LCD_ClrWr()     {GPIO_WriteBit(GPIOD, GPIO_PIN_5, 0);}
/* BLA */
#define LCD_BLA_ON()         {GPIO_WriteBit(GPIOE, GPIO_PIN_3, 1);}
#define LCD_BLA_OFF()     {GPIO_WriteBit(GPIOE, GPIO_PIN_3, 0);}
/* RS */
#define LCD_SetRs()     {GPIO_WriteBit(GPIOE, GPIO_PIN_2, 1);}
#define LCD_ClrRs()     {GPIO_WriteBit(GPIOE, GPIO_PIN_2, 0);}



void init_st7789s(void);
void LCD_Clear(uint16_t Color);
void LCD_ShowString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, uint8_t *p);

#endif

main.c

/*
 * Copyright 2021 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "board_init.h"
#include "hal_flexcan.h"
#include "hal_gpio.h"
#include "led.h"
#include "key.h"
#include "delay.h"
#include "flexcan.h"
#include "st7789s.h"

int main(void)
{
        BOARD_Init();
        delay_init();
        init_led();
        //init_key();
        //init_flexcan1();
        init_st7789s();
        POINT_COLOR=RED;
        LCD_Clear(WHITE);
        printf("\r\nmm32f5270 test.\r\n");
    
        LCD_ShowString(30,40,210,24,24,"MM32F5270"); 
        LCD_ShowString(30,70,200,16,16,"TFTLCD TEST");
        LCD_ShowString(30,90,200,16,16,"ST7789");
        LCD_ShowString(30,110,200,16,16,"2022/09/01");         
        LCD_ShowString(30,150,210,24,24,"https://aijishu.com");    
        
        while(1)
        {                
                GPIO_WriteBit(BOARD_LED0_GPIO_PORT, BOARD_LED0_GPIO_PIN, 1u);
                delay_ms(100);
                GPIO_WriteBit(BOARD_LED0_GPIO_PORT, BOARD_LED0_GPIO_PIN, 0u);
                delay_ms(100);
        }
}

三、程序运行

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以帮您编写一个 MM32F5270 驱动 RC522 外设的代码。以下是一个简单的示例代码,用于初始化并与 RC522 进行通信。请注意,这只是一个基本框架,您可能需要根据您的具体需求进行修改和完善。 ```c #include "mm32f5270.h" #include "rc522.h" // RC522 接定义 #define RC522_SPI_PORT SPI1 #define RC522_SPI_CLK RCC_APB2Periph_SPI1 #define RC522_SPI_GPIO_PORT GPIOA #define RC522_SPI_CS_PIN GPIO_Pin_4 #define RC522_SPI_SCK_PIN GPIO_Pin_5 #define RC522_SPI_MISO_PIN GPIO_Pin_6 #define RC522_SPI_MOSI_PIN GPIO_Pin_7 // RC522 初始化 void RC522_Init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; // 使能 SPI 时钟 RCC_APB2PeriphClockCmd(RC522_SPI_CLK, ENABLE); // 配置 SPI 引脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = RC522_SPI_SCK_PIN; GPIO_Init(RC522_SPI_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RC522_SPI_MISO_PIN; GPIO_Init(RC522_SPI_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RC522_SPI_MOSI_PIN; GPIO_Init(RC522_SPI_GPIO_PORT, &GPIO_InitStructure); // 配置 CS 引脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = RC522_SPI_CS_PIN; GPIO_Init(RC522_SPI_GPIO_PORT, &GPIO_InitStructure); // 初始化 SPI SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(RC522_SPI_PORT, &SPI_InitStructure); SPI_Cmd(RC522_SPI_PORT, ENABLE); } // RC522 发送数据 void RC522_SendByte(uint8_t data) { while (SPI_I2S_GetFlagStatus(RC522_SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(RC522_SPI_PORT, data); while (SPI_I2S_GetFlagStatus(RC522_SPI_PORT, SPI_I2S_FLAG_RXNE) == RESET); SPI_I2S_ReceiveData(RC522_SPI_PORT); } // RC522 接收数据 uint8_t RC522_ReceiveByte(void) { while (SPI_I2S_GetFlagStatus(RC522_SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(RC522_SPI_PORT, 0xFF); while (SPI_I2S_GetFlagStatus(RC522_SPI_PORT, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(RC522_SPI_PORT); } // RC522 读取寄存器 uint8_t RC522_ReadReg(uint8_t addr) { uint8_t value; GPIO_ResetBits(RC522_SPI_GPIO_PORT, RC522_SPI_CS_PIN); RC522_SendByte((addr << 1) | 0x80); value = RC522_ReceiveByte(); GPIO_SetBits(RC522_SPI_GPIO_PORT, RC522_SPI_CS_PIN); return value; } // RC522 写入寄存器 void RC522_WriteReg(uint8_t addr, uint8_t value) { GPIO_ResetBits(RC522_SPI_GPIO_PORT, RC522_SPI_CS_PIN); RC522_SendByte(addr << 1); RC522_SendByte(value); GPIO_SetBits(RC522_SPI_GPIO_PORT, RC522_SPI_CS_PIN); } ``` 请注意,此代码仅涵盖了基本的初始化和读写寄存器操作。具体的功能和应用逻辑需要根据您的需求进行补充和修改。同时,还需要根据您的硬件连接情况进行相应的配置。 希望对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值