UCS1903 tape led

Dependencies:   mbed

Committer:
okini3939
Date:
Thu Jan 17 13:49:36 2013 +0000
Revision:
0:49551c775fc5
1st build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:49551c775fc5 1 /*
okini3939 0:49551c775fc5 2 * UCS1903 tape led IC
okini3939 0:49551c775fc5 3 */
okini3939 0:49551c775fc5 4 #include "mbed.h"
okini3939 0:49551c775fc5 5 #include "LEDTape.h"
okini3939 0:49551c775fc5 6
okini3939 0:49551c775fc5 7 SPI tape(p11, p12, p13);
okini3939 0:49551c775fc5 8
okini3939 0:49551c775fc5 9 //#define tape_write(d) tape.write(d)
okini3939 0:49551c775fc5 10 #ifndef tape_write
okini3939 0:49551c775fc5 11 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
okini3939 0:49551c775fc5 12 void tape_write (int d) {
okini3939 0:49551c775fc5 13 while (! (LPC_SSP0->SR & (1<<1))); // TNF
okini3939 0:49551c775fc5 14 LPC_SSP0->DR = d;
okini3939 0:49551c775fc5 15 }
okini3939 0:49551c775fc5 16 #elif defined(TARGET_LPC11U24)
okini3939 0:49551c775fc5 17 void tape_write (int d) {
okini3939 0:49551c775fc5 18 while (! (LPC_SSP1->SR & (1<<1))); // TNF
okini3939 0:49551c775fc5 19 LPC_SSP1->DR = d;
okini3939 0:49551c775fc5 20 }
okini3939 0:49551c775fc5 21 #endif
okini3939 0:49551c775fc5 22 #endif
okini3939 0:49551c775fc5 23
okini3939 0:49551c775fc5 24 void tapeReset (int flag) {
okini3939 0:49551c775fc5 25 int i;
okini3939 0:49551c775fc5 26
okini3939 0:49551c775fc5 27 __disable_irq();
okini3939 0:49551c775fc5 28 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
okini3939 0:49551c775fc5 29 if (flag) {
okini3939 0:49551c775fc5 30 while ((LPC_SSP0->SR & ((1<<4)|(1<<0))) != (1<<0)); // BSY, TFE
okini3939 0:49551c775fc5 31 LPC_PINCON->PINSEL1 &= ~(0x03<<4); // GPIO
okini3939 0:49551c775fc5 32 LPC_GPIO0->FIODIR0 |= (1<<18);
okini3939 0:49551c775fc5 33 LPC_GPIO0->FIOSET0 = (1<<18);
okini3939 0:49551c775fc5 34 } else {
okini3939 0:49551c775fc5 35 for (i = 0; i < 4; i ++) {
okini3939 0:49551c775fc5 36 tape_write(0x1f);
okini3939 0:49551c775fc5 37 }
okini3939 0:49551c775fc5 38 LPC_PINCON->PINSEL1 &= ~(0x03<<4);
okini3939 0:49551c775fc5 39 LPC_PINCON->PINSEL1 |= (0x02<<4); // SSP MOSI
okini3939 0:49551c775fc5 40 }
okini3939 0:49551c775fc5 41 #elif defined(TARGET_LPC11U24)
okini3939 0:49551c775fc5 42 if (flag) {
okini3939 0:49551c775fc5 43 while ((LPC_SSP1->SR & ((1<<4)|(1<<0))) != (1<<0)); // BSY, TFE
okini3939 0:49551c775fc5 44 LPC_IOCON->PIO1_22 &= ~0x07; // GPIO
okini3939 0:49551c775fc5 45 LPC_GPIO->DIR[1] |= (1<<22);
okini3939 0:49551c775fc5 46 LPC_GPIO->SET[1] |= (1<<22);
okini3939 0:49551c775fc5 47 } else {
okini3939 0:49551c775fc5 48 for (i = 0; i < 4; i ++) {
okini3939 0:49551c775fc5 49 tape_write(0x1f);
okini3939 0:49551c775fc5 50 }
okini3939 0:49551c775fc5 51 LPC_IOCON->PIO1_22 &= ~0x07;
okini3939 0:49551c775fc5 52 LPC_IOCON->PIO1_22 |= 0x02; // SSP MOSI
okini3939 0:49551c775fc5 53 }
okini3939 0:49551c775fc5 54 #endif
okini3939 0:49551c775fc5 55 __enable_irq();
okini3939 0:49551c775fc5 56 }
okini3939 0:49551c775fc5 57
okini3939 0:49551c775fc5 58 void tapeSend (int dat) {
okini3939 0:49551c775fc5 59 int b;
okini3939 0:49551c775fc5 60
okini3939 0:49551c775fc5 61 __disable_irq();
okini3939 0:49551c775fc5 62 for (b = 0x800000; b; b = b >> 1) {
okini3939 0:49551c775fc5 63 if (dat & b) {
okini3939 0:49551c775fc5 64 // 1
okini3939 0:49551c775fc5 65 tape_write(0x01);
okini3939 0:49551c775fc5 66 } else {
okini3939 0:49551c775fc5 67 // 0
okini3939 0:49551c775fc5 68 tape_write(0x0f);
okini3939 0:49551c775fc5 69 }
okini3939 0:49551c775fc5 70 }
okini3939 0:49551c775fc5 71 __enable_irq();
okini3939 0:49551c775fc5 72 }
okini3939 0:49551c775fc5 73
okini3939 0:49551c775fc5 74 void tapeInit (int freq) {
okini3939 0:49551c775fc5 75 tape.format(5, 1);
okini3939 0:49551c775fc5 76 if (freq) {
okini3939 0:49551c775fc5 77 tape.frequency(freq * 1000);
okini3939 0:49551c775fc5 78 } else {
okini3939 0:49551c775fc5 79 // high speed (800kbps)
okini3939 0:49551c775fc5 80 tape.frequency(4000000);
okini3939 0:49551c775fc5 81 }
okini3939 0:49551c775fc5 82 tapeReset(1);
okini3939 0:49551c775fc5 83 }