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

📄 led板显示程序.txt

📁 LED板显示程序 LED板显示程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:
***************************************************/
/****LED管为共阴极:共阴极发光管高电平点亮************************/
/***以0x00||0x55||0xaa作为开始符*********************************/
/****baud=9600 **************************************************/
/****use 6 bit display*******************************************/
/****程序编写者:********************************************/
/****程序编写时间:2003.11.19************************************/

#include <absacc.h>
#include <reg51.h>
#include <stdarg.h>
#include <stdio.h>
#include <intrins.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

//宏定义
#define uchar unsigned char
#define uint  unsigned int
#define ulong unsigned long

#define POSITIVE 10; //存放正号(正号消隐)
#define NEGATIVE 11; //存放负号(负号显示"-")
#define NUM_AXIS 1 ;//存放第几轴显示

sbit A5  = P1^7;
sbit A4  = P3^7;
sbit A3  = P3^5;
sbit A2  = P3^4;
sbit A1  = P3^3;
sbit A0  = P3^2;

//变量初始化定义
uint  data  verify;              //存放CRC校验的返回值
uchar data  check_crcL;          //存放分离后,CRC的低位
uchar data  check_crcH;          //存放分离后,CRC的高位
uchar data  counter=0;           //记录接受字节的个数
uchar data  axis1_high=0;        //接受到轴1数的高位
uchar data  axis1_low=0;         //接受到轴1数的低位
uchar data  axis2_high=0;        //接受到轴2数的高位
uchar data  axis2_low=0;         //接受到轴2数的高位
int   data  axis1_num=0;         //组合后的轴1数
int   data  axis2_num=0;         //组合后的轴2数
uchar data  inbuffer[55];        //存放串口传送过来的数据
int   data  timer_counter=0;     //当定时器十毫秒到时,timer_counter++;
bit   data  sec_timer_flag=0;    //一秒钟到标志
bit   data  switch_flag=1;      //切换显示是第一轴数据显示,还是第二轴显示
bit   data  switch_temp_flag=0;
uchar data  error_num=0;
uchar data  num_axis=0;
//显示缓冲区
uchar data  disp_led1=0;
uchar data  disp_led2=0;
uchar data  disp_led3=0;
uchar data  disp_led4=0;
uchar data  disp_led5=0;   
uchar data  disp_led6=0;
bit   data  receive_flag=0;//接受数据标志
//要显示的数据
int disp_data; 
/* Table of CRC values for high-order byte */
/*static unsigned char code auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
} ; 

//Low Order Byte Table 
/* Table of CRC values for low-order byte */
/*static char code auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 
0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 
0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 
0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 
0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 
0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};
*/
//延时程序
void delayXms(uint time) //延时xxms  time<744
{
  ulong data i;
  for(i=15*time;i>0;i--){}
}

//查表数组
static uchar code Tab_cathode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40};//共阴极显示0--9和"灭"和"-"
static uchar code Tab_anode[]  ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};//共阳极显示0--9和"灭"和"-"

//入口参数:Buffer是将校验的数据的首址,Length 为待校验的长度。
//出口参数:auchCRCHi 是CRC的高位,auchCRCLo 是CRC的低位。
/*uint crc16(uchar *buffer, uint length)
{
    data uchar uchCRCHi = 0xFF ;    /* high CRC byte initialized */
/*    data uchar uchCRCLo = 0xFF ;    /* low CRC byte initialized  */
//    data unsigned index ;            /* will index into CRC lookup*/                                                           
//    while (length--)                /* pass through message buffer  */
//        {
//        index = uchCRCHi ^ * buffer ++ ;    /* calculate the CRC  */
//        uchCRCHi = uchCRCLo ^ auchCRCHi[index] ;
//        uchCRCLo = auchCRCLo[index] ;
//        }
//    return (uchCRCHi<<8|uchCRCLo);
//}

//串口初始化
void init_serialcomm(void)
{
    SCON= 0x50;       //SCON: serail mode 1, 8-bit UART, enable ucvr 
    TMOD= 0x21;       //TMOD: timer 1, mode 2, 8-bit reload 
    PCON= 0x80;       //SMOD=1; 
    TH1=0xfa;
    TL1=0xfa;           //Baud:9600   fosc=11.0592MHz
    ES=1;
    TR1= 1;          //timer 1 run 
}

//10ms定时器初始化
void init_timer(void)
{
    TMOD=0x21;           //timer0:16bit unreload  timer1:16bit unreload
    TH0=-(9216/256);     //load timer0 (10ms per interrupt 11.0592MHz)
    TL0=-(9216%256); 
    ET0=1;               //T/C0开中断  
    TR0=1;                   //启动定时器
}
//利用定时器0的方式1编程,采用中断方式
void timer0 (void) interrupt 1 
{
    TH0=-(9216/256);  //load timer0 (10ms per interrupt 11.0593MHz)
    TL0=-(9216%256);
    timer_counter+=1;
    if(timer_counter>=200)
    {
        timer_counter=0;
        sec_timer_flag=1;
    }
}

//串行中断程序
void serial () interrupt 4 using 3 
{
    ES=0;
    if(RI)//当接受完一个字节后
    {
        uchar ch;
        receive_flag=1;
        ch=SBUF;
        if(counter>=50)
        {
            counter=0;
            
        }
        counter++;
        inbuffer[counter]=ch; 
        if(ch==0xdd)
        {
            if(inbuffer[counter-2]==0xbb&&inbuffer[counter-1]==0xcc&&inbuffer[counter]==0xdd)
            {
                //如果结束符正确
                if(inbuffer[counter-21]==0x00&&inbuffer[counter-20]==0x55&&inbuffer[counter-19]==0xaa)
                {
                    //如果起始位正确
                    if(inbuffer[counter-17]==0xd0) 
                    {
                        
                        //如果功能码正确
                        //verify=crc16(&inbuffer[counter-21],17);//计算CRC校验
                        //check_crcL=verify;            //存放CRC校验的低位
                        //check_crcH=verify>>8;         //存放CRC校验的高位
                        //if(check_crcL==inbuffer[counter-3]&&check_crcH==inbuffer[counter-4])
                         //{      
                             //如果CRC校验通过,取第1组轴数
                             axis1_high=inbuffer[counter-14];//轴1数高位字节
                             axis1_low =inbuffer[counter-13];//轴1数低位字节
                               axis1_num=axis1_high*256+axis1_low; //计算出总的轴数
                               //取第二组轴数
                               axis2_high=inbuffer[counter-12];
                               axis2_low =inbuffer[counter-11];
                               axis2_num =axis2_high*256+axis2_low;
                            counter=0;
                            error_num=0;
                            
                        //}
                        }
                        else 
                        {
                            error_num=3;//function error
                        }
                     }
                     else 
                     {
                         error_num=2;//last errror
                     }
              }
              else 
              {
                  error_num=1;//begin error
              }
            }
          }
        
    RI=0;
    ES=1;
}
//分离BCD码
void seperate_bcd(int d)
{
    int abs_d;
    uchar bcd1,bcd2,bcd3,bcd4,bcd5;
    if(d<0) //如果轴数为负数
    {
        if(d>-10)
        {
            abs_d=abs(d);
            bcd1=abs_d;
            bcd2=NEGATIVE;
            bcd3=10;
            bcd4=10;
            bcd5=10;
            disp_led1=Tab_cathode[bcd1];//第一个数码管显示的值
            disp_led2=Tab_cathode[bcd2];//符号
            disp_led3=Tab_cathode[bcd3];//消隐
            disp_led4=Tab_cathode[bcd4];//消隐
            disp_led5=Tab_cathode[bcd5];//消隐
            disp_led6=Tab_cathode[num_axis];
        }
        else if(d>-100&&d<=-10)
        {
            abs_d=abs(d);
            bcd5=10;
            bcd4=10;
            bcd3=NEGATIVE;
            bcd2=abs_d/10;
            bcd1=abs_d%10;
            disp_led1=Tab_cathode[bcd1];//第一个数码管显示的值

⌨️ 快捷键说明

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