采用MAX7219芯片做的显示驱动代码
单片机源程序如下:
- #include "reg51.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit DIN = P1^0;
- sbit CLK = P1^1;
- sbit LOAD = P1^2;
- sbit StartKey = P1^3;
- sbit StopKey = P1^4;
- sbit StepKey = P1^5;
- uchar StartFlag=0;
- #define NoOp 0x00
- #define Digit0 0x01
- #define Digit1 0x02
- #define Digit2 0x03
- #define Digit3 0x04
- #define Digit4 0x05
- #define Digit5 0x06
- #define Digit6 0x07
- #define Digit7 0x08
- #define DecodeMode 0x09
- #define Intensity 0x0a
- #define ScanLimit 0x0b
- #define ShutDown 0x0c
- #define DisplayTest 0x0f
- #define ShutdownMode 0x00
- #define NormalOperation 0x01
- #define ScanDigit 0x07
- #define DecodeDigit 0xff
- #define IntensityGrade 0x0a
- #define TestMode 0x01
- #define TextEnd 0x00
- uchar DisBuffer[8]= {0,0,0,0,0,0,0,0};
- void delay(uint t)
- {
- uint i;
- while(t--)
- {
- for(i=0; i<8; i++);
- }
- }
- void SendChar(uchar ch)
- {
- uchar i,temp;
- // _nop_();
- for(i=0; i<8; i++)
- {
- temp=ch&0x80;
- ch=ch<<1;
- if(temp)
- {
- DIN=1;
- CLK=0;
- CLK=1;
- }
- else
- {
- DIN=0;
- CLK=0;
- CLK=1;
- }
- }
- }
- void WriteWord(uchar addr,uchar num)
- {
- LOAD=0;
- // _nop_();
- SendChar(addr);
- // _nop_();
- SendChar(num);
- // _nop_();
- LOAD=1;
- }
- void InitDisplay()
- {
- WriteWord(ScanLimit,ScanDigit);
- WriteWord(DecodeMode,DecodeDigit);
- WriteWord(Intensity,IntensityGrade);
- WriteWord(ShutDown,NormalOperation);
- }
- void main()
- {
- unsigned long StepCount = 0;
- uchar i;
- InitDisplay();
- WriteWord(DisplayTest,TestMode);
- delay(3000);
- WriteWord(DisplayTest,TextEnd);
- while(1)
- {
- if(StartKey == 0)
- {
- delay(100);
- if(StartKey == 0)
- {
- StartFlag = 1;
- for(i = 0;i < 8;i++)
- {
- WriteWord(Digit0+i,0);
- }
- }
- while(!StartKey);
- }
- else if(StopKey == 0)
- {
- delay(100);
- if(StopKey == 0)
- {
- StartFlag = 0;
- }
- while(!StopKey);
- }
- if(StartFlag)
- {
- if(StepKey == 0)
- {
- delay(100);
- if(StepKey == 0)
- {
- StepCount++;
- }
- while(!StepKey);
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
简单计步器.zip
(99.61 KB, 下载次数: 17)
|