-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.jal
224 lines (161 loc) · 5 KB
/
main.jal
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
include 18f2553
-- Compiler directives
pragma target CLOCK 48_000_000 -- CPU frequency
--
pragma target OSC XT_PLL -- XT crystal or resonator
-- and using PLL
pragma target PLLDIV P1 -- reduce OSC 4/1 = 4 MHz for PLL input
pragma target CPUDIV P1 -- CPU freq. from PLL(96)/2 -> 48 MHz
-- (without PLL: CPU freq. = OSC freq.)
pragma target USBPLL F48MHZ -- USB clock from PLL module
pragma target FCMEN DISABLED -- no fail-safe clock monitoring
pragma target IESO DISABLED -- no int/ext clock switchover
pragma target BROWNOUT DISABLED -- no brownout detection
pragma target VREGEN ENABLED -- enable USB voltage regulator
-- (when no external regulator at Vusb)
pragma target WDT DISABLED -- no watchdog
pragma target XINST DISABLED -- not supported by JalV2
pragma target DEBUG DISABLED -- no debugging
pragma target LVP DISABLED -- no Low Voltage Programming
pragma target MCLR EXTERNAL -- reset externally
procedure crlf(volatile byte out device) is
pragma inline
device = 13
device = 10
end procedure
include print
include format
include usb_serial
include clock
include heater
include zerocross
include fx8
include pid
const byte cr = 13
const byte lf = 10
const byte bs = 8
var byte input_buffer[40]
OSCCON_SCS = 0b00 -- select primary clock source
enable_digital_io() -- make all pins digital I/O
pin_A0_direction = output
alias led is pin_A0
led = on
usb_serial_init()
procedure clear_buffer is
var byte bufpos
for count(input_buffer) using bufpos loop
input_buffer[bufpos] = " "
end loop
end procedure
procedure print_buf(volatile byte out device) is
var byte idx
crlf(device)
device = ">"
for count(input_buffer) using idx loop
device = input_buffer[idx]
end loop
device = "<"
crlf(device)
end procedure
const byte cmd_time[] = "time"
const byte cmd_demo[] = "demo"
function strcmp(byte in short[], byte in long[]) return bit is
var byte idx = 0
while idx < count(short) loop
if short[idx] != long[idx] then
return false
end if
idx = idx + 1
end loop
return true
end function
procedure print_demo(volatile byte out device) is
var sbyte*FXWIDTH initial
var byte idx
initial = word_to_fx(500000000)
crlf(device)
for 18 using idx loop
format_fx8(device, initial)
crlf(device)
initial = div_fx(initial, word_to_fx(10))
end loop
end procedure
procedure process_cli is
if strcmp(cmd_time, input_buffer) then
print_time()
end if
if strcmp(cmd_demo, input_buffer) then
print_demo(usb_serial_data)
end if
end procedure
clear_buffer()
var byte bufpos = 0
var byte ch
procedure print_status(volatile byte out device) is
print_time()
format_fx8(device, output_value)
format_fx8(device, now() )
format_fx8(device, last_error_p)
end procedure
const timer0_isr_rate = 1000
const DELAY_SLOTS = 2
include timer0_isr_interval
set_delay(0, 1000)
set_delay(1, 10000)
timer0_isr_init()
ac_cycle_isr_init()
var sbyte*FXWIDTH new_setpoint
forever loop
usb_serial_flush()
if( usb_cdc_line_status() != 0x00 ) then
-- port has been opened by host app
led = on
if usb_serial_read( ch ) then
if ch == bs then
-- backspace
if bufpos > 0 then
bufpos = bufpos - 1
end if
input_buffer[bufpos] = " "
usb_serial_data = bs
usb_serial_data = " "
usb_serial_data = bs
elsif ch == cr | ch == lf then
-- enter
process_cli()
clear_buffer()
bufpos = 0
crlf(usb_serial_data)
else
-- self-insert
if bufpos < (count(input_buffer)) then
input_buffer[bufpos] = ch
bufpos = bufpos + 1
usb_serial_data = ch
end if
end if
end if
else
-- port is closed.
led = off
end if
if check_delay(1) then -- 10s interrupt for status
set_delay(1, 10000)
if usb_cdc_line_status() != 0x00 then
print_status(usb_serial_data)
crlf(usb_serial_data)
end if
end if
if check_delay(0) then-- 1hz interrupt for PID
set_delay(0, 1000)
new_setpoint = word_to_fx(100)
update_pid(usb_serial_data, new_setpoint) -- sets output_value
if output_value > fx_one then
output_value = fx_one
end if
if output_value < 0 then
output_value = 0
end if
heater_setting = byte*2(mult_fx(output_value, 0xffff) >> FXFRACBITS)
end if
end loop