下面是源程序,问题就是不能显示。麻烦各位帮看看,初学STM32。
lcd.h文件
#include "stm32f10x.h"
#define LCD_RS_1 GPIOC->BSRR=GPIO_Pin_0 //PC.0--(LCD)RS
#define LCD_RS_0 GPIOC->BRR =GPIO_Pin_0
#define LCD_RW_1 GPIOC->BSRR=GPIO_Pin_1 //PC.1--(LCD)RW
#define LCD_RW_0 GPIOC->BRR =GPIO_Pin_1
#define LCD_EN_1 GPIOC->BSRR=GPIO_Pin_2 //PC.2--(LCD)E
#define LCD_EN_0 GPIOC->BRR =GPIO_Pin_2
#define LcdData GPIOC->ODR //PC[3..10]--(LCD)D0~D7
void delay_nus(unsigned long n);
void delay_nms(unsigned long n);
unsigned char Lcd_CheckBusy(void);
void Lcd_WriteData(unsigned char Data);
void LCD_write_com(unsigned char com);
void Lcd_WriteStr(unsigned char x,unsigned char y,unsigned char *Str);
void Lcd_INIT();
lcd.c文件
#include "stm32f10x.h"
#include "lcd.h"
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
//延时n us
void delay_nus(unsigned long n)
{
unsigned long j;
while(n--)
{ j=8;
while(j--);
}
}
//延时n ms
void delay_nms(unsigned long n)
{
while(n--)
delay_nus(1100);
}
/*************
测试LCD是否处于忙状态
如果忙则返回0x80,否则返回0
**************/
unsigned char Lcd_CheckBusy(void)
{
// GPIO_Write(GPIOC,0xFF);
LCD_RS_0;
delay_nus(1);
LCD_RW_1;
delay_nus(1);
do
{
LCD_EN_0;
delay_nus(200);
LCD_EN_1;
delay_nus(200);
}while(GPIOC->IDR&0X80);
return (u8)0;
}
/*********************************
向LCD写入字节数据
**********************************/
void Lcd_WriteData(unsigned char Data)
{
LCD_EN_0;
delay_nus(1);
LCD_RS_1;
delay_nus(1);
LCD_RW_0;
delay_nus(1);
LcdData=Data;
delay_nus(500);
LCD_EN_1;
delay_nms(1);
LCD_EN_0;
delay_nus(100);
}
/*************************************
想LCD中写入指令代码
**************************************/
void LCD_write_com(unsigned char com)
{
//while(LCD_check_busy()); //忙则等待
LCD_EN_0;
delay_nus(1);
LCD_RS_0;
delay_nus(1);
LCD_RW_0;
delay_nus(1);
LcdData=com;
delay_nus(500);
LCD_EN_1;
delay_nms(1);
LCD_EN_0;
delay_nus(100);
}
/*************************************
向LCD指定起始位置写入一个字符串
*************************************/
void Lcd_WriteStr(unsigned char x,unsigned char y,unsigned char *Str)
{
if (y == 0)
{
LCD_write_com(0x80+x);
}
else
{
LCD_write_com(0xc0+x);
}
while (*Str)
{
Lcd_WriteData(*Str);
Str++;
delay_nus(500);
}
}
/****************************************
LCD初始化
*****************************************/
void Lcd_INIT()
{
delay_nms(15);
LCD_write_com(0x38); //显示模式
delay_nms(5);
LCD_write_com(0x38);
delay_nms(5);
LCD_write_com(0x38);
delay_nms(5);
LCD_write_com(0x38);
delay_nms(5);
LCD_write_com(0x08); //显示关闭
delay_nms(5);
LCD_write_com(0x01); //显示清屏
delay_nms(6);
LCD_write_com(0x06); //显示光标移动设置
delay_nms(5);
LCD_write_com(0x0c); //显示打开及光标设置
delay_nms(5);
}
main.c文件
#include "stm32f10x.h"
#include "stdio.h"
#include "lcd.h"
#include "rs485.h"
void RCC_Configuration()
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);//使能HSE
if(RCC_WaitForHSEStartUp()==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK=SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
//GPIO配置函数
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //PC.0~2
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
Lcd_INIT();
while(1)
{
Lcd_WriteStr(0,0,"happy");
}
} |