proteus下8051驱动12864资料很多,在别人的基础上做了修改,改用stm32,仿真通过.
单片机源程序如下:
- #include "main.h"
- #include "stm32f1xx_hal.h"
- //#include "stm32f1xx_hal_gpio.h"
- #define uchar unsigned char
- #define uint unsigned int
- //#define databus P1
- #define databus GPIOB
- //sbit cs1 = P3^3; //左屏幕选择,低电平有效
- #define cs1 GPIO_PIN_3
- #define set_cs1 GPIO_SetBits(GPIOC, cs1)
- #define clr_cs1 GPIO_ResetBits(GPIOC, cs1)
- //sbit cs2 = P3^4; //右屏幕选择
- #define cs2 GPIO_PIN_4
- #define set_cs2 GPIO_SetBits(GPIOC, cs2)
- #define clr_cs2 GPIO_ResetBits(GPIOC, cs2)
- //sbit e = P3^5; //指令数据控制
- #define e GPIO_PIN_5
- #define set_e GPIO_SetBits(GPIOC, e)
- #define clr_e GPIO_ResetBits(GPIOC, e)
- //sbit wr = P3^6; //读写控制
- #define wr GPIO_PIN_6
- #define set_wr GPIO_SetBits(GPIOC, wr)
- #define clr_wr GPIO_ResetBits(GPIOC, wr)
- //sbit rs = P3^7; //指令数据选择
- #define rs GPIO_PIN_7
- #define set_rs GPIO_SetBits(GPIOC, rs)
- #define clr_rs GPIO_ResetBits(GPIOC, rs)
- void LcdDelay(uint time)
- {
- while(time--);
- }
- void _NOP_(void)
- {
- uint i = 200;
- while(i > 0)
- {
- i--;
- }
- }
- /**********************************
- 写指令
- **********************************/
- void SendCommand(uchar command)
- {
- set_e; //e=1;
- clr_wr; //wr=0;
- clr_rs; //rs=0;
- databus->ODR = (databus->ODR & 0xff00)|command; //databus=command;
- _NOP_();
- clr_e; //e=0;
- _NOP_();
- }
- /**********************************
- 写数据
- **********************************/
- void WriteData(uchar dat)
- {
- set_e; //e=1;
- clr_wr; //wr=0;
- set_rs; //rs=1;
- databus->ODR = (databus->ODR & 0xff00)|dat; //databus=dat;
- _NOP_();
- clr_e; //e=0;
- _NOP_();
- }
- /**********************************
- 显示开/关
- **********************************/
- void SetOnOff(uchar onoff)
- {
- if(onoff==1)
- {
- SendCommand(0x3f);
- }
- else
- {
- SendCommand(0x3e);
- }
- }
- /**********************************
- 选择页
- **********************************/
- void SetLine(uchar line) //12864总共有8页(0~7),每页有8行
- {
- line=line&0x07; //只取后三位xxxx x111 ,这3个是要改变位置的数据
- line=line|0xb8; //页设置的固定格式
- SendCommand(line);
- }
- /**********************************
- 选择列
- **********************************/
- void SetColum(uchar colum) //12864每半屏有64列(0~63),分为左右2屏
- {
- colum=colum&0x3f; //xx11 1111,这个是要改变Y位置的数据
- colum=colum|0x40; //固定格式
- SendCommand(colum);
- }
- /**********************************
- 选择起始行
- **********************************/
- void SetStartLine(uchar startline)
- {
- startline=startline&0x3f; //xx11 1111,这个是要改变x位置的数据
- startline=startline|0xc0; //11xxxxxx,是起始行设置的固定指令
- SendCommand(startline);
- }
- /**********************************
- 选择左右屏 0:左屏,1:右屏,2:全屏
- **********************************/
- void SelectScreen(uchar screen)
- {
- switch(screen)
- {
- case 0:
- clr_cs1; //cs1=0;
- LcdDelay(2);
- set_cs2; //cs2=1;
- LcdDelay(2);
- break;
- case 1:
- set_cs1; //cs1=1;
- LcdDelay(2);
- clr_cs2; //cs2=0;
- LcdDelay(2);
- break;
- case 2:
- clr_cs1; //cs1=0;
- LcdDelay(2);
- clr_cs2; //cs2=0;
- LcdDelay(2);
- break;
- }
- }
- /**********************************
- 显示一个英文字符
- **********************************/
- void Show_english(uchar line,uchar column,uchar *address)
- {
- uchar i;
- SetLine(line);
- SetColum(column);
- for(i=0;i<8;i++)
- {
- WriteData(*address);
- address++;
- }
-
- SetLine(line+1);
- SetColum(column);
- for(i=0;i<8;i++)
- {
- WriteData(*address);
- address++;
- }
- }
- /**********************************
- 清屏
- **********************************/
- void ClearScreen(uchar screen)
- {
- uchar i,j;
- SelectScreen(screen);
- for(i=0;i<8;i++)
- {
- SetLine(i);
- SetColum(0);
- for(j=0;j<64;j++)
- {
- WriteData(0);
- }
- }
- }
- /**********************************
- 12864初始化
- **********************************/
- void InitLcd()
- {
- SetOnOff(0); //显示关
- ClearScreen(2); //清屏
- SetLine(0); //页设置
- SetColum(0); //列设置
- SetStartLine(0); //设置起始页
- SetOnOff(1); //显示开
- }
- TIM_HandleTypeDef htim1;
- void SystemClock_Config(void);
- void Error_Handler(void);
- static void MX_TIM1_Init(void);
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- //HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
- //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
- GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
- GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-
-
- }
- uchar hzk[]=
- {
- /*-- 文字: I --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
- /*-- 文字: --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- /*-- 文字: c --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,
- /*-- 文字: a --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,
- /*-- 文字: n --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,
- /*-- 文字: --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- /*-- 文字: m --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,
- /*-- 文字: a --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,
- /*-- 文字: k --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,
- /*-- 文字: e --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,
- /*-- 文字: --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- /*-- 文字: i --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
- /*-- 文字: t --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,
- };
- int main(void)
- {
- uchar i,line,colum,j ;
- uchar *address ;
-
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
-
- while (1)
- {
-
- line=4;
- colum=0;
- address=hzk;
- SetOnOff(1);
-
- for(i=0;i<13;i++)
- {
- if(i<8)
- {
- SelectScreen(0);
- Show_english(line,colum,address);
- address+=16;
- colum+=8;
- }
- else
- {
- SelectScreen(1);
- Show_english(line,colum,address);
- address+=16;
- colum+=8;
- }
- }
-
- for(i = 0;i < 50;i ++) LcdDelay(30000); //延时
- }
-
- }
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* TIM1 init function */
- static void MX_TIM1_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig;
- TIM_MasterConfigTypeDef sMasterConfig;
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 0;
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim1.Init.Period = 0;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
stm32_12864.zip
(13.48 MB, 下载次数: 453)
|