×

使用MAX7219驱动8x8点阵

Kalet Kalet 发表于2017-09-17 17:53:42 浏览345 评论0

抢沙发发表评论

前言

转至:http://www.jianshu.com/p/245ea9a43c98


本实验使用MAX7219驱动8x8点阵。上位机使用Ubuntu 14.04,下位机使用Raspberry pi 2。

本次实验又有攻略带飞,简直爽。

使用MAX7219点阵模块

使用MAX7219点阵模块除了需要提供GND以及VCC外,只需要再提供三根引脚即可点亮矩阵。其中,DIN引脚输入数据,CS(LOAD)引脚控制数据输入,CLK引脚用于区分每个bit。

整个写入流程为,首先CS引脚置0,表示允许写入。而后从高位顺序写入16个bit。每个bit的写入方式为首先DIN置为要写入的bit值,而后CLK产生一个下降沿(图中为上升沿,不知道为何有差别)即被读入。最后CS引脚置1表示写入结束。

而除了数据传输流程之外,接下来是如何在点阵上显示图案了。

在运行之前,需要进行一次初始化,其行为是向某几个特定的地址写入特定的值。至少需要写入两个地址,第一个是0x0b,写入0x07表示扫描显示所有行。第二个是0x0c,写入1表示进入工作模式。

而后点阵上每一行都有其地址,如第一行是0x01到第八行是0x08,每次向固定行的地址写入一个8位二进制数即可在指定行上显示图案。

据此写成程序即可。

树莓派使用引脚1, 3, 5, 7, 9,其中1为VCC,9为GND。而3, 5, 7分别是GPIO_Pin2, GPIO_Pin3, GPIO_Pin4。3接DIN,5接CS,7接CLK。

由于上一个实验编译内核给我造成了巨大的阴影,我决定这次实验不编译Arduino-ish库,而是直接使用Linux自带的GPIO子系统,通过操作文件的方式控制GPIO。而文件这种东西写起来最方便的就是linux自带的shell了。

#!/bin/bash# DIN, CS, CLK的GPIO口位置DIN=4CS=3CLK=2# 一些文件路径GPIO_BASE=/sys/class/gpioGPIO_EXPORT=${GPIO_BASE}/exportGPIO_UNEXPORT=${GPIO_BASE}/unexportBIN=(00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000)# 生成指定GPIO引脚的文件夹位置function GPIO(){echo ${GPIO_BASE}/gpio$1}# 将某个引脚export到用户态function GPIO_export(){if [ -d `GPIO $1` ]; thenecho GPIO pin $1 found in folder.elseecho $1 > ${GPIO_EXPORT}fi}# unexport某个引脚function GPIO_unexport(){if [ -d `GPIO $1` ]; thenecho $1 > ${GPIO_UNEXPORT}elseecho GPIO pin $1 not found.fi}# 改变某个引脚的方向function GPIO_direction(){echo $2 > `GPIO $1`/direction}# 改变某个引脚的值function GPIO_set(){echo $2 > `GPIO $1`/value}# 改变DIN的值function GPIO_DIN(){GPIO_set $DIN $1}# 改变CS的值function GPIO_CS(){GPIO_set $CS $1}# 改变CLK的值function GPIO_CLK(){GPIO_set $CLK $1}# 向MAX7219发送一个byte的值function Matrix_send_char(){local i=1for ((i=1;i<=8;i++)); dochr=`expr substr $1 $i 1`GPIO_DIN $chrGPIO_CLK 1GPIO_CLK 0done}# 向MAX7219发送一次完整的信号function Matrix_send_word(){GPIO_CS 1GPIO_CS 0GPIO_CLK 0Matrix_send_char $1Matrix_send_char $2GPIO_CS 1}# 初始化GPIO引脚function GPIO_init(){GPIO_export $DINGPIO_export $CSGPIO_export $CLKsleep 2GPIO_direction $DIN outGPIO_direction $CS outGPIO_direction $CLK out}# 清除GPIO引脚function GPIO_clear(){GPIO_unexport $DINGPIO_unexport $CSGPIO_unexport $CLK}# 在点阵上显示数据function Matrix_render(){local i=1for ((i=0;i<8;i++)); doecho $i $1Matrix_send_word ${BIN[$i]} $1shiftdone}# 使用文件中的数据进行显示function Matrix_render_file(){local tmp=(`cat $1`)Matrix_render "${tmp[@]}"}# 使用某个图案清屏function Matrix_clear(){local LM=(1000000010000000100000001001000110011011111101010001000100010001)Matrix_render "${LM[@]}"}# 初始化点阵function Matrix_init(){# 编码模式Matrix_send_word 00001001 00000000# 亮度Matrix_send_word 00001010 00000011# 扫描数码管个数Matrix_send_word 00001011 00000111# 工作模式Matrix_send_word 00001100 00000001# 初始化完毕后清屏显示默认图案Matrix_clear}

使用方式为

source matrix.shGPIO_initMatrix_init

清屏时显示的图案

而后,使用Matrix_render_file可以将某个文件内的数据显示到矩阵上。

pi@raspberrypi ~/src $ cat journey0000000001111010001011000010111001111010000011100101101000000000pi@raspberrypi ~/src $ Matrix_render_file journey0 000000001 011110102 001011003 001011104 011110105 000011106 010110107 00000000

使用journey文件中的数据显示的矩阵

当然,使用上面那个shell程序的弊端在于交不了作业啊,翁老大要求的是c/c++的程序啊。

所以说,照着写一个c版本的就好了喽。

/** 这里原本有一些支持函数,用于将引脚操作映射为文件写入操作* 但是这些函数在地震中消失了*/#define DIN 4#define CS 3#define CLK 2GPIO_Pin din, cs, clk;int write_byte(unsigned char b){unsigned char i, tmp;for (i=0; i<8; i++){tmp = (b & 0x80) > 0;b <<= 1;GPIO_Pin_Write(&din, tmp);GPIO_Pin_Write(&clk, 1);GPIO_Pin_Write(&clk, 0);}}int write_word(unsigned char addr, unsigned char num){GPIO_Pin_Write(&cs, 1);GPIO_Pin_Write(&cs, 0);GPIO_Pin_Write(&clk, 0);write_byte(addr);write_byte(num);GPIO_Pin_Write(&cs, 1);}int Matrix_init(){write_word(0x09, 0x00);write_word(0x0a, 0x03);write_word(0x0b, 0x07);write_word(0x0c, 0x01);}int Matrix_render(unsigned char* tmp){int i;for (i=0; i<8; i++){printf("%d %d\n", i, tmp[i]);write_word(i+1, tmp[i]);}}int Matrix_clear(){unsigned char LM[]={0x80, 0x80, 0x80, 0x91, 0x9b, 0xf5, 0x11, 0x11};Matrix_render(LM);}int main(){GPIO_Pin_export(&din, DIN, GPIO_DIRECTION_OUT);GPIO_Pin_export(&cs, CS, GPIO_DIRECTION_OUT);GPIO_Pin_export(&clk, CLK, GPIO_DIRECTION_OUT);Matrix_init();Matrix_clear();return 0;}

字符设备驱动程序

编译内核模块相关的一些步骤参照Lab6。而本节关心的是如何使用内核模块折腾MAX7219。

该模块在加载到系统内部时,会申请在/dev目录下生成一个matrix文件。使用的时候直接向其输出想要在点阵上显示的字符即可。

而在这个过程中,写入的时候会触发模块中的matrix_write函数。

如果是阻塞式的写入,那么遍历一遍buffer数组,并将其中的每个字符调用Matrix_render显示出来即可,字符与字符之间可以使用mdelay进行毫秒级别的延迟操作。

而如果是非阻塞式的写入,则要使用生产者消费者的思想。写入操作触发的matrix_write是生产者,所做的只需要将当前需要显示的所有内容放置入显示队列内即可返回。而在另一面,有一个定时触发的消费者函数,该函数根据队列内部是否有未显示字符进行操作。

#include <linux/init.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/string.h>#include <linux/delay.h>#include <linux/gpio.h>#include <linux/workqueue.h>MODULE_LICENSE("GPL");MODULE_AUTHOR("lmzqwer2 <lmzqwerty@163.com>");MODULE_DESCRIPTION("In-kernel MAX7219 support.");#define DIN 4#define CS 3#define CLK 2void write_byte(unsigned char b){unsigned char i, tmp;for (i=0; i<8; i++){tmp = (b & 0x80) > 0;b <<= 1;gpio_set_value(DIN, tmp);gpio_set_value(CLK, 1);gpio_set_value(CLK, 0);}}void write_word(unsigned char addr, unsigned char num){gpio_set_value(CLK, 0);gpio_set_value(CS, 1);gpio_set_value(CS, 0);write_byte(addr);write_byte(num);gpio_set_value(CS, 1);}void Matrix_render(unsigned char* tmp){int i;for (i=0; i<8; i++){write_word(i+1, tmp[i]);}}unsigned char digits[][8]={{0x1c, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x1c}, // 0{0x08, 0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x1c}, // 1{0x1c, 0x22, 0x22, 0x04, 0x08, 0x10, 0x20, 0x3e}, // 2{0x1c, 0x22, 0x02, 0x0c, 0x02, 0x02, 0x22, 0x1c}, // 3{0x04, 0x0c, 0x14, 0x14, 0x24, 0x1e, 0x04, 0x04}, // 4{0x3e, 0x20, 0x20, 0x3c, 0x02, 0x02, 0x22, 0x1c}, // 5{0x1c, 0x22, 0x20, 0x3c, 0x22, 0x22, 0x22, 0x1c}, // 6{0x3e, 0x24, 0x04, 0x08, 0x08, 0x08, 0x08, 0x08}, // 7{0x1c, 0x22, 0x22, 0x1c, 0x22, 0x22, 0x22, 0x1c}, // 8{0x1c, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x22, 0x1c}, // 9};unsigned char LM[]={0x80, 0x80, 0x80, 0x91, 0x9b, 0xf5, 0x11, 0x11};void Matrix_clear(void){Matrix_render(LM);}void Matrix_init(void){write_word(0x09, 0x00);write_word(0x0a, 0x03);write_word(0x0b, 0x07);write_word(0x0c, 0x01);Matrix_clear();}#define BUFFERSIZE 128#define DELAYTIME 1unsigned char disp[BUFFERSIZE];int head = 0, tail = 0;static struct timer_list timer;void Matrix_next_display(unsigned long);void ptr_inc(int *ptr){*ptr = (*ptr + 1) % BUFFERSIZE;}static void timer_register(struct timer_list* ptimer){init_timer(ptimer);ptimer->data = DELAYTIME;ptimer->expires = jiffies + (DELAYTIME * HZ);ptimer->function = Matrix_next_display;add_timer(ptimer);}void disp_start(void){timer_register(&timer);}void Matrix_next_display(unsigned long data){if (head != tail){unsigned char *ptr = LM;unsigned char c = disp[head];if ('0' <= c && c <= '9'){ptr = digits[c - '0'];}Matrix_render(ptr);ptr_inc(&head);disp_start();}else{Matrix_clear();}}static int matrix_write(struct file *file, const char __user *buffer,size_t count, loff_t *ppos){int i;if (head == tail && count > 0){disp_start();}for (i=0; i<count; i++){ptr_inc(&tail);if (tail == head)ptr_inc(&head);disp[tail] = buffer[i];}return count;}static struct file_operations matrix_fops = {.owner = THIS_MODULE,.write = matrix_write,.llseek = noop_llseek};static struct miscdevice matrix_misc_device = {.minor = MISC_DYNAMIC_MINOR,.name = "matrix",.fops = &matrix_fops};static int __init matrix_init(void){if (!gpio_is_valid(DIN) || !gpio_is_valid(CLK) || !gpio_is_valid(CS)){printk(KERN_INFO "GPIO_TEST: invalid GPIO\n");return -ENODEV;}misc_register(&matrix_misc_device);gpio_request(DIN, "sysfs");gpio_direction_output(DIN, 0);gpio_request(CS, "sysfs");gpio_direction_output(CS, 1);gpio_request(CLK, "sysfs");gpio_direction_output(CLK, 0);Matrix_init();printk(KERN_INFO"matrix device has been registed.\n");return 0;}static void __exit matrix_exit(void){misc_deregister(&matrix_misc_device);printk(KERN_INFO"matrix device has been unregisted.\n");del_timer(&timer);}module_init(matrix_init);module_exit(matrix_exit);

然后就是熟悉的编译,载入,运行。命令中使用echo命令触发matrix_write函数。

lmuser@LMubt [05:53:21] [~/tmp/miscmatric]% make && scp matrix.ko pi@..........pi@raspberrypi ~ $ sudo insmod matrix.kopi@raspberrypi ~ $ cd /devpi@raspberrypi /dev $ sudo chown pi:pi matrixpi@raspberrypi /dev $ echo 0123456789 > matrix

按顺序显示0-9

参考资料



作者:lmzqwer2
链接:http://www.jianshu.com/p/245ea9a43c98
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。http://www.jianshu.com/p/245ea9a43c98http://www.jianshu.com/p/245ea9a43c98http://www.jianshu.com/p/245ea9a43c98http://www.jianshu.com/p/245ea9a43c98

群贤毕至

访客