app3 prob

Dependencies:   mbed

Committer:
joGenie
Date:
Tue Feb 11 18:26:23 2014 +0000
Revision:
2:c25ba89e3581
Parent:
1:cb05875a0960
Time go;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RufflesAllD 0:6517bada7928 1 #include "mbed.h"
joGenie 1:cb05875a0960 2 #include "rtos.h"
joGenie 1:cb05875a0960 3 #include <bitset>
joGenie 1:cb05875a0960 4 #include <string>
RufflesAllD 0:6517bada7928 5
RufflesAllD 0:6517bada7928 6 Serial pc(USBTX, USBRX);
joGenie 1:cb05875a0960 7
joGenie 1:cb05875a0960 8 DigitalOut led1(LED1);
joGenie 1:cb05875a0960 9 DigitalOut led2(LED2);
joGenie 1:cb05875a0960 10 DigitalOut led3(LED3);
joGenie 1:cb05875a0960 11 DigitalOut led4(LED4);
joGenie 1:cb05875a0960 12
joGenie 2:c25ba89e3581 13 unsigned long tc_periods[9] = {0};
joGenie 1:cb05875a0960 14 unsigned long period = 0;
joGenie 1:cb05875a0960 15 bool type_bit = true;
joGenie 1:cb05875a0960 16 int synchrone = 0;
joGenie 2:c25ba89e3581 17 int count = 0;
joGenie 1:cb05875a0960 18
joGenie 2:c25ba89e3581 19 bool good = true;
joGenie 1:cb05875a0960 20
joGenie 2:c25ba89e3581 21 char message[16] = "";
joGenie 2:c25ba89e3581 22 const unsigned long offset = 200;
joGenie 2:c25ba89e3581 23
joGenie 2:c25ba89e3581 24 bitset<16> bits(string("0101010101101000"));
RufflesAllD 0:6517bada7928 25
joGenie 1:cb05875a0960 26 void readTrame()
joGenie 1:cb05875a0960 27 {
joGenie 2:c25ba89e3581 28 if (synchrone < 8)
joGenie 1:cb05875a0960 29 {
joGenie 1:cb05875a0960 30 tc_periods[synchrone] = LPC_TIM2->CR1 / 2;
joGenie 1:cb05875a0960 31 synchrone++;
joGenie 1:cb05875a0960 32
joGenie 2:c25ba89e3581 33 if (synchrone == 8)
joGenie 1:cb05875a0960 34 {
joGenie 1:cb05875a0960 35 for (int i = 0; i < synchrone; i++)
joGenie 1:cb05875a0960 36 {
joGenie 1:cb05875a0960 37 period += tc_periods[i];
joGenie 1:cb05875a0960 38 }
joGenie 1:cb05875a0960 39
joGenie 2:c25ba89e3581 40 period = period/8;
joGenie 1:cb05875a0960 41 }
joGenie 1:cb05875a0960 42 }
joGenie 1:cb05875a0960 43 else
joGenie 2:c25ba89e3581 44 {
joGenie 2:c25ba89e3581 45 unsigned long tc_count = LPC_TIM2->CR1;
joGenie 2:c25ba89e3581 46 if (tc_count > (period*2 - offset) && tc_count < (period*2 + offset))
joGenie 1:cb05875a0960 47 {
joGenie 1:cb05875a0960 48 type_bit = !type_bit;
joGenie 2:c25ba89e3581 49 good = true;
joGenie 2:c25ba89e3581 50
joGenie 1:cb05875a0960 51 if (type_bit)
joGenie 2:c25ba89e3581 52 message[count] = '1';
joGenie 1:cb05875a0960 53 else
joGenie 2:c25ba89e3581 54 message[count] = '0';
joGenie 2:c25ba89e3581 55
joGenie 2:c25ba89e3581 56 count++;
joGenie 1:cb05875a0960 57 }
joGenie 1:cb05875a0960 58
joGenie 2:c25ba89e3581 59 good = !good;
joGenie 2:c25ba89e3581 60 if (good)
joGenie 2:c25ba89e3581 61 {
joGenie 2:c25ba89e3581 62 if (type_bit)
joGenie 2:c25ba89e3581 63 message[count] = '1';
joGenie 2:c25ba89e3581 64 else
joGenie 2:c25ba89e3581 65 message[count] = '0';
joGenie 2:c25ba89e3581 66
joGenie 2:c25ba89e3581 67 count++;
joGenie 2:c25ba89e3581 68 }
joGenie 1:cb05875a0960 69 }
RufflesAllD 0:6517bada7928 70
joGenie 1:cb05875a0960 71 if (led1 == 0)
joGenie 1:cb05875a0960 72 led1 = 1;
joGenie 1:cb05875a0960 73 else
joGenie 1:cb05875a0960 74 led1 = 0;
joGenie 1:cb05875a0960 75
RufflesAllD 0:6517bada7928 76 LPC_TIM2->TC = 0;
RufflesAllD 0:6517bada7928 77 LPC_TIM2->IR = 0xFF;
RufflesAllD 0:6517bada7928 78 }
RufflesAllD 0:6517bada7928 79
joGenie 1:cb05875a0960 80 void sendTrame(std::bitset<16> bit)
joGenie 1:cb05875a0960 81 {
joGenie 2:c25ba89e3581 82 for (int a = 1; a <= bit.size(); a++)
joGenie 1:cb05875a0960 83 {
joGenie 2:c25ba89e3581 84 if (bit.test(bit.size() - a))
joGenie 1:cb05875a0960 85 {
joGenie 1:cb05875a0960 86 LPC_PWM1->MR1 = SystemCoreClock/2;
joGenie 1:cb05875a0960 87 LPC_PWM1->MR2 = 1;
joGenie 1:cb05875a0960 88 }
joGenie 1:cb05875a0960 89 else
joGenie 1:cb05875a0960 90 {
joGenie 1:cb05875a0960 91 LPC_PWM1->MR1 = 1;
joGenie 1:cb05875a0960 92 LPC_PWM1->MR2 = SystemCoreClock/2;
RufflesAllD 0:6517bada7928 93 }
RufflesAllD 0:6517bada7928 94
joGenie 1:cb05875a0960 95 while(LPC_PWM1->IR != 0x01);
RufflesAllD 0:6517bada7928 96
joGenie 1:cb05875a0960 97 LPC_PWM1->IR = 0xFF;
joGenie 1:cb05875a0960 98 }
joGenie 1:cb05875a0960 99
joGenie 1:cb05875a0960 100 LPC_PWM1->TCR = 0x0;
joGenie 1:cb05875a0960 101
joGenie 2:c25ba89e3581 102 pc.printf("%s\n", message);
joGenie 1:cb05875a0960 103
joGenie 1:cb05875a0960 104 for ( int i = 0; i < 8; i++)
joGenie 1:cb05875a0960 105 {
joGenie 1:cb05875a0960 106 pc.printf("%d : \t", tc_periods[i]);
RufflesAllD 0:6517bada7928 107 }
joGenie 1:cb05875a0960 108
joGenie 2:c25ba89e3581 109 pc.printf("\n%d\n", count);
joGenie 1:cb05875a0960 110 }
joGenie 1:cb05875a0960 111
joGenie 1:cb05875a0960 112 void send(void const *args)
joGenie 1:cb05875a0960 113 {
joGenie 1:cb05875a0960 114 sendTrame(bits);
joGenie 1:cb05875a0960 115 }
joGenie 1:cb05875a0960 116
joGenie 1:cb05875a0960 117 void _send()
joGenie 1:cb05875a0960 118 {
joGenie 1:cb05875a0960 119 if (led1 == 0)
joGenie 1:cb05875a0960 120 led1 = 1;
joGenie 1:cb05875a0960 121 else
joGenie 1:cb05875a0960 122 led1 = 0;
RufflesAllD 0:6517bada7928 123 }
RufflesAllD 0:6517bada7928 124
joGenie 1:cb05875a0960 125 void initialize()
joGenie 1:cb05875a0960 126 {
joGenie 1:cb05875a0960 127 // Set system control
joGenie 1:cb05875a0960 128 LPC_SC->PCONP |= (1 << 22) | (1 << 6); // Enable Timer2 et PWM
joGenie 1:cb05875a0960 129 LPC_SC->PCLKSEL0 |= (1 << 12); // PClk PWM = CCLK
joGenie 1:cb05875a0960 130 LPC_SC->PCLKSEL1 |= (1 << 12); // PClk Timer2 = CCLK
joGenie 1:cb05875a0960 131
joGenie 1:cb05875a0960 132 // Set pin connection
joGenie 1:cb05875a0960 133 LPC_PINCON->PINSEL0 |= (3 << 10); // Pin 29 Capture
joGenie 1:cb05875a0960 134 LPC_PINCON->PINSEL4 |= (1 << 2); // Pin 25 PWM
joGenie 1:cb05875a0960 135
joGenie 1:cb05875a0960 136 //Initialize Timer2 for capture
joGenie 1:cb05875a0960 137
joGenie 1:cb05875a0960 138 LPC_TIM2->TC = 0; // Initialize Time Counter
joGenie 1:cb05875a0960 139 LPC_TIM2->PC = 0; // Initialize Prescale Counter
joGenie 1:cb05875a0960 140 LPC_TIM2->PR = 0; // Initialize Prescale Register
joGenie 1:cb05875a0960 141 LPC_TIM2->TCR |= (1 << 1); // Reset Timer Control Register
joGenie 1:cb05875a0960 142 LPC_TIM2->IR = 0xFF; // Reset Interrupt Register
joGenie 1:cb05875a0960 143 LPC_TIM2->CCR |= (1 << 5) | (1 << 4) | (1 << 3); // Initialize Capture Control Register
joGenie 1:cb05875a0960 144 LPC_TIM2->CTCR = 0x00;
joGenie 1:cb05875a0960 145
joGenie 1:cb05875a0960 146 NVIC_SetVector(TIMER2_IRQn, (uint32_t)&readTrame);
joGenie 1:cb05875a0960 147 NVIC_EnableIRQ(TIMER2_IRQn);
RufflesAllD 0:6517bada7928 148
joGenie 1:cb05875a0960 149 LPC_TIM2->TCR = 0x01; // Start Timer Control Register
RufflesAllD 0:6517bada7928 150
joGenie 1:cb05875a0960 151 //Initialize PWM
joGenie 1:cb05875a0960 152 LPC_PWM1->MCR |= (1 << 1) | (1 << 0); // Initialize Match Control Register Interrupt/Reset
joGenie 1:cb05875a0960 153 LPC_PWM1->PCR |= (1 << 10) | (1 << 2); // Initialize PWM Control Register Output/Double-edge
joGenie 1:cb05875a0960 154
joGenie 1:cb05875a0960 155 LPC_PWM1->MR0 = SystemCoreClock; // Period
joGenie 1:cb05875a0960 156 LPC_PWM1->LER |= (1 << 2) | (1 << 1); // Initialize Latch Enable Register
joGenie 1:cb05875a0960 157 LPC_PWM1->TCR = (1 << 0); // Enable counter
RufflesAllD 0:6517bada7928 158
joGenie 1:cb05875a0960 159 LPC_PWM1->IR = 0xFF; // Reset Interrupt Registe
RufflesAllD 0:6517bada7928 160 }
joGenie 1:cb05875a0960 161
joGenie 1:cb05875a0960 162 int main()
joGenie 1:cb05875a0960 163 {
joGenie 1:cb05875a0960 164 initialize();
joGenie 1:cb05875a0960 165
joGenie 1:cb05875a0960 166 sendTrame(bits);
joGenie 1:cb05875a0960 167
joGenie 1:cb05875a0960 168 while(true) {
joGenie 1:cb05875a0960 169 }
joGenie 1:cb05875a0960 170 }