原创 STM32的12864液晶并行控制程序

2011-9-18 23:45 9104 7 7 分类: MCU/ 嵌入式

    mpu12864.h 文件   

//------------------------------
//       12864驱动程序
//1:#define rs   PAo_0    #define rw   PAo_1
 //  #define en    PAo_2 为液晶控制线(并行操作,其他线悬空就行)
//2:数据口:PD0---PD7:
//3:其中PA0--2和PD0--7,已在mpu12864.c初始化中设
//   为推挽主函数中不用设置,只需打开PA,PD的时钟
//4:在用到的的程序中声明 #include"mpu12864.h"
//   即可用以下函数
//------------------------------------

#ifndef __MPU12864_H__
#define __MPU12864_H__
#include "stm32f10x.h"
extern void write12864_com(u8 com);//写命令
extern void write12864_dat(u8 dat); //写数据
extern void lcd12864_init(void); //初始化
//------定地址函数(y为行数,x行为列数)------*/
//参数:第几行,第几个位置,什么字符*/
//----------------------------------*/
//参数:第几行,第几个位置,什么字符串首地址*/
//-------------------------------------*/
extern void write12864_string(u8 hang,u8 numadd,u8 *p);
#endif 

 

    mpu12864.c 文件        

#include"stm32f10x.h"
#include"mpu12864.h"
#include"gpiobitmap.h"
//---------控制线定义----

#define rs   PAo_0     //P2^5;
#define rw   PAo_1     //P2^6;
#define en    PAo_2    //P2^7;
//------------------------

void delayms(unsigned short time)//nms
{
    unsigned short i, j;
 
 for(; time > 0; time--){
     for(j = 0; j < 10; j++){
      for(i = 0; i < 1000; i++);
  }
 }

}
      
void write12864_com(u8 com) //写命令
  {
   GPIOD->ODR&=0xff00;
      en=0;
   rs=0;
   GPIOD->ODR|=com;
   delayms(1);   //延时太少不行
   en=1;
      delayms(1);
   en=0; 
  }
void write12864_dat(u8 dat)//写数据
  {
   GPIOD->ODR&=0xff00;
   en=0;
   rs=1;
   GPIOD->ODR|=dat;  /
//-------------------------------------/
void write12864_string(u8 hang,u8 numadd,u8 *p)
  {
  
      lcd12864_posxy( hang, numadd);
   while(*p!='\0')
   write12864_dat(*p++);
  }

 

    main .c文件

#include "stm32f10x.h"
#include"mpu12864.h"
#include "gpiobitmap.h"
#include"delay.h"
//-------------------------------------------
EXTI_InitTypeDef EXTI_InitStructure;
ErrorStatus HSEStartUpStatus;
//------------------------------------------

void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);

unsigned char  table[]="2010-4-30";
unsigned char  table0[]="新年快乐!";
//--------------------------------------

//----------------------------------------    

int main(void)
{
 
 
  RCC_Configuration();
  NVIC_Configuration();

  GPIO_Configuration();


  lcd12864_init();

  while (1)
  {
     
   write12864_string(1,4,table); 
   write12864_string(2,1,table0);
   while(1);     
  }
}

void RCC_Configuration(void)
{
  RCC_DeInit();


  RCC_HSEConfig(RCC_HSE_ON);


  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
   
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

   
    FLASH_SetLatency(FLASH_Latency_2);
 
   
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
   
    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_GPIOC|RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOD
  | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
                        
}


void GPIO_Configuration(void)
{
 
  //GPIO_InitTypeDef GPIO_InitStructure;
 
}

 

 

void NVIC_Configuration(void)
{
  //NVIC_InitTypeDef NVIC_InitStructure;
 
#ifdef  VECT_TAB_RAM 
 
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else 
 
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
#endif


}

 

自制位段头文件gpiobitmap.h

#ifndef __GPIOBITMAP_H
#define __GPIOBITMAP_H
#include "stm32f10x.h"
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr))
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C   
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C   

#define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08
 
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //输出
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //输入

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //输出
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //输入

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //输出
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //输入

#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //输出
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //输入

#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //输出
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //输入

#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //输出
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //输入

#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //输出
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //输入

//------------------------------
//*******************************
 //  各GPIO输出口地址定义
//********************************
//--------------------------------/

#define PAo_0  PAout(0)
#define PAo_1  PAout(1)
#define PAo_2  PAout(2)
#define PAo_3  PAout(3)
#define PAo_4  PAout(4)
#define PAo_5  PAout(5)
#define PAo_6  PAout(6)
#define PAo_7  PAout(7)
#define PAo_8  PAout(8)
#define PAo_9  PAout(9)
#define PAo_10  PAout(10)
#define PAo_11  PAout(11)
#define PAo_12  PAout(12)
#define PAo_13  PAout(13)
#define PAo_14  PAout(14)
#define PAo_15  PAout(15)
//----------------------------------//

#define PBo_0  PBout(0)
#define PBo_1  PBout(1)
#define PBo_2  PBout(2)
#define PBo_3  PBout(3)
#define PBo_4  PBout(4)
#define PBo_5  PBout(5)
#define PBo_6  PBout(6)
#define PBo_7  PBout(7)
#define PBo_8  PBout(8)
#define PBo_9  PBout(9)
#define PBo_10  PBout(10)
#define PBo_11  PBout(11)
#define PBo_12  PBout(12)
#define PBo_13  PBout(13)
#define PBo_14  PBout(14)
#define PBo_15  PBout(15)
//--------------------------------//
#define PCo_0  PCout(0)
#define PCo_1  PCout(1)
#define PCo_2  PCout(2)
#define PCo_3  PCout(3)
#define PCo_4  PCout(4)
#define PCo_5  PCout(5)
#define PCo_6  PCout(6)
#define PCo_7  PCout(7)
#define PCo_8  PCout(8)
#define PCo_9  PCout(9)
#define PCo_10  PCout(10)
#define PCo_11  PCout(11)
#define PCo_12  PCout(12)
#define PCo_13  PCout(13)
#define PCo_14  PCout(14)
#define PCo_15  PCout(15)
//---------------------------//
#define PDo_0  PDout(0)
#define PDo_1  PDout(1)
#define PDo_2  PDout(2)
#define PDo_3  PDout(3)
#define PDo_4  PDout(4)
#define PDo_5  PDout(5)
#define PDo_6  PDout(6)
#define PDo_7  PDout(7)
#define PDo_8  PDout(8)
#define PDo_9  PDout(9)
#define PDo_10  PDout(10)
#define PDo_11  PDout(11)
#define PDo_12  PDout(12)
#define PDo_13  PDout(13)
#define PDo_14  PDout(14)
#define PDo_15  PDout(15)
//------------------------------//

#define PEo_0  PEout(0)
#define PEo_1  PEout(1)
#define PEo_2  PEout(2)
#define PEo_3  PEout(3)
#define PEo_4  PEout(4)
#define PEo_5  PEout(5)
#define PEo_6  PEout(6)
#define PEo_7  PEout(7)
#define PEo_8  PEout(8)
#define PEo_9  PEout(9)
#define PEo_10  PEout(10)
#define PEo_11  PEout(11)
#define PEo_12  PEout(12)
#define PEo_13  PEout(13)
#define PEo_14  PEout(14)
#define PEo_15  PEout(15)

//---------------------------//
#define PFo_0  PFout(0)
#define PFo_1  PFout(1)
#define PFo_2  PFout(2)
#define PFo_3  PFout(3)
#define PFo_4  PFout(4)
#define PFo_5  PFout(5)
#define PFo_6  PFout(6)
#define PFo_7  PFout(7)
#define PFo_8  PFout(8)
#define PFo_9  PFout(9)
#define PFo_10  PFout(10)
#define PFo_11  PFout(11)
#define PFo_12  PFout(12)
#define PFo_13  PFout(13)
#define PFo_14  PFout(14)
#define PFo_15  PFout(15)

//---------------------------//

#define PGo_0  PGout(0)
#define PGo_1  PGout(1)
#define PGo_2  PGout(2)
#define PGo_3  PGout(3)
#define PGo_4  PGout(4)
#define PGo_5  PGout(5)
#define PGo_6  PGout(6)
#define PGo_7  PGout(7)
#define PGo_8  PGout(8)
#define PGo_9  PGout(9)
#define PGo_10  PGout(10)
#define PGo_11  PGout(11)
#define PGo_12  PGout(12)
#define PGo_13  PGout(13)
#define PGo_14  PGout(14)
#define PGo_15  PGout(15)
//------------------------------
//********************************
  // 各GPIO输入口地址定义
//********************************
//--------------------------------/
#define PAi_0  PAin(0)
#define PAi_1  PAin(1)
#define PAi_2  PAin(2)
#define PAi_3  PAin(3)
#define PAi_4  PAin(4)
#define PAi_5  PAin(5)
#define PAi_6  PAin(6)
#define PAi_7  PAin(7)
#define PAi_8  PAin(8)
#define PAi_9  PAin(9)
#define PAi_10  PAin(10)
#define PAi_11  PAin(11)
#define PAi_12  PAin(12)
#define PAi_13  PAin(13)
#define PAi_14  PAin(14)
#define PAi_15  PAin(15)
//---------------------------------//
#define PBi_0  PBin(0)
#define PBi_1  PBin(1)
#define PBi_2  PBin(2)
#define PBi_3  PBin(3)
#define PBi_4  PBin(4)
#define PBi_5  PBin(5)
#define PBi_6  PBin(6)
#define PBi_7  PBin(7)
#define PBi_8  PBin(8)
#define PBi_9  PBin(9)
#define PBi_10  PBin(10)
#define PBi_11  PBin(11)
#define PBi_12  PBin(12)
#define PBi_13  PBin(13)
#define PBi_14  PBin(14)
#define PBi_15  PBin(15)
//---------------------------------//
#define PCi_0  PCin(0)
#define PCi_1  PCin(1)
#define PCi_2  PCin(2)
#define PCi_3  PCin(3)
#define PCi_4  PCin(4)
#define PCi_5  PCin(5)
#define PCi_6  PCin(6)
#define PCi_7  PCin(7)
#define PCi_8  PCin(8)
#define PCi_9  PCin(9)
#define PCi_10  PCin(10)
#define PCi_11  PCin(11)
#define PCi_12  PCin(12)
#define PCi_13  PCin(13)
#define PCi_14  PCin(14)
#define PCi_15  PCin(15)
//-------------------------------------//
#define PDi_0  PDin(0)
#define PDi_1  PDin(1)
#define PDi_2  PDin(2)
#define PDi_3  PDin(3)
#define PDi_4  PDin(4)
#define PDi_5  PDin(5)
#define PDi_6  PDin(6)
#define PDi_7  PDin(7)
#define PDi_8  PDin(8)
#define PDi_9  PDin(9)
#define PDi_10  PDin(10)
#define PDi_11  PDin(11)
#define PDi_12  PDin(12)
#define PDi_13  PDin(13)
#define PDi_14  PDin(14)
#define PDi_15  PDin(15)
//-------------------------------------//
#define PEi_0  PEin(0)
#define PEi_1  PEin(1)
#define PEi_2  PEin(2)
#define PEi_3  PEin(3)
#define PEi_4  PEin(4)
#define PEi_5  PEin(5)
#define PEi_6  PEin(6)
#define PEi_7  PEin(7)
#define PEi_8  PEin(8)
#define PEi_9  PEin(9)
#define PEi_10  PEin(10)
#define PEi_11  PEin(11)
#define PEi_12  PEin(12)
#define PEi_13  PEin(13)
#define PEi_14  PEin(14)
#define PEi_15  PEin(15)
//---------------------------------//
#define PFi_0  PFin(0)
#define PFi_1  PFin(1)
#define PFi_2  PFin(2)
#define PFi_3  PFin(3)
#define PFi_4  PFin(4)
#define PFi_5  PFin(5)
#define PFi_6  PFin(6)
#define PFi_7  PFin(7)
#define PFi_8  PFin(8)
#define PFi_9  PFin(9)
#define PFi_10  PFin(10)
#define PFi_11  PFin(11)
#define PFi_12  PFin(12)
#define PFi_13  PFin(13)
#define PFi_14  PFin(14)
#define PFi_15  PFin(15)
//---------------------------------//
#define PGi_0  PGin(0)
#define PGi_1  PGin(1)
#define PGi_2  PGin(2)
#define PGi_3  PGin(3)
#define PGi_4  PGin(4)
#define PGi_5  PGin(5)
#define PGi_6  PGin(6)
#define PGi_7  PGin(7)
#define PGi_8  PGin(8)
#define PGi_9  PGin(9)
#define PGi_10  PGin(10)
#define PGi_11  PGin(11)
#define PGi_12  PGin(12)
#define PGi_13  PGin(13)
#define PGi_14  PGin(14)
#define PGi_15  PGin(15)
//---------------------------------//


 

 

 

 

 

 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
7
关闭 站长推荐上一条 /3 下一条