app3 prob

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include <bitset>
00004 #include <string>
00005 
00006 Serial pc(USBTX, USBRX);
00007 
00008 DigitalOut led1(LED1);
00009 DigitalOut led2(LED2);
00010 DigitalOut led3(LED3);
00011 DigitalOut led4(LED4);
00012 
00013 unsigned long tc_periods[9] = {0};
00014 unsigned long period = 0;
00015 bool type_bit = true;
00016 int synchrone = 0;
00017 int count = 0;
00018 
00019 bool good = true;
00020 
00021 char message[16] = "";
00022 const unsigned long offset = 200;
00023 
00024 bitset<16> bits(string("0101010101101000"));
00025 
00026 void readTrame()
00027 {
00028     if (synchrone < 8)
00029     {
00030         tc_periods[synchrone] = LPC_TIM2->CR1 / 2;
00031         synchrone++;
00032         
00033         if (synchrone == 8)
00034         {
00035             for (int i = 0; i < synchrone; i++)
00036             {
00037                period += tc_periods[i]; 
00038             }
00039             
00040             period = period/8;
00041         }
00042     }
00043     else
00044     {   
00045         unsigned long tc_count = LPC_TIM2->CR1;
00046         if (tc_count > (period*2 - offset) && tc_count < (period*2 + offset))
00047         {
00048             type_bit = !type_bit;
00049             good = true;
00050             
00051             if (type_bit)
00052                 message[count] = '1';
00053             else
00054                 message[count] = '0';
00055                 
00056             count++;
00057         }
00058         
00059         good = !good;
00060         if (good)
00061         {
00062             if (type_bit)
00063                 message[count] = '1';
00064             else
00065                 message[count] = '0';
00066                 
00067             count++;
00068         }
00069     }
00070     
00071     if (led1 == 0)
00072         led1 = 1;
00073     else
00074         led1 = 0;  
00075 
00076     LPC_TIM2->TC = 0;
00077     LPC_TIM2->IR = 0xFF;
00078 }
00079 
00080 void sendTrame(std::bitset<16> bit)
00081 {
00082     for (int a = 1; a <= bit.size(); a++)
00083     {
00084         if (bit.test(bit.size() - a))
00085         {
00086             LPC_PWM1->MR1 = SystemCoreClock/2;
00087             LPC_PWM1->MR2 = 1;
00088         }
00089         else
00090         {
00091             LPC_PWM1->MR1 = 1;
00092             LPC_PWM1->MR2 = SystemCoreClock/2;
00093         }
00094         
00095         while(LPC_PWM1->IR != 0x01);
00096         
00097         LPC_PWM1->IR = 0xFF;
00098     }
00099     
00100     LPC_PWM1->TCR = 0x0;
00101     
00102     pc.printf("%s\n", message);
00103     
00104     for ( int i = 0; i < 8; i++)
00105     {
00106         pc.printf("%d : \t", tc_periods[i]);
00107     }
00108     
00109     pc.printf("\n%d\n", count);
00110 }
00111 
00112 void send(void const *args)
00113 {
00114     sendTrame(bits);
00115 }
00116 
00117 void _send()
00118 {
00119     if (led1 == 0)
00120         led1 = 1;
00121     else
00122         led1 = 0;            
00123 }
00124 
00125 void initialize()
00126 {
00127     // Set system control
00128     LPC_SC->PCONP |= (1 << 22) | (1 << 6); // Enable Timer2 et PWM
00129     LPC_SC->PCLKSEL0 |= (1 << 12); // PClk PWM = CCLK
00130     LPC_SC->PCLKSEL1 |= (1 << 12); // PClk Timer2 = CCLK
00131 
00132     // Set pin connection
00133     LPC_PINCON->PINSEL0 |= (3 << 10); // Pin 29 Capture
00134     LPC_PINCON->PINSEL4 |= (1 << 2);  // Pin 25 PWM
00135 
00136     //Initialize Timer2 for capture
00137 
00138     LPC_TIM2->TC = 0;           // Initialize Time Counter
00139     LPC_TIM2->PC = 0;           // Initialize Prescale Counter
00140     LPC_TIM2->PR = 0;           // Initialize Prescale Register
00141     LPC_TIM2->TCR |= (1 << 1);  // Reset Timer Control Register
00142     LPC_TIM2->IR = 0xFF;        // Reset Interrupt Register
00143     LPC_TIM2->CCR |=  (1 << 5) | (1 << 4) | (1 << 3);   // Initialize Capture Control Register
00144     LPC_TIM2->CTCR = 0x00;
00145 
00146     NVIC_SetVector(TIMER2_IRQn, (uint32_t)&readTrame);
00147     NVIC_EnableIRQ(TIMER2_IRQn);
00148     
00149     LPC_TIM2->TCR = 0x01;       // Start Timer Control Register
00150     
00151     //Initialize PWM
00152     LPC_PWM1->MCR |= (1 << 1) | (1 << 0);   // Initialize Match Control Register Interrupt/Reset
00153     LPC_PWM1->PCR |= (1 << 10) | (1 << 2);  // Initialize PWM Control Register Output/Double-edge
00154     
00155     LPC_PWM1->MR0 = SystemCoreClock;        // Period
00156     LPC_PWM1->LER |= (1 << 2) | (1 << 1);   // Initialize Latch Enable Register
00157     LPC_PWM1->TCR = (1 << 0);              // Enable counter
00158     
00159     LPC_PWM1->IR = 0xFF; // Reset Interrupt Registe
00160 }
00161 
00162 int main()
00163 {
00164     initialize();
00165     
00166     sendTrame(bits);
00167     
00168     while(true) {
00169     }
00170 }