Here is the code I use to access a AD5262 Digital Pot. Maybe it will help you out to see how I got it working. Also depending on the SPI port you are using you may need to look in the PIM(Port Integration Module) and connect the SPI(x) to the pins as needed. Sometimes they default to other functions unless configured using the PIM Registers.
In the code below I have two AD5262's on the same SPI bus so I use ChipSelect to determine which Enable lines to use.
Also here are the HIBYTE and LOWBYTE MAcros used in the code.
/* Returns the low byte of the word */
#define LOBYTE(w) ((BYTE)(w))
/* Returns the high byte of the word */
#define HIBYTE(w) ((BYTE)(((WORD)(w) >> 8) & 0xFF))
void InitializeAD5262(void) {
/* PortP bit3 ON */
PTP_PTP3 = 1;
/* PortM bit0 ON */
PTM_PTM0 = 1;
/* Configure PORTM outputs */
DDRM_DDRM0 = 1;
/* Configure PORTP outputs */
DDRP |= 0x0E;
/* Set the SPI baud rate */
SPI1BR = 0x0;
/* SPI on, master mode, CPHA = 0 */
SPI1CR1 = 80;
}
void AD5262Write(UINT8 ChipSelect, UINT8 Rdac, UINT8 Data) {
/* Local variables */
UINT8 Temp;
UINT16 Timeout;
UINT16 Command = Data;
/* Set address bit (16) */
if(Rdac)
Command |= 256;
/* SS pin low */
if(ChipSelect)
PTM_PTM0 = 0;
else
PTP_PTP3 = 0;
/* Make sure transmit buffer is empty */
while(!SPI1SR_SPTEF) {};
/* Write Address to SPI Data Reg */
SPI1DR = HIBYTE(Command);
/* Wait for transfer complete flag */
Timeout = 250;
while(!SPI1SR_SPIF) {
if(!(Timeout--))
return;
}
/* Read SPI Data Reg. */
Temp = SPI1DR;
/* Make sure transmit buffer is empty */
while(!SPI1SR_SPTEF) {};
/* Write Address to SPI Data Reg */
SPI1DR = LOBYTE(Command);
/* Wait for transfer complete flag */
Timeout = 250;
while(!SPI1SR_SPIF) {
if(!(Timeout--))
return;
}
/* Read SPI Data Reg. */
Temp = SPI1DR;
/* SS pin high */
if(ChipSelect)
PTM_PTM0 = 1;
else
PTP_PTP3 = 1;
}
Further to above, I have now gone back to trying to operate the LCD in 8 bit mode using SPI to transfer the data into a hc595 shift register. I am using 2 general purpose ports for enable and reset bits to the LCD, R/W pin is grounded.
The data seems to shifts into the S/R ok but only appears at the output parallel pins if I connect the storage and shift clocks together. Pulsing the master reset has no effect. I cannot get the LCD to even initialize. I have attached the code if anybody can see where I am going wrong I would be eternally greatful.
Martin
Message Edited by marti on 04-06-200608:56 AM
#define CMD 0 // For command mode RST-low, EN-high
#define DISP 1 // For display mode RST-high, EN-high
#define EN 0x08
#define RS 0x04
#define MR 0x10
/*--- Port direction and SPI setup ---*/
void SPI_Set(void){
DDRM = 0x38; // Set PortM as output
DDRT = 0x1C; // Set PortT as output
SPICR1 = 0x50; // Set SPI control register 1
SPICR2 = 0x0; // Set SPI control register 2
SPIBR = 0x0; //Set max baud rate
PTT |= MR; //Master reset for hc595 set high
}
/*--- Delay Code ---*/
/*--- Needs to be configured to Bus clock, Boot 24Mhz, Run 4Mhz ---*/
void Delay(unsigned int sec, unsigned int mult){
unsigned int i, j;
for(i = 0; i < sec; i++){
for(j = 0; j < mult; j++){
}
}
}
/*-- Routine to send command or character to display --*/
void set_lcd(char n, char reg){
unsigned char dummy;
//First send the byte to the HC595
if(reg == DISP){ // Command or write?
PTT |= EN|RS; // Command EN and RS high
}else{
PTT |= EN; // Write EN only high
}
SPIDR = n; // Send byte into shift register
while((SPISR & 0x80) == 0); // Wait for SPI ready SPIF interrupt flag
PTT &= ~MR; PTT |= MR; /*Pulse MR to load 595 storage register
This doesn't seem to have any effect. Data only latches when shift and storage clocks are connected together*/
dummy = SPIDR; //Clear ready bit
PTT &= ~(EN|RS); // Tell LCD to do it
Delay(3, 100); //100uS delay for LCD operation
}
/*---- Main program ---*/
void main(void){
EnableInterrupts;
SPI_Set(); // Initialize SPI system and serial ports
Delay(40,1000); //Wait 15mS after power on
set_lcd(0x30, CMD); //Function set 8 bits
Delay(10,1000);//wait 4.1mS
set_lcd(0x30, CMD); //Function set 8 bits
set_lcd(0x30, CMD); //Function set 8 bits
set_lcd(0x38, CMD); //Set 8 bit 2 lines
set_lcd(0xE, CMD); //Display on, Cursor on
set_lcd(0x6, CMD); //Set Cursor
for(;{
set_lcd(0x57, DISP); //display "W"
set_lcd(0x45, DISP); //display "e"
}
}
Message Edited by marti on 04-06-200608:59 AM