SHARE
TWEET

max6921.c

a guest Jan 22nd, 2016 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <msp430G2553.h>
  2. #include "MAX6921.h"
  3.  
  4. //Define our pins
  5. #define DATA BIT4  // P1.4
  6. #define CLOCK BIT6 // P1.6
  7. #define LATCH BIT5 // P1.5
  8.  
  9. void setupMax6921(void){
  10.     P1DIR |= (DATA + CLOCK + LATCH);  // Setup pins as outputs
  11. }
  12. // Pulse the clock pin
  13. void pulseClock(void){
  14.   P1OUT |= CLOCK;
  15.   P1OUT ^= CLOCK;
  16. }
  17.  
  18. // Writes a value to the specified bitmask/pin. Use built in defines
  19. // when calling this, as the shiftOut() function does.
  20. // All nonzero values are treated as "high" and zero is "low"
  21. void pinWrite(unsigned int bit, unsigned char val){
  22.   if (val){
  23.     P1OUT |= bit;
  24.   } else {
  25.     P1OUT &= ~bit;
  26.   }
  27. }
  28.  
  29. unsigned int getNum(unsigned int num){
  30.     switch(num){
  31.     //             0b--------ABCDEFGH
  32.     case 0: return 0b0000000011111100;
  33.     case 1: return 0b0000000001100000;
  34.     case 2: return 0b0000000011011010;
  35.     case 3: return 0b0000000011110010;
  36.     case 4: return 0b0000000001100110;
  37.     case 5: return 0b0000000010110110;
  38.     case 6: return 0b0000000010111110;
  39.     case 7: return 0b0000000011100000;
  40.     case 8: return 0b0000000011111110;
  41.     case 9: return 0b0000000011100110;
  42.     default: return 0b0000000000000010;
  43.     }
  44. }
  45.  
  46.  
  47. unsigned int getDigit(unsigned int digit){
  48.     switch(digit){
  49.     //             0b--------12345678;
  50.     case 1: return 0b0000000010000000;
  51.     case 2: return 0b0000000001000000;
  52.     case 3: return 0b0000000000100000;
  53.     case 4: return 0b0000000000010000;
  54.     case 5: return 0b0000000000001000;
  55.     case 6: return 0b0000000000000100;
  56.     case 7: return 0b0000000000000010;
  57.     case 8: return 0b0000000000000001;
  58.     default: return 0b0000000000000000;
  59.  
  60.     }
  61. }
  62.  
  63. void print(unsigned int digit, unsigned int num, unsigned int pm){
  64.     //Set latch to low (should be already)
  65.       P1OUT &= ~LATCH;
  66.       unsigned int i;
  67.       unsigned int digitData;
  68.       unsigned int numData;
  69.       // If pm = 1; then we are drawing the PM dot
  70.       if(pm == 1){
  71.           digitData = 0;
  72.           numData = 1;
  73.           // Shift out the first 2 bits as zeros and the 3rd bit as a 1 (to indicate digit 9)
  74.           P1OUT &= ~DATA;
  75.           for(i = 0; i < 2; i++){
  76.               pulseClock();
  77.               }
  78.           P1OUT |= DATA;
  79.           pulseClock();
  80.           }
  81.       else{
  82.           numData = getNum(num);
  83.           digitData = getDigit(digit);
  84.           // Shift out the first 3 bits as zeros (2 not connected to IV-18, and we're not drawing PM dot)
  85.           P1OUT &= ~DATA;
  86.           for(i = 0; i < 3; i++){
  87.               pulseClock();
  88.           }
  89.       }
  90.      // Shift out digit bits
  91.      for(i=0; i<8; i++){
  92.          pinWrite(DATA, (digitData & (1 << i)));
  93.          pulseClock();
  94.          }
  95.      // Shift out the numner bits
  96.      for(i=0; i<8; i++){
  97.          pinWrite(DATA, (numData & (1 << i)));
  98.          pulseClock();
  99.          }
  100.      // shift out one more NC bit
  101.     P1OUT &= ~DATA;
  102.     pulseClock();
  103.     // Pulse the latch pin to write the values into the storage register
  104.     P1OUT |= LATCH;
  105.     P1OUT &= ~LATCH;
  106. }
RAW Paste Data
Top