L A B 3 C O D E
s e r i a l c o m m u n i c a t i o n c o d e
' PicBasic Pro program to display result of ' 10-bit A/D conversion through serial at 9600 baud ' ' Connect analog input to channel-0 (RA0) include "modedefs.bas" DEFINE osc 4 ' Define ADCIN parameters DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 10 ' Set sampling time in uS TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result TRISD = %00000000 TRISB = %00000000 ADCvar VAR WORD ' Create variable to store result txPin VAR PORTC.6 ylLED VAR PORTD.0 rdLED VAR PORTD.1 'grLED VAR PORTB.7 'redButton VAR PORTB.0 'switchVar VAR BYTE x VAR byte 'INPUT redButton 'OUTPUT grLED OUTPUT ylLED OUTPUT rdLED OUTPUT txPin x = 0 Pause 500 ' Wait .5 second GOSUB blink main: 'ADCIN 0, ADCvar ' Read channel 0 to adval 'serout2 txPin, 16468, ["my adcvar = ", DEC ADCvar, 13, 10] ' with linefeed and carriage return (10, 13) 'if ADCvar > 150 then ' HIGH ylLEd 'else ' LOW ylLED 'endIF for x = 0 to 255 PORTB = x PAUSE 1000 SEROUT2 txPin, 16468, ["number = ", DEC x, 13, 10] NEXT PAUSE 3000 ' switchVar = PORTB.0 ' grLED = switchVar 'if redButton == 1 then ' x = x+1 'ENDIF ' SEROUT2 txPin, 16468, ["switchVar = ", DEC switchVar, 13, 10] ' SEROUT2 txPin, 16468, ["number of times on = ", DEC x, 13, 10] ' Debugging LED 'IF X > 10 THEN ' HIGH rdLED 'ENDIF GoTo main blink: HIGH ylLED PAUSE 300 LOW ylLED PAUSE 300 HIGH ylLED PAUSE 300 LOW ylLED PAUSE 300 HIGH ylLED PAUSE 300 LOW ylLED PAUSE 300 return