Great information, Henrik. Thanks for the detailed explanation. More questions to follow soon.
Great information, Henrik. Thanks for the detailed explanation. More questions to follow soon.
Roadblock #1.
I got a hold of a MCP4911E. If I understand the datasheet correctly, this chip has literally one command that is a word sized variable. I'm using the internal oscillator on a 16F690. What am I doing wrong?
Code:' ================================================================ CS VAR PORTC.7 CLK VAR PORTC.1 SDI VAR PORTC.0 LDAC VAR PORTC.2 ' ' ================================================================ X VAR WORD 'GENERAL TIMER ' PROGRAM INIT ' ================================================================ ' MAIN LOOP ' ================================================================ CS=1 X=0 MAIN: TOGGLE PORTB.4 GOSUB SEND_VOLTS PAUSE 100 GOTO MAIN ' ' ================================================================ SEND_VOLTS: TOGGLE PORTB.7 TOGGLE PORTB.5 TOGGLE PORTB.6 TOGGLE PORTB.7 LOW CS LOW LDAC PAUSE 100 SHIFTOUT SDI,CLK,0, [%011100111110] HIGH CS 'HIGH LDAC RETURN
Hi,
Never used it and don't have access to one to try with.
BUT the datasheet says that all write operations are 16 bits, 4 configuration bits followed by 12 databits where the 2 least significant bits doesn't matter for the 10-bit MCP4911. You're only sending 12 bits.
You're using MODE 0, which accordning to the manual shifts data out LSB first while the MCP4911 expects MSB first. Doesn't really matter as long as you remember to put the bits in the correct order before sending them. SHIFTOUT does 8 bits by default, if you want anything else (up to 32) you need to specify that.
Also, make sure you don't have any analog functions (comparator, ADC) mutliplexed onto the pins you're using. They are usually, but not always, turned on by default - always double check if you haven't already.
I'd probably try:/Henrik.Code:LOW CS SHIFTOUT SDI, CLK, 1, [%0011010101010100\16] HIGH CS LOW LDAC PAUSEUS 100 HIGH LDAC
I'm not getting anything. This is the entire program. Do I need to configure the SSPSTAT register or one that I missed?
Code:include "modedefs.bas" ' REGISTERS AND PINOUT ( 1 = IN; 0 = OUT ) OPTION_REG = %10000000 'PORT A&B Pull-Ups disabled (look WPUA & WPUB) ANSEL = %00000000 'Disable analog inputs Channels 0 to 7 ANSELH = %00000000 'Disable analog inputs Channels 8 to 11 WPUB = %00000000 'Disable weak pull-ups ADCON0 = %00000000 'A/D Module is OFF CM1CON0 = %00000000 'Comparator1 Module is OFF CM2CON0 = %00000000 'Comparator2 Module is OFF INTCON = %00000000 'INTerrupts CONtrol TRISA = %00000000 'Set Input/Output (0 to 5) PORTA = %00000000 'Ports High/Low (0 to 5) TRISB = %00000000 'Set Input/Output (4 to 7) PORTB = %00000000 'Ports High/Low (4 to 7) TRISC = %00000000 'Set Input/Output (0 to 7) PORTC = %00000000 'Ports High/Low (0 to 7) ' ALIAS & MODIFIERS ' ================================================================ CS VAR PORTC.7 CLK VAR PORTC.1 SDI VAR PORTC.0 LDAC VAR PORTC.2 ' VARIABLES & COSTANTS ' ================================================================ X VAR WORD 'GENERAL TIMER ' ' ================================================================ ' MAIN LOOP ' ================================================================ CS=1 X=0 HIGH LDAC MAIN: TOGGLE PORTB.4 GOSUB SEND_DATA PAUSE 1000 GOTO MAIN ' SUB - ROTINES ' ================================================================ SEND_DATA: TOGGLE PORTB.7 TOGGLE PORTB.5 TOGGLE PORTB.6 TOGGLE PORTB.7 LOW CS SHIFTOUT SDI, CLK, 1,[%1001111111111111/16] high cs LOW LDAC PAUSE 10 HIGH LDAC RETURN
Hi,
SHIFTIN/SHIFTOUT are bit-banged commands and does NOT use the SSP/MSSP module so you don't need to set that up. There are no "high level" PBP commands to do SPI (or I2C) with the SSP/MSSP module so you'll need to "drive it" manually - which isn't that hard. But again, since you're using SHIFTOUT it doesn't matter.
Right now you're sending MSB first - which is great. But the very first bit (Bit 15) you're sending is '1' which, if you look at the datasheet, means 'Ignore this command'. So, if everything else is alright, it should do exactly what you're seeing - nothing.
From the datasheet:Do you have a scope or logic analyzer you can use to verify that the pins are doing what they should?bit 15
0 = Write to DAC register
1 = Ignore this command
bit 14 BUF: VREF Input Buffer Control bit
1 = Buffered
0 = Unbuffered
bit 13 GA: Output Gain Selection bit
1 = 1x (VOUT = VREF * D/4096)
0 = 2x (VOUT = 2 * VREF * D/4096)
bit 12 SHDN: Output Shutdown Control bit
1 = Active mode operation. VOUT is available.
0 = Shutdown the device. Analog output is not available. VOUT pin is connected to 500 ktypical)
bit 11-0
D110: DAC Input Data bits. Bit x is ignored.
/Henrik.
You located the problem Henrik! Unless I missed it, I didn't see where the datasheet indicates you must send an extra bit before sending data. I'm guessing this is something I just should have known? Maybe I'm just not understanding it, I don't know.
Anyhow, it seems to work well, I like it. I know there's a different way to do it but I've tried a few different things and I can't get it to work like I want it. I'll be using this in the "off" position or on with a continually adjusting voltage.
If I need it off, I can just send this command: SHIFTOUT sdi,clk, 1, [%00110000001101100\16]
When I send a specific voltage, I would send the above command with the SHDN bit set to zero. Now, when I want to change just the 10 bit number, how can I do it? The first five bits will have two numbers for my application, which are %00110 or %00111. Obviously the 10 bit number changes but isn't it possible to send something like SHIFTOUT sdi,clk, 1, [SET, VOLTAGE] where "set" is %00111 and "voltage" is %000001101100.
Awesome! I found the answer I was looking for. It was located in this post: http://www.picbasic.co.uk/forum/show...6874#post16874
Hi,
I'm glad you've got it working. I don't understand the reference to "an extra bit" but perhaps it doesn't matter now that it works. I see that you have 17 bits in your SHIFTOUT command but you're actually only sending 16.
For reference, this is the datasheet I'm looking at. Section 5.2 explains how the write command works, ie 4 config bits and 12 databits for a total of 16bits. The transfer from the input shift register to the actual device will occur only if 16bits has been shifted in, you can't do less or it will be ignored.
If LDAC is low the output voltage will be updated at the rising edge of CS. If LDAC is high at the rising edge of CS the output will be updated at the falling edge of LDAC. This allows multiple DACs to be connected to the SPI bus and writen one after the other and then, with their LDAC pins connected together, their outputs gets updated simultanously instead of one by one as they are written to by the SPI bus.
On the next page, register 5.2 shows the write command register for the MCP4911 and what each of the config bits do. Again, no "extra bit" as far as I can see.
You can't just write the 10 databits to the device, you must transfer all 16 bits including the config bits - even if they don't actually change. If you don't transfer 16bits the transfer will be ignored as explained in section 5.2.
Here's something you could do to make it more clear what's happening.The above would then shiftout the value 12744, which in binary is 0011000111001000.Code:OutWord VAR WORD WRITE_DAC CON 0 Vref_BUF CON 16384 Vref_UnBuf CON 0 GAIN_1x CON 8192 GAIN_2x CON 0 VOut_ON CON 4096 VOut_OFF CON 0 OutWord = WRITE_DAC + VRef_UnBuf + GAIN_1x + VOut_ON + 456 ' Where 456 is your analog value. SHIFTOUT SDI, CLK, 1, [OutWord\16]
/Henrik.
Bookmarks