democracyman
Joined: 22 Sep 2006 Posts: 101
|
Posted: Mon Aug 02, 2010 6:18 pm Post subject: Working starter test code for MCP3204 12 bit AD coverter. |
|
|
Code: | ' Good clean output for me, enjoy...
' Test starter program for MCP3204, working starter program
' I used software SPI as it was coveient for my development
' board. Used mega 128 on futurlec development board. I used
' a accessory 12 bit AD board from mikroelektronika with the MCP3204.
' I also had the data sheet for MCP3204.
' http://www.mikroe.com/eng/products/view/220/easyadc-board/
$regfile = "m128def.dat"
$crystal = 16000000
$baud = 9600
'This statement to config for the LCD
Config Lcdpin = Pin , Db4 = Porte.4 , Db5 = Porte.5 , Db6 = Porte.6 , Db7 = Porte.7 , E = Porte.3 , Rs = Porte.2
Config Lcd = 20 * 4
Cls
Dim B44 As Word
Dim B4 As Word
Dim B3 As Word
Dim B2 As Word
Dim B As Word
Dim C (2) As Byte
Dim A1 As Byte
Dim A2 As Byte
Dim A3 As Byte
Dim A4 As Byte
'Using Port C on the Futurlec Mega 128 stamp. Using Software Spi.
'Port C was convenient for me to use so I tried the Software SPI.
Config Spi = Soft , Din = Pinc.6 , Dout = Portc.5 , Clock = Portc.7
'Set pins on software SPI
Spiinit
'For Spi control pin PortC.0
Config Portc.0 = Output
'Signal to mcp3204 to give command to read 12 bit ADC data.
A1 = &B00011000 'Channel 0
'
'Start bit
'
'Single/Difference
''' D2, D1, D0 , These give the ADC line to be converted, see page 15, 16 of mcp3204 data sheet.
'SPI Commands to get data out of 1 to 4 channels.
A2 = &B00011001 'Channel 1
A3 = &B00011010 'Channel 2
A4 = &B00011011 'Channel 3
'To be sure to set up communication correctly pre set portC.0 as 1.
Portc.0 = 1
Do
' This is the SPI code to input data, input into C(1) and C(2) two bytes
'Note the msb bit in C(1) is the Null bit. Look at data sheet pg16 so then must
'do weird shifts to get correct output. A1 is the control to get data out of
'Channel 0.
Portc.0 = 0
Spiout A1 , 1
Spiin C (1) , 2
Portc.0 = 1
Waitms 10
' This coverts C(1), a byte, to B2, a word, B2 is 16 bits, to give room for 12 bits
B2 = C (1)
'Look at data sheet, MSB of C(1) is a null bit, means it is always zero. So
'must shift B2 left 5 bits. The three LSB bits of C(1), 02,01,00 are set at zero.
Shift B2 , Left , 5
'Shift C(2) to the right 3 bits, this sets the MSB bits of C(2), 07, 06, 05 as zeros.
Shift C (2) , Right , 3
'Convert C(2) a byte, to B3 a word, B3 is 16 bits so to have room for a 12 bit number.
B3 = C (2)
'Add the two together.
B4 = B2 + B3
' To see what is going on look at this.
Print ; "C(1) " ; Bin(c (1)) ; " B2 " ; Bin(b2 ) ; " B3 " ; Bin(b3 ) ; " B4 " ; Bin(b4 ) ; " B4 " ; B4
'Use a flag to CLS so to have good looking results on LCD.
If B4 <> B44 Then
Cls
Locate 1 , 1 : Lcd ; "B4 " ; B4
B4 = B44
End If
Loop
End |
_________________ Hello
If you need to contact me, my email
is
wretyduf@rocketmail.com |
|