1.代码:参看(【AWorks试用体验】动态扫描4位数码管 - NXP MCU 技术论坛 - 电子技术论坛 - 广受欢迎的专业电子论坛!)
硬件跳线
/home/dengxm2024/linuxProgDir/userside_code/digitdisplay/Digit_Display.c
#include<stdio.h>
#include<linux/spi/spidev.h>
#include<dirent.h>
#include<fcntl.h>
#include<string.h>
#include<sys/ioctl.h>
#define GPIO_RCK_PATH "/sys/class/gpio/gpio117"
int spi_fd;
int RCK_fd;
int SPI1_Init( void )
{
unsigned long mode = 0;
unsigned long speed = 10000;
unsigned long bits = 8;
int fd;
DIR *dir;
dir = opendir( GPIO_RCK_PATH );
if( dir == NULL )
{
fd = open( "/sys/class/gpio/export", O_WRONLY );
if( fd )
{
if( write( fd, "117", strlen( "117") ) == -1 )
{
close( fd );
printf( "GPIO 3.21 export fail" );
return -1;
}
}
close( fd );
}
closedir( dir );
fd = open( GPIO_RCK_PATH"/direction", O_WRONLY );
write( fd, "out", strlen( "out" ) );
close( fd );
RCK_fd = open( GPIO_RCK_PATH"/value", O_RDWR );
spi_fd = open( "/dev/spidev1.0", O_RDWR );
if( spi_fd < 0 )
{
printf( "can not open spi device\n" );
return -1;
}
if( ioctl( spi_fd, SPI_IOC_WR_MODE, &mode ) == -1 )
{
printf( "can not set SPI mode\n" );
return -1;
}
if( ioctl( spi_fd, SPI_IOC_WR_BITS_PER_WORD, &bits ) )
{
printf( "can not set bits per word\n" );
}
if( ioctl( spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed ) == -1 )
{
printf( "can not set max speed HZ\n" );
return -1;
}
return 0;
}
unsigned char led_table[] =
{ 0x89, 0x86, 0xc7, 0xc7, 0x40, 0xFF, 0xFF, 0x86, 0xc7, 0x86, 0xc6, 0x8E, 0x88, 0xc8, 0x92, 0xFF, 0xFF, 0xFF };
int main( void )
{
unsigned char LedValueIndex = 0;
unsigned char LedDig = 0;
unsigned int Times = 0;
unsigned char tx_buffer[2];
struct spi_ioc_transfer tx[] =
{
{
.tx_buf = ( unsigned long ) tx_buffer,
.rx_buf = 0,
.len = 2,
.delay_usecs = 0,
.speed_hz = 10000,
.bits_per_word = 8,
},
};
if( SPI1_Init() == -1 )
{
return 0;
}
while( 1 )
{
tx_buffer[0] = led_table[LedValueIndex + LedDig];
tx_buffer[1] = 1 << LedDig;
ioctl( spi_fd, SPI_IOC_MESSAGE(1), & tx );
write( RCK_fd, "0", 1 );
usleep( 100 );
write( RCK_fd, "1", 1 );
usleep( 1000 );
if( ++LedDig > 3 )
LedDig = 0;
if( ++Times > 500 )
{
Times = 0;
if( ++LedValueIndex > 14 )
LedValueIndex = 0;
}
}
return 0;
}
/home/dengxm2024/linuxProgDir/userside_code/digitdisplay/Makefile
#CROSS_COMPILE是环境变量,是交叉编译工具的前缀,隐式声明编译器
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
#显示声明编译器
#CC = arm-fsl-linux-gnueabi-gcc
#CXX = arm-fsl-linux-gnueabi-g++
CFLAGS = -Wall -g
CXXFLAGS = -Wall -g
# 编译 main 可执行程序
Digit_Display: Digit_Display.o
$(CC) $(CFLAGS) Digit_Display.o -o Digit_Display
Digit_Display.o: Digit_Display.c
$(CC) $(CFLAGS) -c Digit_Display.c -o Digit_Display.o
# 伪目标, 删除所有目标文件
clean:
rm *.o Digit_Display
编译:
dengxm2024@PC-202105142413:~/linuxProgDir/userside_code/digitdisplay$ make
arm-fsl-linux-gnueabi-gcc -Wall -g -c Digit_Display.c -o Digit_Display.o
Digit_Display.c: In function 'SPI1_Init':
Digit_Display.c:30: warning: implicit declaration of function 'write'
Digit_Display.c:32: warning: implicit declaration of function 'close'
Digit_Display.c: In function 'main':
Digit_Display.c:112: warning: implicit declaration of function 'usleep'
arm-fsl-linux-gnueabi-gcc -Wall -g Digit_Display.o -o Digit_Display
传送编译结果到web服务器(Windows上做的web服务器,参看前面文章helloworld)
dengxm2024@PC-202105142413:~/linuxProgDir/userside_code/digitdisplay$ cp Digit_Display /mnt/d/imx280a_exe_folder/
开发板下载编译结果:
root@EasyARM-iMX28x ~# wget http://192.168.0.233:443/imx280a_exe_folder/Digit_Di
splay
Connecting to 192.168.0.233:443 (192.168.0.233:443)
Digit_Display 100% |*******************************| 10852 0:00:00 ETA
root@EasyARM-iMX28x ~# ls
Digit_Display g_file_storage.ko i2c_ds2460_test start_qt
bcmdhd.ko gpio_driver.ko lradc.ko up_wifi_module
beep.ko hello qt_hellow usb_storage_up
beep_test helloworld qt_hellow_zh
修改编译结果,添加可执行属性(文件经过了windows系统传输,可执行的属性丢失了)
root@EasyARM-iMX28x ~# chmod +x Digit_Display
执行编译结果:
root@EasyARM-iMX28x ~# ./Digit_Display
查看开发板,4位led数码管显示屏已经显示了。