Mcucpp
96 строк · 1.6 Кб
1#include <usart.h>
2#include <tiny_ostream.h>
3#include <iopins.h>
4#include <pinlist.h>
5#include <delay.h>
6#include <timers.h>
7#include <timer_utils.h>
8#include <Dispatcher.h>
9#include <atomic.h>
10
11using namespace Mcucpp;
12
13typedef Usart1 MyUsart;
14
15class UsartOut
16{
17public:
18void put(char c)
19{
20MyUsart::Write(c);
21}
22};
23
24typedef basic_ostream<UsartOut> ostream;
25ostream cout;
26
27
28typedef IO::Pc0 Led;
29typedef Timers::Timer1 TheTimer;
30// timer type, target freq, timer src freq
31typedef Timers::TimerFreqSetup<TheTimer, 1000, 8000000> TimerSetup;
32
33uint32_t GetTicks();
34
35void Blink()
36{
37Led::Toggle();
38GetCurrentDispatcher().SetTimer(1000, &Blink);
39}
40
41class Print
42{
43int i;
44public:
45Print()
46:i(0)
47{
48
49}
50
51void Do()
52{
53cout << i << "\r\n";
54GetCurrentDispatcher().SetTimer<Print, &Print::Do>(2000, this);
55}
56};
57
58int main()
59{
60Led::SetConfiguration(Led::Port::Out);
61Led::Set();
62MyUsart::Init(9600);
63MyUsart::SelectTxRxPins(0, 0);
64
65sei();
66
67cout << "Hello, world!\r\n";
68GetCurrentDispatcher().SetTimerFunc(GetTicks);
69
70TheTimer::Start(TimerSetup::divider, TimerSetup::reloadValue);
71TheTimer::EnableInterrupt(TheTimer::OverflowInt);
72
73Blink();
74
75Print print;
76GetCurrentDispatcher().SetTimer<Print, &Print::Do>(2000, &print);
77
78while(1)
79{
80GetCurrentDispatcher().Poll();
81}
82return 0;
83}
84
85static uint32_t ms_ticks = 0;
86
87uint32_t GetTicks()
88{
89return Atomic::Fetch(&ms_ticks);
90}
91
92ISR(TIMER1_OVF_vect)
93{
94ms_ticks++;
95TheTimer::Set(TimerSetup::reloadValue);
96}
97
98