// ------------------- COMMON LIBRARY ------------------- // #include "LPC8xx.h" #include "core_cm0plus.h" #include "system.h" #include "syscon.h" // ------------------- DRIVER LIBRARY ------------------- // #include "delay.h" #include "gpio.h" #include "iocon.h" #include "util.h" #include "swm.h" #include "uart.h" #include "ctimer.h" #include "string.h" #include "i2c.h" // ------------------- SYSTEM LIBRARY ------------------- // #include "ESP8266_Modem.h" #include "IRRemote.h" #include "DRV10983.h" #include "Application_Setup.h" /* Configuration */ #define SYS_TICK_PERIOD_IN_MS 100 // --------------------------------------------------------------------------------------- // #define DEVICE_TYPE WITH_WIFI //#define DEVICE_TYPE WITHOUT_WIFI #define BAUDRATE 9600 #define UART_CLOCK 12000000 // Assuming UART clock frequency // --------------------------------------------------------------------------------------- // void UART_Init() { LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 18); uint32_t baudRateDivisor = UART_CLOCK / (16 * BAUDRATE); // Configure UART LPC_USART0->CFG = (1 << 0); // Enable UART LPC_USART0->CTL = 0; // Disable UART while configuring LPC_USART0->BRG = baudRateDivisor - 1; // Set baud rate LPC_USART0->CFG |= (0 << 2); // 8-bit data LPC_USART0->CFG &= ~(1 << 6); // 1 stop bit LPC_USART0->CFG &= ~(1 << 7); // No parity // Enable UART LPC_USART0->CTL |= (1 << 0); LPC_IOCON->PIO0_0 &= ~(0x3 << 0); // Clear FUNC bits LPC_IOCON->PIO0_0 |= (0x1 << 0); // Set FUNC bits to UART0 TXD LPC_IOCON->PIO0_4 &= ~(0x3 << 0); // Clear FUNC bits LPC_IOCON->PIO0_4 |= (0x1 << 0); // Set FUNC bits to UART0 RXD } void UART_SendChar(char data) { // Wait until the transmit buffer is empty while (((LPC_USART0->STAT) & (1 << 2)==0)); // Send character LPC_USART0->TXDAT = data; } char UART_ReceiveChar() { // Wait until receive data is available while (((LPC_USART0->STAT) & (1 << 0)==0)); // Read received character return LPC_USART0->RXDAT; } void SysTick_Handler(void) { } uint16_t ADC_Value1; uint16_t ADC_Value2; // ------------------------ UART Global Variable ------------------------ // usart_t usart; uint8_t usart_rx_buf[32]; volatile uint8_t Flag_Start_RXTX; // ------------------------ Flash Buff ------------------------ // __attribute__((aligned(8))) uint8_t flash_buf[64]; // ------------------------ IR Remote Global Variable ------------------------ // uint8_t msCount_IR=0; uint8_t timerValue_IR; int8_t pulseCount=0; uint32_t bitPattern=0; uint32_t newKey=0; uint8_t Flag_StartIR_Frame=0; // ------------------------ UART0 ISR ------------------------ // // ------------------------ TIMER0 ISR ------------------------ // void CTIMER0_IRQHandler(void) { LPC_CTIMER0->IR |= 1<RISE & (1<<0)) { timerValue_IR = msCount_IR; msCount_IR = 0; if(timerValue_IR>=16 && Flag_StartIR_Frame==0) { Flag_StartIR_Frame=1; } LPC_PIN_INT->RISE = 1<<0; // Clear the interrupt flag return; } if(LPC_PIN_INT->FALL & (1<<0)) { timerValue_IR = msCount_IR; msCount_IR = 0; if(timerValue_IR>=7 && Flag_StartIR_Frame==1) { Flag_StartIR_Frame=2; bitPattern = 0; pulseCount = 0; } else if(Flag_StartIR_Frame==2) { pulseCount++; if((pulseCount>=0) && (pulseCount<32)) //Accumulate the bit values between 0-31. { if(timerValue_IR>=3) //pulse width greater than 2ms is considered as LOGIC1 { bitPattern |=(uint32_t)1<<(31-pulseCount); } } else if(pulseCount>=32) //This will mark the End of frame as 32 pulses are received { Flag_StartIR_Frame=0; newKey = bitPattern; // Copy the newKey(patter) and set the pulse count to 0; pulseCount = 0; } } LPC_PIN_INT->FALL = 1<<0; // Clear the interrupt flag } } // ------------------------ Main Routine ------------------------ // int main(void) { // Crystal Configuration // chip_setup.h // SystemInit(); // 30 MHz /* if the clock or PLL has been changed, also update the global variable SystemCoreClock */ SystemCoreClockUpdate(); /* set systick and start systick interrupt */ SysTick_Config(main_clk/1000UL*(unsigned long)SYS_TICK_PERIOD_IN_MS); /* Init GPIO Module */ GPIOInit(); /* enable clock for several subsystems */ Enable_Periph_Clock(CLK_IOCON); Enable_Periph_Clock(CLK_SWM); Enable_Periph_Clock(CLK_CTIMER0); Enable_Periph_Clock(CLK_GPIO0); Enable_Periph_Clock(CLK_GPIO_INT); // configure output GPIOSetDir(PORT0, 15, OUTPUT); GPIOSetBitValue(PORT0, 15, 0); // DRV_DIR GPIOSetDir(PORT0, 21, OUTPUT); GPIOSetBitValue(PORT0, 21, 0); // ALL_LED_OFF GPIOSetDir(PORT0, 20, OUTPUT); GPIOSetBitValue(PORT0, 20, 0); // GW1-CPU GPIOSetDir(PORT0, 10, OUTPUT); GPIOSetBitValue(PORT0, 10, 0); // GW2-CPU GPIOSetDir(PORT0, 16, OUTPUT); GPIOSetBitValue(PORT0, 16, 0); // GW3-CPU GPIOSetDir(PORT0, 18, OUTPUT); GPIOSetBitValue(PORT0, 18, 0); // GW4-CPU GPIOSetDir(PORT0, 12, OUTPUT); GPIOSetBitValue(PORT0, 12, 0); // GW5-CPU GPIOSetDir(PORT0, 8, OUTPUT); GPIOSetBitValue(PORT0, 8, 0); // GW6-CPU GPIOSetDir(PORT0, 14, OUTPUT); GPIOSetBitValue(PORT0, 14, 0); // WiFi-EN-CPU GPIOSetDir(PORT0, 7, OUTPUT); GPIOSetBitValue(PORT0, 7, 0); // WiFi-nRESET-CPU GPIOSetDir(PORT0, 19, INPUT); All_LED_OFF; GREEN_L1_ON; GREEN_L2_ON; GREEN_L3_ON; GREEN_L4_ON; GREEN_L5_ON; GREEN_L6_ON; // configure UART // 5 = 115200 Baudrate // P0_4 = Tx // P0_0 = Rx //usart0_init(&usart, 5, 4, 0, usart_rx_buf, 1); // configure Timer // 15090 -> 1ms LPC_CTIMER0->PR = 0; // no prescale LPC_CTIMER0->MR[3] = 7530; // PWM cycle length: one second with 15MHz AHB clock LPC_CTIMER0->MCR |= 1<MCR |= 1<PINTSEL[0] = 11; // Configure as pin interrupts LPC_PIN_INT->ISEL = 0x0; // Configure the Pin interrupt mode register (a.k.a ISEL) for edge-sensitive on PINTSEL1,0 LPC_PIN_INT->IENR = 0x1; // pin interrupt enable rising LPC_PIN_INT->IENF = 0x1; // pin interrupt disable falling LPC_PIN_INT->IST = 0xFF; // Clear any pending or left-over interrupt flags // configure interrupt LPC_USART0->INTENSET = TXRDY; LPC_USART0->INTENSET = RXRDY; NVIC_SetPriority(UART0_IRQn,1); NVIC_EnableIRQ(UART0_IRQn); NVIC_EnableIRQ(CTIMER0_IRQn); NVIC_EnableIRQ(PININT0_IRQn); // INIT ADC /* Disable pullups for those pins, which are used as ADC inputs. This is not mandatory, because adc_read() will do this also, but results will be better if the pullups are disabled as early as possible. */ *get_iocon_by_port(17) &= IOCON_MODE_MASK; // ADC_9 , port 0_17 *get_iocon_by_port(13) &= IOCON_MODE_MASK; // ADC_10 , port 0_13 GPIOSetDir(PORT0, 17, INPUT); GPIOSetDir(PORT0, 13, INPUT); // 12bit ADC // 30 MHz , 1 Clock - 0.033uSec // Sample Time - Default 6.5 ADC Clocks // - certain condition may be required 31 ADC Clocks adc_init(); // enable the timer LPC_CTIMER0->TCR |= 1<RXDAT=='A') { All_LED_OFF; GREEN_L1_OFF; GREEN_L2_OFF; GREEN_L3_ON; GREEN_L4_OFF; GREEN_L5_OFF; GREEN_L6_OFF; delay_micro_seconds(500000); All_LED_OFF; GREEN_L1_OFF; GREEN_L2_OFF; GREEN_L3_OFF; GREEN_L4_OFF; GREEN_L5_OFF; GREEN_L6_OFF; delay_micro_seconds(500000); } /*else{ All_LED_OFF; GREEN_L1_ON; GREEN_L2_OFF; GREEN_L3_OFF; GREEN_L4_OFF; GREEN_L5_OFF; GREEN_L6_OFF; delay_micro_seconds(500000); }*/ } }