接下来就该贴代码了。
7.3.1 74HC595驱动部分
在这里用了两片595,共用移位时钟和所存时钟,详细介绍在代码里头有,俺就不重复了。
/************************************************************
FileName: hc595.h
Author: 原野之狼
Version :V1.0
Date: 2009.12.17
Description:
1 Be care of that the two hc595s are share of the shift clk & latch clk,but the dat line is not share.
First make the dat line of the two hc595s ready,
then just shift it through the shift clk,when 8 bits are done,latch it through the latch clk.
History:
<author> <time> <version > <desc>
************************************************************/
#ifndef _H_HC595_H_
#define _H_HC595_H_
#include "type.h"
#ifdef __cplusplus
extern "C" {
#endif
//some define of the shift clk io
#define PORT_SHCP PORTE
#define DDR_SHCP DDRE
#define BIT_SHCP 5
//some define of the latch clk io
#define PORT_STCP PORTE
#define DDR_STCP DDRE
#define BIT_STCP 4
//set & reset the shift clk
#define SET_SHCP PORT_SHCP|=(0x01<<BIT_SHCP)
#define RST_SHCP PORT_SHCP&=(~(0x01<<BIT_SHCP))
//set & reset the latch clk
#define SET_STCP PORT_STCP|=(0x01<<BIT_STCP)
#define RST_STCP PORT_STCP&=(~(0x01<<BIT_STCP))
//some define of the dat line,it's about the 1# hc595 which is used for segment driver
#define PORT_DAT1 PORTE
#define DDR_DAT1 DDRE
#define BIT_DAT1 2
#define SET_DAT1 PORT_DAT1|=(0x01<<BIT_DAT1)
#define RST_DAT1 PORT_DAT1&=(~(0x01<<BIT_DAT1))
//some define of the dat line,it's about the 2# hc595 which is used for bit driver
#define PORT_DAT2 PORTE
#define DDR_DAT2 DDRE
#define BIT_DAT2 3
#define SET_DAT2 PORT_DAT2|=(0x01<<BIT_DAT2)
#define RST_DAT2 PORT_DAT2&=(~(0x01<<BIT_DAT2))
//delay time slot
#define DELAY595 do{\
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");\
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");\
asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");\
}while(0)
//this can genarate a positive pulse at the shift clk pin of the two hc595s
#define SH_POSITIVE_PULSE do{\
RST_SHCP;\
DELAY595;\
SET_SHCP;\
DELAY595;\
} while(0)
//this can genarate a negtive pulse at the latch clk pin of the two hc595s
#define LT_NEGATIVE_PULSE do{\
RST_STCP;\
DELAY595;\
SET_STCP;\
DELAY595;\
}while(0)
void init_595(void);
void driver_595(UINT8 dat1,UINT8 dat2);
#ifdef __cplusplus
}
#endif
#endif
/************************************************************
FileName: hc595.c
Author: 原野之狼
Version :V1.0
Date: 2009.12.17
Description:
History:
<author> <time> <version > <desc>
************************************************************/
#include "includes.h"
/***********************************************************
Description:
intial the port
Modify Record:
***********************************************************/
void init_595(void)
{
SET_SHCP;
DDR_SHCP |= (0x01 << BIT_SHCP);
SET_STCP;
DDR_STCP |= (0x01 << BIT_STCP);
SET_DAT1;
DDR_DAT1 |= (0x01 << BIT_DAT1);
SET_DAT2;
DDR_DAT2 |= (0x01 << BIT_DAT2);
}
/***********************************************************
Description:
driver the two hc595s
Input:
dat1:data of the 1# hc595
dat2:data of the 2# hc595
Modify Record:
***********************************************************/
void driver_595(UINT8 dat1,UINT8 dat2)
{
UINT8 i;
for (i = 0; i < 8; i++)
{
if (dat1&0x80)
{
SET_DAT1;
}
else
{
RST_DAT1;
}
if (dat2&0x80)
{
SET_DAT2;
}
else
{
RST_DAT2;
}
SH_POSITIVE_PULSE;
dat1 <<= 0x01;
dat2 <<= 0x01;
}
LT_NEGATIVE_PULSE;
}
|