続16f1933 と 書籍 picマイコンのナンチャラ の内容 [jalv2_PIC]
picマイコンのナンチャラと言う書籍を買いました。
ADCについてよく読んでみるとVrefの設定に関して1.8v以上でないと精度が確保できないとか、
マニュアル類をよく読んでも電源電圧が1.8v以上で無いと とは読めたんですが 、
Vrefが差が1.8v以上というのが見つからなくて、著者さんにお聞きしました。
(業界では有名な方です)
お返事は3度戴いたんですが、ズバリの項目が書いて無くて、何だか蒟蒻問答のようになってしまいました。
(見ているマニュアルは DS41364D お返事の引き合いに出たのは DS33023A)
サポートサイトの正誤表を何度か見には入ったんですが、特には増えて無くて???です。
しかし 16f1933で Vrefを1.024vにすると 8bit 精度しか取れないそうですが、
なぜだか未だに判りません。
それが原因かは分かりませんが、第5章のダウンロードサービスはありません。
16f1933の最初の設定 [jalv2_PIC]
;クロックを内部8MHz にしてPLLX4 で32MHz にする
;var volatile byte OSCCON at 0b1111_0100
;OSCCON = 1111_01xx 内部32MHz
;OSCSTAT = 0b1110_1111 不要
;PWM出力はRCフィルターを通りDAC出力として使う、
;次にコンパレータPOSに入って測定電流(コンパレータNEG)に入って
;C1,2OUTからゲートドライバ遮断に使用される、
;同時にA,/A,B,/B と共に一定時間遮断を続け電流が減衰するのを待ち次のサイクルに備える。
;PWMの値は60進テーブル、64進テーブルから選択し、
;60進 1,2,3,4,5,6,10,12,15,20,30,60 と12(9)の選択が出来る。
; 0.0166 0.033 0.05 0.1 0.166 0,2 (0.25 0.5 1.0 64と共通これは省略出来る)
;64進 1,2,4,8,16,32,64 と7の選択が出来る(1つにテストモード自動歩進を入れる))
;0.0156 0.0312 0.0625 0.125 0.25 0.5 1.0
;60/64を分けないで一連の選択にすると9+7で4bitのSWが入り
;60/64に1bit使うと 1+4bitで選択できる。
;各ベクトルは実際には半分のテーブルを折り返し、90度ずらせてやると
;15+16で済んでしまう
;分割歩進テーブルは7と8が必要です。
;分割歩進に従ってベクトルを表すUP/DOWNカウンタが必要です。
;ベクトルに従って Aまたは/A Bまたは/B出力を切り替えます。
;基準電圧をADC(9)で読み込み、ベクトルテーブルに整数乗算四捨五入したものがPWM値となり
;コイル電流に反映されます。
;DAC出力はRA1と重なるので使えず、電源電圧(安定化)した所からVRで設定します。
;リセット後起動まで出力にはHが出されますが、ゲートドライバの遮断にも入るので問題有りません。
;IR2113もゲートドライバは本来ハイサイドローサイド型ですが許容電圧範囲に従って、
;ハイサイド側をローサイドに接続してローサイドドライバ2回路として使用します。
include 16f1933 ;(.jalは書いてはいけない) -- target PICmicro
--
-- This program assumes that a 32 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
-- (unspecified configuration bits may cause a different frequency!)
pragma target clock 20_000_000 -- oscillator frequency
-- configuration memory settings (fuses)
pragma target OSC INTOSC_NOCLKOUT -- intosc
pragma target PLLEN P4 -- PLL on
pragma target WDT disabled -- no watchdog
pragma target DEBUG disabled -- no debugging
pragma target LVP disabled -- no Low Voltage Programming
pragma target MCLR INTERNAL -- reset externally
--
-- Switch analog ports to digital mode (if analog module present).
procedure analog_set() is
pragma inline
ANSELA = 0b0001_1111 -- all analog
ANSELB = 0b0000_0111 -- all digital
end procedure
procedure adc_set() is ;CHS9 = 01001
pragma inline
ADCON0 = 0b0100_1011 -- enable ADC
ADCON1 = 0b0000_0000
end procedure
procedure comparator_on() is
pragma inline
CM1CON0 = 0b1110_0110 -- enable comparator
CM1CON1 = 0b0000_0000
CM2CON0 = 0b1110_0110 -- enable 2nd comparator
CM2CON1 = 0b0000_0001
end procedure
procedure set_digital_io() is
pragma inline
analog_set()
adc_set()
comparator_on()
end procedure
--
FVRCON = 0b1000_0010 ;ADC-9 で2.048vとして使う
pin_C12IN0_NEG_direction = input
pin_C12IN1_NEG_direction = input
pin_C2IN_POS_direction = input
pin_C1IN_POS_direction = input
pin_C1OUT_direction = output
pin_C2OUT_RA5_direction = output
pin_B0_direction = output
pin_B1_direction = output
pin_B2_direction = output
pin_AN9_direction = input
pin_B4_direction = output
pin_B5_direction = output
pin_B6_direction = output
pin_B7_direction = output
pin_P2A_RC1_direction = output
pin_CCP1_direction = output
pin_C3_direction = output
-- These configuration bit settings are only a selection, sufficient for
-- this program, but other programs may need more or different settings.
--
set_digital_io() -- make all pins digital I/O cansel
--
-- Specify the pin to which the LED is connected.
-- A low current (2 mA) led with 2.2K series resistor is recommended
-- since not all pins may be able to drive an ordinary 20mA led.
alias led is pin_C3
pin_C3_direction = output
--
forever loop -- Lチカ
led = on
_usec_delay(250_000)
led = off
_usec_delay(250_000)
end loop
--
;ccp2,3 は他の端子にも出来る?
;
;入出力
;rb0は int を選ぶ ステップの検出
;rb1は dir(方向) を選ぶ
;rb2は Zout(原点位相) を選ぶ
;rb4は /B を選ぶ
;rb5は B を選ぶ
;rb6は /A を選ぶ
;rb7は A を選ぶ
;
;rb4は adc AN9 ADCON0 = 001001S1 を選ぶ SUTATO ENABLE
;adc a11,13 は他のにも選べる
;
;FVRCON FVREN FVRRDY(1) - - CDAFVR1 CDAFVR0 ADFVR1 ADFVR0
;FVR_BUFFER1
;1.024V、2.048V または 4.096V から選ぶ
;
;空きpin
;ra6,7
;rc0,3,4,5,6
;re3 reset 内部で使う
マニュアルを見直し少し変更しました。
JALV2による表示試験 [jalv2_PIC]
mikroCのよる表示を JALV2で置き換えてみました。
位取りの変換ルーチンが動かず、数字は上の桁から下りてきまして、書き換えない所は残ってしまうので
毎回1行クリアを使っています。
位変更のため1/10,1/100をするにはmathルーチンが使えると思っていたら、
16bitしかサポートされていなかったのは、痛かった。
ジタバタしているうちに、レンジ表示とdword割り算の解決法が見つかりました。
上流はまだ解決していません。(変更点は次です)
var dword divs;
const byte msg1[] = " M K H";
const byte msg2[] = " M K ";
const byte msg3[] = "G M K ";
if (freq < 99999999) then
temp = freq;
lcd_cursor_position(1,0)
print_string(lcd, msg1)
elsif (freq < 999999999) then
divs = 10;
temp = freq / divs;
lcd_cursor_position(1,0)
print_string(lcd, msg2)
elsif (freq < 4290000000) then
divs = 100;
temp = freq / divs;
lcd_cursor_position(1,0)
print_string(lcd, msg3)
end if ;"12345678"
lcd_cursor_position(0,0)
lcd_clear_line(0)
print_dword_dec(lcd, temp)
JALV2のためのmikroC による変数表示関数 [jalv2_PIC]
こんばんは 文字列表示関数は「const byte str1[] = "XXX”」「print_string(lcd, str1)」
で出来るんですが、
少し前から調べていた解釈の範囲では変数列を表示するのに「print_byte_dec()」や「format_byte_dec」
「byte」は「word」「byte*3」「dword」等が使えて、
「print」の場合は整数 「format」の場合は 出力デバイス、データ、符号を含む長さ、少数以下を指定できるようです。まだ良く判っていないので、(小数を扱うにはレシプロカルが必須です。)
表示が整数の場合はオートレンジを mkroC で組んでみますと位取りが見やすくなりました。
モード切替で、プリスケーラ、リフレッシュレート等を設定して表示モードに切り換えれば、
使いやすくなります、特に設定値をEEPROMに入れなくても、一番使うモードをデフォルトにしておけば
凝ったプログラムを組まなくてもよいのが助かります。
mikroC の場合は変数表示関数が アンサインを指定していても、マイナスを表示するようです。
また、ただ2倍、1/2にカウントする内容しかないのにほとんどメモリーを使い切ってしまいそうでした。
(実際に作るにはJALV2でないとプログラムが入りきれないでしょう)
void main()
{
static char* msg;
static unsigned long freq, temp; // 0...4294967295
static unsigned char buf[20], cdir;
// アナログの設定
//ANSEL = 0b00000000; // 使用しない。
CMCON = 0b00000111;
// ポートの設定
TRISA = 0b11110000;
TRISB = 0b01000111;
OPTION_REG.F7 = 0; // PORTBをプルアップする。
// LCD(液晶モニタ)の初期化
Lcd_Custom_Config(&PORTA,0,1,2,3,&PORTB,4,5,7);
Lcd_Custom_Cmd(LCD_CURSOR_OFF);
Lcd_Custom_Out(1, 1, "FreqCV7");
Delay_ms(1000);
Lcd_Custom_Cmd(LCD_CLEAR);
freq = 1; //KARISHUUHASUU 1k #########
while (1) {
// 周波数の測定
LED = 1;
if (freq >= 1000000000) { //4294967295
cdir = 0;
} else if (freq < 2) {
freq = 1;
cdir = 1;
}
if (cdir == 1) {
freq = freq * 2; //100k
} else {
freq = freq / 2;
}
Delay_ms(1000);
LED = 0;
//換算
// プリスケーラの切り替え
temp = freq;
if (freq < 99999999) {
msg = " M K H";
}
else if (freq < 999999999) {
temp = temp / 10;
msg = " M K ";
// 12345678
} //4294967295
else if (freq < 4290000000 ) {
temp = temp / 100;
msg = "G M K ";
}
LongToStr(temp, buf);
Lcd_Custom_Out(2, 1, msg);
// 周波数の表示
Lcd_Custom_Out(1, 1, &buf[3]);
//
Delay_ms(10);
}
}
JALV2はlcd表示が弱い [jalv2_PIC]
こんばんは
JALV2で測定値を表示しようとして、頓挫。
直接8桁書き出す関数やライブラリを見つけることが出来ません。
この部分はmikroCで試して、サンプリングレートとプリスケーラを自動化すればsw関数も要らないと思い、
桁表示だけを下列に出し半自動化してみたら、メモリが99%になってしまった。
JALV2なら50%くらいで済むので、表示関数が使えるとほとんど自動で使えるのではないかとおもいます。
C言語の括弧付き変数が良く判らなかったのですが、これは計算値を求めリターンがあるので、
ファンクション関数であるのが判りました。
tmr1のプリスケーラ(8倍)は工夫をすればクロック読み出しも出来るので 、少しなら精度も上げられます。
仮に80MHzの数値を与えて、表示させてみました。
もっと低い所を測るにはレシプロカル測定法を使わねば成りません。
ファンクションの設定や表示は後回しにして [jalv2_PIC]
ファンクション名定数で与え、測定計算の部分だけプログラムを書き換えました。
一応エラーはありませんが、目的のデータが得られたかは、次の表示関数を実装してからでないと分かりません。
-- --------------------------------------------------------------------------
--
-- Title: Test program for lcd_hd44780_4.jal (basic / interface test)
-- Author: Eur Van Andel, Copyright (c) 2008-2010, all rights reserved.
-- Adapted-by: Rob Hamerling, Joep Suijs
-- Compiler: >=2.4m
-- Revision: $Revision: 2811 $
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php)
--
-- Description:
-- This sample shows how to setup an LCD and writes
-- "Hello World" to the first line, using a string and the print library.
-- A counter is printed to show the running forever loop.
-- --
-- This file defines a test for JALLIB testing, using a test-board
-- defined by a BOARD file .
--
-- Sources:
--
-- Notes:
-- setup: an hd44780 compatible display, used in 4-bit mode.
--
-- --------------------------------------------------------------------------
--
-- This file has been generated from:
-- * board: board_16f648a_js.jal
-- * test : test_lcd_hd44780_4bit.jal
--
--
-- LCD
-- 1 Vss 2 Vdd
-- 3 VO 4 RS
-- 5 R/W(G) 6 E
-- 7 D0 8 D1
-- 9 D2 10 D3
-- 11 D4 12 D5
-- 13 D6 14 D7
-- 15 BLA 16 BLK
-- 16F628A
-- 1 RA2 LCD_5 18 RA1 LCD_6
-- 2 RA3 LCD_4 17 RA0 LCD_7
-- 3 RA4 SWI4 16 RA7 OSC1 XTal IN
-- 4 RA5 MCLR ICSP Vpp 15 RA6 OSC2 XTal OUT
-- 5 Vss ICSP Vss 14 Vdd ICSP Vdd
-- 6 RB0 SWI1 13 RB7 LCD_EN ICSP PGD
-- 7 RB1 SWI2 12 RB6 T1CK_IN ICSP PGC
-- 8 RB2 SWI3 11 RB5 uPB1507GV SEL
-- 9 RB3 LED 10 RB4 LCD_RS
include constants_jallib
if false then;
-- lcd_command
lcd'put(byte in data)
lcd_cursor_position(1, 1)
lcd_clear_screen()
lcd_cursor_blink_display(0, 0, 1)
lcd_home()
lcd_clear_line(1)
lcd_write_char(line1[1])
lcd_cursor_position(0,0)
print_string(lcd, str1)
const byte DAY_OF_WEEK[] = "SuMoTuWeThFrSa" -- weekday abbreviations
lcd = DAY_OF_WEEK[weekday * 2] -- day of week
const byte ALARM_SIGN = 0b0101_1100 -- Yen(?)
lcd = ALARM_SIGN
end if
-- ---------------------------------------------------
;@jallib section chipdef
-- chip setup
include 16f628a;
include math;
--
-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target OSC HS -- HS crystal or resonator
pragma target clock 20_000_000; -- oscillator frequency
pragma target WDT disabled;
pragma target LVP disabled;
;@jallib section lcd_hd44780_4
-- ---------------------------------------------------
-- LCD IO definition
alias lcd_en is pin_B7; -- data trigger
alias lcd_rs is pin_B4; -- command/data select.
pin_B7_direction = output;
pin_B4_direction = output;
alias lcd_d4 is pin_A3;
alias lcd_d5 is pin_A2;
alias lcd_d6 is pin_A1;
alias lcd_d7 is pin_A0;
pin_A3_direction = output;
pin_A2_direction = output;
pin_A1_direction = output;
pin_A0_direction = output;
-- ---------------------------------------------------
-- ---------------------------------------------------
const byte LCD_ROWS = 2; -- 2 lines
const byte LCD_CHARS = 8; -- 8 chars per line
-- ---------------------------------------------------
-- LED IO definition
alias led is pin_B3;
pin_B3_direction = output; -- blinking LED
-- uPB1507GV SEL
alias exps is pin_B5;
pin_B5_direction = output; -- EXT PS sel
-- sw io definition
alias swi1 is pin_B0;
pin_B0_direction = input; -- sw1 in
alias swi2 is pin_B1;
pin_B1_direction = input; -- sw2 in
alias swi3 is pin_B2;
pin_B2_direction = input; -- sw3 in
alias swi4 is pin_A4;
pin_A4_direction = input; -- sw4 in
-- ---------------------------------------------------
;@jallib section led
include delay;
--include lcd_hd44780_common;
include lcd_hd44780_4; -- LCD library with 4 data lines
lcd_init(); -- initialize LCD
include print; -- formatted output library
-- ---------------------------------------------------
;var bit swi1,swi2,swi3,swi4;
swi1 = 0;
swi2 = 0;
swi3 = 0;
swi4 = 0;
const byte gatec_time01 = 1
const byte gatec_time06 = 6
const byte gatec_time10 = 10
var word time_count;
var dword freq;
var byte prscler = 8; karisettei 1/8
var bit ifon = 0; karisettei -445 off
var dword freq_meas;
var byte gate_timer = 1; karisettei 0.1Sec
var bit fricfunc = 0; karisettei Hz
-- ----------------------------------------------------------
procedure gatec_time is
pragma interrupt
PIR1_TMR2IF = 0;
time_count = time_count - 1;
if (time_count == 0) then
t1con_tmr1on = 0; -- gate close
t2con_tmr2on = 0; -- timer2 to stop
end if
end procedure
-- ----------------------------------------------------------
; kukannnaishuuhasuukaunnto
;var dword freq_meas(byte gatec_time);
;gate_timer = gatec_time01; ####
;freq_meas = freq_meas * 10;
freq_meas = (freq_meas / gate_timer) * 10;
; var volatile dword freq;
;timer1 is set
pir1_tmr1if = 0;
tmr1l = 0;
tmr1h = 0;
;timer2 is set
pir1_tmr2if = 0;
case gate_timer of
gatec_time10:
block
time_count = 1221; ; 312500=1/((1/20000000) * 4 * 16)
tmr2 = 0x4C; ; 0x4c=256-(312500-(256*1220))
end block
gatec_time06:
block
time_count = 733; ; 31250=0.6/((1/20000000) * 4 * 16)
tmr2 = 0x94; ; 148 0x94=256-(187500-(256*732))
end block
gatec_time01:
block
time_count = 123; ; 31250=0.1/((1/20000000) * 4 * 16)
tmr2 = 0xee; ; 0xee=256-(31250-(256*122))
end block
end case
;
freq = 0;
; enable to interrupt
intcon_peie = 1;
intcon_gie = 1;
; start
t2con_tmr2on = 1; ;timet start
; delay
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
;
t1con_tmr1on = 1; ;open gate
; mes
while (T2CON_TMR2ON != 0) loop
if (PIR1_TMR1IF == 1) then
PIR1_TMR1IF = 0; jyunnkann
freq = freq + 1; ketaagari
end if
end loop
if (PIR1_TMR1IF == 1) then
PIR1_TMR1IF = 0; jyunnkann
freq = freq + 1; ketaagari
end if
;Conversion
freq = freq * 65536;
; freq = freq + (TMR1H * 256) + TMR1L;
freq = freq + TMR1;
;
; return (freq);
-- ----------------------------------------------------------
block
-- static char* msg;
var volatile dword freq, temp; ; 0...4294967295
var volatile byte buf[20], msg[], prescaler, gatetime;
; compa set
CMCON = 0b0000_0111; -- disable comparator
OPTION_REG_NRBPU = 0;
enable_digital_io();
;ansel = 0b00000000; ;not use
; port set
trisa = 0b11100000;
trisb = 0b01001111;
; timer2 is set
pie1_tmr2ie = 1;
pir1_tmr2if = 0;
t2con_toutps = 0;
t2con_tmr2on = 0;
t2con_t2ckps = 3;
tmr2 = 0;
; timer1 is set
pie1_tmr1ie = 0;
pir1_tmr1if = 0;
-- t1con_t1run = 0; 16f88 opnly
t1con_t1ckps = 0;
t1con_t1oscen = 0;
T1CON_NT1SYNC = 1;
t1con_tmr1cs = 1;
t1con_tmr1on = 0;
tmr1 = 0;
;variables is init
prscler = 1;
gatetime = gatec_time10;
Delay_1ms(1000);
lcd_clear_screen()
;
-- --- generate LCD screen activity ---------
var byte counter = 0
forever loop
; while (1) loop
; friq_mes
LED = 1;
freq_meas = freq_meas * gate_timer / 10; #### rifureshulate
; freq = freq_meas(gate_timer);
freq = freq_meas;
LED = 0;
;Conversion
freq = freq * prescaler * gateTime;
; prscler_selct
if (prscler == 1) then
T1CON_T1CKPS = 0;
prescaler = 1;
exps = 0;
; msg = "1/1 ";
end if
if (prscler == 8) then
T1CON_T1CKPS = 3;
prescaler = 8;
exps = 0;
; msg = "1/8 ";
end if
if (prscler == 64) then
T1CON_T1CKPS = 0;
prescaler = 64;
exps = 1;
; msg = "1/64";
end if
; Lcd_Out(2, 1, msg); ##
; print_string(lcd, msg)
; gatec_time _serect
if (gatec_time10 == gatetime) then
; gateTime = gatec_time10;
; msg = "1sec ";
end if
if (gatec_time06 == gatetime) then
; gateTime = gatec_time06;
; msg = "0.6sec ";
end if
if (gatec_time01 == gatetime) then
; gateTime = gatec_time01;
; msg = "0.1sec ";
end if
; Lcd_Out(2, 5, msg); ##
; -if
if (ifon == 1) then
freq = freq - 455000;
; msg = "-455k";
else
; msg = " ";
end if
; Lcd_Out(2, 12, msg); ##
; disp range select
if (fricfunc == 0) then
; LongToStr(freq, buf);
; msg = "Hz ";
else
temp = freq / 1000;
if ((freq - (temp * 1000)) > 500) then
temp = temp + 1;
; LongToStr(temp, buf);
; msg = "kHz";
end if
end if
; Lcd_Out(1, 9, msg); ##
; friq_disp
; Lcd_Out(1, 1, &buf[3]); ##
;
Delay_1ms(500);
end loop
end block
-- ----------------------------------------------------------
Compilation started at :2012/04/24 17:39:37
jal 2.4o (compiled May 8 2011)
Compiler CommandLine: C:\JALPack\compiler\jalv2.exe "D:\_users\_jalvs\n8\5friq_lcd.jal" -s "C:\JALPack\lib" -no-variable-reuse
Errors :0 Warnings :0
Code :870/2048 Data:74/208 Hardware Stack : 3/8 Software Stack :80
C言語をjalv2に書き換え1個のエラーが出ます [jalv2_PIC]
こんにちは
C言語をjalv2に書き換え1個のエラーが出ます、
しかし基本的な間違いを残したままで、直すとかえってエラーだらけになります。
最後の所にLCD表示関数をまとめています。
-- ---------------------------------------------------------------------------- "Hellowld!" + 1byte_hex * blinking LED + cursor
-- Title: Test program for lcd_hd44780_4.jal (basic / interface test)
-- Author: Eur Van Andel, Copyright (c) 2008-2010, all rights reserved.
-- Adapted-by: Rob Hamerling, Joep Suijs
-- Compiler: >=2.4m
-- Revision: $Revision: 2811 $
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php)
--
-- Description:
-- This sample shows how to setup an LCD and writes
-- "Hello World" to the first line, using a string and the print library.
-- A counter is printed to show the running forever loop.
-- --
-- This file defines a test for JALLIB testing, using a test-board
-- defined by a BOARD file .
--
-- Sources:
--
-- Notes:
-- setup: an hd44780 compatible display, used in 4-bit mode.
--
-- --------------------------------------------------------------------------
--
-- This file has been generated from:
-- * board: board_16f648a_js.jal
-- * test : test_lcd_hd44780_4bit.jal
--
--
-- LCD
-- 1 Vss 2 Vdd
-- 3 VO 4 RS
-- 5 R/W(G) 6 E
-- 7 D0 8 D1
-- 9 D2 10 D3
-- 11 D4 12 D5
-- 13 D6 14 D7
-- 15 BLA 16 BLK
-- 16F628A
-- 1 RA2 LCD_5 18 RA1 LCD_6
-- 2 RA3 LCD_4 17 RA0 LCD_7
-- 3 RA4 SWI4 16 RA7 OSC1 XTal IN
-- 4 RA5 MCLR ICSP Vpp 15 RA6 OSC2 XTal OUT
-- 5 Vss ICSP Vss 14 Vdd ICSP Vdd
-- 6 RB0 SWI1 13 RB7 LCD_EN ICSP PGD
-- 7 RB1 SWI2 12 RB6 T1CK_IN ICSP PGC
-- 8 RB2 SWI3 11 RB5 uPB1507GV SEL
-- 9 RB3 LED 10 RB4 LCD_RS
-- ---------------------------------------------------
;@jallib section chipdef
-- chip setup
include 16f628a;
--
-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target OSC HS -- HS crystal or resonator
pragma target clock 20_000_000; -- oscillator frequency
pragma target WDT disabled;
pragma target LVP disabled;
;@jallib section lcd_hd44780_4
-- ---------------------------------------------------
-- LCD IO definition
alias lcd_en is pin_B7; -- data trigger
alias lcd_rs is pin_B4; -- command/data select.
pin_B7_direction = output;
pin_B4_direction = output;
alias lcd_d4 is pin_A3;
alias lcd_d5 is pin_A2;
alias lcd_d6 is pin_A1;
alias lcd_d7 is pin_A0;
pin_A3_direction = output;
pin_A2_direction = output;
pin_A1_direction = output;
pin_A0_direction = output;
-- ---------------------------------------------------
-- ---------------------------------------------------
const byte LCD_ROWS = 2; -- 2 lines
const byte LCD_CHARS = 8; -- 8 chars per line
-- ---------------------------------------------------
-- LED IO definition
alias led is pin_B3;
pin_B3_direction = output; -- blinking LED
-- uPB1507GV SEL
alias exps is pin_B5;
pin_B5_direction = output; -- EXT PS sel
-- sw io definition
alias swi1 is pin_B0;
pin_B0_direction = input; -- sw1 in
alias swi2 is pin_B1;
pin_B1_direction = input; -- sw2 in
alias swi3 is pin_B2;
pin_B2_direction = input; -- sw3 in
alias swi4 is pin_A4;
pin_A4_direction = input; -- sw4 in
-- ---------------------------------------------------
;@jallib section led
include delay;
--include lcd_hd44780_common;
include lcd_hd44780_4; -- LCD library with 4 data lines
lcd_init(); -- initialize LCD
include print; -- formatted output library
;var bit swi1,swi2,swi3,swi4;
swi1 = 0;
swi2 = 0;
swi3 = 0;
swi4 = 0;
--#define gatec_time01 is 10
--#define gatec_time06 is 1/6
--#define gatec_time10 is 1
const byte gatec_time01 = 10
const byte gatec_time06 = 1/6
const byte gatec_time10 = 1
var word time_count;
var dword freq;
--var byte gatec_time;
-- ----------------------------------------------------------
procedure gatec_time is
pragma interrupt
PIR1_TMR2IF = 0;
time_count = time_count - 1;
if (time_count == 0) then
t1con_tmr1on = 0; -- gate close
t2con_tmr2on = 0; -- timer2 to stop
end if
end procedure
-- ----------------------------------------------------------
--function friq_mes (DWORD IN n) RETURN byte IS
var dword freq_meas(byte gatec_time);
;var dword freq_meas[byte gatec_time];
block
var volatile dword freq;
;timer1 is set
pir1_tmr1if = 0;
tmr1l = 0;
tmr1h = 0;
;timer2 is set
pir1_tmr2if = 0;
case gatec_time of
gatec_time10:
time_count = 1221;
tmr2 = 0x4c; ; 312500=1/((1/20000000) * 4 * 16)
; 0x4c=256-(312500-(256*1220))
; gatec_time06:
; time_count = 733;
; tmr2 = 0x94; ; 31250=0.6/((1/20000000) * 4 * 16)
; ; 148 0x94=256-(187500-(256*732))
gatec_time01:
time_count = 123;
tmr2 = 0xee; ; 31250=0.1/((1/20000000) * 4 * 16)
; 0xee=256-(31250-(256*122))
end case
;
freq = 0;
; enable to interrupt
intcon.peie = 1;
intcon.gie = 1;
; start
t2con.tmr2on = 1; ;timet start
; delay
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
asm nop;
;
t1con_tmr1on = 1; ;open gate
; mes
while (T2CON_TMR2ON != 0) loop
if (PIR1_TMR1IF == 1) then
PIR1_TMR1IF = 0;
freq = freq + 1;
end if
end loop
if (PIR1.TMR1IF == 1) then
PIR1_TMR1IF = 0;
freq = freq + 1;
end if
;Conversion
freq = freq * 65536;
freq = freq + (TMR1H * 256) + TMR1L;
;
; return (freq);
--[ exit loop ]
end block
--end function
-- ----------------------------------------------------------
-- static char* msg;
forever loop
var volatile char* msg;
var volatile dword freq, temp; ; 0...4294967295
var volatile byte buf[20], prescaler, gatetime;
CMCON = 0b0000_0111; -- disable comparator
OPTION_REG_NRBPU = 0;
enable_digital_io();
; compa set
;ansel = 0b00000000; ;not use
; port set
trisa = 0b11100000;
trisb = 0b01001111;
;option_reg_nrbpu = 0; ; portb is pullup
; timer2 is set
pie1_tmr2ie = 1;
pir1_tmr2if = 0;
t2con_toutps0 = 0;
t2con_toutps1 = 0;
t2con_toutps2 = 0;
t2con_toutps3 = 0;
t2con_tmr2on = 0;
t2con_t2ckps0 = 1;
t2con_t2ckps1 = 1;
tmr2 = 0;
; timer1 is set
pie1_tmr1ie = 0;
pir1_tmr1if = 0;
t1con_t1run = 0;
t1con_t1ckps0 = 0;
t1con_t1ckps1 = 0;
t1con_t1oscen = 0;
t1con_not_t1sync = 1;
t1con_tmr1cs = 1;
t1con_tmr1on = 0;
; tmr1l = 0;
; tmr1h = 0;
tmr1 = 0;
;variables is init
prs = 1;
gatetime = gatec_time10;
; lcd_init
; Lcd_port_set
LCD_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, "FreqCounter V7");
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
;
while (1) loop
; friq_mes
LED = 1;
freq = FreqMeasurement(gateTime);
; freq = FreqMeasurement[gateTime];
LED = 0;
;Conversion
freq = freq * prescaler * gateTime;
; prs_selct
if (sw1 == 1) then
T1CON_T1CKPS0 = 0;
T1CON_T1CKPS1 = 0;
prescaler = 1;
msg = "1/1 ";
else
T1CON_1CKPS0 = 1;
T1CON_1CKPS1 = 1;
prescaler = 8;
msg = "1/8 ";
end if
Lcd_Out(2, 1, msg);
; gatec_time _serect
if (sw2 == 1) then
gateTime = gatec_time10;
msg = "1sec ";
else
gateTime = gatec_time01;
msg = "0.1sec ";
end if
Lcd_Out(2, 5, msg);
; -if
if (sw3 == 0) then
freq -= 455000;
msg = "-455k";
else
msg = " ";
end if
Lcd_Out(2, 12, msg);
; disp range select
if (sw4 == 1) then
LongToStr(freq, buf);
msg = "Hz ";
else
temp = freq / 1000;
if ((freq - (temp * 1000)) > 500) then
temp = temp + 1;
end if
LongToStr(temp, buf);
msg = "kHz";
end if
Lcd_Out(1, 9, msg);
; friq_disp
Lcd_Out(1, 1, &buf[3]);
;
Delay_ms(500);
end loop
-- lcd_command
__lcd_write(5)
_lcd_write_data(5)
_lcd_write_command(5)
_lcd_line2index(1) return
-- freq =
lcd'put(byte in data)
lcd_cursor_position(1, 1)
lcd_clear_screen()
lcd_cursor_blink_display(0, 0, 1)
lcd_home()
lcd_clear_line(1)
lcd_write_char(line1[1])
lcd_cursor_position(0,0)
print_string(lcd, str1)
const byte DAY_OF_WEEK[] = "SuMoTuWeThFrSa" -- weekday abbreviations
lcd = DAY_OF_WEEK[weekday * 2] -- day of week
const byte ALARM_SIGN = 0b0101_1100 -- Yen(?)
lcd = ALARM_SIGN
--
end loop
-- ----------------------------------------------------------
Compilation started at :2012/04/22 17:37:47
jal 2.4o (compiled May 8 2011)
[Error] (5friq_lcd.jal) [Line 143] unexpected token: (
Compiler CommandLine: C:\JALPack\compiler\jalv2.exe "D:\_users\_jalvs\n8\5friq_lcd.jal" -s "C:\JALPack\lib" -no-variable-reuse
Errors :1 Warnings :0
sw入力のテストをしました [jalv2_PIC]
jalv2の場合 条件判断を複数ロジカル判断によるよりも、単段ロジカル判断にして、
加減算は先に済ませてから規定値内に引き戻す方法で、20バイト位少なくなることが判りました。
下記のソース部分は、液晶駆動の一部とsw入力をデチャッタ処理して押したボタンの数を勘定できる様にまりました。
PIC16fの一部のデバイスはAポートがオープンコレクタになったのがあって、GND側に繋いだLEDが点灯せず、
悩んだことがあります。
電源側を使うのも簡単ですが、ノイズ環境など考えるとGND側に統一するのが良さそうです。
PIC16F628Aの場合ですとADコンがありませんがコンパレータがデフォルトなので、
これをデジタルに設定し、BポートのプルUPを活かしてsw入力の抵抗を省き、リファレンス電圧を禁止にしますと
結構な数の入出力が使えて、周波数カウンタ程度なら組むことが出来そうです。
16f648aを使えばレシプロカルカウンタの出来るかもしれません。
レシプロカルカウンタの原理はいろいろあるようですが、間違った理論が載っていたのを見たことがあります。
やはり自分で理論的裏付けを取って、使うに越したことはないでしょう。
-- --------------------------------------------------------------------------
--
-- This file has been generated from:
-- * board: board_16f648a_js.jal
-- * test : test_lcd_hd44780_4bit.jal
--
--
-- LCD
-- 1 Vss 2 Vdd
-- 3 VO 4 RS
-- 5 R/W(G) 6 E
-- 7 D0 8 D1
-- 9 D2 10 D3
-- 11 D4 12 D5
-- 13 D6 14 D7
-- 15 BLA 16 BLK
-- 16F628A
-- 1 RA2 LCD_5 18 RA1 LCD_6
-- 2 RA3 LCD_4 17 RA0 LCD_7
-- 3 RA4 SW4 16 RA7 OSC1 XTal IN
-- 4 RA5 MCLR ICSP Vpp 15 RA6 OSC2 XTal OUT
-- 5 Vss ICSP Vss 14 Vdd ICSP Vdd
-- 6 RB0 SW1 13 RB7 LCD_EN ICSP PGD
-- 7 RB1 SW2 12 RB6 T1CK_IN ICSP PGC
-- 8 RB2 SW3 11 RB5 uPB1507GV SEL
-- 9 RB3 LED 10 RB4 LCD_RS
-- ---------------------------------------------------
;@jallib section chipdef
-- chip setup
include 16f628a;
--
-- This program assumes a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
pragma target OSC HS -- HS crystal or resonator
pragma target clock 20_000_000; -- oscillator frequency
pragma target WDT disabled;
pragma target LVP disabled;
;@jallib section lcd_hd44780_4
-- ---------------------------------------------------
-- LCD IO definition
alias lcd_en is pin_B7; -- data trigger
alias lcd_rs is pin_B4; -- command/data select.
pin_B7_direction = output;
pin_B4_direction = output;
alias lcd_d4 is pin_A3;
alias lcd_d5 is pin_A2;
alias lcd_d6 is pin_A1;
alias lcd_d7 is pin_A0;
pin_A3_direction = output;
pin_A2_direction = output;
pin_A1_direction = output;
pin_A0_direction = output;
-- ---------------------------------------------------
-- ---------------------------------------------------
const byte LCD_ROWS = 2; -- 2 lines
const byte LCD_CHARS = 8; -- 8 chars per line
-- ---------------------------------------------------
-- LED IO definition
alias led is pin_B3;
pin_B3_direction = output; -- blinking LED
-- uPB1507GV SEL
alias exps is pin_B5;
pin_B5_direction = output; -- EXT PS sel
-- sw io definition
alias sw1 is pin_B0;
pin_B0_direction = input; -- sw1 in
alias sw2 is pin_B1;
pin_B1_direction = input; -- sw2 in
alias sw3 is pin_B2;
pin_B2_direction = input; -- sw3 in
alias sw4 is pin_A4;
pin_A4_direction = input; -- sw4 in
-- ---------------------------------------------------
;@jallib section led
include delay;
--include lcd_hd44780_common;
CMCON = 0b0000_0111; -- disable comparator
OPTION_REG_NRBPU = 0;
enable_digital_io();
include lcd_hd44780_4; -- LCD library with 4 data lines
include print; -- formatted output library
lcd_init(); -- initialize LCD
for 4 loop; -- blink LED 4 times to indicate startup
LED = on;
delay_100ms(2);
LED = off;
delay_100ms(2);
end loop;
const byte str1[] = "FriqC V7"; -- define strings
lcd_cursor_position(0,0); -- to 1st line, 1st char
print_string(lcd, str1); -- show hello world!
delay_100ms(10);
lcd_clear_line(1);
var bit sf1=0,sf2,sf3,sf4;
var byte sn1,cp1;
sn1 = 0;
cp1 = 0;
forever loop -- loop forever
if (sw1 != 1) then
if (sf1 == 0) then
sn1 = sn1 + 1;
delay_1ms(10);
if sn1 > 3 then
sn1 = 3;
sf1 = 1;
led = 1;
cp1 = cp1 + 1;
if cp1 > 7 then
cp1 = 0;
end if
end if
end if
end if
if (sw1 == 1) then
if (sf1 == 1) then
sn1 = sn1 - 1;
delay_1ms(10);
if sn1 <= 0 then
sn1 = 0;
sf1 = 0;
led = 0;
end if
end if
end if
lcd_cursor_position(1,0);
print_byte_hex(lcd, sn1);
lcd_cursor_position(1,3);
print_byte_hex(lcd, cp1);
end loop
ほぼ思い通りに仕上がったが [jalv2_PIC]
こんにちは
ほぼ思い通りに仕上がったんですが、LCDコントロールは結構無理が有って、
アンダーカーソルを使うのは見えにくい。
1個のボタンで、右に流して行き、各設定をもう一個のボタンで切り換えようと思ったんですが、
プリスケーラ 1/1,1/8,1/64 サンプリングタイム1/10、6/10,1/1秒
周波数レンジ GHz、MHz、KHz、mHz、uHz を切り換えたいと思っています。
(意味のない設定もありますが)他には表示の下にアンダーラインを出して位取りにも当てようと思ったのですが
アンダーラインやカーシル(ブリンクしかないみたい)は同時に1カ所すかでないので時分割でチラチラとしかでません。
ボタン(チャタリング防止)は30msにしたはずですが1秒くらいかかっています。
プログラムループのせいか、(まだ計っていません)クロック周波数のせいなのかはハッキリしません、
上の桁のアンダーカーソルはまだ制御せず、下のを時分割で共用しています 。
00はボタンを離したカウントで03に成ると、0から7の間をカラムが右に動きます。
tmr1とtmr2を使うこの方式ではプリスケーラに貯まった分が読み出せれば精度はよくなるんですが、
(TMR1CS)を使ってtmr1の加算を確認するのも結構面倒ですが、AN592の様に抵抗を使わない文絡かも知れません。
カーソルは自由に動かすことが出来ました。 [jalv2_PIC]
こんにちは
カーソルは
アンダーライン、アンダーライン付ブリンキング アンダーライン無しブリンキング、表示消し に対応できて
順次送ることが出来ます、(文字のある場所にも適用できます)
しかしカーソルは1カ所しか表示できないので、周波数計測値の位取りに、CGRAM領域にアンダーカーソルを書き込むべく、
lcd_hd44780_common.jal に一部の追加をしました。
-- ----------------------------------------------------------------------------
-- Sends data LCD_SET_CGRAM_data byte in <value> to LCD
-- ----------------------------------------------------------------------------
procedure lcd_write_CGRAM_data(byte in line,byte in LCD_ROWS,byte in value) is
/line = _lcd_line2index(line)
--LCD_SET_CGRAM_ADDRESS--(line)
_lcd_write_command( lcd_set_ddram_address | lcd_pos )
lcd_rs = high
__lcd_write(value) -- write byte
lcd_rs = low
end procedure
これを 1cursor_16f628a_lcd_hd44780_4bit_command.jal カーソル試験ルーチンから呼び出しますと、
lcd_write_CGRAM_data(0,2,0x0E)
delay_100ms(5)
最後にカーソルを移動した所に カタカナのミの様な字(横
4本線)が表示される様になりました。
なにか書き込んではいるのですが、CGRAMのアドレスやデータなどがまだ不完全なようです。