Toggle LED every 1 second
Toggle LED every 1 second
int main(void)
{
RCC->AHB1ENR |= (1<<3); //Enable GPIOD clock
GPIOD->MODER &=0x0C000000; //Clear pin mode
GPIOD->MODER |=0x01000000; //PD.12 set pin to output mode
while (1)
{
while(!(TIM2->SR & SR_UIF)); // Wait for UIF
TIM2->SR &= ~SR_UIF; // Clear UIF
GPIOD->ODR ^= LED;
int main(void)
{
RCC->AHB1ENR |= (1U<<0); //Enable GPIOA clock
//PA5 mode to alternate function
GPIOA->MODER &=~(1U<<10);
GPIOA->MODER |=(1U<<11);
// configure TIM2 at 1 Hz
RCC->APB1ENR |= (1U<<0); /* enable TIM2 clock */
TIM2->PSC = 1600 - 1; /* divided by 1600 i.e. 16000000/1600=10000*/
TIM2->ARR = 10000 - 1; /* divided by 10000 i.e. 10000/10000=1 */
TIM2->CCMR1 = OC_TOGGLE; //Set output compare toggle mode
TIM2->CCER |= CCER_CC1E; // Enable tim2 ch1 in compare mode
TIM2->CNT = 0; /* clear counter */
TIM2->CR1 = (1U<<0); /* enable TIM2 */
while (1);
}
Toggle LED every one second with IRQ.
int main(void)
{
int WordOffset, BitOffset;
RCC->AHB1ENR |= (1<<3); //Enable GPIOD clock
GPIOD->MODER |=0x01000000; //PD.12 set pin to output mode
while (1);
void TIM2_IRQHandler(void)
{
TIM2->SR &= ~(1<<0); /*Clear update interrupt flag*/
GPIOD->ODR ^= LED; /*Toggle LED*/
toggle = ~ toggle;
}
#include<lpc23xx.h>
int adcdata;
float voltage;
int main(void)
{
//To enable the ADC, first set the PCADC bit, in the PCONP register
PCONP |= (1<<12);
// AD0CR |= (1<<24); to start conversion now, but since we use burst mode, not
needed
// AD0CR=0x00210202; // 0x00210202 --Burst mode // 0x01200202 --- Software
trigger
while(1)
{
while(!(AD0STAT & (1<<1)));
adcdata = (AD0DR1 & 0x0000FFC0);
adcdata = adcdata>>6;v
voltage = ((adcdata/1024.0)*3.3);
delay();
}
}
void delay(void)
{