softdev:pic18
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| softdev:pic18 [2012/02/19 16:47] – [Delay] king | softdev:pic18 [2013/07/15 20:25] (現在) – 外部編集 127.0.0.1 | ||
|---|---|---|---|
| 行 3: | 行 3: | ||
| ===== コンフィグbit ===== | ===== コンフィグbit ===== | ||
| + | Configuration bitsの設定は、IDEの中で可能だが、プログラムの中に記述する事も出来る。PIC12F675などは __CONFIG( の一文ですんでいたのだが、PIC18Fでは、一つ一つ設定が必要らしい。 | ||
| + | <code txt> | ||
| + | #pragma config OSC = INTIO2 // | ||
| + | #pragma config FSCM = OFF // | ||
| + | #pragma config IESO = OFF // | ||
| + | #pragma config PWRT = ON // | ||
| + | #pragma config BOR = ON // | ||
| + | #pragma config BORV = 42 // | ||
| + | #pragma config WDT = OFF // | ||
| + | #pragma config WDTPS = 32768 // | ||
| + | #pragma config MCLRE = ON // | ||
| + | #pragma config STVR = OFF // | ||
| + | #pragma config LVP = OFF // | ||
| + | #pragma config DEBUG = ON // | ||
| + | #pragma config CP0 = OFF //Code Protection bit Block 0 | ||
| + | #pragma config CP1 = OFF //Code Protection bit Block 1 | ||
| + | #pragma config CPB = OFF //Boot Block Code Protection Bit | ||
| + | #pragma config CPD = OFF //Data EEPROM Code Protectio Bit | ||
| + | #pragma config WRT0 = OFF // | ||
| + | #pragma config WRT1 = OFF // | ||
| + | #pragma config WRTB = OFF //Boot Block Write Protection | ||
| + | #pragma config WRTC = OFF // | ||
| + | #pragma config WRTD = OFF //Data EEPROM Write Protection | ||
| + | #pragma config EBTR0 = OFF // | ||
| + | #pragma config EBTR1 = OFF // | ||
| + | #pragma config EBTRB = OFF //Boot Block Table Read Protection | ||
| + | </ | ||
| + | INTIO2のような定数は、どこにも定義が無いのだが、IDEの中のヘルプに書かれており認識するようである。それぞれの持つ意味は、PICのデータシートから紐解くしか無い。いまいちよくわかっとらん。 | ||
| ===== Delay ===== | ===== Delay ===== | ||
| - | サンプルで用意されていたDelay関数が無くなっちゃった。あるのか知れんけど。よくわからんから重宝してたのですが、で、includeフォルダをみたら、delays.hと言うのがあった。 | + | サンプルで用意されていたDelay関数が無くなっちゃった。あるのか知れんけど。よくわからんから重宝してたのですが、で、includeフォルダをみたら、delays.hと言うのがあった。わたしも、あんまりよくわかってないので、行き当たりばったりでよくないんですが。 |
| - | <code c> | + | <code c++> |
| /* C18 cycle-count delay routines. */ | /* C18 cycle-count delay routines. */ | ||
| - | + | /* Passing 0 (zero) results in a delay of 2560 cycles. */ | |
| - | /* Delay of exactly 1 Tcy */ | + | |
| - | #define Delay1TCY() _delay(1) | + | |
| - | + | ||
| - | /* Delay of exactly 10 Tcy */ | + | |
| - | #define Delay10TCY() _delay(10) | + | |
| - | + | ||
| - | /* Delay10TCYx | + | |
| - | * Delay multiples of 10 Tcy | + | |
| - | * Passing 0 (zero) results in a delay of 2560 cycles. | + | |
| - | */ | + | |
| void Delay10TCYx(unsigned char); | void Delay10TCYx(unsigned char); | ||
| - | /* Delay100TCYx | + | /* Passing 0 (zero) results in a delay of 25,600 cycles.*/ |
| - | * Delay multiples of 100 Tcy | + | |
| - | | + | |
| - | */ | + | |
| void Delay100TCYx(unsigned char); | void Delay100TCYx(unsigned char); | ||
| /* Delay1KTCYx | /* Delay1KTCYx | ||
| - | * Delay multiples of 1000 Tcy | + | * Passing 0 (zero) results in a delay of 256,000 cycles. */ |
| - | * Passing 0 (zero) results in a delay of 256,000 cycles. | + | |
| - | */ | + | |
| void Delay1KTCYx(unsigned char); | void Delay1KTCYx(unsigned char); | ||
| - | /* Delay10KTCYx | + | /* Passing 0 (zero) results in a delay of 2,560,000 cycles. */ |
| - | * Delay multiples of 10,000 Tcy | + | |
| - | | + | |
| - | */ | + | |
| void Delay10KTCYx(unsigned char); | void Delay10KTCYx(unsigned char); | ||
| + | </ | ||
| - | #endif | + | で、なんがしか時間待ちするようであるので、オシロの波形で確認したところ、PIC18F1320で8MHz内蔵タイマで下表の結果だった。 |
| + | ^ Function | ||
| + | |Delay1KTCYx(1) | ||
| + | |Delay10KTCYx(1) | ||
| + | |Delay100TCYx(1) | ||
| + | |Delay100TCYx(10) | ||
| + | |Delay100TCYx(100) | ||
| + | |Delay10TCYx(1) | ||
| + | |Delay10TCYx(10) | ||
| + | |Delay10TCYx(20) | ||
| + | |Delay10TCYx(100) | ||
| - | #endif | + | 計測にPort出力のH/ |
| + | 以上から、1TCYx=510ns(@8MHz)と思われる。8MHzは周期125nsから、TCY=4/ | ||
| + | なので、引数がcharなので | ||
| + | <code c++> | ||
| + | /* 1ms - 1275ms */ | ||
| + | void DelayMs(int t){ | ||
| + | if(t< | ||
| + | Delay1KTCYx(t*2); | ||
| + | } else if(t< | ||
| + | Delay10KTCYx(t/ | ||
| + | } else { | ||
| + | Delay10KTCYx(255); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | これで、いいでしょうか。 | ||
| + | |||
| + | ===== 定義済定数 ===== | ||
| + | 参考書とかでは、ポートの出力、例えばポートAのビット0は、RA0=1 などの記述で1出力できたのだが、PIC18Fでは大量のWarningが出る。そのままでも動作に問題は無いのだが、気持ち悪い。RA0の記述が非推奨のようである。 | ||
| + | |||
| + | ヘッダファイルを見ると、PORTAbits.RA0と記載するのかな。でも長いなぁ | ||
| + | |||
| + | <code c> | ||
| + | extern volatile union { | ||
| + | struct { | ||
| + | unsigned RB0 :1; | ||
| + | unsigned RB1 :1; | ||
| + | unsigned RB2 :1; | ||
| + | unsigned RB3 :1; | ||
| + | unsigned RB4 :1; | ||
| + | unsigned RB5 :1; | ||
| + | unsigned RB6 :1; | ||
| + | unsigned RB7 :1; | ||
| + | }; | ||
| + | struct { | ||
| + | unsigned RB :8; | ||
| + | } PORTBbits @ 0xF81; | ||
| </ | </ | ||
| + | よくわからないんだけど、共用体の中に、構造体があるから、PORTBbits.RB0とPORTBbits.RBは同じメモリになるけど、PORTBbits.RB0とPORTBbits.RB1は異なるメモリにあり独立しているという意味だと思うんだけど、< | ||
| + | |||
| + | ADCONの設定をしていないせいでした。とほほ。一部のポートはADCONがデフォルトになっていると言う事もある。 | ||
softdev/pic18.1329637639.txt.gz · 最終更新: (外部編集)
