找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1595|回復: 3
打印 上一主題 下一主題
收起左側

LCD1602顯示一排黃方塊

[復制鏈接]
跳轉到指定樓層
樓主
ID:1077458 發(fā)表于 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 (66.93 KB, 下載次數: 32)

456.jpg
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:262 發(fā)表于 2023-5-14 22:04 | 只看該作者
來看一下51hei論壇里面解決此問題的痛苦經歷吧:
51hei搜索框輸入"1602 方塊"或者"1602 黑塊"或者"1602 白塊"或者"1602 格":

這個是io口設置問題,最終搞定了:http://www.torrancerestoration.com/bbs/dpj-199732-1.html
http://www.torrancerestoration.com/bbs/dpj-168643-1.html
http://www.torrancerestoration.com/bbs/dpj-208189-1.html
http://www.torrancerestoration.com/bbs/dpj-210690-1.html

既有可能是軟件問題也有硬件問題
http://www.torrancerestoration.com/bbs/dpj-187378-1.html
http://www.torrancerestoration.com/bbs/dpj-160754-1.html
回復

使用道具 舉報

板凳
ID:277550 發(fā)表于 2023-5-15 10:02 | 只看該作者

調節(jié)顯示對比度
回復

使用道具 舉報

地板
ID:584814 發(fā)表于 2023-5-15 15:27 | 只看該作者
知道對比度調過了,可以再調回來。
也可以本壇搜索找一下成功的例子。
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表