LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sct_trafficlight.c
Go to the documentation of this file.
1 /*
2  * @brief State Configurable Timer Traffic Lights example
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include "board.h"
35 #include "sct_fsm.h"
36 
67 /*****************************************************************************
68  * Private types/enumerations/variables
69  ****************************************************************************/
70 #ifdef BOARD_HITEX_EVA_18504350
71 #define MCSEL_BIT 23
72 #define MCSEL_PORT 6
73 #endif
74 
75 static uint16_t delay;
76 
77 /*****************************************************************************
78  * Public types/enumerations/variables
79  ****************************************************************************/
80 
81 /*****************************************************************************
82  * Private functions
83  ****************************************************************************/
84 
85 /* Initialise the SCT Pins */
87 {
88 #ifdef BOARD_HITEX_EVA_18504350
89  /* Enable signals on MC connector X19 */
90  Chip_SCU_PinMuxSet(0xD, 9, (SCU_MODE_MODE_INACT | SCU_MODE_FUNC4)); /* PD_9: GPIO 6.23, MCSEL */
91  Chip_GPIO_WriteDirBit(LPC_GPIO_PORT, MCSEL_PORT, MCSEL_BIT, true);
92  Chip_GPIO_WritePortBit(LPC_GPIO_PORT, MCSEL_PORT, MCSEL_BIT, false);
93 
94  /* Pin configuration for SCT */
95  /* PD_7: SCTIN_5 used for the CROSS_REQUEST input (button) */
97 
98  /* PD_10: SCTIN_1, used for SCT clock input */
99  /* Connect JP13 pin 1 with connector X19 pin2 */
101 
102  Chip_SCU_PinMuxSet(0xE, 6, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC1)); /* PE_6: SCTOUT_2 connected to RGB green */
103  Chip_SCU_PinMuxSet(0xE, 5, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC1)); /* PE_5: SCTOUT_3 connected to RGB red */
104  Chip_SCU_PinMuxSet(0xE, 8, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC1)); /* P4_4: SCTOUT_4 connected to RGB blue */
105  Chip_SCU_PinMuxSet(0xE, 7, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC1)); /* P4_3: SCTOUT_5 */
106  Chip_SCU_PinMuxSet(0xD, 3, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC1)); /* PD_3: SCTOUT_6 */
107 
108  /* Global configuration of the SCT */
109  /* use external clock, rising edge, on CTIN1 */
110  /* For Hitex4350, external clock is IRC clock /16 = 12 MHz / 16 = 750 KHz */
111  /* For Hitex1850, external clock is IRC clock /16 = 12 MHz / 16 = 750 KHz */
112  Chip_SCT_Config(LPC_SCT, (SCT_CONFIG_16BIT_COUNTER | (0x2 << 1) | (0x2 << 3)));
113 
114 #elif defined(BOARD_KEIL_MCB_18574357)
115 
116  /* PD_10: SCTIN_2, used for SCT clock input */
117  /* Connect PF_8 pin to CLK3 */
119 
120  Chip_SCU_PinMuxSet(0xD, 11, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC6)); /* PD_11: SCTOUT_14 connected to LED1 */
121  Chip_SCU_PinMuxSet(0xD, 12, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC6)); /* PD_12: SCTOUT_10 connected to LED2 */
122  Chip_SCU_PinMuxSet(0xD, 13, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC6)); /* PD_13: SCTOUT_13 connected to LED3 */
123  Chip_SCU_PinMuxSet(0xD, 14, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC6)); /* PD_14: SCTOUT_11 connected to LED4 */
124  Chip_SCU_PinMuxSet(0xD, 15, (SCU_MODE_MODE_PULLUP | SCU_MODE_FUNC6)); /* PD_15: SCTOUT_8 connected to LED5 */
125 
126  /* Global configuration of the SCT */
127  /* use external clock, rising edge, on CTIN2 */
128  /* For Keil4357, external clock is IRC clock /16 = 12 MHz / 16 = 750 KHz */
129  /* For Keil1857, external clock is IRC clock /16 = 12 MHz / 16 = 750 KHz */
130  Chip_SCT_Config(LPC_SCT, (SCT_CONFIG_16BIT_COUNTER | (0x2 << 1) | (0x4 << 3)));
131 
132 #else
133 #error Board not supported!
134 #endif
135 
136 }
137 
138 /*****************************************************************************
139  * Public functions
140  ****************************************************************************/
141 
146 void SCT_IRQHandler(void)
147 {
148  /* Acknowledge the interrupt source */
149  if (LPC_SCT->EVFLAG & (1 << SCT_IRQ_EVENT_crossingTimeout)) {
150 
151  LPC_SCT->EVFLAG |= (1 << SCT_IRQ_EVENT_crossingTimeout);
152 
153  /* determine the captured RPM measurement */
154  delay = (rand() % 22 + 1) * 2929;
156  }
157 }
158 
163 int main(void)
164 {
165  uint32_t SCT_FRQ;
166 
167  Board_Init();
168 
169  /* Initialize SCT */
171 
172  LPC_SCU->SFSCLK[3] = SCU_MODE_MODE_REPEATER | SCU_MODE_FUNC1; /* function 1; CGU clk out, pull up */
173 
174  /* Attach IRC clock to divider B with a divider of 16 */
176 
177  /* Route divider B output to Clock output base clock and enable base out clock */
179 
180  /* Configure SCT pins */
182 
183  SCT_FRQ = Chip_Clock_GetRate(CLK_MX_SCT);
184  DEBUGOUT("SCT_FRQ: %d\r\n", SCT_FRQ);
185 
188  ENABLE);
189 
190  /* Now use the FSM code to configure the state machine */
191  sct_fsm_init();
192 
193  /* initialize random seed: */
194  srand(0x5EED);
195 
196  /* 12000000 / 256 / 16 = 2929 Hz
197  * 65535 / 2929 =~ 22 */
198  /* generate a random delay from 1 to 22 seconds */
199  delay = (rand() % 22 + 1) * 2929;
200 
201  NVIC_ClearPendingIRQ(SCT_IRQn);
202  NVIC_EnableIRQ(SCT_IRQn);
203 
204  /* Start the SCT */
206 
207  while (1) {
208  __WFI();
209  }
210 }
211