You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
KED/main.c

133 lines
2.3 KiB

#include "main.h"
#include "delay.h"
#include "deviceSetup.h"
#include "usart.h"
#include "ascii.h"
#include "timer.h"
#include "i2c.h"
#include "stm32f042x6.h"
#include "hardwareDescription.h"
volatile uint8_t led_on;
void setup();
void EXTI0_1_IRQHandler(void)
{
if(EXTI->PR & EXTI_PR_PIF1) {
// clear flag
EXTI->PR |= EXTI_PR_PIF1;
led_on = !led_on;
}
}
void TIM2_IRQHandler(void)
{
if(TIM2->SR & TIM_SR_UIF) {
// clear flag
TIM2->SR &= ~TIM_SR_UIF;
pinToggle(pinB3);
//led_on = !led_on;
}
}
void risingEdgeInterruptPinA1()
{
// https://controllerstech.com/external-interrupt-using-registers/
pinConfig(pinA1, input, def_stage, pullDown, def_speed);
// Connect
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
SYSCFG->EXTICR[1] &= ~SYSCFG_EXTICR1_EXTI1_Msk;
SYSCFG->EXTICR[1] |= SYSCFG_EXTICR1_EXTI1_PA;
// Interrupt mask register
EXTI->IMR |= EXTI_IMR_IM1;
// rising trigger selection
EXTI->RTSR |= EXTI_RTSR_RT1;
NVIC_SetPriority (EXTI0_1_IRQn, 0);
NVIC_EnableIRQ(EXTI0_1_IRQn);
while(1)
{
if(led_on){
pinWrite(pinB3,1);
} else{
pinWrite(pinB3,0);
}
}
}
void interruptLEDtoggleOnTIM1counterCompare()
{
timerInitCounter(timer_2, 0xF, 65000, upCounting);
// Enable Update Interrupt
TIM2->DIER |= TIM_DIER_UIE;
NVIC_SetPriority(TIM2_IRQn,0);
NVIC_EnableIRQ(TIM2_IRQn);
timerSart(timer_2);
while(1)
{
if(led_on){
pinWrite(pinB3,1);
} else{
pinWrite(pinB3,0);
}
}
}
int main(int argc, char *argv[])
{
setup();
//risingEdgeInterruptPinA1();
interruptLEDtoggleOnTIM1counterCompare();
return 1;
}
void setup()
{
uint8_t i = 0;
delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library
pinConfig(pinB3, output, pushPull, def_res, def_speed);
pinConfig(pinA0, input, def_stage, pullDown, def_speed);
setupInit(); // This is the sescond call of System init the assebly start code is calling it before the main.
usartInit( usart2,
pinA2,
pinA15,
115200,
eight,
NO_PARITY_CTRL,
noFlowControl);
//clears screen and send the wellcome messgae
print_Usart(usart2, ASCII_clear);
print_Usart(usart2, "Hello to our KED project\n\r");
//blinks 10 times to indicate the sicsessfull init if the device
for(i = 0 ; i < 10 ; i++) {
delayMs(100);
pinToggle(pinB3);
delayMs(100);
}
pinWrite(pinB3,0);
}