ATMEGA16 i mcp3304 13-bit spi adc 8 single channel

Rasprava o AVR mikrokontrolerima, AVR projekti i drugo vezano za AVR...

Moderators: pedja089, stojke369, trax, InTheStillOfTheNight

Post Reply
ilacmano
Posts: 2
Joined: 10-12-2011, 21:53

ATMEGA16 i mcp3304 13-bit spi adc 8 single channel

Post by ilacmano »

Ja sam početnik u Bascom-u i molim za pomoć kako čitati SPI MCP3304 13-bitni adc, 8-single kanala sa Bascom AVR.
Molim ako neko ima primjer koji ispravno radi radi.

Napravio sam sam primjer u Bascomu, ali rezultati koje dobivam nisu oni koje mjerim digitalnim voltmetrom, razlika je cca 0.15 volti. neznam u čemu griješim.

BASCOM AVR KOD:
' Test program for MCP3304 13-Bit SPI external ADC
' I used software SPI as it was coveient for my development board.
' Used Mega 16 on Mikroelektronika development board EasyAVR4. I used
' a accessory 13-bit ADC board with the MCP3304.
' I also had the data sheet for MCP3304.

$regfile = "m16def.dat" ' specify the used micro
$crystal = 8000000 ' used crystal frequency
$baud = 2400 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
$lib "glcdKS108.lbx"

Config Graphlcd = 128 * 64sed , Dataport = Porta , Controlport = Portc , Ce = 0 , Ce2 = 1 , Cd = 2 , Rd = 3 , Reset = 5 , Enable = 4 , Mode = 6
Cls

' Variables
Dim C(2) As Byte , A_mcp3304 As Byte
Dim W_mcp3304 As Word
Dim Vout As Single
Dim Vtxt As String * 6
Dim I As Byte

' MCP3304 channel address
A_mcp3304 = &B11000000 'CH0

'Port B was convenient for me to use so I tried the Software SPI.
'MCP3304 Din --> MOSI, Dout --> MISO, SCK --> CLK, /CS --> PBO
' Software SPI
Config Spi = Soft , Din = Pinb.5 , Dout = Portb.6 , Ss = Portb.0 , Clock = Portb.7

Config Portb.0 = Output ' PORTB.0/4 SPI Control pin
Config Portb.5 = Input ' PORTB.5 Din MOSI
Config Portb.6 = Output ' PORTB.6 Dout MISO
Config Portb.7 = Output ' PORTB.7 CLK SCK

Cs Alias Portb.0 ' SS
Clk Alias Portb.7 ' CLK / SCK
Din Alias Portb.5 ' Din / MOSI
Dout Alias Portb.6 ' Dout / MISO

'Initalize SPI
Spiinit

Set Cs ' CS = 1
Waitms 100
Reset Cs ' CS = 0
Waitms 100
Setfont Font6x8
Cls

Lcdat 1 , 1 , "MCP3304 13-Bit ADC"
Lcdat 2 , 1 , "=================="

Do

For I = 1 To 8
W_mcp3304 = 0
Vout = 0

Select Case I
Case 1 : A_mcp3304 = &B11000000 'CH0
Case 2 : A_mcp3304 = &B11001000 'CH1
Case 3 : A_mcp3304 = &B11010000 'CH2
Case 4 : A_mcp3304 = &B11011000 'CH3
Case 5 : A_mcp3304 = &B11100000 'CH4
Case 6 : A_mcp3304 = &B11101000 'CH5
Case 7 : A_mcp3304 = &B11110000 'CH6
Case 8 : A_mcp3304 = &B11111000 'CH7
End Select

Reset Cs ' CS = 0
Reset Clk
' CLK = 0
'Bring cs low to start talking to device
Shiftout Dout , Clk , A_mcp3304 , 0 , 5
Spiin C(1) , 2

Set Cs ' CS = 1
Set Clk ' CLK = 0

W_mcp3304 = C(1)
Shift W_mcp3304 , Left , 8
Shift C(2) , Right , 6
' Read ADC value
W_mcp3304 = W_mcp3304 + C(2)

Lcdat 3 , 1 , "A_MCP:" ; Bin(a_mcp3304) ; " "
Lcdat 4 , 1 , "W_MCP3304: " ; W_mcp3304 ; " "

'Convert ADC value to Volts
Vout = W_mcp3304 * 5.0 ' !!! 5V Vref if you use other change this to the volt x.xxxx
Vout = Vout / 8192

Vtxt = Fusing(vout , "#.###")
Lcdat 6 , 1 , "Vout: " ; Vtxt ; " V Ch:" ; I ; " "
Waitms 1000
Next I

Loop

End 'end program

$include "Font6x8.font"
User avatar
Kukinjos
Pravi forumaš
Pravi forumaš
Posts: 1724
Joined: 28-03-2012, 18:01
Location: Split

Re: ATMEGA16 i mcp3304 13-bit spi adc 8 single channel

Post by Kukinjos »

Vjerojatno ti nije problem u kodu čim dobijaš relativno dobre rezultate, već u ulaznom otporu izvora ili u referentnom naponu. Nisi postao shemu, pa je teško odgovorit ovako! U pravilu ako koristiš potenciometar za probu, ulazni otpor S/H je 1k, pa se može javit ova greška, zato za veće ulazne otpore trebaš stavit buffer na mjerni ulaz (opeacijsko pojačalo s pojačenjem 1). Stavi shemu :wink:
Cezare, star sam i nemoćan, ali poslušaj moj savjet ....
Post Reply