欢迎光临MCUWAY! 本站网名:单片机之路  永久域名:www.mcuway.com

设为首页

加入收藏

电子基础 初学MCU 单片机类型 MCU程序 MCU应用 设计软件 测控电路 技术论坛
 您现在的位置: 单片机之路 >> 单片机程序 >> 单片机源码 >> 文章正文

能够实现常用码制转换的程序

#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#include <ctype.h> 

#define LongToBin(n) \
(\
((n >> 21) & 0x80) | \
((n >> 18) & 0x40) | \
((n >> 15) & 0x20) | \
((n >> 12) & 0x10) | \
((n >> 9) & 0x08) | \
((n >> 6) & 0x04) | \
((n >> 3) & 0x02) | \
((n ) & 0x01) \
)

#define Bin(n) LongToBin(0x##n##l)

/********** HEX转BCD******/
/***bcd_data(<0x255,>0)***/
unsigned char BCD2HEX(unsigned int bcd_data)
{
unsigned char temp;
temp=((bcd_data>>8)*100)|((bcd_data>>4)*10)|(bcd_data&0x0f);
return temp;
}
/********** HEX转BCD******/
/***hex_data(<0xff,>0)****/
unsigned int HEX2BCD(unsigned char hex_data)
{
unsigned int bcd_data;
unsigned char temp;
temp=hex_data%100;
bcd_data=((unsigned int)hex_data)/100<<8;
bcd_data=bcd_data|temp/10<<4;
bcd_data=bcd_data|temp%10;
return bcd_data;
}

void main(void)
{
unsigned int c;

c= Bin(10101001); // then c = 0xA9
c=BCD2HEX(0x255); //255 转成HEX为0xff
c=HEX2BCD(0xff); //0xff 转成BCD码为 255
}

 

普通文章28课:单片机音乐汇编程序设计
普通文章27课:单片机的一些基本概念
普通文章26课:单片机矩阵式键盘接口技术
普通文章51单片机教程(从原理开始基于汇编)
普通文章1课:单片机概述
普通文章2课:8051单片机引脚介绍
普通文章3课:单片机存储器结构结构分析
普通文章4课:第一个单片机小程序
普通文章5课:单片机延时程序分析
普通文章6课:单片机并行口结构
普通文章7课:单片机的特殊功能寄存器
普通文章8课:单片机寻址方式与指令系统
普通文章9课:单片机数据传递类指令
普通文章10课:单片机数据传送类指令
普通文章11课:单片机算术运算指令
普通文章12课:单片机逻辑运算类指令
普通文章13课:单片机逻辑与或异或指令祥解
普通文章14课:单片机条件转移指令
普通文章15课:单片机位操作指令
普通文章16课:单片机定时器与计数器
普通文章17课:单片机定时器/计数器的方式控制
普通文章18课:单片机的中断系统
普通文章19课:单片机定时器、中断试验
普通文章20课:单片机定时/计数器实验
普通文章21课:单片机串行口介绍

  • 上一篇文章:
  • 下一篇文章:
  • 更多精彩内容>>>

    普通文章电流单位
    普通文章功率单位
    普通文章什么是达林顿管?
    普通文章晶闸管的作用与原理

    学习单片机的必经之路——单片机之路,永久域名:www.mcuway.com