Written by Ayrton Leyssens DMX library, bitbang method for LPC1768

Dependents:   ArtnetDMX

Committer:
Ayrton_L
Date:
Mon May 23 14:26:06 2016 +0000
Revision:
6:622a294f70ab
Parent:
5:256845339155
latest stable state

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ayrton_L 0:55edcc97ba17 1 #include "DMX.h"
Ayrton_L 0:55edcc97ba17 2
Ayrton_L 6:622a294f70ab 3 DMX::DMX (PinName p_tx, PinName p_rx) : _dmx(p_tx, p_rx)
Ayrton_L 0:55edcc97ba17 4 {
Ayrton_L 6:622a294f70ab 5 uint32_t I32_Teller;
Ayrton_L 6:622a294f70ab 6
Ayrton_L 6:622a294f70ab 7 for (I32_Teller = 0; I32_Teller < 512; I32_Teller++)
Ayrton_L 6:622a294f70ab 8 {
Ayrton_L 6:622a294f70ab 9 I8_Data[I32_Teller] = 0;
Ayrton_L 6:622a294f70ab 10 }
Ayrton_L 6:622a294f70ab 11
Ayrton_L 6:622a294f70ab 12 ZendStatus = STOP;
Ayrton_L 6:622a294f70ab 13 if (p_tx == p9)
Ayrton_L 6:622a294f70ab 14 {
Ayrton_L 6:622a294f70ab 15 _uart = LPC_UART3;
Ayrton_L 6:622a294f70ab 16 NVIC_SetPriority(UART3_IRQn, 1);
Ayrton_L 6:622a294f70ab 17 }
Ayrton_L 6:622a294f70ab 18 else if (p_tx == p13)
Ayrton_L 6:622a294f70ab 19 {
Ayrton_L 6:622a294f70ab 20 _uart = (LPC_UART_TypeDef*)LPC_UART1;
Ayrton_L 6:622a294f70ab 21 NVIC_SetPriority(UART1_IRQn, 1);
Ayrton_L 6:622a294f70ab 22 }
Ayrton_L 6:622a294f70ab 23 else if (p_tx == p28)
Ayrton_L 6:622a294f70ab 24 {
Ayrton_L 6:622a294f70ab 25 _uart = LPC_UART2;
Ayrton_L 6:622a294f70ab 26 NVIC_SetPriority(UART2_IRQn, 1);
Ayrton_L 6:622a294f70ab 27 }
Ayrton_L 6:622a294f70ab 28 _dmx.baud(250000);
Ayrton_L 6:622a294f70ab 29 _dmx.format(8, Serial::None, 2); // 8 databits, 2 stopbits
Ayrton_L 0:55edcc97ba17 30
Ayrton_L 0:55edcc97ba17 31 }
Ayrton_L 0:55edcc97ba17 32
Ayrton_L 6:622a294f70ab 33 void DMX::V_PutData (int addr, int data) {
Ayrton_L 6:622a294f70ab 34 if ((addr < 0) or (addr >= 512))
Ayrton_L 6:622a294f70ab 35 {
Ayrton_L 6:622a294f70ab 36 return;
Ayrton_L 6:622a294f70ab 37 }
Ayrton_L 6:622a294f70ab 38 I8_Data[addr] = data;
Ayrton_L 0:55edcc97ba17 39 }
Ayrton_L 0:55edcc97ba17 40
Ayrton_L 6:622a294f70ab 41 void DMX::V_InitTimer()
Ayrton_L 0:55edcc97ba17 42 {
Ayrton_L 6:622a294f70ab 43 switch (ZendStatus)
Ayrton_L 0:55edcc97ba17 44 {
Ayrton_L 6:622a294f70ab 45 case BEGIN:
Ayrton_L 0:55edcc97ba17 46 {
Ayrton_L 6:622a294f70ab 47 T_Timer.detach();
Ayrton_L 6:622a294f70ab 48 _uart->LCR |= (1 << 6);
Ayrton_L 6:622a294f70ab 49 ZendStatus = BREAK;
Ayrton_L 6:622a294f70ab 50 T_Timer.attach_us(this, &DMX::V_InitTimer, BREAK);
Ayrton_L 6:622a294f70ab 51 break;
Ayrton_L 0:55edcc97ba17 52 }
Ayrton_L 3:667d8f669992 53
Ayrton_L 6:622a294f70ab 54 case BREAK:
Ayrton_L 6:622a294f70ab 55 {
Ayrton_L 6:622a294f70ab 56 T_Timer.detach();
Ayrton_L 6:622a294f70ab 57 _uart->LCR &= ~(1 << 6);
Ayrton_L 6:622a294f70ab 58 ZendStatus = MAB;
Ayrton_L 6:622a294f70ab 59 T_Timer.attach_us(this, &DMX::V_InitTimer, MAB);
Ayrton_L 6:622a294f70ab 60 break;
Ayrton_L 6:622a294f70ab 61 }
Ayrton_L 6:622a294f70ab 62 case MAB:
Ayrton_L 6:622a294f70ab 63 {
Ayrton_L 6:622a294f70ab 64 T_Timer.detach();
Ayrton_L 6:622a294f70ab 65 I32_m_Addr = 0;
Ayrton_L 6:622a294f70ab 66 ZendStatus = DATA;
Ayrton_L 6:622a294f70ab 67 _dmx.attach(this, &DMX::V_InitSend, Serial::TxIrq);
Ayrton_L 6:622a294f70ab 68 while(!(_uart->LSR & (1<<5)));
Ayrton_L 6:622a294f70ab 69 _uart->THR = 0; //startcode 0 => Algemene startcode voor dimmer (eigenlijk alles dus wat gewone dmx betreft)
Ayrton_L 6:622a294f70ab 70 break;
Ayrton_L 6:622a294f70ab 71 }
Ayrton_L 6:622a294f70ab 72 default:
Ayrton_L 6:622a294f70ab 73 {
Ayrton_L 6:622a294f70ab 74 printf("Error: DMX StATUS NIET GEKEND");
Ayrton_L 6:622a294f70ab 75 break;
Ayrton_L 6:622a294f70ab 76 }
Ayrton_L 0:55edcc97ba17 77 }
Ayrton_L 0:55edcc97ba17 78 }
Ayrton_L 0:55edcc97ba17 79
Ayrton_L 6:622a294f70ab 80 void DMX::V_InitSend()
Ayrton_L 0:55edcc97ba17 81 {
Ayrton_L 6:622a294f70ab 82 if (ZendStatus == DATA)
Ayrton_L 6:622a294f70ab 83 {
Ayrton_L 6:622a294f70ab 84 if (I32_m_Addr < 512)
Ayrton_L 6:622a294f70ab 85 {
Ayrton_L 6:622a294f70ab 86 _uart->THR = I8_Data[I32_m_Addr];
Ayrton_L 6:622a294f70ab 87 I32_m_Addr++;
Ayrton_L 6:622a294f70ab 88 }
Ayrton_L 6:622a294f70ab 89 else
Ayrton_L 6:622a294f70ab 90 {
Ayrton_L 6:622a294f70ab 91 _dmx.attach(0, Serial::TxIrq); //TxIrq Interrupt zetten om serieel te verzenden :)
Ayrton_L 6:622a294f70ab 92 ZendStatus = BEGIN;
Ayrton_L 6:622a294f70ab 93 T_Timer.attach_us(this, &DMX::V_InitTimer, 10);
Ayrton_L 6:622a294f70ab 94 }
Ayrton_L 6:622a294f70ab 95 }
Ayrton_L 0:55edcc97ba17 96 }
Ayrton_L 0:55edcc97ba17 97
Ayrton_L 6:622a294f70ab 98 void DMX::V_Start()
Ayrton_L 0:55edcc97ba17 99 {
Ayrton_L 6:622a294f70ab 100 if (ZendStatus == STOP)
Ayrton_L 6:622a294f70ab 101 {
Ayrton_L 6:622a294f70ab 102 ZendStatus = BEGIN;
Ayrton_L 6:622a294f70ab 103 T_Timer.attach_us(this, &DMX::V_InitTimer, 10);
Ayrton_L 6:622a294f70ab 104 }
Ayrton_L 0:55edcc97ba17 105 }
Ayrton_L 0:55edcc97ba17 106
Ayrton_L 6:622a294f70ab 107 void DMX::V_Stop ()
Ayrton_L 0:55edcc97ba17 108 {
Ayrton_L 6:622a294f70ab 109 _dmx.attach(NULL, Serial::TxIrq); //dmx stoppen doorinterrupt te laten verwijzen naar NULL-pointer
Ayrton_L 6:622a294f70ab 110 T_Timer.detach();
Ayrton_L 6:622a294f70ab 111 ZendStatus = STOP;
Ayrton_L 0:55edcc97ba17 112 }