├── .cproject
├── .gitignore
├── .project
├── .settings
└── de.innot.avreclipse.core.prefs
├── EV1527_Decoder
├── EV17xx_Decoder.c
└── EV17xx_Decoder.h
├── LICENSE
├── README.md
├── Src
└── Main.c
├── StoreCode
├── eeprom_store.c
└── eeprom_store.h
├── Timer
├── Timer.c
└── Timer.h
└── Uart
├── uart.c
└── uart.h
/.cproject:
--------------------------------------------------------------------------------
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 |
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 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /Release/
2 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4CH_Learn_Remote
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder
10 | clean,full,incremental,
11 |
12 |
13 |
14 |
15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder
16 | full,incremental,
17 |
18 |
19 |
20 |
21 |
22 | org.eclipse.cdt.core.cnature
23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature
24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature
25 | de.innot.avreclipse.core.avrnature
26 |
27 |
28 |
--------------------------------------------------------------------------------
/.settings/de.innot.avreclipse.core.prefs:
--------------------------------------------------------------------------------
1 | avrtarget/ClockFrequency=16000000
2 | avrtarget/ExtRAMSize=0
3 | avrtarget/ExtendedRAM=false
4 | avrtarget/MCUType=atmega328p
5 | avrtarget/UseEEPROM=false
6 | avrtarget/UseExtendedRAMforHeap=true
7 | avrtarget/avrdude/BitBangDelay=
8 | avrtarget/avrdude/Bitclock=
9 | avrtarget/avrdude/EEPROMFile=
10 | avrtarget/avrdude/EEPROMFromConfig=true
11 | avrtarget/avrdude/FlashFile=
12 | avrtarget/avrdude/FlashFromConfig=true
13 | avrtarget/avrdude/NoChipErase=false
14 | avrtarget/avrdude/NoSigCheck=false
15 | avrtarget/avrdude/NoVerify=false
16 | avrtarget/avrdude/NoWrite=false
17 | avrtarget/avrdude/OtherOptions=
18 | avrtarget/avrdude/ProgrammerID=programmerconfig.2
19 | avrtarget/avrdude/UseCounter=false
20 | avrtarget/avrdude/WriteEEPROM=false
21 | avrtarget/avrdude/WriteFlash=true
22 | avrtarget/perConfig=false
23 | eclipse.preferences.version=1
24 |
--------------------------------------------------------------------------------
/EV1527_Decoder/EV17xx_Decoder.c:
--------------------------------------------------------------------------------
1 | /*
2 | Created on: May 9, 2017
3 | Copyright (C) Mohammad Mazarei Zeus @ Sisoog.com
4 | Email : mohammad.mazarei@gmail.com
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include "EV17xx_Decoder.h"
21 | #include
22 | #include
23 | #include "avr/io.h"
24 | #include "Timer.h"
25 |
26 |
27 |
28 | #define IS_Sync_Start_Pulse(T1,T2) (T2 > (T1*29) && T2 < (T1*32))
29 | #define Bit_IS_Zero(T1,T2) (T2 > (T1*2) && T2 < (T1*4))
30 | #define Bit_IS_One(T1,T2) (T1 > (T2*2) && T1 < (T2*4))
31 |
32 | volatile uint32_t EV_Code = 0;
33 | volatile uint8_t Revice_Flag = 0;
34 |
35 |
36 | const uint32_t Bit_Shift[32] PROGMEM =
37 | {
38 | 0x00000001,0x00000002,0x00000004,0x00000008,
39 | 0x00000010,0x00000020,0x00000040,0x00000080,
40 | 0x00000100,0x00000200,0x00000400,0x00000800,
41 | 0x00001000,0x00002000,0x00004000,0x00008000,
42 | 0x00010000,0x00020000,0x00040000,0x00080000,
43 | 0x00100000,0x00200000,0x00400000,0x00800000,
44 | 0x01000000,0x02000000,0x04000000,0x08000000,
45 | 0x10000000,0x20000000,0x40000000,0x80000000,
46 | };
47 |
48 | ISR(INT0_vect) // interrupt Of Micro
49 | {
50 | static uint16_t Time_Falling=0;
51 | static uint16_t Time_Rising=0;
52 | static uint16_t Start_Sync = 0;
53 | static uint8_t Bit_Index = 0;
54 | static uint32_t Receive_Code = 0;
55 |
56 | if(PIND&(1<<2)) // Rising Edge
57 | {
58 |
59 | Time_Falling =Stop_Read_timer();
60 |
61 |
62 | if(IS_Sync_Start_Pulse(Time_Rising,Time_Falling)) /* Start Sync*/
63 | {
64 | Start_Sync = 1;
65 | Bit_Index = 0;
66 | Receive_Code = 0;
67 | }
68 | else
69 | {
70 | if(Start_Sync==1) // Start Sended
71 | {
72 | if(Bit_Index < 23)
73 | {
74 | if(Bit_IS_Zero(Time_Rising,Time_Falling))
75 | {
76 | Bit_Index++;
77 | }
78 | else if(Bit_IS_One(Time_Rising,Time_Falling))
79 | {
80 | Receive_Code |= pgm_read_dword(&Bit_Shift[(23-Bit_Index)]);
81 | Bit_Index++;
82 | }
83 | else
84 | {
85 | Start_Sync = 0;
86 | Bit_Index = 0;
87 | }
88 |
89 | }
90 | else
91 | {
92 | if(Bit_IS_Zero(Time_Rising,Time_Falling))
93 | {
94 | Bit_Index++;
95 | }
96 | else if(Bit_IS_One(Time_Rising,Time_Falling))
97 | {
98 | Receive_Code |= pgm_read_dword(&Bit_Shift[(23-Bit_Index)]);
99 | Bit_Index++;
100 | }
101 | else
102 | {
103 | Start_Sync = 0;
104 | Bit_Index = 0;
105 | }
106 |
107 | if(Start_Sync)
108 | {
109 | // All Bit Recive
110 | Bit_Index = 0;
111 | Start_Sync = 0;
112 | EV_Code = Receive_Code;
113 | Revice_Flag = 1;
114 | }
115 | }
116 | } // End of Start Sync Send
117 | }
118 |
119 | Start_Timer();
120 | }
121 | else //Falling Edge
122 | {
123 |
124 | Time_Rising = Stop_Read_timer() ;
125 | Start_Timer();
126 | }
127 | }
128 |
129 |
130 | uint8_t IS_Recive_Valid_Remote(void)
131 | {
132 | return (Revice_Flag==1);
133 | }
134 |
135 | void Get_Remote_Code(uint8_t *Code)
136 | {
137 | Code[0] = EV_Code&0xFF;
138 | Code[1] = (EV_Code>>8)&0xFF;
139 | Code[2] = (EV_Code>>16)&0xFF;
140 | Revice_Flag = 0;
141 | }
142 |
143 | uint32_t Get_Remote_Code_long(void)
144 | {
145 |
146 | Revice_Flag = 0;
147 | return EV_Code;
148 | }
149 |
--------------------------------------------------------------------------------
/EV1527_Decoder/EV17xx_Decoder.h:
--------------------------------------------------------------------------------
1 | /*
2 | Created on: May 9, 2017
3 | Copyright (C) Mohammad Mazarei Zeus @ Sisoog.com
4 | Email : mohammad.mazarei@gmail.com
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #ifndef EV17XX_DECODER_H_
21 | #define EV17XX_DECODER_H_
22 | #include
23 |
24 | uint8_t IS_Recive_Valid_Remote(void);
25 | void Get_Remote_Code(uint8_t *Code);
26 | uint32_t Get_Remote_Code_long(void);
27 |
28 | #endif /* EV17XX_DECODER_H_ */
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 4Ch_learnRemote
2 | Make 4CH RF Remote With Chip EV1527
3 |
--------------------------------------------------------------------------------
/Src/Main.c:
--------------------------------------------------------------------------------
1 | /*
2 | Main.c
3 | Created on: May 9, 2017
4 | Copyright (C) Mohammad Mazarei Zeus @ Sisoog.com
5 | Email : mohammad.mazarei@gmail.com
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 |
28 | #define Key_Pin 3
29 | #define Key_PORT PORTD
30 | #define Key_DDR DDRD
31 | #define Key_PIN PIND
32 |
33 | #define LED_Pin 4
34 | #define LED_PORT PORTC
35 | #define LED_DDR DDRC
36 |
37 | #define CH0_Pin 0
38 | #define CH1_Pin 1
39 | #define CH2_Pin 2
40 | #define CH3_Pin 3
41 | #define CH_PORT PORTC
42 | #define CH_DDR DDRC
43 |
44 | #define IS_Press_Key() ((Key_PIN&_BV(Key_Pin))==0)
45 | #define LED_Tog() LED_PORT ^= _BV(LED_Pin)
46 | #define LED_On() LED_PORT |= _BV(LED_Pin)
47 |
48 | enum
49 | {
50 | Nurmal = 0,
51 | Learn,
52 | Erase,
53 | };
54 |
55 |
56 |
57 | int main(void)
58 | {
59 | uart_init();
60 | Key_DDR &=~_BV(Key_Pin); /*PORTD.3 As Input*/
61 | Key_PORT |=_BV(Key_Pin); /*PORTD.3 Enable Pullup*/
62 | LED_DDR |= _BV(LED_Pin);
63 | CH_DDR |= (_BV(CH0_Pin) | _BV(CH1_Pin) | _BV(CH2_Pin) | _BV(CH3_Pin));
64 |
65 |
66 | // External Interrupt(s) initialization
67 | // INT0: On
68 | // INT0 Mode: Any change
69 | EICRA=(0< 500)
130 | {
131 | Lcode = 0; /*Reset Last Code*/
132 | }
133 | }
134 |
135 | if(IS_Press_Key())
136 | {
137 | Press_Time++;
138 | }
139 | else
140 | {
141 | if(Press_Time>1000 && Press_Time<3000)
142 | {
143 | printf("Enter Learn Mode\n");
144 | Work_Mode = Learn;
145 | }
146 | else if(Press_Time>5000 && Press_Time<15000)
147 | {
148 | printf("Enter Erase Mode\n");
149 | LED_On();
150 | EE_Format();
151 | _delay_ms(3000);
152 | LED_Tog();
153 | }
154 | Press_Time = 0;
155 | }
156 |
157 | /*Blink Control LED*/
158 | if(Work_Mode==Nurmal)
159 | {
160 | if(LEDTime==0 || (SysTime-LEDTime) > 1000) /*Evry 1Sec*/
161 | {
162 | LEDTime = SysTime;
163 | LED_Tog();
164 | }
165 | }
166 | else if(Work_Mode==Learn)
167 | {
168 | if(LEDTime==0 || (SysTime-LEDTime) > 100) /*Evry 100 mili Sec*/
169 | {
170 | LEDTime = SysTime;
171 | LED_Tog();
172 | }
173 | }
174 |
175 | SysTime++;
176 | _delay_ms(1);
177 | }
178 | return 0;
179 | }
180 |
--------------------------------------------------------------------------------
/StoreCode/eeprom_store.c:
--------------------------------------------------------------------------------
1 | /*
2 | Created on: May 9, 2017
3 | Copyright (C) Mohammad Mazarei Zeus @ Sisoog.com
4 | Email : mohammad.mazarei@gmail.com
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 |
23 | uint32_t EEMEM g_u32eeprom[EE_Total_Rec];
24 | void EE_Format(void)
25 | {
26 | uint32_t Temp = 0xFFFFFFFF;
27 | for(int i=0;i.
18 | */
19 |
20 | #ifndef EEPROM_STORE_H_
21 | #define EEPROM_STORE_H_
22 | #include
23 | #include
24 | #include
25 |
26 | #define EE_Total_Rec 100
27 |
28 | void EE_Format(void);
29 | void EE_StoreCode(uint32_t Code);
30 | bool EE_ISValidCode(uint32_t Code);
31 |
32 | #endif /* EEPROM_STORE_H_ */
33 |
--------------------------------------------------------------------------------
/Timer/Timer.c:
--------------------------------------------------------------------------------
1 | /*
2 | Created on: May 9, 2017
3 | Copyright (C) Mohammad Mazarei Zeus @ Sisoog.com
4 | Email : mohammad.mazarei@gmail.com
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 |
24 |
25 |
26 | void Start_Timer(void)
27 | {
28 | // Timer/Counter 1 initialization
29 | // Clock source: System Clock
30 | // Clock value: 2000.000 kHz
31 | // Mode: Normal top=0xFFFF
32 | // OC1A output: Disconnected
33 | // OC1B output: Disconnected
34 | // Noise Canceler: Off
35 | // Input Capture on Falling Edge
36 | // Timer Period: 32.768 ms
37 | // Timer1 Overflow Interrupt: Off
38 | // Input Capture Interrupt: Off
39 | // Compare A Match Interrupt: Off
40 | // Compare B Match Interrupt: Off
41 | TCCR1A=(0<.
18 | */
19 |
20 |
21 | uint16_t Stop_Read_timer(void);
22 | uint16_t Read_timer(void);
23 | void Stop_Timer(void);
24 | void Start_Timer(void);
25 |
--------------------------------------------------------------------------------
/Uart/uart.c:
--------------------------------------------------------------------------------
1 | /*
2 | Created on: May 9, 2017
3 | Copyright (C) Mohammad Mazarei Zeus @ Sisoog.com
4 | Email : mohammad.mazarei@gmail.com
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 |
21 | #include
22 | #include
23 | #include
24 | #include "uart.h"
25 |
26 | #if defined (__AVR_ATmega328P__)
27 |
28 | #define CHIP_UCSRA UCSR0A
29 | #define CHIP_UCSRB UCSR0B
30 | #define CHIP_UBRRL UBRR0L
31 | #define CHIP_UDR UDR0
32 | #define CHIP_RXEN RXC0
33 | #define CHIP_TXEN TXEN0
34 | #define CHIP_UDRE UDRE0
35 | #define CHIP_DOR DOR0
36 | #define CHIP_FE FE0
37 | #define CHIP_RXC RXC0
38 |
39 | #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega32A__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega16A__)
40 |
41 | #define CHIP_UCSRA UCSRA
42 | #define CHIP_UCSRB UCSRB
43 | #define CHIP_UBRRL UBRRL
44 | #define CHIP_UDR UDR
45 | #define CHIP_RXEN RXC
46 | #define CHIP_TXEN TXEN
47 | #define CHIP_UDRE UDRE
48 | #define CHIP_DOR DOR
49 | #define CHIP_FE FE
50 | #define CHIP_RXC RXC
51 |
52 | #else
53 | #error "This Library Not Support This CPU"
54 | #endif
55 |
56 |
57 |
58 | FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
59 |
60 | void uart_init(void)
61 | {
62 | #if F_CPU < 2000000UL && defined(U2X)
63 | CHIP_UCSRA = _BV(U2X); /* improve baud rate error by using 2x clk */
64 | CHIP_UBRRL = (F_CPU / (8UL * UART_BAUD)) - 1;
65 | #else
66 | CHIP_UBRRL = (F_CPU / (16UL * UART_BAUD)) - 1;
67 | #endif
68 | CHIP_UCSRB = _BV(CHIP_TXEN) | _BV(CHIP_RXEN); /* tx/rx enable */
69 |
70 |
71 | stdout = stdin = &uart_str;
72 | }
73 |
74 | /*
75 | * Send character c down the UART Tx, wait until tx holding register
76 | * is empty.
77 | */
78 | int
79 | uart_putchar(char c, FILE *stream)
80 | {
81 |
82 | if (c == '\a')
83 | {
84 | return 0;
85 | }
86 |
87 | if (c == '\n')
88 | uart_putchar('\r', stream);
89 | loop_until_bit_is_set(CHIP_UCSRA, CHIP_UDRE);
90 | CHIP_UDR = c;
91 | return 0;
92 | }
93 |
94 | /*
95 | * Receive a character from the UART Rx.
96 | *
97 | * This features a simple line-editor that allows to delete and
98 | * re-edit the characters entered, until either CR or NL is entered.
99 | * Printable characters entered will be echoed using uart_putchar().
100 | *
101 | * Editing characters:
102 | *
103 | * . \b (BS) or \177 (DEL) delete the previous character
104 | * . ^u kills the entire input buffer
105 | * . ^w deletes the previous word
106 | * . ^r sends a CR, and then reprints the buffer
107 | * . \t will be replaced by a single space
108 | *
109 | * All other control characters will be ignored.
110 | *
111 | * The internal line buffer is RX_BUFSIZE (80) characters long, which
112 | * includes the terminating \n (but no terminating \0). If the buffer
113 | * is full (i. e., at RX_BUFSIZE-1 characters in order to keep space for
114 | * the trailing \n), any further input attempts will send a \a to
115 | * uart_putchar() (BEL character), although line editing is still
116 | * allowed.
117 | *
118 | * Input errors while talking to the UART will cause an immediate
119 | * return of -1 (error indication). Notably, this will be caused by a
120 | * framing error (e. g. serial line "break" condition), by an input
121 | * overrun, and by a parity error (if parity was enabled and automatic
122 | * parity recognition is supported by hardware).
123 | *
124 | * Successive calls to uart_getchar() will be satisfied from the
125 | * internal buffer until that buffer is emptied again.
126 | */
127 | int
128 | uart_getchar(FILE *stream)
129 | {
130 | uint8_t c;
131 |
132 |
133 | #ifdef USE_INLINE_EDITOR
134 | static char *rxp;
135 | char *cp, *cp2;
136 | static char b[RX_BUFSIZE];
137 |
138 | if (rxp == 0)
139 | for (cp = b;;)
140 | {
141 | loop_until_bit_is_set(UCSRA, RXC);
142 | if (UCSRA & _BV(FE))
143 | return _FDEV_EOF;
144 | if (UCSRA & _BV(DOR))
145 | return _FDEV_ERR;
146 | c = UDR;
147 |
148 |
149 | if (c == '\r')
150 | c = '\n';
151 | if (c == '\n')
152 | {
153 | *cp = c;
154 | uart_putchar(c, stream);
155 | rxp = b;
156 | break;
157 | }
158 | else if (c == '\t')
159 | c = ' ';
160 |
161 | if ((c >= (uint8_t)' ' && c <= (uint8_t)'\x7e') ||
162 | c >= (uint8_t)'\xa0')
163 | {
164 | if (cp == b + RX_BUFSIZE - 1)
165 | uart_putchar('\a', stream);
166 | else
167 | {
168 | *cp++ = c;
169 | uart_putchar(c, stream);
170 | }
171 | continue;
172 | }
173 |
174 | switch (c)
175 | {
176 | case 'c' & 0x1f:
177 | return -1;
178 |
179 | case '\b':
180 | case '\x7f':
181 | if (cp > b)
182 | {
183 | uart_putchar('\b', stream);
184 | uart_putchar(' ', stream);
185 | uart_putchar('\b', stream);
186 | cp--;
187 | }
188 | break;
189 |
190 | case 'r' & 0x1f:
191 | uart_putchar('\r', stream);
192 | for (cp2 = b; cp2 < cp; cp2++)
193 | uart_putchar(*cp2, stream);
194 | break;
195 |
196 | case 'u' & 0x1f:
197 | while (cp > b)
198 | {
199 | uart_putchar('\b', stream);
200 | uart_putchar(' ', stream);
201 | uart_putchar('\b', stream);
202 | cp--;
203 | }
204 | break;
205 |
206 | case 'w' & 0x1f:
207 | while (cp > b && cp[-1] != ' ')
208 | {
209 | uart_putchar('\b', stream);
210 | uart_putchar(' ', stream);
211 | uart_putchar('\b', stream);
212 | cp--;
213 | }
214 | break;
215 | }
216 | }
217 |
218 | c = *rxp++;
219 | if (c == '\n')
220 | rxp = 0;
221 |
222 | #else
223 | loop_until_bit_is_set(CHIP_UCSRA, CHIP_RXC);
224 | if (CHIP_UCSRA & _BV(CHIP_FE))
225 | return _FDEV_EOF;
226 | if (CHIP_UCSRA & _BV(CHIP_DOR))
227 | return _FDEV_ERR;
228 | c = CHIP_UDR;
229 | uart_putchar(c, stream);
230 | #endif
231 |
232 |
233 | return c;
234 | }
235 |
236 |
--------------------------------------------------------------------------------
/Uart/uart.h:
--------------------------------------------------------------------------------
1 | /*
2 | Created on: May 9, 2017
3 | Copyright (C) Mohammad Mazarei Zeus @ Sisoog.com
4 | Email : mohammad.mazarei@gmail.com
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 | #ifndef _UARTCONF
20 | #define _UARTCONF
21 | #include
22 |
23 | // Use uscanf and uprintf function
24 | #ifndef F_CPU
25 | #define F_CPU 4000000UL
26 | #endif
27 |
28 |
29 |
30 | /* UART baud rate */
31 | #define UART_BAUD 9600
32 |
33 | //#define USE_INLINE_EDITOR
34 | #define RX_BUFSIZE 10
35 |
36 |
37 |
38 | void uart_init(void);
39 | int uart_putchar(char c, FILE *stream);
40 | int uart_getchar(FILE *stream);
41 |
42 |
43 |
44 | extern FILE uart_str;
45 |
46 |
47 | // uprintf macro , fprintf with UART Stream
48 | #define uprintf(args...) fprintf(&uart_str,args)
49 | #define uscanf(args...) fscanf(&uart_str,args)
50 |
51 | #endif
52 |
--------------------------------------------------------------------------------