-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD ST7565 Mega64_14_7.c
1503 lines (1182 loc) · 43.6 KB
/
LCD ST7565 Mega64_14_7.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* LCD_ST7565_Mega64_5.c
*
* Created: 03.02.2015 14:55:33
* Author: Sergey
* Board - ATMEGABOARD Atmega64 with EXMEM, AT45DB321D
* Modernized library uart.c, changed function uart1_putc,
* returned it to its original form, at the same time and uncomment uart1_puts uart1_puts_p. Instead, use a new function uart1_putc usart1_put_char.
* Ðàáîòîñïîñîáíîñòü ñîõðàíåíà.
* Implementation DataFlash AT45DB321D library - done
* Implementation FatFS îò Chan - done (FatFs - FAT file system module R0.05 (C)ChaN, 2007)
* Trying to put the files in the different directories of the project - successfully
* Implemetation library I2C clock DS1307 - done
* Added function to read I2C temperature sensor LM75A - done
* Added function to read I2C EEPROM AT24C256 - done
* Display ST7565 library handed in directory "lcd"
* Added setting time
* Added the function of the original initialization time DS1307
* Release Flash=19 300b; RAM=2000b
* 16.02.2015 Added library TUI_lib_0_03 Small-LCD Text User Interface author: ARV
* Slightly brushed display library ST7565, Text output is implemented on the basis of libraries Wiselord with downloadable and scalable fonts.
* In the library TWI added functions to read and write arrays external EEPROM.
* 16.02.2015
* Added displaying graphical splash at the start
* Implemetation Touch Memory library. Author - S.Surov, Radio magazine 2009 ¹1,2 Master_controller - successfully implemented, but not tested
* Added the function Buzzer without generator on port PG3
* Unwired PB4 port from RDY/BUSY AT45DB321D. RDY/BUSY read from register AT45DB321D.
* Read / write arrays of EEPROM AT24C256 is implemented through a pointer - done.
* ...need to do exactly the same job through the structure.
* It is based on version LCD ST7565 Mega64_38. Modified driver dataflesh from LUFA library with FatFS by Chan R0.09a
* A sort of a downgrade. But LUFA driver have a read/write buffer
* FatFS library updated to version R0.11 - done
* Added the function of data logger: File type: 19022015.CSV, every 5 minutes written string type: 19.2.2015 16:38:35, 23.7 Degrees.
* ÔFunction prototype by LUFA
* Trying to place a large array in external EEPROM.
* 09.03.2015 Screwed up function scrolling text on display. Works very crooked
ST7565 pinout:
CS PB0
A0 PE3- wired on PB4
A0 PB4
RES PE2
SID PB2
SCLK PB1
AT45DB321D pinout:
SCK PB1
SI PB2
SO PB3
CS PB5
*/
//Atmega64 - microcontroller
/*The frequency of the clock generator */
#ifndef F_CPU
#define F_CPU 18432000UL
#endif
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <math.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <stddef.h>
#include <string.h>
#include <inttypes.h>
#include "drivers/twim.h"
#include "lcd/lcd.h"
#include "lcd/st7565.h"
#include "lcd/st7565-graphics.h"
#include "lcd/st7565-driver.h"
#include "drivers/uart.h"
#include "drivers/spi.h"
#include "LUFA/DataflashManager.h"
#include "file.system/rtc.h"
#include "lcd/wiselord/fonts.h"
#include "TUI/sedit.h"
#include "TUI/buttons.h"
#include "TUI/mmenu.h"
#include "TM/functions_TM.h"
#include "lcd/logo/addidas_logo.h"
//#include "lcd/logo/nike.h"
//#include "lcd/logo/picture_data.h"
//#include "lcd/logo/sailor_man.h"
//#include "lcd/logo/Bitmap.h"
#define STR_BUFSIZE 16
#define LCD_LINES 8 /**< number of visible lines of the display */ //For TUI
#define LCD_DISP_LENGTH 20 /**< visibles characters per line of the*/
// set up external SRAM prior to anything else to make sure malloc() has access to it
void EnableExternalSRAM (void) __attribute__ ((naked)) __attribute__ ((section (".init1")));
//__attribute__ ((section (".init1"))) EnableExternalSRAM(void)
void EnableExternalSRAM(void)
{
MCUCR |= (1<<SRE);
// MCUCR = (1<<SRE)|(0<<SRW10); /* External memory interface enable */
XMCRA &= ~(1<<SRL2)|(1<<SRL1)|(1<<SRL0)|(1<<SRW01)|(1<<SRW00)|(1<<SRW11);
/*
* Lower sector = N/A
* Upper sector = 0x1100 - 0xFFFF
* No wait cycles for upper sector
*/
XMCRB &= ~(1<<XMBK)|(1<<XMM2)|(1<<XMM1)|(1<<XMM0);
/* External Memory Bus-keeper Disabled;
* External Memory High Mask - no released port pins*/
}
#define DS1307_ADR 104
uint8_t LM75_buff_1;
uint8_t LM75_buff_2;
//float LM75_buff;
uint8_t EEPROM_AT24_buff_read[64];
uint8_t EEPROM_AT24_buff_write[64];
//void *p_m_4; // Ïðîìåæóòî÷íûé óêàçàòåëü íà÷àëüíîãî àäðåñà ìàññèâà logo
//uint8_t *p_b_4; // Óêàçàòåëü àäðåñà îòäåëüíîãî áàéòà ìàññèâà logo
uint8_t *p_b_write; // óêàçàòåëü íà ìàññèâ EEPROM
uint8_t *p_b_read; // óêàçàòåëü íà ìàññèâ EEPROM
uint8_t *p_b_4; // óêàçàòåëü íà ìàññèâ
uint16_t pos_in_string; //Ïîçèöèÿ â ñòðîêå, ñ êîòîðîé íà÷èíàåòñÿ îòîáðàæåíèå (ïîçèöèÿ ïèêñåëÿ, à íå ñèìâîëà)
char *message = " ******Welcome******";
char *message1 = " Very long long super long string...";
//uint8_t logo [128];
//p_b_4 = &logo[0];
#define FALSE 0
#define TRUE 1
#define DISK_SEC_SIZE 512 /* sector area size */
//uint8_t media;
//unsigned int dannie, i, adres;
//uint8_t masiv [4] = {100,150,200,150};
// uint8_t save_8 EEMEM = 2; //internal EEPROM_Example
// uint16_t save_16 EEMEM = 234;
// uint32_t save_32 EEMEM = 10000;
// unsigned char save[4] EEMEM;
uint8_t *mem; //EXMEM_test Example_1
//int massiv[100][100]; // !!! MEMORY EATER !!! EXMEM_test Example_2
volatile uint8_t K;
volatile int ADCvalue, ADCvalue1, ADCvalue2;
volatile int adc_read1, adc_read2;
unsigned char pause, read, adc_read1_1, adc_read1_2, adc_read2_1, adc_read2_2;
volatile int datalog;
/* System time for FatFs */
volatile unsigned long g_SysTIME;
volatile unsigned long start;
// DWORD get_fattime (void) //RTC çàãëóøêà
// {
// /* Pack date and time into a DWORD variable */
// return ((DWORD)(2015 - 1980) << 25)
// | ((DWORD)2 << 21)
// | ((DWORD)15 << 16)
// | ((DWORD)16 << 11)
// | ((DWORD)53 << 5)
// | ((DWORD)6>> 1);
// }
RTC rtc;
BYTE RtcOk; /* RTC is available */
DWORD get_fattime (void)
{
RTC rtc;
if (!RtcOk) return 0;
/* Get local time */
rtc_gettime(&rtc);
/* Pack date and time into a DWORD variable */
return ((DWORD)(rtc.year - 1980) << 25)
| ((DWORD)rtc.month << 21)
| ((DWORD)rtc.mday << 16)
| ((DWORD)rtc.hour << 11)
| ((DWORD)rtc.min << 5)
| ((DWORD)rtc.sec >> 1);
}
unsigned int NUM; // variable for HeartBeat LED
unsigned int c;
//For UART
// Define size of memory block
#define BUFFER_SIZE 512 //èñõîäíûé êîä
/* 9600 baud */ //Peter Fleury
#define UART_BAUD_RATE 115200
// Macro to send strings stored in program memory space
#define PRINTF(format, ...) printf_P(PSTR(format), ## __VA_ARGS__)
int usart1_put_char(unsigned char data, FILE *stream); //My
// Create PRINTF Stream structure
FILE printf_stream = FDEV_SETUP_STREAM (usart1_put_char, NULL, _FDEV_SETUP_WRITE); //èñõîäíûé êîä_
/*****************************************************************************
file system object Protoss
******************************************************************************/
/** FAT Fs structure to hold the internal state of the FAT driver for the Dataflash contents. */
FATFS fs;
FATFS *p_fs = &fs;
char buff[128]; // Read-write buffer
/** FAT Fs structure to hold a FAT file handle for the log data write destination. */
FIL fil_obj;
unsigned int ByteWrite = 0;
char* var="Hello! ";
FRESULT scan_files (char* path) //Scan files and directories on path avrlab.com
{
FRESULT res;
FILINFO fno;
DIR dir;
// int i;
char *fn;
res = f_opendir(&dir, path);
if (res == FR_OK) {
// i = strlen(path);
for (;;)
{
res = f_readdir(&dir, &fno); //Read dir items
if (res != FR_OK || fno.fname[0] == 0) break;
if (fno.fname[0] == '.') continue;
fn = fno.fname;
if (fno.fattrib & AM_DIR) { // Check item type, DIR or FILE
PRINTF ("DIR: ");
uart1_puts (fn);
PRINTF ("\r\n");
}
else {
PRINTF ("FILE: ");
uart1_puts (fn);
PRINTF ("\r\n");
}
}
}
return res;
}
//TUI Start
#define menu_one 1 // êîä âûáîðà ïóíêòà ìåíþ "îäíîñòðî÷íîå ìåíþ"
#define menu_multi 2 // êîä âûáîðà ïóíêòà ìåíþ "ìíîãîñòðî÷íîå ìåíþ"
#define menu_test 3 // êîä âûáîðà ïóíêòà ìåíþ "òåñò"
#define menu_tes 10 // êîä âûáîðà ïóíêòà ìåíþ "òåñò1"
PROGMEM const char mmm1[] = "ONE LINE"; // ñòðîêà ïóíêòà ìåíþ "îäíîñòðî÷íîå ìåíþ"
PROGMEM const char mmm2[] = "MULTI LINE"; // ñòðîêà ïóíêòà ìåíþ "ìíîãîñòðî÷íîå ìåíþ"
PROGMEM const char mm1[] = "MENU MODE"; // ñòðîêà ïóíêòà ìåíþ "ðåæèì ìåíþ"
PROGMEM const char mm2[] = "EDIT"; // ñòðîêà ïóíêòà ìåíþ "ðåäàêòèðîâàíèå"
PROGMEM const char mm3[] = "LEDS"; // ñòðîêà ïóíêòà ìåíþ "ñâåòîäèîäû"
PROGMEM const char mm4[] = "TEST"; // ñòðîêà ïóíêòà ìåíþ "òåñò"
PROGMEM const char mm5[] = "TEST1"; // ñòðîêà ïóíêòà ìåíþ "òåñò"
static char tr[16]; // âñïîìîãàòåëüíûé ìàññèâ-ñòðîêà
static uint16_t number1 = 100; // ïåðåìåííàÿ äëÿ äåìîíñòðàöèè ðåäàêòîðà áåççíàêîâûõ ÷èñåë
static int16_t number2 = -12; // ïåðåìåííàÿ äëÿ äåìîíñòðàöèè ðåäàêòîðà ÷èñåë ñî çíàêîì
static uint8_t number3 = 10; // ïåðåìåííàÿ äëÿ äåìîíñòðàöèè ðåäàêòèðîâàíèÿ çàïîëíåííîé øêàëîé
static uint8_t number4 = 20; // ïåðåìåííàÿ äëÿ äåìîíñòðàöèè ðåäàêòèðîâàíèÿ øòðèõîì íà øêàëå
// îïðåäåëåíèå ôóíêöèé äëÿ àâòîìàòè÷åñêîé ãåíåðàöèè ñòðîê â ìåíþ (îíè æå ìåíÿþò çíà÷åíèÿ ïåðåìåííûõ)
// øêàëà â âèäå ñïëîøíîé ïîëîñû
static pchar ed_scal1(void* data, uint8_t v, uint8_t ch);
// øêàëà â âèäå âåðíüåðà
static pchar ed_scal2(void* data, uint8_t v, uint8_t ch);
// ðåäàêòèðîâàíèå áåççíàêîâîãî ÷èñëà
static pchar ed_uint(void* data, uint8_t v, uint8_t ch);
// ðåäàêòèðîâàíèå ÷èñëà ñî çíàêîì
static pchar ed_int(void* data, uint8_t v, uint8_t ch);
// ðåäêàòèðîâàíèå óðîâíåé íà ïîðòó Ñ
static pchar port_val(void* data, uint8_t v, uint8_t ch);
// ìàññèâ ìåíþ èçìåíåíèÿ ðåæèìà ìåíþ
PROGMEM t_menu const m_mode[] = {
item_sim(mmm1,menu_one),
item_sim(mmm2,menu_multi)
};
// ìàññèâ ìåíþ ðåäàêòèðîâàíèÿ
PROGMEM t_menu const m_edit[] = {
item_opt(ed_uint, &number1, 0),
item_opt(ed_int, &number2, 0),
item_opt(ed_scal1, &number3, 0),
item_opt(ed_scal2, &number4, 0)
};
// ìàññèâ ìåíþ óïðàâëåíèÿ ñâåòîäèîäàìè
// PROGMEM t_menu const m_flags[] = {
// item_opt(port_val, &PORTF, (0x01)<<4),
// item_opt(port_val, &PORTF, (0x02)<<4),
// item_opt(port_val, &PORTF, (0x04)<<4),
// item_opt(port_val, &PORTF, (0x08)<<4)
// };
// PROGMEM t_menu const m_flags[] = {
// item_opt(port_val, &PORTF, 0x01),
// item_opt(port_val, &PORTF, 0x02),
// item_opt(port_val, &PORTF, 0x04),
// item_opt(port_val, &PORTF, 0x08)
// };
PROGMEM t_menu const m_flags[] = {
item_opt(port_val, &PORTF, 0x10),
item_opt(port_val, &PORTF, 0x20),
item_opt(port_val, &PORTF, 0x40),
item_opt(port_val, &PORTF, 0x80)
};
// ìàññèâ îñíîâíîãî ìåíþ
PROGMEM t_menu const m_menu[] = {
item_sub(mm1,m_mode), // ïîäìåíþ ðåæèìîâ îòîáðàæåíèÿ ìåíþ
item_sub(mm2,m_edit), // ïîäìåíþ ðåäàêòîðîâ
item_sub(mm3,m_flags), // ïîäìåíþ ôëàæêîâ
item_sim(mm4,menu_test), // ïðîñòîé ïóíêò - íåáîëüøàÿ àíèìàöèÿ
item_sim(mm5,menu_tes) // ïðîñòîé ïóíêò - íåáîëüøàÿ àíèìàöèÿ
};
// ðåäàêòèðîâàíèå óðîâíåé íà ïîðòó Ñ - óïðàâëåíèå ñâåòîäèîäàìè
static pchar port_val(void* data, uint8_t v, uint8_t ch){
pchar ts;
if(ch){ // ïàðàìåòð ch íå ðàâíûé íóëþ óêàçûâàåò, ÷òî íàäî èçìåíèòü ñîñòîÿíèå
// ïðè ýòîì óêàçàòåëü data ñ÷èòàåì óêàçàòåëåì íà áàéò
*((uint8_t*)data) ^= v;
}
// ãîòîâèì òåêñò â çàâèñèìîñòè îò òîãî, êàêîé ñâåòîäèîä îáðàáàòûâàåòñÿ
switch(v){
case 16: ts = PSTR("LED 1"); break;
case 32: ts = PSTR("LED 2"); break;
case 64: ts = PSTR("LED 3"); break;
case 128: ts = PSTR("LED 4"); break;
}
// ãîòîâèì ñòðîêó äëÿ âîçâðàòà
strcpy_P(tr,ts); // êîïèðóåì ïåðâóþ ÷àñòü ñòðîêè
if(*((uint8_t*)data) & v) // ïðîâåðÿåì ñîñòîÿíèå ôëàãà è
// ïðèáàâëÿåì ñîîòâåòñòâóþùèé òåêñò
strcat_P(tr,PSTR(" ON"));
else
strcat_P(tr,PSTR(" OFF"));
// âîçâðàùàåì ïîëó÷åííóþ ñòðîêó
disp_send_frame();
return tr;
}
// ðåäàêòèðîâàíèå áåççíàêîâîãî öåëîãî ÷èñëà
static pchar ed_uint(void* data, uint8_t v, uint8_t ch){
char ts[6];
if(ch){ // åñëè íàäî èçìåíèòü - ïðîñòî âûçûâàåì íóæíûé ðåäàêòîð
disp_clear();
*(uint16_t*)data = edit_uint(0,PSTR("Unsigned: "),*(uint16_t*)data,50,20000);
}
// ïîäãîòàâëèâàåì ñòðîêó, êîòîðóþ âèäíî â ìåíþ
strcpy_P(tr,PSTR("UINT EDIT "));
ltoa((uint16_t)(*(uint16_t*)data),ts,10);
strcat(tr,ts);
disp_send_frame();
return tr;
}
// ðåäàêòèðîâàíèå öåëîãî ñî çíàêîì - àíàëîãè÷íî ïðåäûäóùåìó
static pchar ed_int(void* data, uint8_t v, uint8_t ch){
char ts[7];
if(ch){
disp_clear();
*(int16_t*)data = edit_sint(0,PSTR("Signed: "),*(int16_t*)data,-1500,20000);
}
strcpy_P(tr,PSTR("INT EDIT "));
itoa(*(uint16_t*)data,ts,10);
strcat(tr,ts);
disp_send_frame();
return tr;
}
// øêàëà â âèäå âåðíüåðà
static pchar ed_scal2(void* data, uint8_t v, uint8_t ch){
uint8_t tmp = *(uint8_t*)data;
uint8_t old = tmp;
uint8_t event;
char ts[5];
if(ch){ // ðåäàêòèðîâàíèå âûïîëíÿåòñÿ ïðÿìî â ýòîé ôóíêöèè
init_hbar_scale_sym(); // ïîäãîòàâëèâàåì çíàêîãåíåðàòîð
// ðåäàêòèðóåì
disp_clear();
gdSetXY(0,0);
lcd_puts_p(PSTR("SCALE 2"));
disp_send_frame();
do{
se_percent(0,1,tmp); // ïðîñòî ðèñóåì òåêóùåå ñîñòîÿíèå øêàëû
// îæèäàåì ñîáûòèÿ
do{
event = get_event();
} while(!event);
// âûïîëíÿåì îáðàáîòêó ïîñòóïèâøåãî ñîáûòèÿ
switch(event){
case EV_ESCAPE:
tmp = old; // ïðè îòìåíå âîññòàíàâëèâàåì èñõîäíîå çíà÷åíèå
case EV_ENTER:
*(uint8_t*)data = tmp; // ïî ENTER ãîòîâèìñÿ âåðíóòü òåêóùåå çíà÷åíèå
goto exit;
// ñíîïêè ÁÎËÜØÅ-ÌÅÍÜØÅ ïðîñòî ìåíÿþò çíà÷åíèå â çàäàííûõ ïðåäåëàõ
case EV_NEXT:
if(++tmp > 100) tmp = 100;
break;
case EV_PREV:
if(tmp > 0) tmp--;
}
disp_send_frame();
}while(1);
}
// ãîòîâèì ñòðîêó, êîòîðóþ âèäíî â ìåíþ
exit:
strcpy_P(tr,PSTR("SCALE VBAR "));
itoa(tmp,ts,10);
strcat(tr,ts);
return tr;
}
// øêàëà â âèäå ñïëîøíîé ïîëîñû - àíàëîãè÷íî ïðåäûäóùåé
static pchar ed_scal1(void* data, uint8_t v, uint8_t ch){
uint8_t tmp = *(uint8_t*)data;
uint8_t old = tmp;
uint8_t event;
char ts[5];
if(ch){
// ðåäàêòèðóåì
// lcd_init(LCD_DISP_OFF);
init_h_scale_sym();
// lcd_init(LCD_DISP_ON);
disp_clear();
gdSetXY(0,0);
lcd_puts_p(PSTR("SCALE 1"));
disp_send_frame();
do{
// se_percent(0,1,tmp);
do{
event = get_event();
} while(!event);
switch(event){
case EV_ESCAPE:
tmp = old;
case EV_ENTER:
*(uint8_t*)data = tmp;
goto exit;
case EV_NEXT:
if(++tmp > 100) tmp = 100;
break;
case EV_PREV:
if(tmp > 0) tmp--;
}
disp_send_frame();
}while(1);
}
exit:
strcpy_P(tr,PSTR("SCALE HBAR "));
itoa(tmp,ts,10);
strcat(tr,ts);
return tr;
}
//TUI Stop
//LUFA Data Logger
/** Opens the log file on the Dataflash's FAT formatted partition according to the current date */
void OpenLogFile(void)
{
char LogFileName[12];
/* Get the current date for the filename as "DDMMYY.csv" */
// TimeDate_t CurrentTimeDate;
// RTC_GetTimeDate(&CurrentTimeDate);
sprintf(LogFileName, "%02d%02d%02d.csv", rtc.mday, rtc.month, rtc.year);
/* Mount the storage device, open the file */
f_mount(&fs, "", 0);
f_open(&fil_obj, LogFileName, FA_OPEN_ALWAYS | FA_WRITE);
f_lseek(&fil_obj, fil_obj.fsize);
}
/** Closes the open data log file on the Dataflash's FAT formatted partition */
void CloseLogFile(void)
{
/* Sync any data waiting to be written, unmount the storage device */
f_sync(&fil_obj);
f_close(&fil_obj);
}
void LogFileSave (void)
{
if (datalog > 27000) //5400 = 1 munute???
{
// TimeDate_t CurrentTimeDate;
// RTC_GetTimeDate(&CurrentTimeDate);
char LineBuffer[100];
uint16_t BytesWritten;
BytesWritten = sprintf(LineBuffer, "%02d/%02d/20%02d, %02d:%02d:%02d, %d Degrees\r\n",
rtc.mday, rtc.month, rtc.year,
rtc.hour, rtc.min, rtc.sec,
LM75_buff_1);
f_write(&fil_obj, LineBuffer, BytesWritten, &BytesWritten);
f_sync(&fil_obj);
PRINTF("In Log: %u.%u.%u %02u:%02u:%02u, %d.%d Degrees\r", rtc.mday, rtc.month, rtc.year, rtc.hour, rtc.min, rtc.sec, LM75_buff_1,LM75_buff_2);
datalog = 0;
}
}
//LUFA Data Logger
void ADC_init()
{ //Íàñòðîéêà ÀÖÏ
ADCSRA |=(1<<ADEN); //Çàäåéñòâîâàòü ÀÖÏ
// ADCSRA |=(1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0); //Äåëèòåëü 64 - 125 êÃö ïðè ÷àñòîòå ïðîöåññîðà 8 MHz
ADCSRA |=(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //Äåëèòåëü 128 - 125 êÃö ïðè ÷àñòîòå ïðîöåññîðà 16 MHz
ADCSRB &= ~(1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0); // Set free running mode
ADMUX |= (1<<REFS1)|(1<<REFS0); //Îïîðíîå íàïðÿæåíèå 2.56 Â âíóòðåííåå
// ADMUX |= (1<<ADLAR); //Ñ ïîìîùüþ ýòîãî áèòà äåëàåì ëåâîå âûðàâíèâàíèå ðåçóëüòàòà è ÷èòàåì ðåçóëüòàò èç ADCH (ÀÖÏ 8áèò)
ADMUX &= ~(1<<ADLAR); //Ñ ïîìîùüþ ýòîãî áèòà äåëàåì ïðàâîå âûðàâíèâàíèå ðåçóëüòàòà (ÀÖÏ 10áèò)
ADMUX &= ~(1<<MUX4)|(1<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0); //Âûáîð âûâîäà
ADCSRA |= (1 << ADIE); // Enable Interrupts
ADCSRA |= (1 << ADIF); // Interrupt Flag - write 1 to clear!
ADCSRA |= (1 << ADATE); // ADATE = Auto Trigger Enable
ADCSRA |= (1 << ADSC); // Start the ADC conversion
}
void Timer_init()
{
//Íàñòðîéêà 8-ðàçðÿäíîãî Òàéìåðà TC0
OCR0=200; // ðåãèñòð ïîðîãà ñðàâíåíèÿ - çàäàåì âåðøèíó ñ÷åòà ñ÷åò÷èêà,
// åñëè çíà÷åíèå ñ÷åò÷èêà (TCNT0) ñîâïàäàåò ñî çíà÷åíèåì ðåãèñòðà OCR0 òî ñ÷åò÷èê îáíóëÿåòñÿ (TCNT0=0)
TCCR0 |= (0<<FOC0)|(0<<WGM00)|(0<<COM01)|(0<<COM00)|(1<<WGM01)|(1<<CS02)|(1<<CS01)|(1<<CS00); //ñáðîñ ñ÷åò÷èêà ïðè ñîâïàäåíèè,
//ôóíêöèÿ îáû÷íîãî ïîðòà ââîäà-âûâîäà OC0 îòêëþ÷åí. |<- ïðåääåëèòåëü /1024 ->|
TIMSK|=(1<<OCIE0); //Ðàçðåøåíèå ïðåðûâàíèÿ ïî ðåçóëüòàòó ñðàâíåíèÿ òàéìåðà-ñ÷åò÷èêà 0
//Íàñòðîéêà 8-ðàçðÿäíîãî Òàéìåðà TC2
TCCR2 = ((1 << CS22)|(0 << CS21)|(1 << CS20)| /* clkT0S/1024 */
(1 << WGM21)|(0 << WGM20)| /* Waveform Generation Mode - CTC */
(0 << COM21)|(0 << COM20)); /* Nothing Toggle on compare match */
OCR2 = 180; // ðåãèñòð ïîðîãà ñðàâíåíèÿ-çàäàåì âåðøèíó ñ÷åòà ñ÷åò÷èêà TC2 (setup output compare unit)
TIMSK |= (1 << OCIE2); //Ðàçðåøåíèå ïðåðûâàíèÿ ïî ðåçóëüòàòó ñðàâíåíèÿ òàéìåðà-ñ÷åò÷èêà TC2
/*****************************************************************************
System timer (TC0) configuration !!! Âàæíî !!! ñ ôàéëà Sys.TIME.cfg Protoss
******************************************************************************/
//Ðàñ÷åò ñèñòåìíûõ òèêîâ â çàâèñèìîñòè îò ÷àñòîòû ñèñòåìíîãî ãåíåðàòîðà
/* TC0 compare value for create periodic interrupt 10 mc - 100Hz*/
//#define TC0_INCLUDE_32768_OSC 1 /* include external quartz 32.768 Hz */
//#if (1 == TC0_INCLUDE_32768_OSC)
// #define TC0_COMPARE_CONST 2 /* for external 32.768 osc
// 32768 / 128(prescaler) = 256;
// (14400 - 1)/143 = 100 tick per secs */
//#else
// #define TC0_COMPARE_CONST 143 /* for CPU_CLOCK = 14.745600 hz
// CPU_CLOCK / 1024(prescaler) = 14400;
// (14400 - 1)/143 = 100 tick per secs */
//#endif /* #if (1 == TC0_INCLUDE_32768_QRZ) */
// 143 /* for CPU_CLOCK = 18.432000 hz
// CPU_CLOCK / 1024(prescaler) = 18000;
// (18000 - 1)/180 = 100 tick per secs */
}
void mig_mig()
{
if ((PIND & (1 << PD6))==0) // Åñëè ñâåòîäèîä ïîòóøåí
{
if (pause > 180)
{
PORTD |= (1 << PD6); // Ëîã. 1 íà âûâîäå PD6 - çàæèãàåì ñâåòîäèîä
pause = 0;
}
}
else // Åñëè ñâåòîäèîä ãîðèò
{
if (pause > 20)
{
PORTD &= ~(1 << PD6); // Ëîã. 0 íà âûâîäå PD6 - òóøèì ñâåòîäèîä
pause = 0;
}
}
}
void read_parametr()
{
if (read > 90)
{
rtc_gettime(&rtc);
LM75_Read ();
adc_read1 = ((ADCvalue1*2.56*(5/2.56))/1023)*10;
adc_read2 = ((ADCvalue2*2.56*(5/2.56))/1023)*10;
adc_read1_1 = adc_read1 / 10;
adc_read1_2 = adc_read1 % 10;
adc_read2_1 = adc_read2 / 10;
adc_read2_2 = adc_read2 % 10;
read = 0;
}
}
void EXMEM_example ()
{
uint16_t index;
PRINTF("\nExternal Memory Example\n");
// Allocate external memory
mem = malloc(BUFFER_SIZE);
PRINTF("%d Byte buffer (starting at 0x%04X) filled with 0's:\n",BUFFER_SIZE,mem);
// Fill memory with 0's
for(index = 0; index < BUFFER_SIZE; index++)
{
mem[index] = 0x00;
}
for(index = 0; index < BUFFER_SIZE; index++)
{
PRINTF("%02X ",mem[index]);
if((index&0x0F) == 0x0F)
{
PRINTF("\n");
}
}
PRINTF("\n%d Byte buffer (starting at 0x%04X) filled with incrementing numbers:\n",
BUFFER_SIZE, mem);
// Fill memory with incrementing numbers
for(index = 0; index < BUFFER_SIZE; index++)
{
mem[index] = index;
}
for(index = 0; index < BUFFER_SIZE; index++)
{
PRINTF("%02X ", mem[index]);
if((index&0x0F) == 0x0F)
{
PRINTF("\n");
}
}
PRINTF("\n");
}
/* buzzer(frequency, duration);
Sound a tone, this time without interrupts. CPU intensive!
call arguments: frequency is in Hz, usable range 100-10,000 Hz, duration is in milliseconds
This routine uses _delay_loop_2() to generate timing (4 cpu cycles per loop)
For correct operation, be sure to define F_CPU in Hz! */
void buzzer(unsigned int frequency, unsigned int duration)
{
unsigned int wait_units, loops, i;
// DDRG |= (1<<DDG4); //make PG4 output
wait_units = F_CPU/(8UL * (long int) frequency); //half of the tone period, in delay loop units
loops = duration*F_CPU/(8000UL*wait_units); //number of loops to execute
for (i=1; i<=loops; i++) { //sound the buzzer
PORTG |= (1<<PG3); //on
_delay_loop_2(wait_units); //wait 1/2 tone period
PORTG &= ~(1<<PG3); //off
_delay_loop_2(wait_units); //wait 1/2 tone period
}
}
//#define NULL_PIXEL 2
#define SCREEN_WIDTH 190
int string_width (char s[])
{
int i;
i = 0;
while (s[i] != '\0')
++i;
return i*6;
}
void scrolling(char *Buffer)
{
// char tmp[50]; //ìàññèâ äëÿ âðåìåííîãî õðàíåíèÿ âûâîäèìîé ñòðîêè
uint16_t bufferlen;
//uint16_t len;
// uint16_t pos = *ScrlPos;
//uint16_t pos = pos_in_string;
// uint8_t index = 0, offset = 0;
bufferlen = string_width(Buffer); // äëèííà ïîïèêñåëüíî ñòðîêè èñòî÷íèêà
// char tmp[bufferlen];
// tmp[0] = 0;
// if(bufferlen > pos) //åñëè ïîïèêñåëüíàÿ äëèííà ñòðîêè áîëüøå ïîçèöèîííîãî íîìåðà
// {
// do
// {
// tmp[offset+1] = Buffer[offset]; //ïåðåïèñûâàåì ñòðîêó â ìàññèâ äëÿ âðåìåííîãî õðàíåíèÿ âûâîäèìîé ñòðîêè
// offset++;
// }while((string_width(tmp) - 1) < pos); // ïîêà ïîïèêñåëüíàÿ äëèííà ìàññèâà âðåìåííîãî õðàíåíèÿ ñòðîêè ìåíüøå ïîçèöèîííîãî íîìåðà
// while(string_width(tmp) < pos) // ïîêà ïîïèêñåëüíàÿ äëèííà ìàññèâà âðåìåííîãî õðàíåíèÿ ñòðîêè ìåíüøå ïîçèöèîííîãî íîìåðà
// {
// tmp[offset] = Buffer[offset]; //ïåðåïèñûâàåì ñòðîêó â ìàññèâ äëÿ âðåìåííîãî õðàíåíèÿ âûâîäèìîé ñòðîêè
// offset++;
// }
// offset--;
// tmp[offset] = 0;
// pos = pos_in_string - string_width(tmp);
// memset(tmp, 0, sizeof(tmp));
// while(string_width(tmp) < (SCREEN_WIDTH + pos))
// {
// if((index + offset) >= strlen(Buffer))
// break;
// tmp[index] = Buffer[index + offset];
// index++;
// }
//tmp[index] = 0;
// }
// else
// {
// pos -= bufferlen;
// pos = pos - bufferlen;
// memset(tmp, 0, sizeof(tmp));
// }
// len = string_width(tmp); // äëèííà ïîïèêñåëüíî ñòðîêè âðåìåííîãî õðàíåíèÿ
// if(len < (SCREEN_WIDTH + pos))
// {
// tmp[index++] = (char)'-';
// tmp[index++] = (char)'-';
// tmp[index++] = (char)'-';
// tmp[index] = 0;
// len = string_width(tmp, NULL_PIXEL);
// len = string_width(tmp);
// if(len < (SCREEN_WIDTH + pos))
// offset = 0;
// while(string_width(tmp) < (SCREEN_WIDTH + pos))
// {
// tmp[index] = Buffer[offset];
// index++;
// offset++;
// }
// tmp[index] = 0;
// }
// string_print(tmp, pos);
// gdSetXY(pos, 2);
gdSetXY(pos_in_string, 2);
// gdSetXY(2, 2);
// gdWriteString(tmp);
gdWriteString(Buffer);
// gdSetXY(60,10);
// gdWriteString(mkNumString(bufferlen, 3, ' ', 10));
// gdSetXY(80,10);
// gdWriteString(mkNumString(offset, 3, ' ', 10));
// gdSetXY(100,10);
// gdWriteString(mkNumString(pos, 3, ' ', 10));
// gdSetXY(80,18);
// gdWriteString(mkNumString(index, 3, ' ', 10));
// gdSetXY(100,18);
// gdWriteString(mkNumString(pos_in_string, 3, ' ', 10));
// *(ScrlPos)++;
pos_in_string++;
// if(*ScrlPos > (bufferlen + string_width(" - ")))
// if(pos_in_string > (bufferlen + string_width(" - ")))
if(pos_in_string > bufferlen+36)
// *ScrlPos = 0;
pos_in_string = 0;
}
int main( void )
{
/* Initialize UART library, pass baudrate and AVR cpu clock
* with the macro UART_BAUD_SELECT() (normal speed mode )
* or UART_BAUD_SELECT_DOUBLE_SPEED() ( double speed mode)*/
uart1_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); //Peter Fleury
// Route stdout stream to printf_stream
stdout = &printf_stream; //èñõîäíûé êîä
ADC_init();
Timer_init(); //initialize system timer
TWI_MasterInit(400); //Èíèöèàëèçèðóåì ìîäóëü TWI ñ ÷àñòîòîé îáìåíà 100 êÃö
SPI_Init();
Dataflash_Init();
sei();
lcd_init(); //Èíèöèàëèçèðóåì äèñïëåé
DDRE &= ~(1<<DDE4)|(1<<DDE5)|(1<<DDE6)|(1<<DDE7); //setup on PORTE (Enable as input for buttons)
PORTE |= (1<<PE4)|(1<<PE5)|(1<<PE6)|(1<<PE7); //setup on PORTE (Enable pull UPs for buttons)
DDRD |= (1<<DDD6)|(1<<DDD7); //setup on PORTD (Enable as output for Heart Beat & Info LEDs)
DDRF |= (1<<DDF4)|(1<<DDF5)|(1<<DDF6)|(1<<DDF7); //setup on PORTF (Enable as output for LEDs)
// PORTF = DDRF;
PORTG &= ~(1<<PG3); //setup on PORTG (Disable pull UPs)
DDRG |= (1<<DDG3); //setup on PORTG (Enable as output for Buzzer)
//TUI Start
char s[16];
EVENT ev = EV_NONE;
// uint8_t mode=1;
uint8_t menu_r;
//TUI Stop
//uint16_t pos_in_string; //áåãóùàÿ ñòðîêà
//uint8_t pos_in_string; //áåãóùàÿ ñòðîêà
disp_clear();
// uint8_t logo [1024];
// p_b_write = &logo[0x3C0];
p_b_read = &EEPROM_AT24_buff_read[0];
//p_b_4 = &logo[0];
// for(uint8_t i=0; i < 16; i++)
// {
// EEReadArray(64, i*64, EEPROM_AT24_buff_read[i*64]);
// // EEReadArray(64, i*64, p_b_read + i * 64);
//
// _delay_ms(200);
// }
gdWriteIcon(addidas_logo);
// gdWriteIcon_EEPROM(logo);
// gdWriteIcon_EEPROM(EEPROM_AT24_buff_read);
// gdWriteIcon_EEPROM(logo);
disp_send_frame();
_delay_ms(5000);
disp_clear();
buzzer(200,1000); //beep @ 200 Hz for 1 second
p_b = &tm_manag[0];
//read_FM25640(0, 0);
read_TM_manag();
p_b = &tm_oper[0];
//read_FM25640(0, 10);
read_TM_oper();
if (rtc_gettime(&rtc)) //Initialize RTC
{
RtcOk = 1;
PRINTF("\n \nCurrent time: %u.%u.%u %02u:%02u:%02u\r\n", rtc.mday, rtc.month, rtc.year, rtc.hour, rtc.min, rtc.sec);
}
else
{
PRINTF("RTC is not available.\n");
}
// first_init_DS1307 (); // Ðàññêîìåíòèðîâàòü â ñëó÷àå îñòàíîâêè ÷àñîâ!!!
// PRINTF("\n%d Byte buffer (starting at 0x%04X) filled with incrementing numbers:\n",
// BUFFER_SIZE, massiv);
/* initialize file system */
FRESULT res = f_mount(&fs, "", 0);
PRINTF("f_mount return: ");
f_PrintError(res);
unsigned long cls = 0;
DWORD fre_sect, tot_sect;
res = f_getfree("", &cls, &p_fs);
PRINTF("f_getfree return: ");
f_PrintError(res);
if(FR_NO_FILESYSTEM == res)
{
res = f_mkfs(0, 0, 4 * DISK_SEC_SIZE);
PRINTF("f_mkfs return: ");
f_PrintError(res);
res = f_getfree("", &cls, &p_fs);
PRINTF("f_getfree return: ");
f_PrintError(res);
}
if(res)
{
PRINTF("main: File System inited failed!\r\n");
return FALSE;
}