Mcucpp

Форк
0
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

11
using namespace Mcucpp;
12

13
typedef Usart1 MyUsart;
14

15
class UsartOut
16
{
17
	public:
18
	void put(char c)
19
	{
20
		MyUsart::Write(c);
21
	}
22
 };
23

24
typedef basic_ostream<UsartOut> ostream;
25
ostream cout; 
26

27

28
typedef IO::Pc0 Led;
29
typedef Timers::Timer1 TheTimer;
30
	                 // timer type, target freq, timer src freq
31
typedef Timers::TimerFreqSetup<TheTimer, 1000, 8000000> TimerSetup;
32

33
uint32_t GetTicks();
34

35
void Blink()
36
{
37
	Led::Toggle();
38
	GetCurrentDispatcher().SetTimer(1000, &Blink);
39
}
40

41
class Print
42
{
43
	int i;
44
public:
45
	Print()
46
		:i(0)
47
	{
48
		
49
	}
50
	
51
	void Do()
52
	{
53
		cout << i << "\r\n";
54
		GetCurrentDispatcher().SetTimer<Print, &Print::Do>(2000, this);
55
	}
56
};
57

58
int main()
59
{
60
	Led::SetConfiguration(Led::Port::Out);
61
	Led::Set();
62
	MyUsart::Init(9600);
63
	MyUsart::SelectTxRxPins(0, 0);
64
	
65
	sei();
66
	
67
	cout << "Hello, world!\r\n";
68
	GetCurrentDispatcher().SetTimerFunc(GetTicks);
69
	
70
	TheTimer::Start(TimerSetup::divider, TimerSetup::reloadValue);
71
	TheTimer::EnableInterrupt(TheTimer::OverflowInt);
72
	
73
	Blink();
74
	
75
	Print print;
76
	GetCurrentDispatcher().SetTimer<Print, &Print::Do>(2000, &print);
77
	
78
	while(1)
79
	{
80
		GetCurrentDispatcher().Poll();
81
	}
82
	return 0;
83
}
84

85
static uint32_t ms_ticks = 0;
86

87
uint32_t GetTicks()
88
{
89
	return Atomic::Fetch(&ms_ticks);
90
}
91

92
ISR(TIMER1_OVF_vect)
93
{
94
	ms_ticks++;
95
	TheTimer::Set(TimerSetup::reloadValue);
96
}
97

98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.