-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.c
139 lines (118 loc) · 3.99 KB
/
project.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
#include <avr/io.h>
#include <inttypes.h>
#include <stdlib.h>
#include <util/delay.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "signal_gen.h"
#include "freq_sweep.h"
#include "adc.h"
#include "lcd.h"
#include "debounce.h"
#define BUFFER 64
void write_to_lcd_from_program_space_string(uint8_t index);
void print_updated_freq_and_units(uint8_t ret, uint64_t freq);
uint8_t freq_step_down(uint64_t freq_value);
uint64_t current_freq_value;
const char msg_vswr_value[] PROGMEM = "VSWR:";
const char msg_vswr_analyser[] PROGMEM = "VSWR annalyser";
const char msg_vswr_unit[] PROGMEM = " Volt";
const char msg_vswr_freq[] PROGMEM = "DDS Freq:";
const char msg_dds_freq_Khz[] PROGMEM = " Khz";
const char msg_dds_freq_Mhz[] PROGMEM = " Mhz";
const char msg_unkown_error[] PROGMEM = "Unkown Error :-(";
const PGM_P const messages[] PROGMEM = {
msg_vswr_value,
msg_vswr_analyser,
msg_vswr_unit,
msg_vswr_freq,
msg_dds_freq_Khz,
msg_dds_freq_Mhz,
msg_unkown_error
};
int main(void){
float vswr_val = 0;
char tmp[BUFFER];
uint64_t initial_freq_value = 27e6;
uint8_t button_pressed;
current_freq_value = initial_freq_value;
lcd_init(LCD_DISP_ON);
lcd_clrscr();
adc_setup();
ad9850_setup();
timer_intr_setup();
freq_send(initial_freq_value);
//Write "VSWR annalyser" to LCD correct position
lcd_gotoxy(3, 0);
write_to_lcd_from_program_space_string(1);
_delay_ms(100);
//Write "VSWR:" to LCD correct position
lcd_gotoxy(0, 1);
write_to_lcd_from_program_space_string(0);
_delay_ms(100);
//Write " Volt" to LCD correct position
lcd_gotoxy(14, 1);
write_to_lcd_from_program_space_string(2);
_delay_ms(100);
//Write "DDS Freq" to LCD correct position
lcd_gotoxy(0, 2);
write_to_lcd_from_program_space_string(3);
_delay_ms(100);
adc_read(&vswr_val);
dtostrf(vswr_val, 5, 2, tmp);
lcd_gotoxy(5, 1);
lcd_puts(tmp);
print_updated_freq_and_units(freq_step_down(current_freq_value), current_freq_value);
for( ; ;){
button_pressed = get_key_press(0x0c);
if( button_pressed & 0x04 )
sweep_sta = SWEEP_STA_UP;
else if( (button_pressed & 0x08) )
sweep_sta = SWEEP_STA_DOWN;
if( sweep_sta != SWEEP_STA_OFF ){
freq_sweep();
print_updated_freq_and_units(freq_step_down(current_freq_value), current_freq_value);
adc_read(&vswr_val);
dtostrf(vswr_val, 5, 2, tmp);
lcd_gotoxy(5, 1);
lcd_puts(tmp);
//_delay_ms(1000);
}
}
return 0;
}
/*Function to read strings from Program Space to Data Space*/
void write_to_lcd_from_program_space_string(uint8_t index){
char buffer_str[BUFFER];
strcpy_P(buffer_str, (PGM_P) pgm_read_word(&(messages[index])));
lcd_puts(buffer_str);
}
/*
*
*/
uint8_t freq_step_down(uint64_t freq_value){
if(freq_value >= 1 && freq_value <= 9e5)
return 0; // freq / 1e3 - Khz
if(freq_value > 9e5 && freq_value <= 39e6)
return 1; // freq / 1e6 - Mhz
return -1;
}
void print_updated_freq_and_units(uint8_t ret, uint64_t freq){
char tmp[BUFFER];
switch(ret){
case 0: dtostrf(freq / 1e3, 5, 1, tmp); //Khz divider
lcd_gotoxy(9, 2);
lcd_puts(tmp);
lcd_gotoxy(14, 2);
write_to_lcd_from_program_space_string(4);
break;
case 1: dtostrf(freq / 1e6, 5, 2, tmp); //Mhz divider
lcd_gotoxy(9, 2);
lcd_puts(tmp);
lcd_gotoxy(14, 2);
write_to_lcd_from_program_space_string(5);
break;
default: lcd_clrscr(); //Unnkown Error
write_to_lcd_from_program_space_string(6);
}
}