vk1072段码驱动C语言,亲测
单片机源程序如下:
- /**
- ******************************************************************************
- * @file vk1072C.c
- * @author kevin_guo
- * @version V1.0.0
- * @date 05-17-2018
- * @brief This file contains all the vk1072C functions.
- * 此文件适用于 VK1072C
- ******************************************************************************
- * @attention
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "vk1072C.h"
-
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define VK1072C_BASE100MS 226757 //通过n个nop指令实现100mS延时,根据单片机修改此常数
- #define VK1072C_CLK 10 //WR信号线频率 ,由Vk1072C_Delay实现, =10->100kHz =100->11.11kHz
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- const unsigned char vk1072C_segtab[VK1072C_SEGNUM]={
- 0,1,2,3,4,5,13,14,15,16,17,18,19,26,27,28,29,30
- };
- unsigned char vk1072C_addrbit=6;//驱动ic的ram地址线数A5-A0,
- unsigned char vk1072C_segi,vk1072C_comi;
- unsigned char vk1072C_maxcom=4;//驱动的com数可以是4com,3com,2com
- unsigned char vk1072C_dispram[VK1072C_SEGNUM];//显示RAM buffer只使用低4位,bit0->com0,bit1->com1,bit2->com2,bit3->com3
- /* Private function prototypes -----------------------------------------------*/
- /**
- * @brief Inserts a delay time.
- * @param ucDat-> 延时时间
- * @retval None
- */
- void Vk1072C_Delay(unsigned int ucDat)
- {//ucDat=10->100kHz ucDat=100->11.11kHz ucDat=226757->100mS
- while(ucDat--)
- {//nop指令根据单片机做相应的修改
- __nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();
- __nop();__nop();__nop();__nop();__nop();
- __nop();__nop();//__nop();__nop();__nop();
- }
- }
- /**
- * @brief Vk1072C clk
- * @param None
- * @retval None
- */
- void WriteClockVk1072C(void)
- {
- VK1072C_WR_L();
- Vk1072C_Delay(VK1072C_CLK);
- VK1072C_WR_H();
- Vk1072C_Delay(VK1072C_CLK);
- }
- /**
- * @brief Write Vk1072C Command
- * @param FunctonCode->功能/命令码
- * @retval None
- */
- void WriteCommandVk1072C(unsigned char FunctonCode)
- {
- unsigned char Shift = 0x80;
- unsigned char i;
-
- VK1072C_CS_L(); //CS 片选开
- Vk1072C_Delay(VK1072C_CLK/2);
- VK1072C_DATA_H();
- WriteClockVk1072C();
- VK1072C_DATA_L();
- WriteClockVk1072C();
- VK1072C_DATA_L();
- WriteClockVk1072C();
- for(i = 0; i < 8; i++)
- {
- if(Shift & FunctonCode)
- VK1072C_DATA_H();
- else
- VK1072C_DATA_L();
- WriteClockVk1072C();
- Shift = Shift >> 1;
- }
- VK1072C_DATA_L();
- WriteClockVk1072C(); //发送一个 0 16xx中最后一位 X
- VK1072C_CS_H(); //CS 片选关
- Vk1072C_Delay(VK1072C_CLK/2);
- VK1072C_DATA_H();
- }
- /**
- * @brief Write 1 data to Vk1072C
- * @param Addr->写入ram的地址
- * @param Databuf->写入ram的数据
- * @retval None
- */
- void Write1DataVk1072C(unsigned char Addr,unsigned char Dat)
- {
- unsigned char i;
- unsigned char Shift;
-
- Shift = 0x20; //屏蔽高3位 只用5位
-
- VK1072C_CS_L(); //CS 片选开
- Vk1072C_Delay(VK1072C_CLK/2);
- VK1072C_DATA_H();
- WriteClockVk1072C(); //101
- VK1072C_DATA_L();
- WriteClockVk1072C();
- VK1072C_DATA_H();
- WriteClockVk1072C();
- for( i = 0; i < vk1072C_addrbit; i++)
- {
- if (Addr & Shift)
- VK1072C_DATA_H();
- else
- VK1072C_DATA_L();
- WriteClockVk1072C();
- Shift = Shift >> 1;
- }
- Shift = 0x01;
- for (i = 0; i < 4; i++)
- {
- if( Dat & Shift)
- VK1072C_DATA_H();
- else
- VK1072C_DATA_L();
- WriteClockVk1072C();
- Shift = Shift << 1;
- }
- VK1072C_CS_H(); //CS 片选关
- Vk1072C_Delay(VK1072C_CLK/2);
- VK1072C_DATA_H();
- }
- /**
- * @brief Write n data to Vk1072C
- * @param Addr->写入ram的起始地址
- * @param Databuf->写入ram的数据buffer
- * @param Cnt->写入ram的数据个数
- * @retval None
- */
- void WritenDataVk1072C(unsigned char Addr,unsigned char *Databuf,unsigned char Cnt)
- {
- unsigned char i,j;
- unsigned char Shift;
- unsigned char dataval;
-
- VK1072C_CS_L(); //CS 片选开
- Vk1072C_Delay(VK1072C_CLK/2);
- VK1072C_DATA_H();
- WriteClockVk1072C(); //101
- VK1072C_DATA_L();
- WriteClockVk1072C();
- VK1072C_DATA_H();
- WriteClockVk1072C();
- Shift = 0x20;
- for( i = 0; i < vk1072C_addrbit; i++)
- {
- if (Addr & Shift)
- VK1072C_DATA_H();
- else
- VK1072C_DATA_L();
- WriteClockVk1072C();
- Shift = Shift >> 1;
- }
- for (j = 0; j < Cnt; j++)
- {
- Shift = 0x01;
- dataval=*Databuf++;
- for (i = 0; i < 4; i++)
- {
- if( dataval & Shift)
- VK1072C_DATA_H();
- else
- VK1072C_DATA_L();
- WriteClockVk1072C();
- Shift = Shift << 1;
- }
- }
- VK1072C_CS_H(); //CS 片选关
- Vk1072C_Delay(VK1072C_CLK/2);
- VK1072C_DATA_H();
- }
- /**
- * @brief lcd全显或全灭
- * @param state=1->lcd全亮
- * @param state=0->lcd全灭
- * @retval None
- */
- void Vk1072C_DisAll(unsigned char state)
- {
- unsigned char segi;
- unsigned char dispram[31];
-
- for(segi=0;segi<VK1072C_SEGNUM;segi++)
- {
- dispram[segi]=0;
- }
- for(segi=0;segi<VK1072C_SEGNUM;segi++)
- {
- if(state==1)
- {
- if(vk1072C_maxcom==2)
- vk1072C_dispram[segi]=0x03;
- else if(vk1072C_maxcom==3)
- vk1072C_dispram[segi]=0x07;
- else//maxcom==4
- vk1072C_dispram[segi]=0x0f;
- }
- else
- {
- vk1072C_dispram[segi]=0x00;
- }
- dispram[vk1072C_segtab[segi]]=vk1072C_dispram[segi];
- }
- WritenDataVk1072C(0,dispram,31);
- }
- /**
- * @brief lcd显示单个点(1comx1seg)
- * @param Seg->点对应的seg
- * @param Com->点对应的com
- * @retval None
- */
- void Vk1072C_DisSegOn(unsigned char Seg,unsigned char Com)
- {
- vk1072C_dispram[Seg-vk1072C_segtab[0]]|=(1<<Com);
- Write1DataVk1072C(Seg,vk1072C_dispram[Seg-vk1072C_segtab[0]]);
- }
- /**
- * @brief lcd关闭单个点(1comx1seg)
- * @param None
- * @retval None
- */
- void Vk1072C_DisSegOff(unsigned char Seg,unsigned char Com)
- {
- vk1072C_dispram[Seg-vk1072C_segtab[0]]&=~(1<<Com);
- Write1DataVk1072C(Seg,vk1072C_dispram[Seg-vk1072C_segtab[0]]);
- }
- /**
- * @brief 配置Vk1072C通信线GPIO.
- * @param None
- * @retval None
- */
- void Vk1072C_Lowlevel_Init(void)
- {
- //通信线电平不同,建议加电平转换电路
- //此函数根据客户单片机做相应的修改
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable the Vk1072C GPIO Clock */
- RCC_APB2PeriphClockCmd(VK1072C_CS_GPIO_CLK , ENABLE);
- RCC_APB2PeriphClockCmd(VK1072C_WR_GPIO_CLK , ENABLE);
- RCC_APB2PeriphClockCmd(VK1072C_DAT_GPIO_CLK , ENABLE);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin = VK1072C_CS_PIN;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//通讯线电平不同,可设为开漏输出,1072C有内部上拉电阻
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通信线电平一样,可设为推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(VK1072C_CS_GPIO_PORT, &GPIO_InitStructure);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin =VK1072C_WR_PIN;
- GPIO_Init(VK1072C_WR_GPIO_PORT, &GPIO_InitStructure);
- /* Configure the GPIO pin */
- GPIO_InitStructure.GPIO_Pin =VK1072C_DAT_PIN;
- GPIO_Init(VK1072C_WR_GPIO_PORT, &GPIO_InitStructure);
- }
- /**
- * @brief Vk1072C进入掉电低功耗模式
- * @param None
- * @retval None
- */
- void Vk1072C_Enter_Standby(void)
- {
- //连续使用命令LCDOFF和SYSDIS,系统将处于掉电低功耗状态。
- //只有使用片内RC时钟源时,才能使系统进入掉电低功耗状态。
- WriteCommandVk1072C(OSC_OFF);
- WriteCommandVk1072C(DISP_OFF);
- }
- /**
- * @brief Vk1072C退出掉电低功耗模式
- * @param None
- * @retval None
- */
- void Vk1072C_Exit_Standby(void)
- {
- //退出掉电低功耗状态重新配置vk1072C
- WriteCommandVk1072C(OSC_ON);
- WriteCommandVk1072C(DISP_ON);
- WriteCommandVk1072C(COM_1_3__4);vk1072C_maxcom=4; //1/3bias 4com
- // WriteCommandVk1072C(COM_1_3__3);vk1072C_maxcom=3; //1/3bias 3com
- // WriteCommandVk1072C(COM_1_3__2);vk1072C_maxcom=2; //1/3bias 2com
- // WriteCommandVk1072C(COM_1_2__4);vk1072C_maxcom=4; //1/2bias 4com
- // WriteCommandVk1072C(COM_1_2__3);vk1072C_maxcom=3; //1/2bias 3com
- // WriteCommandVk1072C(COM_1_2__2);vk1072C_maxcom=2; //1/2bias 2com
- }
- /**
- * @brief Vk1072C Init
- * @param None
- * @retval None
- */
- void Vk1072C_Init(void)
- {
- //管脚配置根据客户单片机做相应的修改
- Vk1072C_Lowlevel_Init();
- //基本配置
- WriteCommandVk1072C(OSC_ON);
- WriteCommandVk1072C(DISP_ON);
- WriteCommandVk1072C(COM_1_3__4);vk1072C_maxcom=4; //1/3bias 4com
- // WriteCommandVk1072C(COM_1_3__3);vk1072C_maxcom=3; //1/3bias 3com
- // WriteCommandVk1072C(COM_1_3__2);vk1072C_maxcom=2; //1/3bias 2com
- // WriteCommandVk1072C(COM_1_2__4);vk1072C_maxcom=4; //1/2bias 4com
- // WriteCommandVk1072C(COM_1_2__3);vk1072C_maxcom=3; //1/2bias 3com
- // WriteCommandVk1072C(COM_1_2__2);vk1072C_maxcom=2; //1/2bias 2com
- //上电默认配置(以下未用功能关闭降低功耗)
- WriteCommandVk1072C(TIMER_DIS);
- WriteCommandVk1072C(WDT_DIS);
- WriteCommandVk1072C(BUZZ_OFF);
- WriteCommandVk1072C(IRQ_DIS);
- }
- /**
- * @brief Vk1072C测试主程序
- * @param None
- * @retval None
- */
- void Vk1072C_Main(void)
- {
- Vk1072C_Init();
- Vk1072C_DisAll(0);
- while(1)
- {
- Vk1072C_DisAll(1); //LCD全显
- Vk1072C_Delay(50*VK1072C_BASE100MS); //延时5S
- Vk1072C_DisAll(0); //LCD全关
- Vk1072C_Delay(50*VK1072C_BASE100MS); //延时5S
- for(vk1072C_segi=0;vk1072C_segi<VK1072C_SEGNUM;vk1072C_segi++)//seg
- {
- for(vk1072C_comi=0;vk1072C_comi<vk1072C_maxcom;vk1072C_comi++)//com
- {
- Vk1072C_DisSegOn(vk1072C_segtab[vk1072C_segi],vk1072C_comi); //LCD单个seg点亮
- Vk1072C_Delay(3*VK1072C_BASE100MS); //延时300mS
- Vk1072C_DisAll(0); //LCD全关
- }
- }
- Vk1072C_DisAll(1); //LCD全显
- Vk1072C_Delay(10*VK1072C_BASE100MS); //延时1S
- for(vk1072C_segi=0;vk1072C_segi<VK1072C_SEGNUM;vk1072C_segi++)//seg
- {
- for(vk1072C_comi=0;vk1072C_comi<vk1072C_maxcom;vk1072C_comi++)//com
- {
- Vk1072C_DisSegOff(vk1072C_segtab[vk1072C_segi],vk1072C_comi); //LCD单个seg关闭
- Vk1072C_Delay(3*VK1072C_BASE100MS); //延时300mS
- Vk1072C_DisAll(1); //LCD全显
- }
- }
- Vk1072C_Delay(10*VK1072C_BASE100MS);//延时1S
- Vk1072C_DisAll(0); //LCD全关
- Vk1072C_Delay(10*VK1072C_BASE100MS);//延时1S
- Vk1072C_Enter_Standby(); //Vk1072C进入掉电低功耗模式
- Vk1072C_Delay(50*VK1072C_BASE100MS);//延时5S
- Vk1072C_Exit_Standby(); //Vk1072C退出掉电低功耗模式
- }
- }
- /************************END OF FILE****/
复制代码
所有资料51hei提供下载:
vk1072.rar
(3.62 KB, 下载次数: 22)
|