0% found this document useful (0 votes)
26 views7 pages

Toggle LED Every 1 Second

Uploaded by

Yeshvanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Toggle LED Every 1 Second

Uploaded by

Yeshvanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Toggle LED every 1 second.

#include "stm32f4xx.h" // Device header


#define PIN12 (1U<<12)
#define LED PIN12
#define SR_UIF (1U<<0)

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

// configure TIM2 to wrap around 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->CNT = 0; /* clear counter */
TIM2->CR1 = (1U<<0); /* enable TIM2 */

while (1)
{
while(!(TIM2->SR & SR_UIF)); // Wait for UIF
TIM2->SR &= ~SR_UIF; // Clear UIF
GPIOD->ODR ^= LED;

Toggle LED with Output Compare


#define OC_TOGGLE ((1U<<4)|(1U<<5))
#define CCER_CC1E (1U<<0)

int main(void)
{
RCC->AHB1ENR |= (1U<<0); //Enable GPIOA clock
//PA5 mode to alternate function
GPIOA->MODER &=~(1U<<10);
GPIOA->MODER |=(1U<<11);

//PA5 alternate function type to TIM2_CH1 (AF01)


GPIOA->AFR[0] |= 0x00100000;

// 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.

#include "stm32f4xx.h" // Device header


#define PIN12 (1<<12)
#define LED PIN12
int toggle = 1;

int main(void)
{
int WordOffset, BitOffset;
RCC->AHB1ENR |= (1<<3); //Enable GPIOD clock
GPIOD->MODER |=0x01000000; //PD.12 set pin to output mode

// configure TIM2 to wrap around at 1 Hz


RCC->APB1ENR |= (1<<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->CNT = 0; /* clear counter */
TIM2->CR1 = (1<<0); /* enable TIM2 */
TIM2->DIER |= (1<<0); /*Enable TIM interrupt*/

//NVIC_EnableIRQ(TIM2_IRQn); /*Enable TIM interrupt in NVIC*/

WordOffset = 28 >> 5; // Word Offset = IRQn/32


BitOffset = 28 & 0x1F; // Bit Offset = IRQn mod 32
NVIC->ISER[WordOffset]= 1<< BitOffset ; // Enable interrupt

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);

PINSEL1 |= (1<<16); //Enable P0.24 pin as AD0.1


PINSEL1 &= ~(1<<17); // Clear lingering bits so only ADC is enabled
// AD0INTEN |= (1<<1); enable interrupt if needed

AD0CR &= ~(1<<0); // Clear bits of


AD0CR |= (1<<1); // Select AD0.1
AD0CR |= (1<<9); // 4 MHz clock for ADC
AD0CR |= (1<<16); // Select burst mode – conversion happens continuous
AD0CR |= (1<<21); // Enable ADC

// 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();

//AD0CR |= (1<<24); // Add this when software trigger require


//AD0CR=0x01200202; // Add this when software trigger require

}
}

void delay(void)
{

T0TCR=(1<<1); //Reset Timer0


T0MR0=1199999 ; //Loading match register value
T0PR=1199; //Loading Prescalar register – will generate 2 minutes
delay
T0PC=T0TC=0;
T0MCR=(1<<0)|(1<<2); //Generates interrupt and stop on match
T0TCR=(1<<0); //Starting Timer
while(!(T0IR&(1<<0))); //Waiting for interrupt
T0IR=(1<<0); //Clearing interrupt:Writing a logic one to the
corresponding IR
//bit will reset the interrupt.

You might also like