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

📄 gpio_led.c

📁 GPIO口接等源码
💻 C
字号:

//Includes
#include<stdio.h>					//stand input and output libs
#include<stdlib.h>					//stand sys libs
#include<string.h>					//string libs
#include<unistd.h>					//posix libs
#include<sys/io.h>					//io driver libs

unsigned int STOP=0;				      //sharded variable for terminate triggering the watch dog
int addrreg=0x2e;
int datareg=0x2f;
void delay(int i)					//delay function for io ports operation taking effect
{
	int j=0;
	for(j=0;j<i;j++)
	{
		usleep(100000);			//here, 100 ms is enough
	}
}



int main(int argc,char *argv[])			//the major function
{
	int value;					//variable for the time for setting watch dog 's time out
	int tmp;					//variable for io ports access
	int pre;					//for thread return

fprintf(stdout,"========================================================================\n");
fprintf(stdout,"==                                                                    ==\n");
fprintf(stdout,"==         led testing program  Ver:1.0   2007-03-02              ==\n");
fprintf(stdout,"==                                                                    ==\n");
fprintf(stdout,"========================================================================\n");



//call the io ports driver for io ports access
	if(ioperm(addrreg,1,1))
	{
		fprintf(stdout,"Error for 0x4e\n");
		exit(1);
	}
	if(ioperm(datareg,1,1))
	{
		fprintf(stdout,"Error for 0x4f\n");
		exit(1);
	}
//enable configuration mode
outb(0x87,addrreg);
outb(0x87,addrreg);       // manual   ???

//select logical device gpio1
outb(0x07,addrreg);
outb(0x07,datareg);

//select function general purpose i/o port
outb(0x2a,addrreg);
outb(0xff,datareg);

//activate GPIO Function
outb(0x30,addrreg);
outb(0x01,datareg);


//CRF0 GP0~GP17 I/O Selection register
//when set to "1",respective GPIO is programmed as an input port
//when set to "0",respective GPIO is programmed as an output port
outb(0x0f0,addrreg);
outb(0x00,datareg);    //set all 8 bit as output
//CRF1 GP0~GP17 I/O DATA register
//port is set to output,so data register can be read or written
outb(0x0f1,addrreg);
fprintf(stdout,"output------0\r\n");
outb(0,datareg);    //set all 8 bit   outvalue=0
delay(100);
fprintf(stdout,"output------1\r\n");
outb(0xff,datareg);    //set all 8 bit   outvalue=1
delay(100);
return(0);
}

⌨️ 快捷键说明

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