找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 5354|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

LPC1114驅(qū)動(dòng)1602液晶程序(第一部分)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:73735 發(fā)表于 2015-2-18 23:15 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
說明:第二部分為驅(qū)動(dòng)程序核心部分,需要第二部分:http://www.torrancerestoration.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(時(shí)鐘配置程序)

#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時(shí)鐘源選中系統(tǒng)振蕩器 */
  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 等待系統(tǒng)振蕩器鎖定*/

  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,即系統(tǒng)PLL上電 */
  while ( !(LPC_SYSCON->SYSPLLSTAT & 0x01) );    /* Wait until it's locked */

  LPC_SYSCON->MAINCLKSEL = 0x01;              /* Select PLL clock output--選擇PLL輸出作為主時(shí)鐘 */
  LPC_SYSCON->MAINCLKUEN = 0x01;              /* Update MCLK clock source --允許更新主時(shí)鐘*/
  LPC_SYSCON->MAINCLKUEN = 0x00;              /* Toggle update register once */
  LPC_SYSCON->MAINCLKUEN = 0x01;
  while ( !(LPC_SYSCON->MAINCLKUEN & 0x01) );    /* Wait until updated 等待主時(shí)鐘鎖定*/

  LPC_SYSCON->SYSAHBCLKDIV = SYS_AHB_DIV_Val;    /* SYS AHB clock, typical is 1 or 2 or 4 --SYSAHBCLKDIV的值為1,即不分頻 AHB時(shí)鐘為12*4=48MHz*/

#if MAIN_PLL_SETUP
  SystemFrequency = ClockSource * (MAIN_PLL_M_Val+1);    /*系統(tǒng)時(shí)鐘=時(shí)鐘源*4MHz*/
#else
  SystemFrequency = ClockSource;
#endif
  SystemAHBFrequency = (uint32_t)(SystemFrequency/SYS_AHB_DIV_Val);   /*AHB時(shí)鐘=系統(tǒng)時(shí)鐘/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,即系統(tǒng)系統(tǒng)振蕩器上電 */
  /* 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配置模塊的時(shí)鐘
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);                   //開啟定時(shí)器Timer16_1時(shí)鐘


LPC_SYSCON->SYSAHBCLKDIV = SYS_AHB_DIV_Val;
SystemAHBFrequency = (uint32_t)(SystemFrequency/SYS_AHB_DIV_Val);   /*AHB時(shí)鐘=系統(tǒng)時(shí)鐘/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();                             //系統(tǒng)初始化--主要是設(shè)置主時(shí)鐘和開啟所需模塊時(shí)鐘
GPIOInit();                                //GPIO初始化--主要是開啟GPIO時(shí)鐘


LPC_IOCON->PIO0_3 &= (~0x07);              //設(shè)置P2.0為GPIO
LPC_GPIO0->DIR |= (0x1<<3);                //設(shè)置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);                          //延時(shí)

GPIOSetValue( 0, 3, 1 );                 //給P0.3位寫1

Delay_Ms(1000);                          //延時(shí)
  }
}

Delay.c程序:(延時(shí)程序使用Timer16_1實(shí)現(xiàn))

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

/*系統(tǒng)時(shí)鐘配置為48MHz,AHB不分頻,一次定時(shí)器時(shí)鐘為48Mhz,因此得出以下分頻值,匹配值*/

void Delay_Us(unsigned int DelayTimes)     /** setup timer #1 for delay */
{
//LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     /*開啟定時(shí)器Timer16_1時(shí)鐘--時(shí)鐘配置中開啟該時(shí)鐘*/
  LPC_TMR16B1->TCR = 0x02;               /* reset timer */
  LPC_TMR16B1->PR  = 3;                 /* set prescaler to zero 根據(jù)AHB時(shí)鐘確定分頻值*/
  LPC_TMR16B1->MR0 = DelayTimes;             /* 所需的延時(shí)時(shí)間 */
  LPC_TMR16B1->IR  = 0xff;               /* reset all interrrupts 復(fù)位所有的中斷*/
  LPC_TMR16B1->MCR = 0x04;               /* stop timer on match --達(dá)到匹配停止*/
  LPC_TMR16B1->TCR = 0x01;               /* start timer 啟動(dòng)定時(shí)器*/

while (LPC_TMR16B1->TCR & 0x01);           /*等待直到時(shí)間到*/
LPC_TMR16B1->TCR = 0x00;               /* Close timer --關(guān)閉定時(shí)器*/
}

void Delay_Ms(unsigned int DelayTimes)       /** setup timer #1 for delay */
{
//LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);       //開啟定時(shí)器Timer16_1時(shí)鐘
  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);           /*等待直到時(shí)間到*/
LPC_TMR16B1->TCR = 0x00;               /* Close timer */
}

void Delay_Sec(unsigned int DelayTimes)      /* * setup timer #1 for delay */
{
//LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);     //開啟定時(shí)器Timer16_1時(shí)鐘
  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);           /*等待直到時(shí)間到*/
LPC_TMR16B1->TCR = 0x00;               /* Close timer */
}


Delay.h程序:(延時(shí)程序使用Timer16_1實(shí)現(xiàn))


#ifndef     __DELAY_H__
#define     __DELAY_H__

void Delay_Us(unsigned int DelayTimes);            /*微秒延時(shí)函數(shù)*/

void Delay_Ms(unsigned int DelayTimes);            /*毫秒延時(shí)函數(shù)*/

void Delay_Sec(unsigned int DelayTimes);          /*秒延時(shí)函數(shù)*/



#endif








分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表