- #include "stm32f10x.h"
- #include "delay.h"
- #include "display12864.h"
- #define u16 unsigned short
- #define u8 unsigned char
- #define DisIO GPIOA
- #define DisClk RCC_APB2Periph_GPIOA
- #define Data GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7
- #define RS GPIO_Pin_8
- #define RW GPIO_Pin_9
- #define EN GPIO_Pin_10
- //#define PSB GPIO_Pin_14
-
- GPIO_InitTypeDef GPIOStru;
- void IOInitOut(void)
- {
- GPIOStru.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIOStru.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIOStru.GPIO_Pin = Data|RS|RW|EN;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
- GPIO_Init(GPIOA ,&GPIOStru);
- }
- void IOInitIn(void)
- {
- GPIOStru.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIOStru.GPIO_Speed = GPIO_Speed_50MHz;
- GPIOStru.GPIO_Pin = Data;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
- GPIO_Init(GPIOA ,&GPIOStru);
- }
- void WaitBusy(void)
- {
- IOInitIn();
- GPIO_ResetBits(DisIO,RS);
- GPIO_SetBits(DisIO,RW);
- GPIO_SetBits(DisIO,EN);
- while(GPIO_ReadInputData(DisIO) & 0x0080);
- GPIO_ResetBits(DisIO,EN);
- IOInitOut();
- }
- void WriteCmd(u8 cmd)
- {
- WaitBusy();
- GPIO_ResetBits(DisIO,RS);
- GPIO_ResetBits(DisIO,RW);
- GPIO_SetBits(DisIO,EN);
- DisIO->ODR=((DisIO->ODR & 0xff00)|cmd);
-
-
-
- delay_ms(2);
- GPIO_ResetBits(DisIO,EN);
- delay_ms(2);
- }
- void WriteData(u8 data)
- {
- WaitBusy();
- GPIO_SetBits(DisIO,RS);
- GPIO_ResetBits(DisIO,RW);
- GPIO_SetBits(DisIO,EN);
- DisIO->ODR=((DisIO->ODR & 0xff00)|data);
- delay_ms(2);
- GPIO_ResetBits(DisIO,EN);
- delay_ms(2);
- }
- void InitDis(void)
- {
- IOInitOut();
-
- delay_init();
-
- delay_ms(2);
- WriteCmd(0x30);
- delay_ms(2);
- WriteCmd(0x0c);
- delay_ms(2);
- WriteCmd(0x01);
- delay_ms(2);
- WriteCmd(0x06);
- delay_ms(2);
- WriteCmd(0x80);
- delay_ms(2);
- }
- void DisStr(u8 *s)
- {
- while(*s != '\0')
- {
- WriteData(*s);
- s++;
- delay_ms(2);
- }
- }
- void DisInt(long int num)
- {
- u8 temp[17];
- u8 str[17];
- int i=0,j=0;
- while(num != 0)
-
- {
- temp[i] = (num%10)+0x30;
- num/=10;
- i++;
- }
- i--;
-
- while(i != -1)
- {
- str[j] = temp[i];
- j++;
- i--;
- }
- str[j]='\0';
-
- DisStr(str);
- }
- void DisFloat(float fnum)
- {
- long int num = fnum*10000;
- u8 temp[17];
- u8 str[17];
- int i=0,j=0;
- while(num != 0)
- {
- temp[i] = (num%10)+0x30;
- num/=10;
- i++;
- if(i == 4)
- {
- temp[i] = '.';
- i++;
- }
- }
- i--;
- while(i != -1)
- {
- str[j] = temp[i];
- j++;
- i--;
- }
- str[j]='\0';
- DisStr(str);
- }
- void lcd_clear(void)
- {
- WriteCmd(0x01);
- }
- void lcd_locate(u8 X,u8 Y)
- {
- switch(X)
- {
- case 1: WriteCmd(0x80+Y); break;
- case 2: WriteCmd(0x90+Y); break;
- case 3: WriteCmd(0x88+Y); break;
- case 4: WriteCmd(0x98+Y); break;
- }
- }
- void lcd_DisStr(u8 X,u8 Y,u8 *s)
- {
- lcd_locate( X, Y);
- DisStr( s );
- }
復制代碼 |