PIC12F1822でDACをやってみた。
DACをやるのがはじめてなんてちょっと自分でもびっくりだ。
何で今までやらなかったんだろう。
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 |
#include <xc.h> // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled) #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = ON // PLL Enable (4x PLL enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) void main(){ unsigned char dacvalue; OSCCON = 0b01110010; ANSELA = 0b00000000; TRISA = 0b00000000; PORTA = 0b00000000; DACCON0bits.DACEN = 1;//DAC is Enabled DACCON0bits.DACLPS = 1;//DAC Positive reference source selected DACCON0bits.DACOE = 1;//DAC voltage level is also an output on the DACOUT pin while(1) { for(dacvalue=0; dacvalue<32; dacvalue++){ DACCON1 = dacvalue; } for(dacvalue=31; dacvalue>0; dacvalue--){ DACCON1 = dacvalue; } } } |
出力を見てみるとこんな感じに意図したとおりになっている。
あらかじめ計算した値を配列に準備しておいてSINカーブもためしてみた。
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 |
#include <xc.h> // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled) #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PLLEN = ON // PLL Enable (4x PLL enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 32000000 unsigned char v[32] = {16,19,22,24,27,29,30,31,31,31,30,29,26,24,21,18,15,12,9,6,4,2,0,0,0,0,1,3,5,8,11,14}; void sdelay(unsigned char); void main(){ unsigned char i; unsigned char dly; OSCCONbits.IRCF = 0b1110;//Internal Oscillator Frequency 8MHz( x 4) ANSELA = 0b00000000; TRISA = 0b00000000; PORTA = 0b00000000; DACCON0 = 0b11100000;//VDD/VSSを使用、DACOUTピン(RA0)出力する while(1) { for( dly = 1; dly < 200; dly++ ){ for( i = 1; i < 32; i++ ){ DACCON1 = v[i]; sdelay(dly/10); } } } } void sdelay(unsigned char j){ unsigned char i; for( i = 1; i < j; i++ ){} } |
そのままだとスピーカー(8Ω 0.5W)はほとんど鳴らないので、PICのデータシートにもあるようにオペアンプを取り付けた。
オペアンプの電源を共通にすると出力が頭打ちになるので、PICの電源は5Vでオペアンプの電源は9Vと、ちょっと煩雑になった。
出力をPCのマイク入力につないで録音したもの(左チャンネルのみ音あり) PIC12F1822_DAC_Sound
Tweet