📄 hd7279c.c
字号:
/*
;**************************************************************
;* 北京达盛科技有限公司
;* 研 发 部
;*
;* http://www.techshine.com
;*************************************************************/
/*----------------------- 文件信息 ----------------------------
;*
;* 文件名称 : HD7279c.C
;* 适用平台 : E-PLAY-SOPC 1C12 CPU板
;* CPU类型 :SOPC板
;* 软件环境 : NIOS II IDE
;* 扩展模块 : 7279 E-PLAY 扩展板
;* 试验现象 : 运行程序。观察7279模块上数码管的显示,当最右端数码管闪烁"_"时,请按键。
**********************************************************************/
//------------------头文件--------------------------------------
#include "stdio.h"
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include "unistd.h"
#include "sys/alt_irq.h"
#include "string.h"
/*********************** 宏定义 ***************
*************************************************************/
#define CMD_RESET 0xa4 //复位
#define CMD_TEST 0xbf //测试
#define RTL_UNCYL 0xa1 //左移
#define RTR_UNCYL 0xa0 //右移
#define RTL_CYCLE 0xa3 //循环左移
#define RTR_CYCLE 0xa2 //循环右移
#define DECODE0 0x80 //下载数据按方式0译码
#define DECODE1 0xc8 //下载数据按方式1译码
#define UNDECODE 0x90 //下载数据但不译码
#define BLINKCTL 0x88 //闪烁控制
#define ACTCTL 0x98 //消隐控制
#define SEGON 0xe0 //段点亮
#define SEGOFF 0xc0 //段关闭
#define CMD_READ 0x15 //读键盘数据
//----------------------------------------------------------
unsigned char temp=0;
unsigned int CommFlag = 0;
int j=0;
/***************************************************
函数定义
***************************************************/
void dellay(unsigned int t)
{
unsigned int i;
for(i=0;i<t*2;i++); //延时等待转换完成
}
void SetCS(void)
{
IOWR(CS7279_BASE,0,0x01);
}
void SetCLK(void)
{
IOWR(CLK7279_BASE,0,0x01);
}
void SetDAT(void) //LEDG[0]
{
IOWR(DATA7279_BASE,1,0x01);
IOWR(DATA7279_BASE,0,0x01);
}
void ClrCS(void)
{
IOWR(CS7279_BASE,0,0x00);
}
void ClrCLK(void)
{
IOWR(CLK7279_BASE,0,0x00);
}
void ClrDAT(void)
{
IOWR(DATA7279_BASE,1,0x01);
IOWR(DATA7279_BASE,0,0x00);
}
unsigned char read_DATA()
{
unsigned char value=0;
IOWR(DATA7279_BASE,1,0x00);
value = IORD(DATA7279_BASE,0) & 0x01;
return value;
}
unsigned char read_KEY() //BUTTON[0]
{
unsigned char value=0;
value = IORD(KEY7279_BASE,0)& 0x01;
return value;
}
void Send7279(unsigned char data)
{
unsigned char i;
ClrCS();
dellay(50);
for(i=0;i<8;i++)
{
if((data&0x80)==0x80)
SetDAT();
else ClrDAT();
SetCLK();
dellay(20);
ClrCLK();
dellay(20);
data<<=1;
}
}
unsigned char Receive7279(void)
{
unsigned char i,buff;
ClrDAT();
dellay(50);
for(i=0;i<8;i++)
{
buff<<=1;
SetCLK(); //7279clk高
dellay(20);
if (( read_DATA() & 0x1) == 0x1)
buff|=1;
else
buff&=0xfe;
ClrCLK(); //7279clk低
dellay(20);
}
SetCS();
dellay(20);
return (buff);
}
void Reset7279(void)
{
Send7279(CMD_RESET); //复位指令
SetCS();
dellay(50000); //25ms延迟 --复位延迟
}
void Test7279(void)
{
Reset7279();
SetCS();
Send7279(CMD_TEST); //测试指令
SetCS();
for(j=0;j<10;j++)
{
dellay(50000);
}
Reset7279();
for(j=0;j<5;j++)
{
dellay(50000);
}
SetCS();
}
void Disp7279(unsigned char shiftmode,unsigned char decodemode,unsigned char dispdata)
{
Send7279(decodemode);
if(dispdata !=0xff)
{
dispdata = dispdata & 0xf;
Send7279(dispdata);
}
for(j=0;j<8;j++)
{
dellay(10000);
}
Send7279(shiftmode); //数据左移指令
SetCS();
for(j=0;j<8;j++)
{
dellay(10000);
}
}
/****************************************************
* 主函数
****************************************************/
int main()
{
unsigned temp;
IOWR(DATA7279_BASE,1,0x01);
//----------------------------------------------------
dellay(50000);
dellay(50000);
Test7279(); //运行测试程序
dellay(50000);
dellay(50000);
dellay(50000);
dellay(50000);
for(temp=0;temp<16;temp++) //送出数据0x00~0x0F
{
Disp7279(RTL_UNCYL,DECODE1,temp);
}
Reset7279(); //清屏
dellay(50000);
dellay(50000);
Send7279(BLINKCTL); //闪烁指令
Send7279(0xfe); //第一位闪烁
Send7279(SEGON); //段显示指令
Send7279(0x03); //显示第一位数码管的D
while(1)
{
if( read_KEY() == 0x00) //是否有键按下
{
Send7279(CMD_READ); //读键值指令
temp = Receive7279(); //读取键值
Disp7279(RTL_UNCYL,DECODE1,temp); //显示键值
Send7279(SEGON); //段显示指令
Send7279(0x03); //显示第一位数码管的D
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -