目录
一、显示屏显示颜色
一个屏幕是800*480个像素
是将准备好的颜色数据写入到显示屏的设备中(/dev/fb0),由操作系统利用文件控制显示屏硬件
操作步骤:
1.准备800*480个像素点颜色数据
2.打开显色屏设备文件
参数:
Pathname:打开文件的路径名
flags:权限
O_RDONLY:只读
O_WRONLY:只写
O_RDWR:可读可写
返回值:
成功:文件对应的文件描述符
失败:-1
查看函数的使用方法:man 2 函数名
3.将颜色数据写入设备文件
参数:
fd:要写入数据的文件描述符
buf:要写入数据的缓冲去(泛型指针)
count:要写入的字节数
返回值:
成功:正确写入的字节数
失败:
4.关闭设备文件
close()
例1:全屏显示红色
代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char **argv)
{
/*1.准备颜色 */
int color[800*480];
for(int i=0;i<800*480;i++)
color[i]= 0xff0000;//红色数据
/*2.打开设备 */
int fd = open("/dev/fb0",O_WRONLY);
if(-1==fd)
{
printf("文件打开失败");
return -1;
}
/*3.将颜色数据写入设备文件*/
int ret=write(fd,color,800*480*4);//每个元素占4个字节
if(-1==ret)
{
printf("文件写入失败");
return -2;
}
/*4.关闭设备文件*/
close(fd);
return 0;
}
再ubuntu上编译:
在securecrt上编译传到开发板上:
开发板上就显示了红色
例2:横屏显示三色
代码:
方法一:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char **argv)
{
/*1.准备颜色 */
int color[480][800];
int i,j;
//循环屏幕的第一块颜色的每一行
for(i=0;i<160;i++)
{
for(j=0;j<800;j++)//循环每一列
{
color[i][j]=0x00ff0000;
}
}
//循环屏幕的第二块颜色的每一行
for(i=161;i<320;i++)
{
for(j=0;j<800;j++)
{
color[i][j]=0x0000ff00;
}
}
//循环屏幕的第三块颜色的每一行
for(i=321;i<480;i++)
{
for(j=0;j<800;j++)
{
color[i][j]=0x000000ff;
}
}
/*2.打开设备 */
int fd = open("/dev/fb0",O_WRONLY);
if(-1==fd)
{
printf("文件打开失败");
return -1;
}
/*3.将颜色数据写入设备文件*/
int ret=write(fd,color,800*480*4);//每个元素占4个字节
if(-1==ret)
{
printf("文件写入失败");
return -2;
}
/*4.关闭设备文件*/
close(fd);
return 0;
}
方法二:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char **argv)
{
/*1.准备颜色 */
/*****方法二****/
int color[800*480];
int i = 0;
for(;i<800*160;i++)
color[i] = 0xff0000;//显示红色
for(;i<800*320;i++)
color[i] = 0xff00;//显示绿色
for(;i<800*480;i++)
color[i] = 0xff;//显示蓝色
/*2.打开设备 */
int fd = open("/dev/fb0",O_WRONLY);
if(-1==fd)
{
printf("文件打开失败");
return -1;
}
/*3.将颜色数据写入设备文件*/
int ret=write(fd,color,800*480*4);//每个元素占4个字节
if(-1==ret)
{
printf("文件写入失败");
return -2;
}
/*4.关闭设备文件*/
close(fd);
return 0;
}
结果如图:
例3:竖屏显示三色
代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char **argv)
{
/*1.准备颜色 */
int color[800*480];
for(int i=0;i<800*480;i++)
{
int tmp = i%800;
if(tmp<200)
color[i] = 0xff0000;//显示红色
else if(tmp<500)
color[i] = 0xff00;//显示绿色
else
color[i] = 0xff;//显示蓝色
}
/*2.打开设备 */
int fd = open("/dev/fb0",O_WRONLY);
if(-1==fd)
{
printf("文件打开失败");
return -1;
}
/*3.将颜色数据写入设备文件*/
int ret=write(fd,color,800*480*4);//每个元素占4个字节
if(-1==ret)
{
printf("文件写入失败");
return -2;
}
/*4.关闭设备文件*/
close(fd);
return 0;
}
结果显示:
二、使用rz上传文件
1.利用rx将rz上传开发板
2.修改rz的执行权限 chmod u+x rz
3.将rz移到【/bin] mv rz /bin
4.将需要上传的文件直接拖到CRT界面
5.松开鼠标之后,选择【发送Zmodel]