C標(biāo)準(zhǔn)沒有輸出二進(jìn)制的,不過用itoa()可以實(shí)現(xiàn)到二進(jìn)的轉(zhuǎn)換。
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、萬安ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的萬安網(wǎng)站制作公司
1、在C語言程序中,可以使用標(biāo)準(zhǔn)庫函數(shù)中printf()來向屏幕輸出信息,或者使用sprintf()向緩沖區(qū)輸出信息。對整數(shù)而言,可以使用%d、%o、%x(或%X)輸出十進(jìn)制形式、八進(jìn)制、十六進(jìn)制形式,但貌似缺乏二進(jìn)制形式。
2、解決方式有兩種,一種是利用Windows提供的itoa()函數(shù),另一種自行設(shè)計(jì)一個新的函數(shù):
代碼一:
/** Test.h */
#pragma once
#ifndef Test_H
#define Test_H
/** use itoa() to print integers in the form of binary
* @param[in] n the integer to print
* @return void
*/
void printBinaryByItoa(unsigned int n);
代碼二:
/** Test.c */
#include stdio.h
#include stdlib.h
void printBinaryByItoa(unsigned int n)
{
unsigned char data[33];
_itoa_s(n, data, 33, 2);
printf("%sb\n", data);
}
void printBinary(unsigned int n, unsigned char separator, unsigned char needPrefixZeros)
{
unsigned int i = 0;
unsigned int mask = 0; /* the mask code to get each bit of n */
unsigned char data[40]; /* the buffer to store the bits of n in the form of characters */
unsigned int index = 0; /* used to access data[] */
unsigned int start = 0; /* to point out where and when to store bits of n */
data[39] = '\0';
我在很久以前用printf輸出過自制并行口數(shù)據(jù),我相信此方法可行:printf(0x**,0x16),其中**表示地址,我已經(jīng)有十年沒編程了,據(jù)現(xiàn)在的情況發(fā)展不是很快,此法應(yīng)該可以。注意在用的過程中要和緩沖器的關(guān)系處理好!試試吧,祝你成功。
是這樣的,keil的stdio.h提供了一堆函數(shù),大致分兩類,一類是通過串口在上位機(jī)上輸入輸出,另一類是指定一個指針變量,向其輸入輸出,這樣便可以將得到的字符數(shù)組指針的內(nèi)容輸出到LCD一類設(shè)備上了,也可通過指針獲得按鍵輸入。
對于一類,你必須得軟件初始化串口,硬件與電腦連接好,然后利用windows的超級終端就可以顯示單片機(jī)中程序里的printf等函數(shù)打印出的內(nèi)容了,你也可以使用getchar獲得超級終端的按鍵碼。(當(dāng)然也可以使用串口助手之類軟件代替超級終端,注意波特率,數(shù)據(jù)位,校驗(yàn)位,等設(shè)置要保持一致)
對于第二類,是不用初始化串口的,因?yàn)楦跊]任何關(guān)系,你只要用指針虛擬設(shè)備就可以了,輸入輸出都是你自己做的硬件。
附串口初始化程序:
#define T1_INIT_VALUE 0x0D //定時器1初始值設(shè)定 9600bps@11.0592MHz
void UartInit(void) {
SCON = 0x50; //8位數(shù)據(jù),可變波特率
TMOD = 0x0f; //清除定時器1模式位
TMOD |= 0x20; //設(shè)定定時器1為8位自動重裝方式
TL1 = T1_INIT_VALUE; //設(shè)定定時初值
TH1 = T1_INIT_VALUE; //設(shè)定定時器重裝值
ET1 = 0; //禁止定時器1中斷
TR1 = 1; //啟動定時器1
ES = 0; //禁止串行口中斷
TI = 1; //必須置高TI,RI
RI = 1;
puts("Uart Initialize Success!");
// *.調(diào)用printf之前應(yīng)該關(guān)閉串口中斷使能
}
數(shù)據(jù)最大值可以確定,可以根據(jù)最大值定義一個ASCII數(shù)組,數(shù)組的每一個單元存放整形數(shù)據(jù)的一位。
發(fā)送前先對發(fā)送整形數(shù)組里的單元轉(zhuǎn)換成ASCII數(shù)組,然后再按照通用的發(fā)送函數(shù)進(jìn)行發(fā)送。
void InttoChar (uint IntNumber)
//---------------------------------------------------------
// Name: void InttoChar (int IntNumber)
// Func.: Translate integer to ASCII charactor array
// Char.: IntNumber number to be translated to ASCII charactor
//---------------------------------------------------------
{
if (IntNumber 10)
{
AsciiArray[0] = IntNumber + 0x30;
AsciiArray[1] = 0x20;
AsciiArray[2] = 0x20;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber 100)
{
AsciiArray[0] = IntNumber / 10 + 0x30;
AsciiArray[1] = IntNumber % 10 + 0x30;
AsciiArray[2] = 0x20;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber 1000)
{
AsciiArray[0] = IntNumber / 100 + 0x30;
AsciiArray[1] = IntNumber % 100 / 10 + 0x30;
AsciiArray[2] = IntNumber % 10 + 0x30;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber 10000)
{
AsciiArray[0] = IntNumber / 1000 + 0x30;
AsciiArray[1] = IntNumber % 1000 / 100 + 0x30;
AsciiArray[2] = IntNumber % 100 / 10 + 0x30;
AsciiArray[3] = IntNumber % 10 + 0x30;
AsciiArray[4] = 0x20;
return;
}
else
{
AsciiArray[0] = IntNumber / 10000 + 0x30;
AsciiArray[1] = IntNumber % 10000 / 1000 + 0x30;
AsciiArray[2] = IntNumber % 1000 / 100 + 0x30;
AsciiArray[3] = IntNumber % 100 / 10 + 0x30;
AsciiArray[4] = IntNumber % 10 + 0x30;
return;
}
}
打印,就是輸出一個語句!比如printf(“Hello word!”); 里面可以是字符串,可以是變量!
printf()是以規(guī)定的格式向單片機(jī)的串口輸出數(shù)據(jù) 原型如下: extern int printf (const char *, ...);
const char *是格式控制字符串指針,必須以%開始, %[flags][width][.precision]][modified] type
scanf()函數(shù)是依規(guī)定的格式從串口輸入數(shù)據(jù),extern int scanf( const char * , ....)
與printf相同其格式控制符需要以%開始,一一個格式字符結(jié)束,中間可以插入附加字符:
%[*][width][modifiers]type
分享標(biāo)題:c51語言輸出函數(shù),C51函數(shù)
標(biāo)題鏈接:http://muchs.cn/article18/hcgggp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、微信公眾號、定制開發(fā)、定制網(wǎng)站、電子商務(wù)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)