-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.c
422 lines (354 loc) · 11.7 KB
/
Application.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
/*
* File: Application.c
* Author: Kareem Taha Abdelfatah Mohammed
* https://www.linkedin.com/in/kareem-taha-ba451621a/
* Created on September 13, 2022, 9:10 PM
*/
#include "Application.h"
#include "MCAL_Layer/ADC/hal_adc.h"
STD_ReturnType ret = E_OK;
/*
ch_lcd_4bit_t lcd_1 = {
.lcd_rs.port = PORTC_INDEX,
.lcd_rs.pin = GPIO_PIN0,
.lcd_rs.logic = GPIO_LOW,
.lcd_rs.direction = GPIO_OUTPUT_DIRECTION,
.lcd_en.port = PORTC_INDEX,
.lcd_en.pin = GPIO_PIN1,
.lcd_en.logic = GPIO_LOW,
.lcd_en.direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_4_bit[0].port = PORTC_INDEX,
.lcd_data_4_bit[0].pin = GPIO_PIN2,
.lcd_data_4_bit[0].logic = GPIO_LOW,
.lcd_data_4_bit[0].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_4_bit[1].port = PORTC_INDEX,
.lcd_data_4_bit[1].pin = GPIO_PIN3,
.lcd_data_4_bit[1].logic = GPIO_LOW,
.lcd_data_4_bit[1].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_4_bit[2].port = PORTC_INDEX,
.lcd_data_4_bit[2].pin = GPIO_PIN4,
.lcd_data_4_bit[2].logic = GPIO_LOW,
.lcd_data_4_bit[2].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_4_bit[3].port = PORTC_INDEX,
.lcd_data_4_bit[3].pin = GPIO_PIN5,
.lcd_data_4_bit[3].logic = GPIO_LOW,
.lcd_data_4_bit[3].direction = GPIO_OUTPUT_DIRECTION
};
ch_lcd_8bit_t lcd_2 = {
.lcd_rs.port = PORTC_INDEX,
.lcd_rs.pin = GPIO_PIN6,
.lcd_rs.logic = GPIO_LOW,
.lcd_rs.direction = GPIO_OUTPUT_DIRECTION,
.lcd_en.port = PORTC_INDEX,
.lcd_en.pin = GPIO_PIN7,
.lcd_en.logic = GPIO_LOW,
.lcd_en.direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[0].port = PORTD_INDEX,
.lcd_data_8_bit[0].pin = GPIO_PIN0,
.lcd_data_8_bit[0].logic = GPIO_LOW,
.lcd_data_8_bit[0].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[1].port = PORTD_INDEX,
.lcd_data_8_bit[1].pin = GPIO_PIN1,
.lcd_data_8_bit[1].logic = GPIO_LOW,
.lcd_data_8_bit[1].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[2].port = PORTD_INDEX,
.lcd_data_8_bit[2].pin = GPIO_PIN2,
.lcd_data_8_bit[2].logic = GPIO_LOW,
.lcd_data_8_bit[2].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[3].port = PORTD_INDEX,
.lcd_data_8_bit[3].pin = GPIO_PIN3,
.lcd_data_8_bit[3].logic = GPIO_LOW,
.lcd_data_8_bit[3].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[4].port = PORTD_INDEX,
.lcd_data_8_bit[4].pin = GPIO_PIN4,
.lcd_data_8_bit[4].logic = GPIO_LOW,
.lcd_data_8_bit[4].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[5].port = PORTD_INDEX,
.lcd_data_8_bit[5].pin = GPIO_PIN5,
.lcd_data_8_bit[5].logic = GPIO_LOW,
.lcd_data_8_bit[5].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[6].port = PORTD_INDEX,
.lcd_data_8_bit[6].pin = GPIO_PIN6,
.lcd_data_8_bit[6].logic = GPIO_LOW,
.lcd_data_8_bit[6].direction = GPIO_OUTPUT_DIRECTION,
.lcd_data_8_bit[7].port = PORTD_INDEX,
.lcd_data_8_bit[7].pin = GPIO_PIN7,
.lcd_data_8_bit[7].logic = GPIO_LOW,
.lcd_data_8_bit[7].direction = GPIO_OUTPUT_DIRECTION
};
keypad_t kaypad_1 = {
.keypad_row_pins[0].pin = GPIO_PIN0,
.keypad_row_pins[1].pin = GPIO_PIN1,
.keypad_row_pins[2].pin = GPIO_PIN2,
.keypad_row_pins[3].pin = GPIO_PIN3,
.keypad_row_pins[0].port = PORTD_INDEX,
.keypad_row_pins[1].port = PORTD_INDEX,
.keypad_row_pins[2].port = PORTD_INDEX,
.keypad_row_pins[3].port = PORTD_INDEX,
.keypad_row_pins[0].logic = GPIO_LOW,
.keypad_row_pins[1].logic = GPIO_LOW,
.keypad_row_pins[2].logic = GPIO_LOW,
.keypad_row_pins[3].logic = GPIO_LOW,
.keypad_row_pins[0].direction = GPIO_OUTPUT_DIRECTION,
.keypad_row_pins[1].direction = GPIO_OUTPUT_DIRECTION,
.keypad_row_pins[2].direction = GPIO_OUTPUT_DIRECTION,
.keypad_row_pins[3].direction = GPIO_OUTPUT_DIRECTION,
.keypad_colomns_pins[0].pin = GPIO_PIN4,
.keypad_colomns_pins[1].pin = GPIO_PIN5,
.keypad_colomns_pins[2].pin = GPIO_PIN6,
.keypad_colomns_pins[3].pin = GPIO_PIN7,
.keypad_colomns_pins[0].port = PORTD_INDEX,
.keypad_colomns_pins[1].port = PORTD_INDEX,
.keypad_colomns_pins[2].port = PORTD_INDEX,
.keypad_colomns_pins[3].port = PORTD_INDEX,
.keypad_colomns_pins[0].logic = GPIO_LOW,
.keypad_colomns_pins[1].logic = GPIO_LOW,
.keypad_colomns_pins[2].logic = GPIO_LOW,
.keypad_colomns_pins[3].logic = GPIO_LOW,
.keypad_colomns_pins[0].direction = GPIO_INPUT_DIRECTION,
.keypad_colomns_pins[1].direction = GPIO_INPUT_DIRECTION,
.keypad_colomns_pins[2].direction = GPIO_INPUT_DIRECTION,
.keypad_colomns_pins[3].direction = GPIO_INPUT_DIRECTION,
};
void welcome_massege(void){
for(int i = 1; i <= 6; i++){
lcd_4_bit_send_string_pos(&lcd_1, 1, i, "Hello All");
lcd_4_bit_send_string_pos(&lcd_1, 2, 5, "!Calculator!");
_delay(250000);
lcd_4_bit_send_string_pos(&lcd_1, 1, i, " ");
}
lcd_4_bit_send_string_pos(&lcd_1, 1, 1, " ");
lcd_4_bit_send_string_pos(&lcd_1, 1, 6, "Hello All");
}
void loading(void){
lcd_4_bit_send_string_pos(&lcd_1, 1, 1, "Loading");
for(int i = 8; i <= 15; i++){
lcd_4_bit_send_char_data_pos(&lcd_1, 1, i, '.');
_delay(250000);
}
}*/
/*
led_t led1 = {
.led_status = LED_LOW,
.pin_number = GPIO_PIN0,
.port_name = PORTC_INDEX,
};
led_t led2 = {
.led_status = LED_LOW,
.pin_number = GPIO_PIN1,
.port_name = PORTC_INDEX,
};
led_t led3 = {
.led_status = LED_LOW,
.pin_number = GPIO_PIN2,
.port_name = PORTC_INDEX,
};
led_t led4 = {
.led_status = LED_LOW,
.pin_number = GPIO_PIN3,
.port_name = PORTC_INDEX,
};
void RB_int0_app_isr(void){
ret = led_toggle_logic(&led1);
}
void RB_int1_app_isr(void){
ret = led_toggle_logic(&led2);
}
void RB_int2_app_isr(void){
ret = led_toggle_logic(&led3);
}
void RB_int3_app_isr(void){
ret = led_toggle_logic(&led4);
}
Interrupt_RBx_t int1_obj = {
.Interrupt_Handler = RB_int0_app_isr,
.priority = INTERRUPT_Low_Priority,
.mcu_pin.direction = GPIO_INPUT_DIRECTION,
.mcu_pin.port = PORTB_INDEX,
.mcu_pin.logic = GPIO_LOW,
.mcu_pin.pin = GPIO_PIN4,
};
Interrupt_RBx_t int2_obj = {
.Interrupt_Handler = RB_int1_app_isr,
.priority = INTERRUPT_Low_Priority,
.mcu_pin.direction = GPIO_INPUT_DIRECTION,
.mcu_pin.port = PORTB_INDEX,
.mcu_pin.logic = GPIO_LOW,
.mcu_pin.pin = GPIO_PIN5,
};
Interrupt_RBx_t int3_obj = {
.Interrupt_Handler = RB_int2_app_isr,
.priority = INTERRUPT_Low_Priority,
.mcu_pin.direction = GPIO_INPUT_DIRECTION,
.mcu_pin.port = PORTB_INDEX,
.mcu_pin.logic = GPIO_LOW,
.mcu_pin.pin = GPIO_PIN6,
};
Interrupt_RBx_t int4_obj = {
.Interrupt_Handler = RB_int3_app_isr,
.priority = INTERRUPT_Low_Priority,
.mcu_pin.direction = GPIO_INPUT_DIRECTION,
.mcu_pin.port = PORTB_INDEX,
.mcu_pin.logic = GPIO_LOW,
.mcu_pin.pin = GPIO_PIN7,
};*/
/*
void int0_app_isr(void){
ret = led_toggle_logic(&led1);
}
void int1_app_isr(void){
ret = led_toggle_logic(&led2);
}
void int2_app_isr(void){
ret = led_toggle_logic(&led3);
}
Interrupt_INTx_t int0_obj = {
.Interrupt_Handler = int0_app_isr,
.edge = INTERRUPT_RAISING_EDGE,
.priority = INTERRUPT_High_Priority,
.source = INTERRUPT_EXTERNAL_INT0,
.mcu_pin.direction = GPIO_INPUT_DIRECTION,
.mcu_pin.port = PORTB_INDEX,
.mcu_pin.logic = GPIO_LOW,
.mcu_pin.pin = GPIO_PIN0,
};
Interrupt_INTx_t int1_obj = {
.Interrupt_Handler = int1_app_isr,
.edge = INTERRUPT_RAISING_EDGE,
.priority = INTERRUPT_Low_Priority,
.source = INTERRUPT_EXTERNAL_INT1,
.mcu_pin.direction = GPIO_INPUT_DIRECTION,
.mcu_pin.port = PORTB_INDEX,
.mcu_pin.logic = GPIO_LOW,
.mcu_pin.pin = GPIO_PIN1,
};
Interrupt_INTx_t int2_obj = {
.Interrupt_Handler = int2_app_isr,
.edge = INTERRUPT_RAISING_EDGE,
.priority = INTERRUPT_High_Priority,
.source = INTERRUPT_EXTERNAL_INT2,
.mcu_pin.direction = GPIO_INPUT_DIRECTION,
.mcu_pin.port = PORTB_INDEX,
.mcu_pin.logic = GPIO_LOW,
.mcu_pin.pin = GPIO_PIN2,
};*/
//uint8 epprom_cnt = 0, value = 0;
pin_config_t pin_1 = {
.direction = GPIO_OUTPUT_DIRECTION,
.logic = GPIO_LOW,
.pin = GPIO_PIN3,
.port = PORTC_INDEX,
};
led_t led2 = {
.led_status = LED_LOW,
.pin_number = GPIO_PIN1,
.port_name = PORTC_INDEX,
};
led_t led3 = {
.led_status = LED_LOW,
.pin_number = GPIO_PIN2,
.port_name = PORTC_INDEX,
};
volatile uint32 counter_value = 0;
void timer0_defaultHandler(void){
counter_value++;
//led_toggle_logic(&led1);
//led_toggle_logic(&led2);
//led_toggle_logic(&led3);
};
void timer2_defaultHandler(void){
led_toggle_logic(&led2);
led_toggle_logic(&led3);
};
timer2_t timer2_obj = {
.TIMER2_InterruptHandler = NULL,
.timer2_postscaler = TIMER2_POSTSCALER_DIV_BY_1,
.timer2_prescaler = TIMER2_PRESCALER_DIV_BY_1,
.timer2_preload_value = 0,
};
timer0_t timer0_obj = {
.TIMER0_InterruptHandler = timer0_defaultHandler,
.timer0_mode = TIMER0_SOURCE_TIMER_MOD,
.timer0_reg_size = TIMER0_16_BIT,
.prescaler_en = TIMER0_PRESCLAER_ENABLE_CFG,
.prescaler_value = TIMER0_PRESCALER_DIV_BY_16,
.timer0_preload_value = 3036
};
void timer1_defaultHandler(void){
};
timer1_t timer1_obj = {
.TIMER1_InterruptHandler = timer1_defaultHandler,
.timer1_mode = TIMER1_SOURCE_TIMER_MOD,
.priority = INTERRUPT_Low_Priority,
.timer1_preload_value = 3036,
.timer1_prescaler = TIMER1_PRESCALER_DIV_BY_8,
.timer1_reg_rw_mode = TIMER1_RW_REG_MODE_16BIT,
};
void timer3_defaultHandler(void){
};
timer3_t timer3_obj = {
.TIMER3_InterruptHandler = timer3_defaultHandler,
.timer3_mode = TIMER3_SOURCE_TIMER_MOD,
.priority = INTERRUPT_Low_Priority,
.timer3_preload_value = 3036,
.timer3_prescaler = TIMER3_PRESCALER_DIV_BY_8,
.timer3_reg_rw_mode = TIMER3_RW_REG_MODE_16BIT,
};
timer1_t counter1_obj = {
.TIMER1_InterruptHandler = NULL,
.timer1_mode = TIMER1_SOURCE_COUNTER_MOD,
.timer1_preload_value = 0,
.timer1_prescaler = TIMER1_PRESCALER_DIV_BY_1,
.timer1_reg_rw_mode = TIMER1_RW_REG_MODE_16BIT,
.timer1_counter_mode = TIMER1_ASYNC_COUNTER_MOD,
};
ccp1_t ccp1_obj = {
.CCP1_InterruptHandler = NULL,
.PWM_Frequency = 20000,
.ccp1_mode = CCP1_PWM_MODE_,
.pin_ccp1.direction = GPIO_OUTPUT_DIRECTION,
.pin_ccp1.pin = GPIO_PIN2,
.pin_ccp1.port = PORTC_INDEX,
.timer2_postscaler = 1,
.timer2_prescaler = 1,
};
uint16 v_digital = 0;
adc_conf_t adc_obj = {
.ADC_InterruptHandler = NULL,
.acquicition_time = ADC_8_TAD,
.format_status = ADC_RESULT_RIGHT,
.onversion_clock = ADC_CONVERSION_CLOCK_FOSC_DIV_16,
.select_channel = ADC_CHANNEL_AN0,
.voltage_ref = ADC_VOLTAGE_REF_DISABLE,
};
uint8 is_conversion_done = 0;
int main() {
Application_initialize();
ret = ADC_Init(&adc_obj);
ret = ADC_Start_Conversion(&adc_obj);
ret = CCP1_Init(&ccp1_obj);
ret = timer2_init(&timer2_obj);
ret = CCP1_PWM_Set_Duty(100);
ret = CCP1_PWM_Start();
while(1){
ADC_Is_Conversion_done(&adc_obj, &is_conversion_done);
if(is_conversion_done == 1){
ADC_GetConversion_Status_Result(&adc_obj, &v_digital);
ADC_Start_Conversion(&adc_obj);
}
if(v_digital <= 41){
CCP1_PWM_Set_Duty(0);
}
else if(v_digital >= 60 && v_digital <= 80){
CCP1_PWM_Set_Duty(50);
}
else if(v_digital >= 90){
CCP1_PWM_Set_Duty(100);
}
}
return (EXIT_SUCCESS);
}
/* initialize any pin with direction & logic */
void Application_initialize(void){
gpio_pin_direction_init(&pin_1);
}