아두이노

시프트 레지스터 (MAX 7219)를 이용한 세븐 시그먼트 8개 표시하기 - 튜토리얼(코딩1)

ILETCTM 2014. 2. 27. 22:20
반응형

 

하룻동안 컴퓨터 화면만 보고 있으니 피곤하다

게임할때는 하나도 안 피곤하더니.. 역시 아무리 좋아해도 공부는 일인것 같다.

우선 불러온 튜토리얼에서 코딩 되어있는 부분을 복붙하였다.


import muvium.compatibility.arduino.*;

public class Demo7219Digits extends Arduino

{

 
 /* code for max 7219 from maxim,
reduced and optimised for useing more then one 7219 in a row,
______________________________________

 Code History:
 --------------

The orginal code was written for the Wiring board by:
 * Nicholas Zambetti and Dave Mellis /Interaction Design Institute Ivrea /Dec 2004
 * http://www.potemkin.org/uploads/Wiring/MAX7219.txt

First modification by:
 * Marcus Hannerstig/  K3, malm� h�gskola /2006
 * http://www.xlab.se | http://arduino.berlios.de

This version is by:
 * tomek ness /FH-Potsdam / Feb 2007
 * http://design.fh-potsdam.de/

 * @acknowledgements: eric f.

-----------------------------------

General notes:


-if you are only using one max7219, then use the function maxSingle to control
 the little guy ---maxSingle(register (1-8), collum (0-255))

-if you are using more then one max7219, and they all should work the same,
then use the function maxAll ---maxAll(register (1-8), collum (0-255))

-if you are using more than one max7219 and just want to change something
at one little guy, then use the function maxOne
---maxOne(Max you wane controll (1== the first one), register (1-8),
collum (0-255))

/* During initiation, be sure to send every part to every max7219 and then
 upload it.
For example, if you have five max7219's, you have to send the scanLimit 5 times
before you load it-- other wise not every max7219 will get the data. the
function maxInUse  keeps track of this, just tell it how many max7219 you are
using.
*/

 int dataIn = 2;
 int load = 3;
 int clock = 4;

 int maxInUse = 4;    //change this variable to set how many MAX7219's you'll use

 int e = 0;           // just a varialble
 int offset = 0;
 
 // define max7219 registers
 byte max7219_reg_noop = 0x00;
 byte max7219_reg_digit0 = 0x01;
 byte max7219_reg_digit1 = 0x02;
 byte max7219_reg_digit2 = 0x03;
 byte max7219_reg_digit3 = 0x04;
 byte max7219_reg_digit4 = 0x05;
 byte max7219_reg_digit5 = 0x06;
 byte max7219_reg_digit6 = 0x07;
 byte max7219_reg_digit7 = 0x08;
 byte max7219_reg_decodeMode = 0x09;
 byte max7219_reg_intensity = 0x0a;
 byte max7219_reg_scanLimit = 0x0b;
 byte max7219_reg_shutdown = 0x0c;
 byte max7219_reg_displayTest = 0x0f;

 void putByte(int data) {
  int i = 8;
  int mask;
  while(i > 0) {
   mask = 0x01 << (i - 1);      // get bitmask
   digitalWrite(clock, LOW);   // tick
   if ( ( data & mask ) != 0 ){            // choose bit
    digitalWrite(dataIn, HIGH); // send 1
   }else{
    digitalWrite(dataIn, LOW); // send 0
   }
   digitalWrite(clock, HIGH);   // tock
   --i;                         // move to lesser bit
  }
 }

 void maxSingle(int reg, int col) {   
  //maxSingle is the "easy"  function to use for a     //single max7219

  digitalWrite(load, LOW);       // begin     
  putByte(reg);                  // specify register
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data   
  digitalWrite(load, LOW);       // and load da shit
  digitalWrite(load, HIGH);
 }

 void maxAll (int reg, int col) {    // initialize  all  MAX7219's in the system
  int c = 0;
  digitalWrite(load, LOW);  // begin     
  for ( c =1; c <= maxInUse; c++) {
   putByte(reg);  // specify register
   putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
  }
  digitalWrite(load, LOW);
  digitalWrite(load, HIGH);
 }

 void maxOne(int maxNr, int reg, int col) {   
  //maxOne is for adressing different MAX7219's,
  //whilele having a couple of them cascaded

  int c = 0;
  digitalWrite(load, LOW);  // begin    

  for ( c = maxInUse; c > maxNr; c--) {
   putByte(0);    // means no operation
   putByte(0);    // means no operation
  }

  putByte(reg);  // specify register
  putByte(col); //((data & 0x01) * 256) + data >> 1); // put data

  for ( c =maxNr - 1; c >= 1; c--) {
   putByte(0);    // means no operation
   putByte(0);    // means no operation
  }

  digitalWrite(load, LOW); // and load da shit
  digitalWrite(load, HIGH);
 }


 
 // The setup() method runs once, when the sketch starts 
 public void setup(){  
 
  // Your setup code goes here


  pinMode(dataIn, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(load, OUTPUT);

  Serial.begin(9600);
  
  digitalWrite(13, HIGH); 

  //initiation of the max 7219
  maxAll(max7219_reg_scanLimit, 0x07);     
  maxAll(max7219_reg_decodeMode, 0xFF);  // using an led matrix (not digits)
  maxAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
  maxAll(max7219_reg_displayTest, 0xFF); // no display test
  for (e=1; e <= 8; e++) {    // empty registers, turn all LEDs off 
   maxAll(e, 0);
  }
  maxAll(max7219_reg_intensity, 0x0f & 0x0f);    // the first 0x0f is the value you can set
  // range: 0x00 to 0x0f
 } 
 

 // the loop() method runs over and over again,
 // as long as the Arduino has power 
 public void loop(){
 
 //if you use just one MAX7219 it should look like this
  for( int i = 0; i < 8; i++ ){
   maxSingle(i+1, i);  
  }
  
  delay(500);
 }
}

 

초심자에게는 정말 긴 것 같은 코딩이다.

해당 코드들은 시프트레지스터가 여러개 있을 경우에 해당하는 코드들이므로 이해하기가 어려워 시프트 레지스터를 하나만 쓸 때 필요한 코드들을 추려 내어 보기로 하였다.

 

 

 

대충 느낌으로 추려내어야 할 것을 찾아보니..

int maxInUse = 4; 이부분은 최대 사용하는 시프트 레지스터숫자니까 빼고,

maxALL함수도 빼고..

maxOne도 빼고..

함수들을 뺏으니 setup함수나 loop함수에서도 maxALL이나 maxOne 함수를 maxSingle로 바꿔보았다.

그 결과



 

그나마 간략한 코드가 나왔다.

위 코드를 다섯구간으로 분류를 해 보자면..

 

1- 코딩에 필요한 변수선언구간

2- 시프트 레지스터의 16종류 저장소의 주소

3- 시프트 레지스터의 16개의 저장소에 16비트의 데이터의 각각 자릿수를 CLK와 연동하여 저장하는 구간

4- 시프트 레지스터를 켜고 끄면서 입력되는 16비트의 데이터의 종류를 씨앗을 받아 구성하는 구간

5- 시프트 레지스터의 옵션을 조정하고 0,1,2,3,4,5,6,7 의 변수를 쓰면서 각각 다른 데이터의 씨앗을 넣어주는 구간.

복잡하다..

 

다음글은 각 구간에대한 자세한 설명이 이어질 예정!

반응형