十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
#include stdio.h #include fcntl.h #include string.h #include sys/ioctl.h

專注于為中小企業(yè)提供成都網(wǎng)站制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)汝州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
#define STEPMOTOR_IOCTRL_PHASE 0x13 staticintstep_fd = -1;
char *STEP_DEV="/dev/exio/0raw"; //定義一個(gè)指針指向步進(jìn)電機(jī)的驅(qū)動(dòng) 程序
/********* A, AB, B, BC, C CD, D, DA ***/
char stepdata[]={0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};//各 個(gè)相位對(duì)應(yīng)的值
void Delay(int t) //延時(shí)函數(shù) { int i;
for(;t0;t--)
for(i=0;i400;i++); }
/***************************************************** ***********/
int main(intargc, char **argv) {
int i = 0;
if((step_fd=open(STEP_DEV, O_WRONLY))0){ printf("Error opening /dev/exio/0raw device\n"); return 1; } /*
打開設(shè)備的驅(qū)動(dòng)程序,由于LINUX把所有的設(shè)備都模擬成文件。 step_fd=open(STEP_DEV,0_WRONLY)實(shí)際調(diào)用的函數(shù)為:
staticint s3c2410_exio_open(structinode *inode, struct file *filp) //驅(qū)動(dòng)程序中的設(shè)備打開程序 */
for (;;) {
for (i=0; isizeof(stepdata)/sizeof(stepdata[0]); i++) { ioctl(step_fd, STEPMOTOR_IOCTRL_PHASE, stepdata[i]); }
/*程序進(jìn)入一個(gè)死循環(huán),這樣可以使電機(jī)在沒有人為停止的狀況下,一直的 轉(zhuǎn)動(dòng)下去。
*第二層for語句循環(huán)一次即電機(jī)轉(zhuǎn)動(dòng)一周。函數(shù)ioctl()對(duì)應(yīng)函 數(shù)*s3c2410_exio_ioctl()
*而這個(gè)函數(shù)最終將調(diào)用函數(shù)do_stepmotor_run((char)arg);使步進(jìn)電 機(jī)轉(zhuǎn)動(dòng)起來。 */
printf("Delay(100)\n"); Delay(100); }
close(step_fd); //程序結(jié)束時(shí)關(guān)閉設(shè)備 printf("Step motor start running!\n"); return 0; }本文來自百度文庫(kù),你可以搜搜,其中答案更詳細(xì)的!
函數(shù)名: circle 功 能: 在給定半徑以(x, y)為圓心畫圓 用 法: void far circle(int x, int y, int radius); 程序例: #include graphics.h #include stdlib.h #include stdio.h #include conio.h int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy; int radius = 100; /* initialize graphics and local variables */ initgraph(gdriver, gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor()); /* draw the circle */ circle(midx, midy, radius); /* clean up */ getch(); closegraph(); return 0; }
clock()是C/C++中的計(jì)時(shí)函數(shù),而與其相關(guān)的數(shù)據(jù)類型是clock_t。
它的具體功能是返回處理器調(diào)用某個(gè)進(jìn)程或函數(shù)所花費(fèi)的時(shí)間。函數(shù)返回從“開啟這個(gè)程序進(jìn)程”到“程序中調(diào)用clock()函數(shù)”時(shí)之間的CPU時(shí)鐘計(jì)時(shí)單元(clock tick)數(shù),其中clock_t是用來保存時(shí)間的數(shù)據(jù)類型。
在time.h文件中,我們可以找到對(duì)它的定義:
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
clock_t其實(shí)就是long,即長(zhǎng)整形。該函數(shù)返回值是硬件滴答數(shù),要換算成秒或者毫秒,需要除以CLK_TCK或者 CLK_TCK CLOCKS_PER_SEC。比如,在VC++6.0下,這兩個(gè)量的值都是1000,這表示硬件滴答1000下是1秒,因此要計(jì)算一個(gè)進(jìn)程的時(shí)間,用clock()除以1000即可。
clock的返回值一直是0的原因:
1、編譯器優(yōu)化,for循環(huán)實(shí)際根本沒執(zhí)行,直接跳過去了,所以時(shí)間為0。
2、clock計(jì)算的是程序占用cpu的時(shí)間,如果你的程序執(zhí)行的動(dòng)作很少,那么clock算出的時(shí)間也很少。
3、建議使用time gettimeofday函數(shù)來計(jì)時(shí)。
擴(kuò)展資料:
C語言中clock()函數(shù)的程序例1:(TC下運(yùn)行通過)
#include?stdio.h
#include?time.h
int?main(void)
{
clock_t?start,?end;
start?=?clock();
delay(2000);
end?=?clock();
printf("The?time?was:?%f\n",?(double)(end?-?start)?/?CLK_TCK);
return?0;
}
說明:CLK_TCK?定義在TC中的time.h中:#define?CLK_TCK18.2。
在VC6.0中也有關(guān)于CLK_TCK的宏定義,不過其值不再是18.2,而是1000。
實(shí)際上在VC6.0中CLK_TCK已完全等同CLOCKS_PER_SEC。
參考資料來源:百度百科-clock()
使用i2c通信先配置地址,數(shù)據(jù)位,超時(shí)時(shí)間等等通信規(guī)則。
這里的ioctl函數(shù)就是用來配置這些參數(shù)的。
I2C_SLAVE:安全的配置,如果i2c已經(jīng)配置過會(huì)返回失敗。
I2C_SLAVE_FORCE:總是成功,不管其他人有沒有在使用,確定只有你使用這個(gè)i2c的時(shí)候使用。
同時(shí)ioctl還能使用I2C_TENBIT,I2C_PEC,I2C_TIMEOUT等參數(shù)。
這里只是配置i2c通信規(guī)則,還沒有發(fā)送數(shù)據(jù)。
配置完成后,調(diào)用write,read函數(shù)可以接收發(fā)送數(shù)據(jù)。
這個(gè)函數(shù)本身沒什么特別的, 但是參數(shù)的使用跟具體設(shè)備和驅(qū)動(dòng)有關(guān)系, 所以其實(shí)沒法解釋這個(gè)東西具體該怎么用, 你要操作什么設(shè)備就得仔細(xì)去讀這個(gè)設(shè)備驅(qū)動(dòng)的文檔
1、在Linux系統(tǒng)中通過C語言獲取硬盤序列號(hào),可以借助于ioctl()函數(shù),該函數(shù)原型如下:
int?ioctl(int?fd,?unsigned?long?request,?...);
ioctl的第一個(gè)參數(shù)是文件標(biāo)識(shí)符,用open()函數(shù)打開設(shè)備時(shí)獲取。
ioctl第二個(gè)參數(shù)為用于獲得指定文件描述符的標(biāo)志號(hào),獲取硬盤序列號(hào),一般指明為HDIO_GET_IDENTITY。
ioctl的第三個(gè)參數(shù)為一些輔助參數(shù),要獲取硬盤序列號(hào),需要借助于struct?hd_driveid結(jié)構(gòu)體來保存硬盤信息?,該結(jié)構(gòu)體在Linux/hdreg.h中,struct?hd_driveid的聲明如下
struct?hd_driveid?{
unsigned?short????config;????????/?lots?of?obsolete?bit?flags?*/
unsigned?short????cyls;????????/*?Obsolete,?"physical"?cyls?*/
unsigned?short????reserved2;????/*?reserved?(word?2)?*/
unsigned?short????heads;????????/*?Obsolete,?"physical"?heads?*/
unsigned?short????track_bytes;????/*?unformatted?bytes?per?track?*/
unsigned?short????sector_bytes;????/*?unformatted?bytes?per?sector?*/
unsigned?short????sectors;????/*?Obsolete,?"physical"?sectors?per?track?*/
unsigned?short????vendor0;????/*?vendor?unique?*/
unsigned?short????vendor1;????/*?vendor?unique?*/
unsigned?short????vendor2;????/*?Retired?vendor?unique?*/
unsigned?char????serial_no[20];????/*?0?=?not_specified?*/
unsigned?short????buf_type;????/*?Retired?*/
unsigned?short????buf_size;????/*?Retired,?512?byte?increments
*?0?=?not_specified
*/
……
};
2、源代碼如下
#include?stdio.h
//ioctl()的聲明頭文件
#include?sys/ioctl.h
//硬盤參數(shù)頭文件,?hd_driveid結(jié)構(gòu)聲明頭文件
#include?linux/hdreg.h
//文件控制頭文件
#include?sys/fcntl.h
int?main()
{
//用于保存系統(tǒng)返回的硬盤數(shù)據(jù)信息
struct?hd_driveid?id;
//這里以第一塊硬盤為例,用戶可自行修改
//用open函數(shù)打開獲取文件標(biāo)識(shí)符,類似于windows下的句柄
int?fd?=?open("/dev/sda",?O_RDONLY|O_NONBLOCK);
//失敗返回?
if?(fd??0)?{
perror("/dev/sda");
return?1;?}
//調(diào)用ioctl()
if(!ioctl(fd,?HDIO_GET_IDENTITY,?id))
{
printf("Serial?Number=%s\n",id.serial_no);
}
return?0;
}
編譯完成后,執(zhí)行效果如下: