找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1004|回复: 3
收起左侧

LCD1602显示一排黄方块

[复制链接]
ID:1077458 发表于 2023-5-14 20:09 | 显示全部楼层 |阅读模式

对比度调过了,硬件连接也跟仿真一样没啥问题,大佬们帮我看看程序哪里有问题,急!!!lcd1602.c




#include "bsp-lcd1602.h"

void LCD1602_GPIO_Config(void)
{
        RCC_APB2PeriphClockCmd(LCD1602_CLK, ENABLE);
        GPIO_InitTypeDef LCD1602_GPIOStruct;
        LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_PP;
        LCD1602_GPIOStruct.GPIO_Speed = GPIO_Speed_10MHz;
        LCD1602_GPIOStruct.GPIO_Pin =  LCD1602_E | LCD1602_RS | LCD1602_RW ;
        GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
        LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_OD;
        LCD1602_GPIOStruct.GPIO_Pin =   DB0 |  DB1 | DB2 |DB3 |  DB4 | DB5|
                                                                                                                                        DB6 |  DB7 ;   
        GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
}

void LCD1602_WaitReady(void
{
        uint8_t sta;

        GPIOB->ODR =0x00FF;
        RSO(0);
        RWO(1);
        EO(1);
        SysTick_Delay_Us(1);
        do{
                sta=GPIO_ReadInputDataBit(LCD1602_GPIO_PORT,GPIO_Pin_7);
                EO(0);
        }while(sta);
}

void LCD1602_WriteCmd(uint8_t cmd
{
        LCD1602_WaitReady();
        RSO(0);
        RWO(0);
        EO(0);
        SysTick_Delay_Us(1);
        EO(1);
        LCD1602_GPIO_PORT->ODR &= (cmd|0xFF00);
        EO(0);
        SysTick_Delay_Us(400);
}

void LCD1602_WriteDat(uint8_t dat
{
        LCD1602_WaitReady();
        RSO(1);
        RWO(0);
        SysTick_Delay_Us(30);
        EO(1);
        LCD1602_GPIO_PORT->ODR &=(dat|0xFF00);
        EO(0);
        SysTick_Delay_Us(400);
}

void LCD1602_SetCursor(uint8_t x, uint8_t y)
{
    uint8_t addr;

    if (y == 0)
        addr = 0x00 + x;
    else
        addr = 0x40 + x;
    LCD1602_WriteCmd(addr|0x80);
}

void LCD1602_ShowStr(uint8_t x, uint8_t y, uint8_t *str, uint8_t len)
{
    LCD1602_SetCursor(x, y);
    while (len--)      
    {
        LCD1602_WriteDat(*str++);
    }
}

void LCD_ShowNum(uint8_t x, uint8_t y,uint8_t num)
{     

            LCD1602_SetCursor(x, y);
    LCD_ShowChar(x,y,num+'0');
       
}

void LCD_ShowChar(uint8_t x, uint8_t y,uint8_t dat)
{

            LCD1602_SetCursor(x, y);
        LCD1602_WriteDat(dat);
}



void LCD1602_Init(void)
{
          LCD1602_GPIO_Config();

    LCD1602_WriteCmd(0X38);
         
    LCD1602_WriteCmd(0x0C);
       
    LCD1602_WriteCmd(0x06);
       
    LCD1602_WriteCmd(0x01);
       
}
       


main.c



#include "stm32f10x.h"
#include "bsp-lcd1602.h"
#include "delay.h"
#include "sys.h"
#include "adc.h"
#define LED PAout(3)
#define KEY1 PAin(8)
#define KEY2 PAin(9)
#define KEY3 PAin(10)
void LED_Init(void)
{

GPIO_InitTypeDef  GPIO_InitStructure;
       
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
       
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;                       
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;        
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_3);                                       


}
void KEY_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
       
       
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
}
int main(void)
{
int a,b,c,d;
        float temp;
       
        delay_init();            
        LCD1602_Init();
  ADC1_GPIO_Config();
  ADC_Config();  
        LCD1602_ShowStr(2,0,"Qiti=0.0mg/L",13);
        LED_Init();
        KEY_Init();       
        while(1)
        {
        b=ADC_GetConversionValue(ADC1);
        temp=(float)b*(3.4/4096);
        a=temp/1;
        c=temp*10;
        d=c%10;
        LCD_ShowNum(7,0,a);
        LCD_ShowNum(9,0,d);
                if(KEY3==0)
                {
                if(temp>2) LED=1;
                else LED=0;
                }
                else
                {
                if(KEY1==0) LED=1;

                if(KEY2==0) LED=0;
                }
                delay_ms(200);

        }
}


       


456.jpg
回复

使用道具 举报

ID:262 发表于 2023-5-14 22:04 | 显示全部楼层
来看一下51hei论坛里面解决此问题的痛苦经历吧:
51hei搜索框输入"1602 方块"或者"1602 黑块"或者"1602 白块"或者"1602 格":

这个是io口设置问题,最终搞定了:http://www.51hei.com/bbs/dpj-199732-1.html
http://www.51hei.com/bbs/dpj-168643-1.html
http://www.51hei.com/bbs/dpj-208189-1.html
http://www.51hei.com/bbs/dpj-210690-1.html

既有可能是软件问题也有硬件问题
http://www.51hei.com/bbs/dpj-187378-1.html
http://www.51hei.com/bbs/dpj-160754-1.html
回复

使用道具 举报

ID:277550 发表于 2023-5-15 10:02 | 显示全部楼层

调节显示对比度
回复

使用道具 举报

ID:584814 发表于 2023-5-15 15:27 | 显示全部楼层
知道对比度调过了,可以再调回来。
也可以本坛搜索找一下成功的例子。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表