PDA

View Full Version : How can I adjust in USBDemo A/D converter from 8 to 10 Bit resolution?



Kuba230
- 1st July 2008, 12:02
Hi all,
I’m beginner and I tried modified A/D converter of USBDemo from 8 to 10 Bits, but does not work. I insert definition for 10 Bits resolution, but output value is still 8 Bits.
How can I adjust in USBDemo A/D converter from 8 to 10 Bit resolution?

Thanks, Kuba230


' USBDemo
' =======
'
' File name : USBDemo.pbp
' Company : Mister E
' Programmer : Steve Monfette
' Date : December 30,2006
' Device : PIC18F4550 & 4MHZ crystal
'
'
' Short and sweet demo to send and read from USB bus.
'
' Hardware:
' ---------
' 4 Push Buttons on PORTA<5:2> with pull-down resistors
' 2 Trim pot on PORTA<1:0>
' 8 LEDs attach between PORTB and GND
' 2 LEDs attach to CCP<2:1> pins
' 4 MHZ crystal & all the usual USB stuff
'
'
' Software:
' ---------
' A ready to run PC interface is also available, it's VB6 source code as well
'
' Enjoy!
'
'
' Pic Configuration
' =================
asm
__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
; ; ; USB clock source comes from the 96 MHz PLL divided by 2
; ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
; No prescale (4 MHz oscillator input drives PLL directly)


__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48

Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 20 ' puvodni (50) Set sampling time in uS

Darrel Taylor
- 1st July 2008, 20:19
There are 2 parts to Steve's USBdemo.

The PicBasic Pro program for the PIC, and a visual basic program for the PC.
It would have to be changed on both ends.

I can show you how to change it on the PIC side. But I don't do Visual Basic.
<br>

Kuba230
- 1st July 2008, 20:56
If you show me how to change it on the PIC side, it's enough. For Visual Basic I will find way to change it.

Thanks,
Kuba230


USBDemo
' =======
' File name : USBDemo.pbp
' Company : Mister E
' Programmer : Steve Monfette
' Date : December 30,2006
' Device : PIC18F4550 & 4MHZ crystal
'
' Short and sweet demo to send and read from USB bus.
'
' Hardware:
' ---------
' 4 Push Buttons on PORTA<5:2> with pull-down resistors
' 2 Trim pot on PORTA<1:0>
' 8 LEDs attach between PORTB and GND
' 2 LEDs attach to CCP<2:1> pins
' 4 MHZ crystal & all the usual USB stuff

' Software:
' ---------
' A ready to run PC interface is also available, it's VB6 source code as well
'
' Enjoy!
'
' Pic Configuration
' =================
asm
__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
; ; ; USB clock source comes from the 96 MHz PLL divided by 2
; ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
; No prescale (4 MHz oscillator input drives PLL directly)


__CONFIG _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
; ; ; Oscillator Switchover mode disabled
; ; Fail-Safe Clock Monitor disabled
; XT oscillator, PLL enabled, XT used by USB
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE OSC 48

Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 20 ' puvodni (50) Set sampling time in uS

' Hardware configuration
' ======================
'
' I/O and PORTs
' -------------
PORTB = 0
PORTC = 0
PORTD = 0
PORTE = 0
TRISB = 0
TRISC = 0
TRISA = $FF
TRISD = 0
TRISE = 0
'
' A/D converter
' -------------
ADCON0 = %00000001 ' A/D converter on, CH0 selected
ADCON1 = %00001101 ' AN<1:0> Analog, other digital
ADCON2 = %10010110 ' FOSC/64, 4 TAD, right justified
'
' CCP/PWM
' -------
CCP1CON = %00001100 ' CCP1, PWM mode
CCP2CON = %00001100 ' CCP2, PWM mode
PR2 = 249 ' 12khz PWM Freq
T2CON = %00000101 ' TMR2 on, prescaler 1:4
'
' USB module
' ----------
UCFG var byte EXT ' include UCFG register... Yeah Melabs didn't :o(
ucfg = %00010100 ' enable internal USB pull-up, Full speed USB

'
' Interrupt definition
' ====================
' TMR0 interrupt used to keep USB connection alive
' by sending USBSERVICE at each 100uSec or so.
INTCON = %10100000 ' Enable global and TMR0 interrupts
T0CON = %10000000 ' TMR0, CLK internal, prescaler 1:2, T0ON

'
' Variables & constants definition
' ================================
USBBufferSizeTX con 8 ' input
USBBufferSizeRX con 8 ' output
USBBufferCount Var Byte '
USBBufferIn var byte[8] ' store incomming USB data
USBBufferOut Var Byte[8] ' store outgoing USB data
DataToSend var byte[8] ' store ADCs & pushButton data to send to USB
String1 var byte[8] ' hold 'USB DEMO' string
string1[0]="U"
string1[1]="S"
string1[2]="B"
string1[3]=" "
string1[4]="D"
string1[5]="E"
string1[6]="M"
string1[7]="O"

@ADRead = ADRESL ; use to read both ADCs register in one shot
ADRead VAR WORD EXT ' make it available to use in PBP
'
@Duty1=_USBBufferIn+1 ; use to read both Duty bytes from specifics
@Duty2=_USBBufferIn+3 ; USBBufferIn locations in one shot
Duty1 var word EXT ' make them available
Duty2 var word EXT ' to use in PBP

'
' Constants definition
' ====================
TMR0IF VAR INTCON.2 ' TMR0 overflow int flag
TMR0ON VAR T0CON.7 ' TMR0 on/off bit
TMR0IE VAR INTCON.5 ' TMR0 interrupt enable/disable bit
GoDone var ADCON0.1 ' ADC conversion

'
' Macro(s) definition
' ===================
goto SwHwInit ' skip macros
asm
Reload_TMR0 macro
; Use to stop, reload, clear overflow flag and restart TMR0
BCF _TMR0ON ; stop timer
MOVE?CW d'65000',TMR0L ; reload for timebase ~100uSec
BCF _TMR0IF ; clear overflow flag
BSF _TMR0ON ; start timer
ENDM

SendUSB macro array
; Use to Copy an specific array to USBBufferOut AND send it
; to USB bus
variable i=0
while i<8
MOVE?BB (array+i),(_USBBufferOut+i)
i+=1
endw
L?CALL _DoUSBOut
endm
endasm

'
' Software/Hardware initialisation
' ================================
SwHwInit:
pause 500 ' Settle delay
usbinit ' initialise USB
@ Reload_TMR0 ; Reload timer0
ON INTERRUPT GOTO DoUSBService

'
' Main program start
' ==================
Start:
' Here we will check the status of push buttons on PORTA<5:2>
' do adc conversion on AN0 and AN1 and send everything to
' the USB bus
'
' DataToSend[0&1]: AN0, AN1 8 bits ADC results
' DataToSend[5:2]: PORTA<5:2> status
' ------------------------------------------------------------
'
' Look if there's any push button pressed, if so, send String1
' to USB bus
' ------------------------------------------------------------
if (PORTA>>2)!=0 then
@ SendUSB _String1
endif
'
' Do ADC conversion and save it to specific DataToSend array
' ----------------------------------------------------------
ADCON0.2=ADCON0.2 ^ 1 ' Switch between AN0 & AN1
PAUSEUS 10 ' arbitrary SamplingTime
GODONE=1 ' start conversion
WHILE GODONE : WEND ' wait 'till finish
DATATOSEND[ADCON0.2]=(ADREAD>>2) ' save it to DataToSend array
'
' Read pushbutton status and save it to DataToSend array
' ------------------------------------------------------
datatosend[2] = PORTA.2
datatosend[3] = PORTA.3
datatosend[4] = PORTA.4
datatosend[5] = PORTA.5
datatosend[6] = 0
datatosend[7] = 0
'
' Send data to USB bus
' --------------------
@ SendUSB _DataToSend
'
' Check if there's any incomming data
' -----------------------------------
gosub dousbin
'
' Redo from start
' ---------------
goto start
'
' Subroutines area
' ================
'
DoUSBIn:
'
' Check and receive data from the USB bus
' =======================================
' USBBufferIn[0] : PORTB led status
' USBBufferIn[1&2]: CCP1 PWM duty cycle LSB first (Duty1)
' USBBufferIn[3&4]: CCP2 PWM duty cycle LSB First (Duty2)
tmr0ie = 0 ' disbale TMR0 int
USBBufferCount = USBBufferSizeRX ' RX buffer size
USBService ' keep connection alive
USBIn 1, USBBufferin, USBBufferCount, Timeout ' read data, if available
Timeout: '
@ Reload_TMR0
tmr0ie = 1 ' re-enable TMR0 int
PORTB = USBBUFFERIN[0] ' output to PORTB

CCP1CON.5=DUTY1.1 ' load CCP1 duty value
CCP1CON.4=DUTY1.0 ' with Duty1
CCPR1L=DUTY1>>2 '

CCP2CON.5=DUTY2.1 ' load CCP2 duty value
CCP2CON.4=DUTY2.0 ' with Duty2
CCPR2L=DUTY2>>2 '
return
'
'
DoUSBOut:
'
' Send data to the USB bus & Wait for USB interface to attach
' ================================================== =========
TMR0IE = 0 ' Disable TMR0 interrupt
WaitPC: '
USBBufferCount = USBBufferSizeTX ' TX buffer size
USBService ' keep connection alive
USBOut 1, USBBufferOut, USBBufferCount, Waitpc ' if bus available, transmit data
@ Reload_TMR0
tmr0ie=1 ' Re-Enable TMR0 interrupt
return

DISABLE
DoUSBService:
usbservice ' keep connection alive
@ Reload_TMR0 ; reload timer
RESUME ' getout of here
ENABLE

Darrel Taylor
- 1st July 2008, 21:26
Steve was controlling the A/D converter manually. So adding the ADC_ DEFINE's won't have any effect.

He was also getting a bit tricky (in a good way), so this might seem a little odd too.


' Do ADC conversion and save it to specific DataToSend array
' ----------------------------------------------------------
ADCON0.2=ADCON0.2 ^ 1 ' Switch between AN0 & AN1
PAUSEUS 10 ' arbitrary SamplingTime
GODONE=1 ' start conversion
WHILE GODONE : WEND ' wait 'till finish
; DATATOSEND[ADCON0.2]=(ADREAD>>2) ' save it to DataToSend array
DATATOSEND[ADCON0.2*2]=ADREAD.LowByte
DATATOSEND[ADCON0.2*2+1]=ADREAD.HighByte
'
' Read pushbutton status and save it to DataToSend array
' ------------------------------------------------------
datatosend[4] = PORTA.2
datatosend[5] = PORTA.3
datatosend[6] = PORTA.4
datatosend[7] = PORTA.5
; datatosend[6] = 0
; datatosend[7] = 0

With the above changes, the packet being sent will have AN0 in the first 2 bytes, 0 is the lowbyte.

AN1 is in bytes 2 and 3. and the pushbutton status is moved to bytes 4-7.

Hope you can move things easily on the VB side.

hth,

Kuba230
- 4th July 2008, 16:12
Hi Mr. Taylor,

I did not tray still your advice for modification of USBDemo program, because I take my holiday. When I com back, then I will try.

Thanks,
Kuba230

Kuba230
- 11th July 2008, 22:10
Hi Mr. Taylor,
I made changes of USBDemo picbasic code according your advice and after compilation I obtain error messages:

Error[113 ] c:\pbp_prog\usbdemo.asm 61 : Symbol not previously defined (ADRead)
Error[113 ] c:\pbp_prog\usbdemo.asm 62 : Symbol not previously defined (ADRead)

I do not know why is happen?

Thanks,
Kuba230

Darrel Taylor
- 11th July 2008, 23:12
Oops, I should have compiled it first.

Try these 2 lines instead...
DATATOSEND[ADCON0.2*2]=ADRESL
DATATOSEND[ADCON0.2*2+1]=ADRESH
<br>

Kuba230
- 30th July 2008, 08:45
Hi Mr. Taylor,
I did those changes to display 10 Bits value in Visual Basic program for USBDemo, it works perfectly, but only for one channel, AN0. What I have to change in picbasic USBDemo program to obtain two 10 Bits channels, AN0 and AN1?

;incomming ADC reading and PORTA status
----------------------------------------

;pAN0.Value = BufferIn(1) ' Display ADC results to the progress bars
pAN0.Value = Val(BufferIn(1)) + Val(BufferIn(2)) * 256 'Display ADC for AN0
;pAN1.Value = BufferIn(2) ' Display ADC results to the progress bars

'
;lValAN0.Text = Val(BufferIn(1)) ' Display Progress bars Values
lValAN0.Text = Val(BufferIn(1)) + (Val(BufferIn(2)) * 256 'Display ADC for AN0
;lValAN1.Text = Val(BufferIn(2)) ' Display Progress bars Values

Note:
Also is necessary to change maximal value of progressbar properties pAN0 from 255 to 1023

Thanks
Kuba230

Darrel Taylor
- 30th July 2008, 10:45
lValAN1.Text = Val(BufferIn(3)) + (Val(BufferIn(4)) * 256 'Display ADC for AN1

Kuba230
- 31st July 2008, 08:56
Hi Mr. Taylor,
Everything is working very well.
Thank you very much for your help.

Kuba230