Fixed issues with timer

master
_N0x 3 years ago
parent 2bd2215ee9
commit 0eace5b6fe

@ -28,18 +28,20 @@
<ResetRule>0</ResetRule> <ResetRule>0</ResetRule>
<eraseonlaunchrule>0</eraseonlaunchrule> <eraseonlaunchrule>0</eraseonlaunchrule>
<EraseKey /> <EraseKey />
<avrtool>com.atmel.avrdbg.tool.simulator</avrtool> <avrtool>com.atmel.avrdbg.tool.stk500</avrtool>
<avrtoolserialnumber /> <avrtoolserialnumber />
<avrdeviceexpectedsignature>0x1E9705</avrdeviceexpectedsignature> <avrdeviceexpectedsignature>0x1E9705</avrdeviceexpectedsignature>
<com_atmel_avrdbg_tool_simulator> <com_atmel_avrdbg_tool_simulator>
<ToolOptions xmlns=""> <ToolOptions>
<InterfaceProperties> <InterfaceProperties>
</InterfaceProperties> </InterfaceProperties>
<InterfaceName>
</InterfaceName>
</ToolOptions> </ToolOptions>
<ToolType xmlns="">com.atmel.avrdbg.tool.simulator</ToolType> <ToolType>com.atmel.avrdbg.tool.simulator</ToolType>
<ToolNumber xmlns=""> <ToolNumber>
</ToolNumber> </ToolNumber>
<ToolName xmlns="">Simulator</ToolName> <ToolName>Simulator</ToolName>
</com_atmel_avrdbg_tool_simulator> </com_atmel_avrdbg_tool_simulator>
<AsfFrameworkConfig> <AsfFrameworkConfig>
<framework-data xmlns=""> <framework-data xmlns="">
@ -53,6 +55,20 @@
</dependencies> </dependencies>
</framework-data> </framework-data>
</AsfFrameworkConfig> </AsfFrameworkConfig>
<avrtoolinterface>ISP</avrtoolinterface>
<com_atmel_avrdbg_tool_stk500>
<ToolOptions>
<InterfaceProperties>
<IspClock>115200</IspClock>
</InterfaceProperties>
<InterfaceName>ISP</InterfaceName>
</ToolOptions>
<ToolType>com.atmel.avrdbg.tool.stk500</ToolType>
<ToolNumber>
</ToolNumber>
<ToolName>STK500</ToolName>
</com_atmel_avrdbg_tool_stk500>
<avrtoolinterfaceclock>115200</avrtoolinterfaceclock>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<ToolchainSettings> <ToolchainSettings>

@ -12,29 +12,27 @@ volatile uint16_t cntr = 0;
void void
Timer_init (void){ Timer_init (void){
TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */
TCCR0B |= (1<<1); /* Set the clock select bit to pre-scaler 2 TCCR0B |= (1<<1); /* Set the clock select bit to pre-scaler 2
1MHz / 8 (pre-scaler 2) ==> 125KHz */ 1MHz / 8 (pre-scaler 2) ==> 125KHz */
TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */
TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */ TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
OCR0A = 125; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms OCR0A = 125; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
1MHz / 8 (pre-scaler 2) / 125 ==> 1KHz (1ms) */ 1MHz / 8 (pre-scaler 2) / 125 ==> 1KHz (1ms) */
} }
uint16_t uint16_t
Timer_getTick(void){ Timer_getTick(void){
return cntr; uint16_t tmp;
cli();
tmp = cntr;
sei();
return tmp;
} }
ISR(TIMER0_COMPA_vect){ ISR(TIMER0_COMPA_vect){
cntr++; cntr++;
} }
/*
ISR(TIMER0_OVF_vect){
cntr++;
}
//*/

@ -21,6 +21,7 @@ void tast2 (void);
/* global variables */ /* global variables */
volatile int g_counter = 0; volatile int g_counter = 0;
int ledStatus = 0; int ledStatus = 0;
uint16_t startMS;
/* functions */ /* functions */
int int
@ -34,30 +35,35 @@ main (void)
Timer_init(); Timer_init();
/* Run Task 1
while (1) while (1)
{ {
/* Run Task 1 */ task1();
//task1(); }//*/
/* Run Task 2 */
task2(); /* Run Task 2 */
} startMS=Timer_getTick();
while (1)
{
task2();
}//*/
} }
/* Task 2 (2021-10-28) */ /* Task 2 (2021-10-28) */
void void
task2 () { task2 () {
if(Timer_getTick()-startMS >= 1000) { /* Wait 1000ms before switching LED1 */
if(Timer_getTick() % 1000 == 0){ startMS=Timer_getTick();
if(ledStatus % 2 == 0){
Led1_On();
} else {
Led1_Off();
}
ledStatus++; ledStatus++;
} }
if(ledStatus % 2 == 0){
Led1_On();
} else {
Led1_Off();
}
} }

Loading…
Cancel
Save