|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2011-4-4 22:44:09
|
显示全部楼层
2011/04/05 0:26
-------------------------------------------------------HT1632.C
#include "HT1632C.H"
#include "common.h"
#include "FONT_5x7.H"
#include "FONT_7x16.H"
#include "BINARY.H"
#include <intrins.h>
#define NOP() _nop_();_nop_();_nop_();_nop_();_nop_() //延时, 单片机的时钟相对于HT1632C的时钟较快
//接口定义
sbit HT1632C_DATA = P1^3;
sbit HT1632C_WR = P1^2;
sbit HT1632C_RD = P1^1;
sbit HT1632C_CS1 = P2^4;
sbit HT1632C_CS2 = P1^0;
sbit HT1632C_CS3 = P1^5;
sbit HT1632C_CS4 = P1^4;
static u8 xdata DisplayBuffer[4][48]; //显示缓存, 牺牲空间换取时间
static u8 xdata TimeUI_2_Buffer[2][6]={10,10,10,10,10,10,10,10,10,10,10,10}; //时间数据,分别保存上一次和这一次的数据
static u8 xdata TimeUI_2_Step=0; //时间特效显示步骤
static u8 code posX[6]={0,7,17,24,34,41}; //每一位数字显示的X位置
static s8 xdata VisIndex=0; //频谱编号
static s8 xdata VisPeakFallTime=0; //峰值回落延时
static s8 xdata VisPeak[16]={0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; //频谱峰值
static u8 code VisTable[16][2]={ //相当于频谱的"字模" 需要 "BINARY.H" 支持
B00000000,B00000001,
B00000000,B00000011,
B00000000,B00000111,
B00000000,B00001111,
B00000000,B00011111,
B00000000,B00111111,
B00000000,B01111111,
B00000000,B11111111,
B00000001,B11111111,
B00000011,B11111111,
B00000111,B11111111,
B00001111,B11111111,
B00011111,B11111111,
B00111111,B11111111,
B01111111,B11111111,
B11111111,B11111111,
};
static u8 code VisPeakTable[16][2]={ //频谱峰值的"字模"
B00000000,B00000001,
B00000000,B00000010,
B00000000,B00000100,
B00000000,B00001000,
B00000000,B00010000,
B00000000,B00100000,
B00000000,B01000000,
B00000000,B10000000,
B00000001,B00000000,
B00000010,B00000000,
B00000100,B00000000,
B00001000,B00000000,
B00010000,B00000000,
B00100000,B00000000,
B01000000,B00000000,
B10000000,B00000000,
};
//HT1632C片选
static void HT_CS(u8 ucCSNo)
{
switch(ucCSNo)
{
case 1:
HT1632C_CS2=1;
HT1632C_CS3=1;
HT1632C_CS4=1;
HT1632C_CS1=0;
break;
case 2:
HT1632C_CS1=1;
HT1632C_CS3=1;
HT1632C_CS4=1;
HT1632C_CS2=0;
break;
case 3:
HT1632C_CS1=1;
HT1632C_CS2=1;
HT1632C_CS4=1;
HT1632C_CS3=0;
break;
case 4:
HT1632C_CS1=1;
HT1632C_CS2=1;
HT1632C_CS3=1;
HT1632C_CS4=0;
break;
case 0xAA:
HT1632C_CS1=0;
HT1632C_CS2=0;
HT1632C_CS3=0;
HT1632C_CS4=0;
default:
HT1632C_CS1=1;
HT1632C_CS2=1;
HT1632C_CS3=1;
HT1632C_CS4=1;
}
}
//HT1632C发送数据
static void HT_SendData(u8 ucDat,u8 ucNo)
{
u8 i;
for (i=0;i<ucNo;i++)
{
NOP();
HT1632C_WR=0;
ucDat<<=1;
HT1632C_DATA=CY;
NOP();
HT1632C_WR=1;
}
}
//向对应的HT1632C发送命令
void HT_CMD(u8 ucCS, u8 ucCMD)
{
HT_CS(ucCS);
HT_SendData(0x80,3); //B100x xxxx
HT_SendData(ucCMD,9); //9bit
HT_CS(0xFF);
}
//初始化
void HT_Init(void)
{
u8 i;
HT1632C_DATA = 1;
HT1632C_WR=1;
HT1632C_RD=1;
HT1632C_CS1=1;
HT1632C_CS2=1;
HT1632C_CS3=1;
HT1632C_CS4=1;
HT_CMD(1,CMD_MODE_MASTER);
HT_CMD(1,CMD_NMOS_16COM);
HT_CMD(1,CMD_OSC_ON);
HT_CMD(1,CMD_PWM_DUTY+5);
HT_CMD(1,CMD_LED_ON);
HT_CMD(2,CMD_MODE_SLAVE);
HT_CMD(2,CMD_NMOS_16COM);
HT_CMD(2,CMD_OSC_ON);
HT_CMD(2,CMD_PWM_DUTY+5);
HT_CMD(2,CMD_LED_ON);
HT_CMD(3,CMD_MODE_SLAVE);
HT_CMD(3,CMD_NMOS_16COM);
HT_CMD(3,CMD_OSC_ON);
HT_CMD(3,CMD_PWM_DUTY+5);
HT_CMD(3,CMD_LED_ON);
HT_CMD(4,CMD_MODE_SLAVE);
HT_CMD(4,CMD_NMOS_16COM);
HT_CMD(4,CMD_OSC_ON);
HT_CMD(4,CMD_PWM_DUTY+5);
HT_CMD(4,CMD_LED_ON);
for (i=0;i<48;i++) //清空"显存"
{
DisplayBuffer[0][i]=0x00;
DisplayBuffer[1][i]=0x00;
DisplayBuffer[2][i]=0x00;
DisplayBuffer[3][i]=0x00;
}
}
//将显存刷新到点阵
void HT_Buffer2Screen(void)
{
u8 i;
HT_CS(1);
HT_SendData(0xA0,3); //B101x xxxx
HT_SendData(0x00,7); //Address
for (i=0;i<24;i++)
{
HT_SendData(DisplayBuffer[0][i],8);
HT_SendData(DisplayBuffer[2][i],8);
}
HT_CS(2);
HT_SendData(0xA0,3); //B101x xxxx
HT_SendData(0x00,7); //Address
for (i=24;i<48;i++)
{
HT_SendData(DisplayBuffer[0][i],8);
HT_SendData(DisplayBuffer[2][i],8);
}
HT_CS(3);
HT_SendData(0xA0,3); //B101x xxxx
HT_SendData(0x00,7); //Address
for (i=0;i<24;i++)
{
HT_SendData(DisplayBuffer[1][i],8);
HT_SendData(DisplayBuffer[3][i],8);
}
HT_CS(4);
HT_SendData(0xA0,3); //B101x xxxx
HT_SendData(0x00,7); //Address
for (i=24;i<48;i++)
{
HT_SendData(DisplayBuffer[1][i],8);
HT_SendData(DisplayBuffer[3][i],8);
}
HT_CS(0xFF);
}
//向点阵发送一列数据
void HT_DataXY(u8 ucX, u8 ucDatR, u8 ucDatG)
{
HT_CS(ucX/24+1); //由X坐标计算该片选哪个
HT_SendData(0xA0,3); //B101x xxxx
HT_SendData((((ucX%24)*4)<<1),7); //Address
HT_SendData(ucDatR,8);
HT_SendData(ucDatG,8);
HT_CS(0xFF);
}
//打印5x7字符
void HT_Put5x7(u8 ucX, u8 ucASCIIR, u8 ucASCIIG)
{
u8 i;
for(i=0;i<6;i++)
{
HT_DataXY(ucX,FONT_5x7[ucASCIIR][i],FONT_5x7[ucASCIIG][i]);
ucX++;
}
}
//打印5x7字符串
void HT_Str1(u8 * pStr)
{
u8 i=0;
while(*pStr)
{
HT_Put5x7(i,* pStr,* pStr);
pStr++;
i+=6;
}
}
//输出一位时钟到显存中, ucIndex 位号, ucStep: 动态效果到第几步
static void HT_TimeUI_2_A0(u8 ucIndex,u8 ucStep)
{
u8 i,Redir[7],NewBuffer=0; //Newbuffer=0:即将翻转消隐的旧字符, =1:翻转出新字符
switch(ucStep) //数字翻转的效果, 不同的步骤取字模中不同的列
{
case 0:
for(i=0;i<7;i++) { Redir[i]=i; }
NewBuffer=0;
break;
case 5:
NewBuffer=1;
for(i=0;i<7;i++) { Redir[i]=i; }
break;
case 1:
NewBuffer=0;
Redir[0]=6;Redir[1]=0;Redir[2]=2;Redir[3]=3;Redir[4]=5;Redir[5]=6;Redir[6]=6;
break;
case 4:
NewBuffer=1;
Redir[0]=6;Redir[1]=0;Redir[2]=2;Redir[3]=3;Redir[4]=5;Redir[5]=6;Redir[6]=6;
break;
case 2:
NewBuffer=0;
Redir[0]=6;Redir[1]=6;Redir[2]=0;Redir[3]=5;Redir[4]=6;Redir[5]=6;Redir[6]=6;
break;
case 3:
NewBuffer=1;
Redir[0]=6;Redir[1]=6;Redir[2]=0;Redir[3]=5;Redir[4]=6;Redir[5]=6;Redir[6]=6;
break;
}
for(i=0;i<7;i++)
{
DisplayBuffer[2][posX[ucIndex]+i]= FONT_7x16[TimeUI_2_Buffer[NewBuffer][ucIndex]][Redir[i]][0];
DisplayBuffer[3][posX[ucIndex]+i]= FONT_7x16[TimeUI_2_Buffer[NewBuffer][ucIndex]][Redir[i]][1];
}
}
//更新新旧时间缓存的数据
void HT_TimeUI_2(u8 ucH, u8 ucM, u8 ucS)
{
u8 i;
for (i=0;i<6;i++) //使当前时间变为旧时间
{
TimeUI_2_Buffer[0][i]=TimeUI_2_Buffer[1][i];
}
//获取新时间
TimeUI_2_Buffer[1][0]=ucH/10;
TimeUI_2_Buffer[1][1]=ucH%10;
TimeUI_2_Buffer[1][2]=ucM/10;
TimeUI_2_Buffer[1][3]=ucM%10;
TimeUI_2_Buffer[1][4]=ucS/10;
TimeUI_2_Buffer[1][5]=ucS%10;
//特效归零,需要从头开始
TimeUI_2_Step=0;
}
//时间模式前需要清屏, 以及显示冒号
void HT_TimeUI_2_Dot(void)
{
u8 i;
for (i=0;i<48;i++) //清空显存
{
DisplayBuffer[0][i]=0x00;
DisplayBuffer[1][i]=0x00;
DisplayBuffer[2][i]=0x00;
DisplayBuffer[3][i]=0x00;
}
for (i=0;i<6;i++) //清空时间缓存
{
TimeUI_2_Buffer[0][i]=10;
TimeUI_2_Buffer[1][i]=10;
}
//显存中冒号的字模
DisplayBuffer[2][14]=0x0C;
DisplayBuffer[2][15]=0x0C;
DisplayBuffer[2][16]=0x00;
DisplayBuffer[2][31]=0x0C;
DisplayBuffer[2][32]=0x0C;
DisplayBuffer[2][33]=0x00;
DisplayBuffer[3][14]=0x30;
DisplayBuffer[3][15]=0x30;
DisplayBuffer[3][16]=0x00;
DisplayBuffer[3][31]=0x30;
DisplayBuffer[3][32]=0x30;
DisplayBuffer[3][33]=0x00;
}
//刷新时间
void HT_TimeUI_2_Update(void)
{
u8 i=0;
s8 iStep2;
if (TimeUI_2_Step>18) return;
for (i=0;i<6;i++) //计算每一位时间到了动态效果的哪一步
{
if (TimeUI_2_Buffer[0][i]!=TimeUI_2_Buffer[1][i])
{
iStep2=TimeUI_2_Step-(5-i)*2;
if (iStep2<0) iStep2=0;
if (iStep2>5) iStep2=5;
HT_TimeUI_2_A0(i,iStep2); //刷新显存中的每一位
}
}
TimeUI_2_Step++; //动态效果步骤
HT_Buffer2Screen();
}
void HT_VisReset(void) //频谱重置
{
VisIndex=0;
}
void HT_VisData(u8 ucData) //压入频谱数据
{
//更新对应缓存, 两列一样, 第三列为空
DisplayBuffer[2][VisIndex*3]=VisTable[ucData][0];
DisplayBuffer[3][VisIndex*3]=VisTable[ucData][1];
DisplayBuffer[2][VisIndex*3+1]=VisTable[ucData][0];
DisplayBuffer[3][VisIndex*3+1]=VisTable[ucData][1];
DisplayBuffer[2][VisIndex*3+2]=0x00;
DisplayBuffer[3][VisIndex*3+2]=0x00;
if (ucData>VisPeak[VisIndex]) VisPeak[VisIndex]=ucData; //大于峰值, 则更新峰值
VisIndex++;
if (VisIndex>15) VisIndex=15;
}
void HT_VisUpdate(void) //刷新频谱效果
{
u8 i;
for (i=0;i<16;i++) //填充峰值数据(红色)
{
DisplayBuffer[0][i*3]=VisPeakTable[VisPeak[i]][0];
DisplayBuffer[1][i*3]=VisPeakTable[VisPeak[i]][1];
DisplayBuffer[0][i*3+1]=VisPeakTable[VisPeak[i]][0];
DisplayBuffer[1][i*3+1]=VisPeakTable[VisPeak[i]][1];
DisplayBuffer[0][i*3+2]=0x00;
DisplayBuffer[1][i*3+2]=0x00;
}
VisPeakFallTime++; //峰值回落计数, 可以调节回落速度
if (VisPeakFallTime>2)
{
for (i=0;i<16;i++) //开始回落
{
VisPeak[i]--;
if (VisPeak[i]<0) VisPeak[i]=0; //到最低, 不再变化
}
VisPeakFallTime=0; //峰值回落计数归零
}
HT_Buffer2Screen(); //刷新显存到屏幕
}
<font color=red>2011:04:05 10:30
-----------------------------HT1632C.H 文件内容--------------------------
#ifndef __HT1632C_H__
#define __HT1632C_H__
#include "common.h"
#define CMD_SYS_OFF 0x00
#define CMD_OSC_ON 0x01
#define CMD_LED_OFF 0x02
#define CMD_LED_ON 0x03
#define CMD_BLINK_OFF 0x08
#define CMD_BLINK_ON 0x09
#define CMD_MODE_SLAVE 0x10
#define CMD_MODE_MASTER 0x18
#define CMD_MODE_MASEXT 0x1C
#define CMD_NMOS_8COM 0x20
#define CMD_NMOS_16COM 0x24
#define CMD_PMOS_8COM 0x28
#define CMD_PMOS_16COM 0x2C
#define CMD_PWM_DUTY 0xA0
void HT_Init(void);
void HT_CMD(u8 ucCS, u8 ucCMD);
void HT_Buffer2Screen(void);
void HT_Put5x7(u8 ucX, u8 ucASCIIR, u8 ucASCIIG);
void HT_Str1(u8 * pStr);
void HT_TimeUI_2(u8 ucH, u8 ucM, u8 ucS);
void HT_TimeUI_2_Dot(void);
void HT_TimeUI_2_Update(void);
void HT_VisReset(void);
void HT_VisData(u8 ucData);
void HT_VisUpdate(void);<font color="#0000FF">
#endif |
|