📄 gpio.c
字号:
// Blink pin 3 on port B at 1 Hz
// Just add an LED and see the light! ;)
//
//Created by Dingo_aus, 7 January 2009
//email: dingo_aus [at] internode <dot> on /dot/ net
//
//Created in AVR32 Studio (version 2.0.2) running on Ubuntu 8.04
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
FILE *pb0,*pb1,*pb2;
//create a variable to store whether we are sending a '1' or a '0'
char set_value[4];
void inicializar (void){
//**********Inicializa PB0 como salida
//Using sysfs we need to write "32" to /sys/class/gpio/export
//This will create the folder /sys/class/gpio/gpio32
if ((pb0 = fopen("/sys/class/gpio/export", "ab")) == NULL)
{
printf("Cannot open export file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(pb0);
//Write our value of "32" to the file
strcpy(set_value,"32");
fwrite(&set_value, sizeof(char), 2, pb0);
fclose(pb0);
printf("...export file accessed, new pin now accessible\n");
//SET DIRECTION
//Open the LED's sysfs file in binary for reading and writing, store file pointer in fp
if ((pb0 = fopen("/sys/class/gpio/gpio32/direction", "rb+")) == NULL)
{
printf("Cannot open direction file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(pb0);
//Write our value of "out" to the file
strcpy(set_value,"out");
fwrite(&set_value, sizeof(char), 3, pb0);
fclose(pb0);
printf("...direction set to output\n");
//**********Inicializa PB1 como salida
//Using sysfs we need to write "33" to /sys/class/gpio/export
//This will create the folder /sys/class/gpio/gpio33
if ((pb1 = fopen("/sys/class/gpio/export", "ab")) == NULL)
{
printf("Cannot open export file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(pb1);
//Write our value of "33" to the file
strcpy(set_value,"33");
fwrite(&set_value, sizeof(char), 2, pb1);
fclose(pb1);
printf("...export file accessed, new pin now accessible\n");
//SET DIRECTION
//Open the LED's sysfs file in binary for reading and writing, store file pointer in fp
if ((pb1 = fopen("/sys/class/gpio/gpio33/direction", "rb+")) == NULL)
{
printf("Cannot open direction file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(pb1);
//Write our value of "out" to the file
strcpy(set_value,"out");
fwrite(&set_value, sizeof(char), 3, pb1);
fclose(pb1);
printf("...direction set to output\n");
//**********Inicializa PB2 como salida
//Using sysfs we need to write "34" to /sys/class/gpio/export
//This will create the folder /sys/class/gpio/gpio34
if ((pb2 = fopen("/sys/class/gpio/export", "ab")) == NULL)
{
printf("Cannot open export file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(pb2);
//Write our value of "34" to the file
strcpy(set_value,"34");
fwrite(&set_value, sizeof(char), 2, pb2);
fclose(pb2);
printf("...export file accessed, new pin now accessible\n");
//SET DIRECTION
//Open the LED's sysfs file in binary for reading and writing, store file pointer in fp
if ((pb2 = fopen("/sys/class/gpio/gpio34/direction", "rb+")) == NULL)
{
printf("Cannot open direction file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(pb2);
//Write our value of "out" to the file
strcpy(set_value,"out");
fwrite(&set_value, sizeof(char), 3, pb2);
fclose(pb2);
printf("...direction set to output\n");
}
void escribir_pb0 (char bit){ //escribe bit (que es un char "1") a donde indique el pointer
if ((pb0 = fopen("/sys/class/gpio/gpio32/value", "rb+")) == NULL)
{
printf("Cannot open value file.\n");
exit(1);
}
strcpy(set_value,"1");
//Set pointer to begining of the file
rewind(pb0);
//Write our value of "1" to the file
strcpy(set_value,&bit);
fwrite(&set_value, sizeof(char), 1, pb0);
fclose(pb0);
//printf("...enable (pb0) set to %c \n",bit);
}
void escribir_pb1 (char bit){ //escribe bit (que es un char "1") a donde indique el pointer
if ((pb1 = fopen("/sys/class/gpio/gpio33/value", "rb+")) == NULL)
{
printf("Cannot open value file.\n");
exit(1);
}
strcpy(set_value,"1");
//Set pointer to begining of the file
rewind(pb1);
//Write our value of "1" to the file
strcpy(set_value,&bit);
fwrite(&set_value, sizeof(char), 1, pb1);
fclose(pb1);
//printf("...clock (pb1) set to %c \n",bit);
}
void escribir_pb2 (char bit){ //escribe bit (que es un char "1") a donde indique el pointer
if ((pb2 = fopen("/sys/class/gpio/gpio34/value", "rb+")) == NULL)
{
printf("Cannot open value file.\n");
exit(1);
}
strcpy(set_value,"1");
//Set pointer to begining of the file
rewind(pb2);
//Write our value of "1" to the file
strcpy(set_value,&bit);
fwrite(&set_value, sizeof(char), 1, pb2);
fclose(pb2);
//printf("...data (pb2) set to %c \n",bit);
}
void reloj (void){
escribir_pb1('0');
usleep(10);
escribir_pb1('1');
usleep(10);
}
void rs_on(void){ //envia 8 bits, dejando rs on
int i=0,temp=0;
int reg=0x72;
escribir_pb0('0');
usleep(1);
for( i=0; i<8; i++){
temp=(reg<<i);
if ((temp & 0x80) == 0x80){
escribir_pb2('1');
//printf("Pin out -> 0x01\n");
}else{
escribir_pb2('0');
//printf("Pin out -> 0x00\n");
}
reloj();
}
}
void rs_off(void){ //envia 8 bits, dejando rs off
int i=0,temp=0;
int reg=0x70;
escribir_pb0('0');
for( i=0; i<8; i++){
temp=(reg<<i);
if ((temp & 0x80) == 0x80){
escribir_pb2('1');
//printf("Pin out -> 0x01\n");
}else{
escribir_pb2('0');
//printf("Pin out -> 0x00\n");
}
reloj();
}
}
void write_reg(int reg){ //envia 16 bits, formato write_reg(0x0000) con rs_off
int i=0,temp=0;
printf("write_reg 0x%04X \n",reg);
rs_off();
for( i=0; i<16; i++){
temp=(reg<<i);
if ((temp & 0x8000) == 0x8000){
escribir_pb2('1');
//printf("Pin out -> 0x01\n");
}else{
escribir_pb2('0');
//printf("Pin out -> 0x00\n");
}
reloj();
}
usleep(10);
escribir_pb2('1');
escribir_pb0('1');
usleep(100);
}
void write_dat(int reg){ //envia 16 bits, formato write_reg(0x0000) con rs_on
int i=0,temp=0;
printf("write_dat 0x%04X \n",reg);
rs_on();
for( i=0; i<16; i++){
temp=(reg<<i);
if ((temp & 0x8000) == 0x8000){
escribir_pb2('1');
//printf("Pin out -> 0x01\n");
}else{
escribir_pb2('0');
//printf("Pin out -> 0x00\n");
}
reloj();
}
usleep(10);
escribir_pb2('1');
escribir_pb0('1');
usleep(100);
}
int main(int argc, char** argv)
{
printf("\n**********************************\n"
"* Bienvenido, Programa para *\n"
"* ...inicializar la PH320240 *\n"
"* ...Camilo Guevara G. *\n"
"**********************************\n");
inicializar();
//Run an infinite loop - will require Ctrl-C to exit this program
/* while(1)
{
//Set it so we know the starting value in case something above doesn't leave it as 1
strcpy(set_value,"1");
if ((pb0 = fopen("/sys/class/gpio/gpio32/value", "rb+")) == NULL)
{
printf("Cannot open value file.\n");
exit(1);
}
reloj();
sleep(1);
}
return 0;*/
escribir_pb2('1');
escribir_pb0('1');
escribir_pb1('1');
usleep(1000000);
write_reg(0x0001);
write_dat(0x7300);
write_reg(0x0002);
write_dat(0x0200);
write_reg(0x0003);
write_dat(0x6364);
write_reg(0x0004);
write_dat(0x04C7);
write_reg(0x0005);
write_dat(0xFC80);
write_reg(0x000A); //Contrast/Brightness control;
write_dat(0x4008);
write_reg(0x000D); //Power control(2);
write_dat(0x3229);
write_reg(0x000E); //Power control(3);VOML
write_dat(0x3200);
write_reg(0x001E); //Power control(4);COMH
write_dat(0x00D2); // lo cambie.. le original es D2
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -