hi
I am facing one problem during writing code for MSP430F5438 to connect ADC1248.I have use port4 as GPIO.M getting data on SDI line which is sending data to ADC.M putting my code down plz tell me the correction in receive function.
//Sample test program for MSP430-5438STK//Test RS//Return echo ot RS#include "io430.h"#define OSC_FREQ 18000000#define BAUDRATE 9600#define WAKEUP 0x00#define SLEEP 0x02#define SYNC 0x04#define RESET 0x06#define NOP 0xFF#define RDATA 0x12#define RDATAC 0x14#define SDATAC 0x16#define RREG 0x20 #define WREG 0x40 #define SYSOCAL 0x60#define SYSGCAL 0x61#define SELFOCAL 0x62#define cell 0x22 //unsigned char high_byte,low_byte;unsigned char put,byte;unsigned char reg;void CLOCK_XT2_Init(){ int timeout = 5000; P11DIR = P11DIR | 0x02 | 0x04; //set P11.1 & P11.2 as output P11SEL = P11SEL | 0x02 | 0x04; //set P11.1 & P11.2 as MCLK/SMCLK function P5SEL = P5SEL | 0x04 | 0x08; //set P5.2 & P5.3 as XT2IN/XT2OUT UCSCTL6 = UCSCTL6 & (~XT2OFF); //set XT2OFF=0 (bit8 = 0) --> enable XT2 UCSCTL3 = UCSCTL3 | 0x20; //set SELREF = 010 --> FLL clock source = REFOCLK UCSCTL4 = UCSCTL4 | 0x200; //set SELA = 010 --> ACLK=REFO do { if (!(UCSCTL7 & 0x08)) break; UCSCTL7 = UCSCTL7 & ~(0x0F); // Clear XT2,XT1,DCO fault flags SFRIFG1 = SFRIFG1 & ~(0x02); // Clear fault flags (SFRIFG1.OFIFG = 0) timeout--; } while ((SFRIFG1 & 0x02) && timeout); // Test oscillator fault flag UCSCTL5 = UCSCTL5 | 0x0005; //MCLK/SMCLK divisor = 32 UCSCTL4 = UCSCTL4 | 0x55; //SMCLK=MCLK=XT2 UCSCTL4 = ((UCSCTL4 & ~0x100) & ~0x200) & ~0x400; //ACLK=XT1 (SELA = 0b000)}void UART2_Init(unsigned long baud){ float n, x; P9DIR = P9DIR | 1<<4; //set P9.4=1 (Transmitter) as output P9DIR = P9DIR & ~(1<<5); //set P9.5=0 (Receiver) as input P9SEL = P9SEL | 1<<4 | 1<<5; //set P9.4=1 & P9.5=1 as TX/RX UCA2CTL1 = UCA2CTL1 | 0x01; //UCSWRST = 1 (bit0 = 1) //page 417 UCA2CTL1 = (UCA2CTL1 | 1<<7) & ~(1<<6); //select SMCLK as BaudRate source clock (bit6 = 1; bit7 = 1) //calculate registers for the BaudRate + UCA2MCTL = UCA2MCTL | 0x01; //UCOS16 = 1 //calculate UCBR n = OSC_FREQ / baud; UCA2BR0 = (int)(n/16) % 256; UCA2BR1 = (int)(n/16) / 256; //caluclate UCBRS x = (int)((n-(int)(n))*8+0.5); UCA2MCTL = UCA2MCTL | (int)(x)<<1; //caluclate UCBRF x = (int)(((n/16 - (int)(n/16))*16) + 0.5); UCA2MCTL = UCA2MCTL | (int)(x)<<4; //calculate registers for the BaudRate - UCA2CTL0 = 0x00; //Set Format Frame: 8 bit data; no parity; 1 stop bit UCA2CTL1 = UCA2CTL1 & ~(0x01); //UCSWRST = 0 (bit0 = 0) //page 417}unsigned char UART2_Receive(){ // if (UCA2IFG & 0x01) //return UCA2RXBUF; // else // return 0; put=UCA2RXBUF;}void UART2_Transmit (){ //while (!(UCA2IFG & 0x02)); UCA2TXBUF ;//= cell}/*-------------Delay----------------------*/void Delay(void) { int j; for (j=0; j<10000; j++) for (j=0; j<10000; j++);}/*------------transfer byte-------------------------*/void transfer_byte(char data){ unsigned char x; P4OUT &=~ 0x08; /*clear SCLK*/ for(x=0;x<8;x++) { if((data & 0x80)==0) { P4OUT &=~ 0x04; } else P4OUT |= 0x04; P4OUT |= 0x08; /*SET SCLK*/ P4OUT &=~ 0x08; /*clear SCLK*/ data = data<<1; } }/*------------------receive_byte------------------------------------*/unsigned char receive_byte(){ { unsigned char y,a; for(y=0;y<8;y++) { a=(P4OUT & 0x02); // P4OUT |= 0x08; /*SET SCLK*/ /*if((reg & 0x80)==0) { reg==0; } else { reg==1; }*/ reg=a; reg = reg<<1; P4OUT &=~ 0x08; /*clear SCLK*/ } } return reg;}/*--------------------------write_to_register---------------------------------*/void write_to_register(char byte) //char command_first,char command_second{ P4OUT &=~ 0x01; /* Clear CSb */ //transfer_byte(command_first); // transfer_byte(command_second); transfer_byte(0x22); P4OUT |= 0x01; /* Set CSb */ }/*---------------------Read_register---------------------------*/void read_register() //char command_first,char command_second{ P4OUT &=~ 0x01; /*Clear CSb*/ // transfer_byte(command_first); /*Send Command*/ // transfer_byte(command_second); /*Send Command*/ put=receive_byte(); P4OUT |= 0x01; /*Set CSb*/}int main( void ){ // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; //char c; CLOCK_XT2_Init(); UART2_Init(BAUDRATE); P4DIR |= 0x01; //--------------set as a out put. P4SEL &=~ 0x01; //--------------set as GPIO Pin 4.0----------- P4OUT &=~ 0x01; //--------------set pin as low i.e CS=low P4DIR &=~ 0x02; //--------------set as a in put P4SEL &=~ 0x02; //--------------set as GPIO Pin 4.1----------- P4OUT &=~ 0x02; //--------------set pin as low i.e DRDY/DOUT P4DIR |= 0x04; //--------------set as a out put. P4SEL &=~ 0x04; //--------------set as GPIO Pin 4.2----------- P4OUT &=~ 0x04; //--------------set pin as low i.e DIN=low P4DIR |= 0x08; //--------------set as a out put. P4SEL &=~ 0x08; //--------------set as GPIO Pin 4.3----------- P4OUT &=~ 0x08; //--------------set pin as low i.e SCLK=low P4DIR |= 0x10; //--------------set as a out put. P4SEL &=~ 0x10; //--------------set as GPIO Pin 4.4----------- P4OUT &=~ 0x10; //--------------set pin as low i.e START=low P4DIR |= 0x20; //--------------set as a out put. P4SEL &=~ 0x20; //--------------set as GPIO Pin 4.5----------- P4OUT |= 0x20; //--------------set pin as low P4OUT |= 0x10; //start high Delay(); P4OUT &=~ 0x10; //start low Delay(); while (1) { write_to_register(0x11); //0x40,0x00, read_register(); //0x40,0x00 // UCA2TXBUF=transfer_byte(put); UART2_Transmit(); UART2_Receive(); }}
Sujit,
If I understand correctly, you are trying to bit-bang the port pins. Is this because you cannot use one of the SPI peripherals? If I'm following the code, you are not giving the full command to the device. You need to give the WREG command which will include the starting register you wish to write and the number of registers you wish to write (less one). This will be followed by the register data. To write one register you need to send three bytes of data. To write to the SYS0 register, for example, to change the gain to 128 and the data rate to 640 sps the bytes would be:
0x43 // this is write register 3
0x00 // write only one register (1-1=0)
0x77 // write gain=128 and data rate=640
All three bytes must have CS low throughout the total transfer (in other words it cannot toggle.)
It would be helpful to send me scope pictures of the communication, as that will show me the timing and the actual data being transmitted.
Best regards,
Bob B