by Crossware
23. March 2016 12:32
Toggling a pin is one of the first things to do on a new microcontroller board.
Our board has an LED on port B pin 18 and the C code for toggling that pin is shown below.
The program is built to run in the SRAM of the K70 microcontroller avoiding the need to over-write anything in the on-chip flash memory.
main()
{
// disable the watchdog timer
g_pWDOG->UNLOCK = 0XC520;
g_pWDOG->UNLOCK = 0XD928;
g_pWDOG->STCTRLH &= ~WDOG_WDOGEN;
// enable the PORTB module
g_pSIM->SCGC5 |= SCGC5_PORTB;
// set PTB18 to ALT1 (GPIO)
g_pPORTB->PCR18 &= ~(PORT_MUX0 | PORT_MUX1 | PORT_MUX2);
g_pPORTB->PCR18 |= 1 << 8;
// set PTB18 as an output
g_pGPIOB->PDDR = 1 << 18;
while (1)
{
// toggle the PTB18
g_pGPIOB->PTOR = 1 << 18;
for (int i = 0; i < 100000; i++);
}
}