四位数码管显示温度源码程序--STC89C52实时用PCF8591采集温度四位数码管显示
一、创建头文件74hc595.h代码如下:
#ifndef __74HC595_H_ #define __74HC595_H_ #include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit DIO = P1^0; //串行数据输入 sbit HCRCLK = P1^1; //时钟脉冲信号——上升沿有效 sbit HCSCLK = P1^2; //打入信号————上升沿有效 void LED4_Display (void); // LED显示 void LED_OUT(uchar X); // LED单字节串行移位函数 void LED4_data(uint num); #endif
二、创建编译文件74hc595.c代码如下:
#include "74hc595.h" unsigned char smgduan[]={0,1,2,3,4,5,6,7,8,9}; //----------------------------------------------------------------------------- // 函数原形定义 unsigned char code LED_0F[]; // LED字模表 //----------------------------------------------------------------------------- // 全局变量 uchar LED[8]; //用于LED的8位显示缓存 uint num; void LED4_data(uint num) { LED[3]=smgduan[num/10]; LED[2]=smgduan[num%10]; LED[1]=17; LED[0]=12; } void LED4_Display (void) { unsigned char code *led_table; // 查表指针 uchar i; //显示第1位 led_table = LED_0F + LED[0]; i = *led_table; LED_OUT(i); LED_OUT(0x01); HCRCLK = 0; HCRCLK = 1; //显示第2位 led_table = LED_0F + LED[1]; i = *led_table; LED_OUT(i); LED_OUT(0x02); HCRCLK = 0; HCRCLK = 1; //显示第3位 led_table = LED_0F + LED[2]; i = *led_table; LED_OUT(i); LED_OUT(0x04); HCRCLK = 0; HCRCLK = 1; //显示第4位 led_table = LED_0F + LED[3]; i = *led_table; LED_OUT(i); LED_OUT(0x08); HCRCLK = 0; HCRCLK = 1; } void LED_OUT(uchar X) { uchar i; for(i=8;i>=1;i--) { if (X&0x80) DIO=1; else DIO=0; X<<=1; HCSCLK = 0; HCSCLK = 1; } } unsigned char code LED_0F[] = {// 0 1 2 3 4 5 6 7 8 9 A b C d E F - 摄氏度 关 共阳极 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0x8E,0xbf,0x9C,0xFF };