找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4592|回复: 0
打印 上一主题 下一主题
收起左侧

LPC1114驱动1602液晶程序(第一部分)

[复制链接]
跳转到指定楼层
楼主
ID:73735 发表于 2015-2-18 23:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
说明:第二部分为驱动程序核心部分,需要第二部分:http://www.51hei.com/bbs/dpj-31563-1.html

第二部分LCD.h文件:

LCD.h文件

#ifndef     __LCD_H__
#define     __LCD_H__


#define   TESTBIT(a,b) ((a)&(1<<(b)))

#define                LCD_RS_Low()                 LPC_GPIO2->DATA &=~(1<<8)          //给P2.8位写0
#define                LCD_RS_High()                 LPC_GPIO2->DATA |=(1<<8)           //给P2.8位写1

#define                LCD_RW_Low()                 LPC_GPIO2->DATA &=~(1<<9)          //给P2.9位写0
#define                LCD_RW_High()                 LPC_GPIO2->DATA |=(1<<9)           //给P2.9位写1

#define                LCD_EN_Low()     LPC_GPIO2->DATA &=~(1<<10)         //给P2.10位写0
#define                LCD_EN_High()    LPC_GPIO2->DATA |=(1<<10)          //给P2.10位写1




void Set_GPIO_Output(void);
void Set_GPIO_Input(void);

void LCD_GPIO_Init(void);
void Write_Data_Port(unsigned char Data);

unsigned char Read_Data_Port(void);
unsigned char Chk_LCD_busy(void);

void Write_LCD_command(unsigned char gcmd,unsigned char gvalue);
void Write_LCD_data(unsigned char gdata);

void INIT_LCD(void);
void Display_LCD_string(unsigned char gadd_start,unsigned char gline,unsigned char glength,const unsigned char *pdata);

void DispChar_XY_LCD(unsigned char x,unsigned char y,unsigned char gdata);
void DispNum_XY_LCD(unsigned char x,unsigned char y,unsigned char gdata);

void Clear_Display(void);

#endif

system_LPX11xx.c(时钟配置程序)

#include <stdint.h>
#include "LPC11xx.h"

#define CLOCK_SETUP           0

#define MAIN_PLL_SETUP        0
#define MAIN_CLKSRCSEL_Val    0x00000001
#define MAIN_PLL_M_Val        0x00000003
#define MAIN_PLL_P_Val        0x00000001
#define SYS_AHB_DIV_Val       4/* 1 through 255, typical is 1 or 2 or 4 */

/*
//-------- <<< end of configuration section >>> ------------------------------
*/

/*----------------------------------------------------------------------------
  DEFINES
*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
  Define clocks
*----------------------------------------------------------------------------*/
#define XTAL        (12000000UL)        /* Oscillator frequency               */
#define OSC_CLK     (      XTAL)        /* Main oscillator frequency          */
#define IRC_OSC     (12000000UL)        /* Internal RC oscillator frequency   */
#define WDT_OSC     (  250000UL)        /* WDT oscillator frequency           */

/*----------------------------------------------------------------------------
  Clock Variable definitions
*----------------------------------------------------------------------------*/
uint32_t ClockSource = IRC_OSC;
uint32_t SystemFrequency = IRC_OSC; /*!< System Clock Frequency (Core Clock)  */
uint32_t SystemAHBFrequency = IRC_OSC;

/**
* Misc. clock generation modules
*
* @param  none
* @return none
*
* @brief  Setup the microcontroller system.
*         Initialize the System and update the SystemFrequency variable.
*/
void Main_PLL_Setup ( void )
{
  uint32_t regVal;

  ClockSource = OSC_CLK;
  LPC_SYSCON->SYSPLLCLKSEL = MAIN_CLKSRCSEL_Val;   /* Select system OSC--PLL时钟源选中系统振荡器 */
  LPC_SYSCON->SYSPLLCLKUEN = 0x01;                 /* Update clock source */
  LPC_SYSCON->SYSPLLCLKUEN = 0x00;                 /* toggle Update register once */
  LPC_SYSCON->SYSPLLCLKUEN = 0x01;
  while ( !(LPC_SYSCON->SYSPLLCLKUEN & 0x01) );    /* Wait until updated 等待系统振荡器锁定*/

  regVal = LPC_SYSCON->SYSPLLCTRL;
  regVal &= ~0x1FF;
  LPC_SYSCON->SYSPLLCTRL = (regVal | (MAIN_PLL_P_Val<<5) | MAIN_PLL_M_Val);/* P=1, M=4 */

  /* Enable main system PLL, main system PLL bit 7 in PDRUNCFG. */
  LPC_SYSCON->PDRUNCFG &= ~(0x1<<7);              /* PDRUNCFG第七位写0,即系统PLL上电 */
  while ( !(LPC_SYSCON->SYSPLLSTAT & 0x01) );    /* Wait until it's locked */

  LPC_SYSCON->MAINCLKSEL = 0x01;              /* Select PLL clock output--选择PLL输出作为主时钟 */
  LPC_SYSCON->MAINCLKUEN = 0x01;              /* Update MCLK clock source --允许更新主时钟*/
  LPC_SYSCON->MAINCLKUEN = 0x00;              /* Toggle update register once */
  LPC_SYSCON->MAINCLKUEN = 0x01;
  while ( !(LPC_SYSCON->MAINCLKUEN & 0x01) );    /* Wait until updated 等待主时钟锁定*/

  LPC_SYSCON->SYSAHBCLKDIV = SYS_AHB_DIV_Val;    /* SYS AHB clock, typical is 1 or 2 or 4 --SYSAHBCLKDIV的值为1,即不分频 AHB时钟为12*4=48MHz*/

#if MAIN_PLL_SETUP
  SystemFrequency = ClockSource * (MAIN_PLL_M_Val+1);    /*系统时钟=时钟源*4MHz*/
#else
  SystemFrequency = ClockSource;
#endif
  SystemAHBFrequency = (uint32_t)(SystemFrequency/SYS_AHB_DIV_Val);   /*AHB时钟=系统时钟/AHB分频值*/
  return;
}

/**
* Initialize the system
*
* @param  none
* @return none
*
* @brief  Setup the microcontroller system.
*         Initialize the System and update the SystemFrequency variable.
*/
void SystemInit (void)
{
  //uint32_t i;

#ifdef __DEBUG_RAM   
  LPC_SYSCON->SYSMEMREMAP = 0x1;/* remap to internal RAM */
#else
#ifdef __DEBUG_FLASH   
  LPC_SYSCON->SYSMEMREMAP = 0x2;/* remap to internal flash */
#endif
#endif

#if (CLOCK_SETUP)                       /* Clock Setup */
  /* bit 0 default is crystal bypass,
  bit1 0=0~20Mhz crystal input, 1=15~50Mhz crystal input. */
  LPC_SYSCON->SYSOSCCTRL = 0x01;

  /* main system OSC run is cleared, bit 5 in PDRUNCFG register */
  LPC_SYSCON->PDRUNCFG &= ~(0x1<<5);    /* PDRUNCFG的第五位写0,即系统系统振荡器上电 */
  /* Wait 200us for OSC to be stablized, no status
  indication, dummy wait. */
  for ( i = 0; i < 0x100; i++ );

#if (MAIN_PLL_SETUP)
  Main_PLL_Setup();  
#endif

#endif/* endif CLOCK_SETUP */

  /* System clock to the IOCON needs to be enabled or
  most of the I/O related peripherals won't work. */
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16);                  //使能IO配置模块的时钟
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);                   //开启定时器Timer16_1时钟


LPC_SYSCON->SYSAHBCLKDIV = SYS_AHB_DIV_Val;
SystemAHBFrequency = (uint32_t)(SystemFrequency/SYS_AHB_DIV_Val);   /*AHB时钟=系统时钟/AHB分频值*/

  return;
}

main.c程序:


#include "LPC11XX.h"
#include "gpio.h"
#include "timer16.h"
#include "main.h"
#include "Delay.h"
#include "LCD.h"
#include "Key.h"

const unsigned char P[] = {"Bei-Jing"};

int main(void)
{
SystemInit();                             //系统初始化--主要是设置主时钟和开启所需模块时钟
GPIOInit();                                //GPIO初始化--主要是开启GPIO时钟


LPC_IOCON->PIO0_3 &= (~0x07);              //设置P2.0为GPIO
LPC_GPIO0->DIR |= (0x1<<3);                //设置P0.3为输出


INIT_LCD();


DispChar_XY_LCD(15, 0, 'A');               //显示A

DispChar_XY_LCD(15, 1, 'Y');               //显示Y

Display_LCD_string(1, 0, 8, P);
Display_LCD_string(1, 1, 8, P);


while(1)
{
    GPIOSetValue( 0, 3, 0 );                 //给P0.3位写0

Delay_Ms(1000);                          //延时

GPIOSetValue( 0, 3, 1 );                 //给P0.3位写1

Delay_Ms(1000);                          //延时
  }
}

Delay.c程序:(延时程序使用Timer16_1实现)

#include "LPC11XX.h"
#include "gpio.h"
#include "timer16.h"
#include "Delay.h"

/*系统时钟配置为48MHz,AHB不分频,一次定时器时钟为48Mhz,因此得出以下分频值,匹配值*/

void Delay_Us(unsigned int DelayTimes)     /** setup timer #1 for delay */
{
//LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /*开启定时器Timer16_1时钟--时钟配置中开启该时钟*/
  LPC_TMR16B1->TCR = 0x02;               /* reset timer */
  LPC_TMR16B1->PR  = 3;                 /* set prescaler to zero 根据AHB时钟确定分频值*/
  LPC_TMR16B1->MR0 = DelayTimes;             /* 所需的延时时间 */
  LPC_TMR16B1->IR  = 0xff;               /* reset all interrrupts 复位所有的中断*/
  LPC_TMR16B1->MCR = 0x04;               /* stop timer on match --达到匹配停止*/
  LPC_TMR16B1->TCR = 0x01;               /* start timer 启动定时器*/

while (LPC_TMR16B1->TCR & 0x01);           /*等待直到时间到*/
LPC_TMR16B1->TCR = 0x00;               /* Close timer --关闭定时器*/
}

void Delay_Ms(unsigned int DelayTimes)       /** setup timer #1 for delay */
{
//LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);       //开启定时器Timer16_1时钟
  LPC_TMR16B1->TCR = 0x02;               /* reset timer */
  LPC_TMR16B1->PR  = 3000;               /* set prescaler to zero */
  LPC_TMR16B1->MR0 = DelayTimes;
  LPC_TMR16B1->IR  = 0xff;               /* reset all interrrupts */
  LPC_TMR16B1->MCR = 0x04;               /* stop timer on match */
  LPC_TMR16B1->TCR = 0x01;               /* start timer */

while (LPC_TMR16B1->TCR & 0x01);           /*等待直到时间到*/
LPC_TMR16B1->TCR = 0x00;               /* Close timer */
}

void Delay_Sec(unsigned int DelayTimes)      /* * setup timer #1 for delay */
{
//LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     //开启定时器Timer16_1时钟
  LPC_TMR16B1->TCR = 0x02;               /* reset timer */
  LPC_TMR16B1->PR  = 3000000;           /* set prescaler to zero */
  LPC_TMR16B1->MR0 = DelayTimes;
  LPC_TMR16B1->IR  = 0xff;               /* reset all interrrupts */
  LPC_TMR16B1->MCR = 0x04;               /* stop timer on match */
  LPC_TMR16B1->TCR = 0x01;               /* start timer */

while (LPC_TMR16B1->TCR & 0x01);           /*等待直到时间到*/
LPC_TMR16B1->TCR = 0x00;               /* Close timer */
}


Delay.h程序:(延时程序使用Timer16_1实现)


#ifndef     __DELAY_H__
#define     __DELAY_H__

void Delay_Us(unsigned int DelayTimes);            /*微秒延时函数*/

void Delay_Ms(unsigned int DelayTimes);            /*毫秒延时函数*/

void Delay_Sec(unsigned int DelayTimes);          /*秒延时函数*/



#endif








分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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