|
/**************************************************************/
#ifndef __HC595_H__
#define __HC595_H__
#include <STC8.h>
#include <intrins.h>
#ifndef u8 // 重命名
#define u8 unsigned char
#endif
#ifndef u16 // 重命名
#define u16 unsigned int
#endif
sbit P_HC595_SRCLK = P3^5; // 數(shù)據(jù)輸入時鐘線
sbit P_HC595_RCLK = P3^4; // 輸出存儲器鎖存時鐘線
sbit P_HC595_SER = P3^7; // 串行數(shù)據(jù)輸入
void SEG_HC595send(unsigned char x); // hc595發(fā)送一個字節(jié)數(shù)據(jù)
void display(unsigned char pos,unsigned char dat); // pos位數(shù)碼管顯示數(shù)字dat
#endif
/****************************************************************/
#include "hc595.h"
// 段選:dp、g、f、e、d、c、b、a
unsigned char const LedData[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xFF , 0x00, 0xbf,0x7f,0x89};
// "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" "全滅" "全亮" "-" "." "H"
// 位選:CS1、CS2、CS3、CS4、CS5、CS6、CS7、CS8
unsigned char const LedPos[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void delay_ms(unsigned int xms) // 延時xms毫秒
{
unsigned char i, j;
unsigned int x;
for(x=xms;x>0;x--)
{
i = 16;
j = 147;
do
{
while (--j);
} while (--i);
}
}
/*************************************************
* 函數(shù)名:Send_595
* 描述 :hc595發(fā)送一個字節(jié)數(shù)據(jù)
* 參數(shù) :dat (位選或段選)
* 返回值:無
* 調(diào)用 :內(nèi)部調(diào)用
*************************************************/
void Send_595(unsigned char dat)
{
u8 i;
for(i=0; i<8; i++)
{
dat <<= 1;
P_HC595_SER = CY;
P_HC595_SRCLK = 0;
_nop_();
P_HC595_SRCLK = 1;
}
}
/*************************************************
* 函數(shù)名:display
* 描述 :pos位數(shù)碼管顯示數(shù)字dat
* 參數(shù) :pos,dat
* 返回值:無
* 調(diào)用 :外部調(diào)用
*************************************************/
void display(unsigned char pos,unsigned char dat)
{
Send_595(LedPos[pos]); //
Send_595(LedData[dat]);
P_HC595_RCLK = 0;
_nop_();
P_HC595_RCLK = 1;
delay_ms(2);
}
/**********************************************************/ |
|