2011年3月9日水曜日

PIC18F14K50でCDC-ADC

たまにはソフトよりのことも書いてみます。
ADCの内容をCDCに書き出すJalV2コードです。
初めてJalV2で書いたプログラムなので自信ないですけど、間違ってたら添削お願いします。

シリアルに対して0-7を送るとADCの値を返すだけの簡単なプログラムになってます。
対応表は下記の通り
0 -> AN4
1 -> AN5
2 -> AN6
3 -> AN7
4 -> AN8
5 -> AN9
6 -> AN10
7 -> AN11
8 -> テスト文字列

動作回路とブートローダは
http://hp.vector.co.jp/authors/VA000177/html/pic18boot.html
を想定しているのでORGは0x800からになってます。
Bootボタンを押すとpicbootのモードへ移行します。

USBリセットはこれでOKなのかな?

-- My bootloader Origin = 0x800
--  picboot -r -s800 14k50_CDC_ADC.hex
PRAGMA BOOTLOADER LOADER18 0x800
include 18f14k50                    -- target
include print                       -- print_word_binary, print_string
-- FUSE Settings
pragma target OSC  HS              -- HS = crystal
pragma target clock 12_000_000     -- 12MHz
pragma target WDT  disabled        -- no watchdog
pragma target LVP  enabled         -- Low Voltage Programming
pragma target MCLR external        -- reset externally
pragma target CPUDIV       P1      -- NO_CPU_SYSTEM_CLOCK_DIVIDE
pragma target USBDIV       P1      -- USB_CLOCK_COMES_DIRECTLY_FROM_THE_OSC1_OSC2_OSCILLATOR_BLOCK_NO_DIVIDE
pragma target PLLEN        P4      -- OSCILLATOR_MULTIPLIED_BY_4
-- ADC settings
const bit ADC_HIGH_RESOLUTION = true
const byte ADC_NVREF = 0
const byte ADCON1_ADREF = 0
const word ADC_RSOURCE = 2_000
include adc
-- USB settings
-- const byte USB_CDC_RX_BUFFER_SIZE = 0x  -- set receive FIFO size
-- const byte USB_CDC_TX_BUFFER_SIZE = 0x??  -- set transmit FIFO size
const word USB_SERIAL_PRODUCT_ID = 0x000a -- set USB device product ID
const word USB_SERIAL_VENDOR_ID = 0x04d8  -- set USB device vendor ID
-- const byte USB_STRING0[] = { .. }         -- set USB language string
-- const byte USB_STRING1[] = { .. }         -- set USB mfg string
--const byte USB_STRING2[] = { .. }         -- set USB product string
const byte USB_STRING2[32] =
{
 32, -- bLength
 0x03, -- bDescriptorType
 "C", 0x00,
 "D", 0x00,
 "C", 0x00,
 " ", 0x00,
 "R", 0x00,
 "S", 0x00,
 "-", 0x00,
 "2", 0x00,
 "3", 0x00,
 "2", 0x00,
 " ", 0x00,
 "D", 0x00,
 "e", 0x00,
 "m", 0x00,
 --30
 "o", 0x00 
}
--
include usb_serial
INTCON_GIE = false
usb_serial_init()
-- init adc
adc_init()
ADCON1 = 0      -- Vref = Vdd - Vss
;ADCON1_PVCFG = 0 -- PVref = Vdd
;ADCON1_NVCFG = 0 -- NVref = Vss
const BYTE adcPortNum[] = {4, 5, 6, 7, 8, 9, 10, 11, 255} -- 255 = EOL
-- set analog ports
var BYTE arrayPos_i = 0
while (adcPortNum[ arrayPos_i] < 255) loop
      set_analog_pin( adcPortNum[ arrayPos_i] )
      arrayPos_i = arrayPos_i + 1
end loop
;ADCON0_ADON = true

-- declare const vars
const byte gainer_str[] = "pi-pi- gagaga zazaza-"
-- LED
set_digital_pin(4)
set_digital_pin(5)
alias led             is pin_c0
alias led_direction   is pin_c0_direction
alias led2            is pin_c1
alias led2_direction  is pin_c1_direction
-- bootloader
set_digital_pin(6)
alias bootloader_program_pin    is pin_c2
alias bootloader_program_pin_direction  is pin_c2_direction
led_direction = output
led2_direction = output
bootloader_program_pin_direction = input
--main loop
var byte ch
var word adcValue_w
var byte adcValue_byte
--
var bit newLine_bit
led = off
led2 = on
forever loop
        usb_serial_flush()
        if ( usb_serial_read( ch ) ) THEN
          newLine_bit = True
          if ( ch == 0x39 ) then -- 9
             print_string( usb_serial_data, gainer_str )
             led = !led
          elsif ( ch >= 0x30 ) & ( ch <= 0x37 ) THEN -- 0 to 8
            if (ADC_HIGH_RESOLUTION) then
               -- 10bits ADC
               adcValue_w = adc_read(adcPortNum[ ch - 0x30 ])
               print_word_dec( usb_serial_data, adcValue_w )
            else
               -- 8bits ADC
               adcValue_byte = adc_read_low_res(adcPortNum[ ch - 0x30 ])
               print_byte_dec( usb_serial_data, adcValue_byte )
            end if
          else
             newLine_bit = False
          end if
          if (newLine_bit) THEN
             usb_serial_data = 0x0d
             usb_serial_data = 0x0a
          end if
        end if
        if ( bootloader_program_pin == low ) THEN
          analog_off()
          adc_off()
          comparator_off()
          var word big_counter = 0xFFFF
          -- disable timer 1
          T1CON = 0x00
          -- disable USB
          UCON = 0x00
          -- Long Long Loop
          while big_counter != 0  loop
           big_counter = big_counter - 1
          end loop
          ASSEMBLER
            reset
            --goto 0x0000
          end ASSEMBLER
        end if
end loop
--

0 件のコメント:

コメントを投稿