⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ceshi.c

📁 单片机8位数码管换屏交替显示
💻 C
字号:
#include <at892051.h>
#include <math.h>
#include <stdio.h>

#define TIMER0_ENABLE TL0=TH0; TR0=1; //TR0 = 1,启动T
#define TIMER0_DISABLE TR0=0;

sbit RESET = P1^7;
sbit SCK = P1^6;
sbit RCK = P1^5;
sbit SER = P1^4;
sbit DB = P1^2;
sbit ZB = P1^3;

unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,          //0,1,2,3,4
                               0x92,0x82,0xf8,0x80,0x90,		  //5,6,7,8,9
                               0x88,0x83,0xc6,0xa1,0x86,		  //a,b,c,d,e
							   0xff};						      //f-灭 
/*
unsigned char code dispcode[]={0x40,0x79,0x24,0x30,0x19,          //0,1,2,3,4
                               0x12,0x02,0x78,0x00,0x10,		  //5,6,7,8,9
                               0x08,0x03,0x46,0x21,0x06,		  //a,b,c,d,e
							   0x7f,0x00};						  //f,灭
*/
							                                
unsigned char bdata SF;
sbit S7 =  SF^7;
sbit S6 =  SF^6;
sbit S5 =  SF^5;
sbit S4 =  SF^4;
sbit S3 =  SF^3;
sbit S2 =  SF^2;
sbit S1 =  SF^1;
sbit S0 =  SF^0;

unsigned char getch;
unsigned long DBData;	                  //大包数据
unsigned long ZBData;					  //中包数据
unsigned long FactData;
unsigned long mul = 10000;
unsigned char data PartData[5];
void PSendChar(unsigned char getch);
void DisPlay(unsigned long FactData);
void delay_5ms(unsigned int t);

void main ( )
{
  

  EA = 0;
  TMOD=0x22;             //定时器0为工作模式2(8位自动重装),0为模式2(8位自动重装) 
  PCON=0x00;
  TH0=(256-96);         //9600bps 就是 1000000/9600=104.167微秒 执行的时间是104.167*11.0592/12= 96 
  TL0=TH0;
  ET0=1;                //定时器/记数器T0的溢出中断允许位,ET,允 许中断
  EA=1;
  DB = 0;
  ZB = 0;
  RCK = 0;
  SCK = 0;
  while (1)
   {
	  RESET = 0;	                             //复位,清屏
      RESET = 1;
      DB = 1;									 //置大包标志
      DisPlay(0x3ABC);
	  delay_5ms(15000);							 //延时1.25ms
	  DB = 0;
	  RESET = 0;	                             //复位,清屏
      RESET = 1;	 							 
	  SCK = 0;
	  ZB = 1;									 //置中包标志
	  DisPlay(0x1f78);
	  delay_5ms(15000);							 //延时1.25ms
	  ZB = 0;
	}
}
//***********************************************************************************//
//                                5位串行移位数码显示程序							 //
//									   (74LS595)									 //
//***********************************************************************************//
void DisPlay(unsigned long FactData)
{
   unsigned char i;
   unsigned char j;
   unsigned long mul = 10000;

   for (j = 0;j < 5;j++)						 //十六进制转换为十进制
    {
	  PartData[4-j] = FactData/mul;
	  FactData = FactData%mul	;
	  mul = mul / 10;
    }
      if (PartData[4] == 0 )					//高位灭0处理
	   {
	     PartData[4] = 0x0f;
		 if (PartData[3] == 0 ) 
		   {
		     PartData[3] = 0x0f;
		   }
	   }        											
   RCK = 0;
   for(i = 0; i<= 4; i++)					 //赋显示值
    {
      getch = dispcode[PartData[i]];
	  if (i == 2)				             //小数点第3位固定显示
	   {
	     getch = getch & 0x7f;
	   } 
	  PSendChar(getch);   	   
    }
   RCK = 1;
   for (j = 0;j < 5;j++)					 //清显示缓冲区
     {
	   PartData[j] = 0x00;
	 }
}

/*
void IntTimer0() interrupt 1    //定时器计数器0的中断    
{
    SCK = 0;
}



void PSendChar(unsigned char getch)
{
    SF = getch;
    TIMER0_ENABLE;   //记数器0启动
    SER = S7;        //先送出低位
	SCK =1;
   while(SCK) ;
    SER = S6; 
	SCK = 1;
   while(SCK) ;
    SER = S5; 
	SCK = 1;
   while(SCK);
    SER = S4;
	SCK = 1;
   while(SCK);
    SER = S3;
	SCK = 1;
   while(SCK);				 
    SER = S2;
	SCK = 1;
   while(SCK);
    SER = S1;
	SCK = 1;
   while(SCK);
    SER = S0; 
	SCK = 1;
   while(SCK);
    TIMER0_DISABLE; //停止timer
}
*/

void PSendChar(unsigned char getch)			 //移位输出数据
{
    unsigned char m;
	SF = getch;
    for (m=0;m<8;m++) 
	{
      if (S7 == 1) 
	  SER =1; 
	  else SER=0;						      //移位时钟
      SCK = 0;
      SF <<= 1;
      SCK = 1;
	}
    return;
}

//************************************************************************//
//                              延时50ms程序                //
//************************************************************************// 
void delay_5ms(unsigned int t)
{
  unsigned char j;  
  for(;t>0;t--)   
  for(j=19000;j>0;j--) ;
  return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -