笔记 c51,单片机用B二进制显示

1、由于用arduino,有时候,需要用B00010001类似的二进制,但是用51单片机的时候,不能这么写,所以需要添加步骤。

https://blog.csdn.net/ReadAir/article/details/88948410?utm_medium=distribute.pc_relevant.none-task-blog-2 default baidujs_title~default-1-88948410-blog-96433325.pc_relevant_default&spm=1001.2101.3001.4242.2&utm_relevant_index=4

http://www.laogu.com/cms/xw_288779.htm
添加

#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 B(n) LongToBin(0x##n##l)

在这里插入图片描述
在这里插入图片描述

#include <stc8a8k.h>
#include <intrins.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 B(n) LongToBin(0x##n##l)


sbit LED=P1^7;
	
sbit HC595_LATCH = P1^0;//latch pin or rck pin  STCP  ss 595pin12
sbit HC595_OE   = P1^1;//oe blank pin  595pin13

// 0data of 74hc595 pin14  ---->mosi P13
//clk of 595 shcp 595pin11        ---->sclk P15

void SendTo595(	unsigned char byteData);
void Delay10us();		//@22.1184MHz


void Delay500ms()		//@22.1184MHz
{
	unsigned char i, j, k;

	i = 57;
	j = 27;
	k = 112;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void portmode()
{
	P0M0=0x00;P0M1=0x00;
	P1M0=0x00;P1M1=0x00;
	P2M0=0x00;P2M1=0x00;
	P3M0=0x00;P3M1=0x00;
	P4M0=0x00;P4M1=0x00;
	P5M0=0x00;P5M1=0x00;
	P6M0=0x00;P6M1=0x00;
	P7M0=0x00;P7M1=0x00;
}


void Delay30us()		//@22.1184MHz
{
	unsigned char i;
	i = 219;
	while (--i);
}

void main()
{
	unsigned char c;
  c = B(00100111); //相当于c = 0x27
	
	  portmode();

	  LED=0;
		Delay500ms();
		LED=1;
		Delay500ms();
		
		LED=0;
		Delay500ms();
		LED=1;
		Delay500ms();
	
  LED=0;
		Delay500ms();
		LED=1;
		Delay500ms();
		
		LED=0;
		Delay500ms();
		LED=1;
		Delay500ms();
	

    SPCTL = 0x50;                               //??SPI????
    SPSTAT = 0xc0;                              //?????

	HC595_LATCH =1;
  HC595_OE =0;	

	while(1)
	{
	//低电平亮
		
        HC595_LATCH = 0;                       //????SS??
        SPDAT = ~c;                           //
        while (!(SPSTAT & 0x80));              //??????
        SPSTAT = 0xc0;                         //?????
        HC595_LATCH = 1;   
	}
	
}

void Delay10us()		//@22.1184MHz
{
	unsigned char i;

	i = 71;
	while (--i);
}

void SendTo595(	unsigned char byteData)
{
//   char i=0;
//   for(;i<8;i++)
//   {
//        P13 = byteData>>7;
0000 0001 >>1  0000 0000
0000 0001 <<1  0000 0010		 
//        byteData= byteData<<1;      
//        P10 = 0;         
//        Delay10us();   
//        P10 = 1;        
//   }  
//   HC595_LATCH = 0;    //p10    
//	

//   Delay10us();
//   HC595_LATCH =1; 
//   HC595_OE=0;
 }
//        HC595_LATCH = 0;                                 //????SS??
//        SPDAT = 0x5a;                           //??????
//        while (!(SPSTAT & 0x80));               //??????
//        SPSTAT = 0xc0;                          //?????
//        HC595_LATCH = 0;   



版权声明:本文为lmf666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。