原创 STM32 串行控制12864液晶 只用2个IO口加电源地

2011-9-18 23:40 1973 11 11 分类: MCU/ 嵌入式


mpu12864.h



/
#ifndef __MPU12864_H__
#define __MPU12864_H__
#include "stm32f10x.h"
extern void write_char(u8 y,u8 x,u8 a);
extern void write_string(u8 y,u8 x,u8 *s);
extern void lcdinit(void);
#endif






mpu12864.c

#include"stm32f10x.h"
#include"mpu12864.h"
#include"gpiobitmap.h"
/
#define SID  PDo_0     //P2^6;
#define SCLK    PDo_1    //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 sendbyte(u8 zdata)
{
u8 i;
for(i=0; i<8; i++)
{
  if((zdata << i) & 0x80)
  {
   SID = 1;
  }
  else
  {
   SID = 0;
  }
  SCLK = 0;
  delayms(1);
  SCLK = 1;
  delayms(1);
}
}

/
void write_com(u8 cmdcode)
{
sendbyte(0xf8);
delayms(1);
sendbyte(cmdcode & 0xf0);
delayms(1);
sendbyte((cmdcode << 4) & 0xf0);
delayms(1);//nms
}

/
void write_data(u8 Dispdata)
{
sendbyte(0xfa);
delayms(1);
sendbyte(Dispdata & 0xf0);
delayms(1);
sendbyte((Dispdata << 4) & 0xf0);
delayms(1);//nms
}

/
void lcdinit()
{
    //ch=0;
//rst = 0;
//delay(100);
//rst = 1;
GPIOD->CRL&=0XFFFFFF00;
GPIOD->CRL|=0X00000033;//PD0-1推挽输出
delayms(1);
write_com(0x30);
delayms(1);
write_com(0x0c);
delayms(1);
write_com(0x06);
delayms(1);
write_com(0x01);
delayms(1);

}


void Mpu12864_posxy(u8 y,u8 x)  //取地址函数(y为行数,x行为列数)
{

u8 i;
switch(y)
         {
       case 1: y=0x80;break;
    case 2: y=0x90;break;
    case 3: y=0x88;break;
    case 4: y=0x98;break;

   }
i=x-1+y;
    write_com(i);
}

// 输入 : *s
/
void write_string(u8 y,u8 x,u8 *s)
{
    Mpu12864_posxy(y,x);
while(*s > 0)
    {
  write_data(*s);
  s++;
  delayms(1);
    }
}
void write_char(u8 y,u8 x,u8 a)
{
    Mpu12864_posxy(y,x);
write_data(a);
}




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();


    lcdinit();

  while (1)
  {
    
   write_string(1,1,table);
   write_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,delay.c and .h文件见并行程序篇或其他关于stm32的程序,不粘了,呵呵。。。。
 
PARTNER CONTENT

文章评论0条评论)

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