基于STM32的12864串行程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/**********12864.c文件*************/
#include "stm32f10x.h"
#include "Lcd12864.h"
#include <stdio.h>
#define BIT(Z) (1<<Z)
void CS_Set_H() //RS
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; /*选中管脚1*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*上拉输出*/
GPIO_Init(GPIOC, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
GPIO_SetBits(GPIOC,GPIO_Pin_0);
}
void CS_Set_L() //RS
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; /*选中管脚1*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*上拉输出*/
GPIO_Init(GPIOC, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
}
void SCLK_Set_H() //E
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /*选中管脚1*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*上拉输出*/
GPIO_Init(GPIOC, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
GPIO_SetBits(GPIOC,GPIO_Pin_2);
}
void SCLK_Set_L() //E
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /*选中管脚1*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*上拉输出*/
GPIO_Init(GPIOC, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
}
void SID_Set_H() //RW
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; /*选中管脚1*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*上拉输出*/
GPIO_Init(GPIOC, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
GPIO_SetBits(GPIOC,GPIO_Pin_13);
}
void SID_Set_L() //RW
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; /*选中管脚1*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*上拉输出*/
GPIO_Init(GPIOC, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
}
void Write_com_12864(u8 com)
{
u8 i=0;
CS_Set_L();
SCLK_Set_L();
delay_ms(5); //延时等待忙碌状态过去
CS_Set_H();
for(i=1;i<25;i++)
{
SCLK_Set_H();
switch(i)
{
case 1: {SID_Set_H(); break;}
case 2: {SID_Set_H(); break;}
case 3: {SID_Set_H(); break;}
case 4: {SID_Set_H(); break;}
case 5: {SID_Set_H(); break;}
case 6: {SID_Set_L(); break;}
case 7: {SID_Set_L(); break;}
case 8: {SID_Set_L(); break;}
case 9: {
if((com>>7)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 10:{
if((com>>6)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 11:{
if((com>>5)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 12:{
if((com>>4)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 13: {SID_Set_L(); break;} case 14: {SID_Set_L(); break;} case 15: {SID_Set_L(); break;} case 16: {SID_Set_L(); break;}
case 17:{
if((com>>3)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 18:{
if((com>>2)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 19:{
if((com>>1)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 20:{
if(com&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 21: {SID_Set_L(); break;}
case 22: {SID_Set_L(); break;}
case 23: {SID_Set_L(); break;}
case 24: {SID_Set_L(); break;}
default : break;
}
delay_ms(1);
SCLK_Set_L();
delay_ms(1);
}
}
void Write_data_12864(u8 data)
{
u8 i=0;
CS_Set_L();
SCLK_Set_L();
delay_ms(1);
CS_Set_H();
for(i=1;i<25;i++)
{
SCLK_Set_H();
switch(i)
{
case 1: {SID_Set_H(); break;}
case 2: {SID_Set_H(); break;}
case 3: {SID_Set_H(); break;}
case 4: {SID_Set_H(); break;}
case 5: {SID_Set_H(); break;}
case 6: {SID_Set_L(); break;}
case 7: {SID_Set_H(); break;}
case 8: {SID_Set_L(); break;}
case 9: {
if((data>>7)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 10:{
if((data>>6)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 11:{
if((data>>5)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 12:{
if((data>>4)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 13: {SID_Set_L(); break;}
case 14: {SID_Set_L(); break;}
case 15: {SID_Set_L(); break;}
case 16: {SID_Set_L(); break;}
case 17:{
if((data>>3)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 18:{
if((data>>2)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 19:{
if((data>>1)&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 20:{
if(data&BIT(0))
SID_Set_H();
else
SID_Set_L();
break;
}
case 21: {SID_Set_L(); break;}
case 22: {SID_Set_L(); break;}
case 23: {SID_Set_L(); break;}
case 24: {SID_Set_L(); break;}
default : break;
}
delay_us(1);
SCLK_Set_L();
delay_us(1);
}
}
void Lcd12864_init(void) //初始化LCD
{
Write_com_12864(0x30); //初级指令集
delay_ms(1);
Write_com_12864(0x02); //地址归位
delay_ms(1);
Write_com_12864(0x0C); //整体显示,游标off,游标位置off
delay_ms(1);
Write_com_12864(0x01); //清DDRAM
delay_ms(1);
Write_com_12864(0x1C); //整体显示右移
delay_ms(1);
}
/***************************main.c文件*****************************/
#include "stm32f10x.h"
#include "LCD.h"
//#include "USART.h"
#include "eval.h"
#include "SysTickDelay.h"
#include "UART_INTERFACE.h"
#include "chinese.h"
#include <stdio.h>
#include "Lcd12864.h"
#define BIT(Z) (1<<Z)
//u16 people_num=100,in_out=0;
//u16 people_num_In=0,people_num_Out=0;
/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define countof(a) (sizeof(a) / sizeof(*(a)))
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOA TING;
//GPIO_Init(GPIOA, &GPIO_InitStructure);#ifdef USE_STM3210C_EV AL
/* Initialize LEDs and Key Button mounted on STM3210X-EV AL board */
//STM_EV AL_LEDInit(LED1);
//STM_EV AL_LEDInit(LED2);
/*----------------------- Configure all the GPIOA in Input Floating mode ------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; /*选中管脚8*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;/*上拉输出*/
GPIO_Init(GPIOA, &GPIO_InitStructure); /*GPIO完成上面三行设置*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /*选中管脚2*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;/*上拉输出*/
GPIO_Init(GPIOD, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; /*选中管脚5*/
GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_4; /*选中管脚4*/
GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_11;/*选中管脚11*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;/*最高输出速率10MHz*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; /*上拉输入*/
GPIO_Init(GPIOC, &GPIO_InitStructure); /*GPIO完成上面三行设置*/
}
//系统中断管理
void NVIC_Configuration(void)
{
//NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置优先级分组:先占优先级0位,从优先级4位
//设置向量表的位置和偏移
#ifdef VECT_TAB_RAM
/* Set the V ector Table base location at 0x20000000 */
NVIC_SetV ectorTable(NVIC_V ectTab_RAM, 0x0); //向量表位于RAM #else /* VECT_TAB_FLASH */
/* Set the V ector Table base location at 0x08000000 */
NVIC_SetV ectorTable(NVIC_V ectTab_FLASH, 0x0); //向量表位于FLASH #endif
}
//配置系统时钟,使能各外设时钟
void RCC_Configuration(void)
{
SystemInit();
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA // |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
// |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
// |RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO
// |RCC_APB2Periph_SPI1, ENABLE );
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4
// |RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2
// , ENABLE );
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
RCC_PLLCmd(ENABLE); /* Enable the PLL */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* 选择PLL作为系统时钟*/
RCC_HCLKConfig(RCC_SYSCLK_Div1); /*设置AHB时钟=72 MHz*/ RCC_PCLK1Config(RCC_HCLK_Div2); /*设置APB1时钟=36 MHz(APB1时钟最大值)*/
RCC_PCLK2Config(RCC_HCLK_Div1); /*设置APB2时钟=72 MHz*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能GPIOD时钟
}
//配置所有外设
void Init_All_Periph(void)
{
RCC_Configuration();
//RCC_chushihua();
NVIC_Configuration();
GPIO_Configuration();
//USART1_Configuration();
USART_Configuration(9600);
}
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
int main(void)
{
//u8 a[40]={"abcdefghijklmnopqretuvwxyzabcdefghijklmn"};
u8 a[54]={"山东建筑大学信息与电气工程学院检测技术与自动化装置专业"};
u8 j=0;
Init_All_Periph();
SysTick_Initaize();
Lcd12864_init();
GPIO_SetBits(GPIOD,BIT(2));
GPIO_SetBits(GPIOA,BIT(8));
while(1)
{
Write_com_12864(0X80);
for(j=0;j<16;j++)
{
Write_data_12864(a[j]);
delay_ms(100);
}
Write_com_12864(0X90);
for(j=16;j<32;j++)
{
Write_data_12864(a[j]);
delay_ms(100);
}
Write_com_12864(0X88);
for(j=32;j<48;j++)
{
Write_data_12864(a[j]);
delay_ms(100);
}
Write_com_12864(0X98);
for(j=48;j<54;j++)
{
Write_data_12864(a[j]);
delay_ms(100);
}
//Write_com_12864(0x01); //初级指令集
//delay_ms(1000);
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number * where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif。