summaryrefslogtreecommitdiff
path: root/sim/ucsim/stm8.src/test/ltim.c
blob: 55740d6eb15f170ec49441e8664c7d1234f5fffa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Source code under CC0 1.0
#include <stdint.h>

#include "stm8.h"

#define PC GPIOC
#define PE GPIOE

volatile unsigned long clk= 0;

void tim1_up_isr(void) __interrupt(TIM1_UP_IRQ)
{
  TIM1->sr1&= ~TIM_SR1_UIF;
  clk++;
  //PE->odr^= 0x80;
}

unsigned long clock(void)
{
  unsigned long c;
  TIM1->ier&= ~TIM_IER_UIE;
  c= clk;
  TIM1->ier|= TIM_IER_UIE;
  return c;
}

unsigned long last_tick1= 0;

void tick1(unsigned long c)
{
  //unsigned long c= clock();
  if (c - last_tick1 > 500)
    {
      last_tick1= c;
      PE->odr^= 0x80;
    }
}

unsigned long last_tick2= 0;

void tick2(unsigned long c)
{
  //unsigned long c= clock();
  if (c - last_tick2 > 1000)
    {
      last_tick2= c;
      PC->odr^= 0x80;
    }
}

void main(void)
{
  CLK->ckdivr = 0x00; // Set the frequency to 16 MHz
  CLK->pckenr2 |= 0x02; // Enable clock to timer

  // Configure timer
  // 16 MHz clock for timer
  TIM1->pscrh = 0;//0x3e;
  TIM1->pscrl = 0;//0x80;
  // Update event at every 1 ms (16000 count)
  #define AR 16000
  TIM1->arrh = AR >> 8;
  TIM1->arrl = AR & 0xff;
  // Enable timer
  TIM1->cr1 = TIM_CR1_CEN;

  // Enable interrupt for timer1 update
  TIM1->ier|= TIM_IER_UIE;
  EI;
  
  // Configure pins
  PE->ddr = 0x80;
  PE->cr1 = 0x80;

  PC->ddr = 0x80;
  PC->cr1 = 0x80;

  for(;;)
    {
      unsigned long c= clock();
      tick1(c);
      tick2(c);
    }
}