Arduino使用蓝牙控制8×8点阵显示
8×8点阵简介
查看往期文章——Arduino控制8×8点阵显示中的相关介绍
HC-08蓝牙模块
查看往期文章——蓝牙控制一位数码管显示中的相关介绍
连接线路图
使用引脚数量为16个,所以需要占用开发板16个引脚。考虑到使用的开发板只有14个数字引脚,6个模拟引脚,所以这里将模拟引脚做数字引脚使用。使用的数字引脚为编号2 ~ 13,模拟引脚为A0 ~ A3。
连接电路图时为避免线路缠绕,采用按顺序一次连接引脚的方式。点阵1~16引脚分别对应开发板的10,11,12,13,A3,A2,A1,A0,2,3,4,5,6,7,8,9。连接时考虑到保护点阵,在各个引脚进线处增加了电阻。
编写控制程序
- 理清引脚-编号关系,列表查看如下:
因此定义控制点阵列引脚数组:
//Col : 13,3,4,10,6,11,15,16(点阵引脚)
int colPin[]= {6,A2,A3,3,12,4,8,9}; //pin
C++
定义控制点阵行引脚数组:
//Row : 9,14,8,12,1,7,2,5(点阵引脚)
int rowPin[]= {2,7,10,5,A0,11,A1,13}; //pin
C++
编写程序如下:
在setup()中:
- 启动串口通讯:
Serial.begin(9600);
C++
- 设置所有引脚为输出模式:
for(int i=0;i<8;i++){
pinMode(colPin[i],OUTPUT);//设置输出
pinMode(rowPin[i],OUTPUT);//设置输出
}
C++
- 调用reset()实现点阵显示清空,reset()定义如下:
void reset(){
for(int i=0;i<8;i++){
digitalWrite(colPin[i],LOW);
digitalWrite(rowPin[i],HIGH);
}
}
C++
此外,需要定义一些函数:
- 定义显示函数showChar(),用于使点阵显示相应字符:
void showChar(int data[8][8]){
for(int i=0;i<100;i++){
for(int row=0;row<8;row++){
digitalWrite(rowPin[row],LOW);
for(int col=0;col<8;col++){
digitalWrite(colPin[col],data[row][col]);
}
delay(1);
reset();
}
}
delay(1000);
}
C++
所需传入参数为8×8的二维整型数组data;最外层i-for循环100次用于延长单次显示时间,大约持续零点几秒,否则显示时间过短,效果不好;row-for循环中,每次循环首先将该行控制的点阵行通低电平:digitalWrite(rowPin[row],LOW);然后再col-for循环遍历每列,将该列控制的点阵列通data数组相应的电平值:digitalWrite(colPin[col],data[row][col]);
调用示例如下:
//显示数字‘0’
int data[8][8]={
0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,0,1,0,0,1,0,0,
0,0,0,1,1,0,0,0
};
showChar(data);
C++
- 读取蓝牙串口传输的数据并显示:
void loop(){
if(Serial.available()>0){
char c = Serial.read();
dealChar(c);
Serial.print("char value:");
Serial.println(c);
}
}
C++
其中dealChar()为定义的根据传入字符来控制具体显示的函数,详见下文。
案例
编写程序依次显示数字H,e,l,l,o,A,r,d,u,i,n,o,!,0,1,2,3,4,5,6,7,8,9和爱心,并实现可由蓝牙进行控制:
//Col : 13,3,4,10,6,11,15,16(点阵引脚)
const int colPin[]= {6,A2,A3,3,12,4,8,9}; //pin
//Row : 9,14,8,12,1,7,2,5(点阵引脚)
const int rowPin[]= {2,7,10,5,A0,11,A1,13}; //pin
const int PROGMEM data_0[8][8]=
{
0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,0,1,0,0,1,0,0,
0,0,0,1,1,0,0,0
};
const int PROGMEM data_1[8][8]=
{
0,0,0,1,1,0,0,0,
0,0,1,0,1,0,0,0,
0,0,0,0,1,0,0,0,
0,0,0,0,1,0,0,0,
0,0,0,0,1,0,0,0,
0,0,0,0,1,0,0,0,
0,0,0,0,1,0,0,0,
0,1,1,1,1,1,1,0
};
const int PROGMEM data_2[8][8]=
{
0,0,1,1,1,0,0,0,
0,1,0,0,0,1,0,0,
0,1,0,0,0,0,1,0,
0,0,0,0,0,0,1,0,
0,0,0,0,0,1,0,0,
0,0,0,0,1,0,0,0,
0,0,1,1,0,0,0,0,
0,1,1,1,1,1,1,0
};
const int PROGMEM data_3[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0,0,0,1,0,
0,0,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,0,1,1,1,1,0,0
};
const int PROGMEM data_A[8][8]=
{
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,0,1,1,1,1,0,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1
};
const int PROGMEM data_a[8][8]=
{
0,0,1,1,1,1,1,0,
0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,1,
0,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,1,1,
0,1,1,1,1,1,0,1
};
const int PROGMEM data_b[8][8]=
{
0,1,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,
0,1,0,1,1,1,0,0,
0,1,1,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,1,0,0,
0,1,1,1,1,0,0,0
};
const int PROGMEM data_c[8][8]=
{
0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,
0,1,0,0,0,0,1,0,
0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0
};
const int PROGMEM data_d[8][8]=
{
0,0,0,0,0,0,1,0,
0,0,0,0,0,0,1,0,
0,0,0,0,0,0,1,0,
0,1,1,1,1,0,1,0,
1,0,0,0,0,1,1,0,
1,0,0,0,0,0,1,0,
1,0,0,0,0,1,1,0,
0,1,1,1,1,0,0,1
};
const int PROGMEM data_e[8][8]=
{
0,0,0,0,0,0,0,0,
0,0,0,1,1,1,0,0,
0,0,1,0,0,0,1,0,
0,1,1,1,1,1,0,0,
0,1,0,0,0,0,0,0,
0,1,0,0,0,0,1,0,
0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0
};
const int PROGMEM data_H[8][8]=
{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,0,
0,0,1,0,0,0,0,0,
0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,0
};
const int PROGMEM data_u[8][8]=
{
0,0,0,0,0,0,0,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,0,1,0,
0,1,0,0,0,1,1,0,
0,0,1,1,1,0,1,0,
0,0,0,0,0,0,0,0
};
const int PROGMEM data_warning[8][8]=
{
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0
};
const int PROGMEM data_default[8][8]=
{
0,0,0,0,0,0,0,0,
0,1,1,1,0,1,1,0,
1,0,0,0,1,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
0,1,0,0,0,0,1,0,
0,0,1,0,0,1,0,0,
0,0,0,1,1,0,0,0
};
void setup() {
Serial.begin(9600); //启动串口通讯
for(int i=0;i<8;i++){
pinMode(colPin[i],OUTPUT);//设置输出
pinMode(rowPin[i],OUTPUT);//设置输出
}
reset();
initView();
}
void reset(){
for(int i=0;i<8;i++){
digitalWrite(colPin[i],LOW);
digitalWrite(rowPin[i],HIGH);
}
}
void dealChar(char ch){
switch(ch){
case '0' :{
showChar(data_0);
break;
}
case '1' :{
showChar(data_1);
break;
}
case '2' :{
showChar(data_2);
break;
}
case '3' :{
showChar(data_3);
break;
}
case '4' :{
showChar(data_4);
break;
}
case '5' :{
showChar(data_5);
break;
}
case '6' :{
showChar(data_6);
break;
}
case '7' :{
showChar(data_7);
break;
}
case '8' :{
showChar(data_8);
break;
}
case '9' :{
showChar(data_9);
break;
}
case 'A' :{
showChar(data_A);
break;
}
case 'a' :{
showChar(data_a);
break;
}
case 'b' :{
showChar(data_b);
break;
}
case 'c' :{
showChar(data_c);
break;
}
case 'd' :{
showChar(data_d);
break;
}
case 'e' :{
showChar(data_e);
break;
}
case 'H' :{
showChar(data_H);
break;
}
case 'h' :{
showChar(data_h);
break;
}
case 'i' :{
showChar(data_i);
break;
}
case 'l' :{
showChar(data_l);
break;
}
case 'n' :{
showChar(data_n);
break;
}
case 'o' :{
showChar(data_o);
break;
}
case 'r' :{
showChar(data_r);
break;
}
case 'u' :{
showChar(data_u);
break;
}
case '!' :{
showChar(data_warning);
break;
}
default :{
showChar(data_default);
break;
}
}
}
void showChar(const int data[8][8]){
for(int i=0;i<50;i++){
for(int row=0;row<8;row++){
digitalWrite(rowPin[row],LOW);
for(int col=0;col<8;col++){
digitalWrite(colPin[col],pgm_read_byte(&data[row][col]));
}
delay(1);
reset();
}
}
delay(100);
}
void showCharList(const char * list){
for(int i=0;i<strlen(list);i++){
dealChar(list[i]);
}
}
void initView(){
showCharList("HelloArduino!0123456789.");
}
void loop() {
if(Serial.available()>0){
char c = Serial.read();
dealChar(c);
Serial.print("char value:");
Serial.println(c);
}
}
C++