' ============================================================================ ' TR1_ad_test.bas ' TR1 analog to digital converter test using Proton Basic ' This program performs the following tests... ' Send loop number to the serial port at 9600 baud ' Blink status led ' Read the A to D converters ' Write digital values to serial port ' Delay and loop ' ============================================================================ ' ============================================================================ ' DEFINE HARDWARE CONFIGURATION PARAMETERS Device 18F2620 ' Select device type XTAL = 40 ' Set clock frequency to 40 MHz CONFIG_START ' Set hardware configuration parameters OSC = HSPLL ' Set 4x PLL on (10 MHz crystal * 4x = 40 MHz) CONFIG_END ' ============================================================================ ' DEFINE PHYSICAL IO PINS SYMBOL NAMES Symbol ad0_pin = PORTA.0 : TRISA.0 = 1 ' Analog Input, analog conn pin 6 Symbol ad1_pin = PORTA.1 : TRISA.1 = 1 ' Analog Input, analog conn pin 5 Symbol ad2_pin = PORTA.2 : TRISA.2 = 1 ' Analog Input, analog conn pin 4 Symbol ad3_pin = PORTA.3 : TRISA.3 = 1 ' Analog Input, analog conn pin 3 Symbol lcd_out = PORTA.4 : TRISA.4 = 0 ' LCD serial output Symbol key_in = PORTA.5 : TRISA.5 = 1 ' Primary trigger input Symbol net_assert = PORTC.0 : TRISC.0 = 0 ' OC transistor output to assert net traffic Symbol status_led = PORTC.1 : TRISC.1 = 0 ' System status LED Symbol pwm_pin = PORTC.2 : TRISC.2 = 0 ' PWM Output, analog conn pin 1 Symbol net_cd = PORTC.3 : TRISC.3 = 1 ' Network carrier detect - 0 = traffic present Symbol out1 = PORTC.4 : TRISC.4 = 0 Symbol out2 = PORTC.5 : TRISC.5 = 0 Symbol out3 = PORTB.0 : TRISB.0 = 0 Symbol out4 = PORTB.1 : TRISB.1 = 0 Symbol out5 = PORTB.2 : TRISB.2 = 0 Symbol ptt_in = PORTB.3 : TRISB.3 = 1 Symbol inhibit_in = PORTB.4 : TRISB.4 = 1 ' ============================================================================ ' DEFINE PIC HW REGISTER CONFIGURATION PARAMETERS SPBRG = 64 ' Set baud rate to 9600 ' For 9600 baud, 64 for 40 MHz osc, 31 for 20 MHz osc, 15 for 10 MHz osc ' 10 = 57600 (40 MHz osc) ' 32 = 19200 (40 MHz osc) RCSTA = %10010000 ' Enable serial continuous receive TXSTA = %00100000 ' Enable transmit asynchronous mode ADCON1 = %00001011 ' Value=11 - Set AN0-AN3 to analog, the rest to digital PORTB_PULLUPS = On Declare ADIN_RES 10 ' Set A/D converter to 10 bit resolution Declare ADIN_TAD 32_fosc ' adin_tad = ' Set A/D converter oscillator (FRC = RC) ' adin_delay = 50 ' Set A/D converter sample time to 50 uS ' ============================================================================ ' DEFINE WORKING VARIABLES Dim i As Byte Dim x As Byte Dim loop_count As Word Dim msg As String * 128 ' ============================================================================ ' VARIABLE DECLARATION - A to D converters Symbol adquanta = 5.0 / 1024 ' Calibrate the output in volts from 0 to 5v. 256 for 8 bit, 1024 for 10 bit. Dim adscale As Float 'adscale = 1800/(10000+1800) ' Resistor dividers 10k input and a 1.8k to ground adscale = 6.55555 ' = 1 / (1800/(10000+1800)) Dim adraw1 As Word Dim adraw2 As Word Dim adraw3 As Word Dim adraw4 As Word Dim advolts1 As Float Dim advolts2 As Float Dim advolts3 As Float Dim advolts4 As Float Dim iii As Float ' ============================================================================ ' =====[ INITIALIZATION CODE ]================================================ ' ============================================================================ DelayMS 200 'Delay startup to make sure the CPU is stable ' INITIALIZE VARIABLES loop_count = 0 ' SET ALL OUTPUTS OFF Low out1 Low out2 Low out3 Low out4 Low out5 ' ============================================================================ ' =====[ MAIN PROGRAM LOOP ]================================================== ' ============================================================================ main_loop: ' ---------------------------------------------------------------------------- loop_count = loop_count + 1 ' Increment the counter ' ---------------------------------------------------------------------------- ' Blink status LED i times. DelayMS 400 For i = 1 To 3 Low status_led ' Set pin C1 low (0 volts) LED on DelayMS 50 ' Delay 50 ms High status_led ' Set pin C1 high (5 volts) LED off DelayMS 50 ' Delay 50 ms Next i DelayMS 400 ' ---------------------------------------------------------------------------- ' Read analog inputs adraw1 = ADIn 0 ' Read AD converter input 1 adraw2 = ADIn 1 ' Read AD converter input 2 adraw3 = ADIn 2 ' Read AD converter input 3 adraw4 = ADIn 3 ' Read AD converter input 4 ' Write AD input values to the serial port msg = "AD input pass #" + Str$(Dec loop_count) + ": " +_ Str$(Dec adraw1) + ", "+_ Str$(Dec adraw2) + ", "+_ Str$(Dec adraw3) + ", "+_ Str$(Dec adraw4) HSerOut [msg,13,10] ' ---------------------------------------------------------------------------- GoTo main_loop ' Go to the top of the loop ' ============================================================================ ' ============================================================================