Havig trouble with SPI in PIC24FJ256GB412 - c

I am trying to configure a PIC24FJ256GB412 to use the SPI interface with an ADC module(ADS114S08). And now, I can view data by oscilloscope(by measuring the SDI pin). But the SPI1BUFL didn't receive correct data(always received 0x01). Below is the relevant code that I am using to configure the SPI.
uint8_t DATA_S[1000]; // SPI data buffer for Receiving
uint8_t DATA1; // SPI data buffer for Receiving
uint16_t DATA_counter=0;
void System_Initial(void)
{
//Setup IO
TRISDbits.TRISD0 = 0; //SCLK
TRISDbits.TRISD5 = 0; //ADC RESET
TRISDbits.TRISD10 = 0; //ADC START
TRISDbits.TRISD11 = 1; //ADC DRDY
TRISFbits.TRISF3 = 1; //SDI
TRISFbits.TRISF4 = 0; //SDO
ANSELDbits.ANSELD0 = 0; //Digital pins
ANSELDbits.ANSELD5 = 0;
ANSELDbits.ANSELD10 = 0;
ANSELDbits.ANSELD11 = 0;
ANSELFbits.ANSELF3 = 0;
ANSELFbits.ANSELF4 = 0;
//Setup SPI
//MCU operates in 16MHz, ADC internal clk 4MHz
SPI1BRGL = 1; //Baud rate = 4MHz
SPI1CON1L = 0x8020; //8bit mode //SCLK enable //Master Mode
SPI1CON1H = 0x2000; //AUDEN=0
SPI1CON2L = 0x0007; //8bit data
//Setup PPS
__builtin_write_OSCCONL(OSCCON & 0xBF);
_RP11R = _RPOUT_SCK1OUT; // RP11 -> SCK1.
RPINR20bits.SDI1R = 16; // RP16 -> SDI1.
_RP10R = _RPOUT_SDO1; // RP10 -> SDO1.
__builtin_write_OSCCONL(OSCCON | 0x40);
}
//SPI1 - Set ADS114S08
void SPI1_SetADC()
{
//set PGA
//Input MUX Register, address = 02h
//Gain setting Register, address = 03h
SPI1BUFL = 0x42; //WREG at 02h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //02h //AINP = AIN0, AINN = AIN1
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0B; //03h //PGA enabled, Gain = 011 =8
while( SPI1STATLbits.SPIBUSY==1);
//set Mode
//Data Rate Register, address = 04h
//Reference Control Register, address = 05h
SPI1BUFL = 0x44; //WREG at 04h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x14; //04h //Continuous conversion mode //0100 : 20 SPS
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x30; //05h
while( SPI1STATLbits.SPIBUSY==1);
//set Excitation Current Sources
//Excitation Current Register1(IDACMAG), address = 06h
//Excitation Current Register2(IDACMUX), address = 07h
SPI1BUFL = 0x46; //WREG at 06h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x01; //Two bytes
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x05; //06h //IDAC = 0101 = 500uA
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0xF0; //07h //use IDAC1 => AIN0
while( SPI1STATLbits.SPIBUSY==1);
}
//SPI1 - RREG
void SPI1_RREG()
{
SPI1BUFL = 0x20; //send RREG Command //read 00h
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x00; //1 byte
while( SPI1STATLbits.SPIBUSY==1);
SPI1BUFL = 0x0000; //send DUMMY data
while( SPI1STATLbits.SPIBUSY==1); //Wait Data
uint8_t register1= SPI1BUFL; //get data
DATA1 = register1;
DATA_S[DATA_counter%1000] = register1;
}
int main(void)
{
System_Initial();
LATDbits.LATD10 = 0; //ADC START=0
LATDbits.LATD5 = 0; //ADC RESET=0 //Enter RESET Status
uint32_t loop = 0;
loop = 4*4; //MCU 16Mhz, ADC internal clk 4Mhz
while (loop--); //wait 4clk => ADS114S08 tw(RSL)
LATDbits.LATD5 = 1; //ADC RESET low -> high //Enter Standby Mode
loop=4096*4;
while (loop--); //wait 4096clk => ADS114S08 td(RSSC)
SPI1_SetADC(); //set ADC Configuration
LATDbits.LATD10 = 1; //START high -> Start Conversion
//main while
while(1)
{
DATA_counter++;
SPI1_RREG();
}
}
bold
italic
As mentioned above, I can see data on SDI pin by oscilloscope.
enter image description here
And another data test.
enter image description here
By the waveform that we can see on the oscilloscope. The ADC module probably works the right way.
But... Let's see the MPLAB IDE side.
enter image description here
I can't get the right data(SPI1BUF = 0x0001). I have tried changing baudrate lower. But the result is no different. Is there a parameter I'm missing? Thanks for your help.
------------------------------------------0530-----------------------------------------------
Hi Craig, I have tried the code.
SPI1BUFL = 0x20; //send RREG Command //read 00h
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
SPI1BUFL = 0x00; //set 1 byte
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty
uint8_t register1=0;
SPI1BUFL = 0x00; //send DUMMY data
while(!SPI1STATLbits.SPIRBF); //hang until data is received
register1 = SPI1BUFL; //get data
The results were no difference. And I recorded it.
Oscilloscope:
enter image description here
IDE:
enter image description here
enter image description here

The SPI should clear SPI1BUFL every time as you transmit data.
So I try the code like this.
uint8_t SPI_EXCH(uint8_t data)
{
SPI1BUFL = data; // write to buffer for TX
while(!SPI1STATLbits.SPIRBF); // wait for transfer to complete
return SPI1BUFL; // read the received value
}
And every time you try to do an SPI exchange, use the function above.

Related

STM32F207 bare metal SPI RXNE and TXE not working?

I need to communicate with an eeprom chip(25lc1024) via SPI.
I can get the following code to work but have to resort to delays isntead of the flags alone.
Otherwise I can see on the scope that it is not waiting for all the bits to be shifted out and proceeds.
Any ideas why the flags are not stopping it?
void Init_SPI(void){
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; // enable SPI clock
GPIOA->AFR[0] |= SET_AFL_A_FOR_SPI1;
GPIOB->AFR[0] |= SET_AFL_B_FOR_SPI1;
SPI1_NSS = DESELECT_CHIP; // set the SS pin high
// initialize the SPI configuration register
SPI1->CR1 = SPI_CR1_MSTR // SPI master mode
| SPI_CR1_BR_1; // 010 bit rate prescale /8 (60MHz/8 = 7.5MHz)
SPI1->CR2 = SPI_CR2_SSOE;
SPI1->CR1 |= SPI_CR1_SPE; // enable SPI
}
unsigned int eeprom_read(unsigned long address, unsigned int chars_to_read)
{
char temp;
unsigned int result = 0;
SPI1_NSS = SELECT_CHIP; // set the SS pin low
SPI1->DR = READ_FROM_SPI; // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
SPI1->DR = (address & 0xFF0000) >> ADJ16; // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
SPI1->DR = (address & 0x00FF00) >> ADJ8; // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
SPI1->DR = (address & 0x0000FF); // send data out SPI
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer empty
delay();
temp = SPI1->DR;
SPI1->DR = DUMMY_8BIT; // send dummy 8 bits to generate clock
chars_to_read--;
while( !(SPI1->SR & SPI_SR_RXNE) ); // wait until receive buffer is not empty
//USART2->DR = SPI1->DR;
result = SPI1->DR;
if(chars_to_read != 0){
while((SPI1->SR & SPI_SR_RXNE) ); // wait until receive buffer is empty
delay();
SPI1->DR = DUMMY_8BIT; // send dummy 8 bits to generate clock
//delay();
while( !(SPI1->SR & SPI_SR_TXE) ); // wait until transmit buffer is empty
while( !(SPI1->SR & SPI_SR_RXNE) ); // wait until receive buffer is not empty
temp = SPI1->DR;
result = (result*256) + temp;
chars_to_read--;
}
delay();
SPI1_NSS = DESELECT_CHIP; // set the SS pin high
return result;
}
static void delay(void)
{
unsigned int j = FALSE;
for (j = 0; j < 25; j++) {
}
}

PIC18F97J60 interfacing with LT1661 chip

Currently I am trying to create a program that will output an analog signal to an LED using the LTC1661 chip with the PIC18F97J60.
The dac ltc1661 chip is connected to my board with the ports:
RC3 for SCK1
RC5 for SDO1
with the data pin being connected to my 8bit SPIBUF buffer.
Here is my code, its outputting to the led but I can't seem to make the led gradually increase in bright ness, its just a sudden on and off.
Any help would be appreciated.
#include "amt.h"
#include <p18f97j60.h>
int main(void) {
TRISCbits.RC3 = 0;
TRISCbits.RC5 = 0;
TRISCbits.RC7 = 0;
SSP1CON1 = 0b00100010; // Enable SPI, CKP=0, Master, Fosc/64
SSP1STATbits.CKE = 1; // CKE=1, (rising edge for shift register)
char temp;
while(1) {
//SSP1BUF = 0x00;
PORTCbits.RC7 = 0;
SSP1BUF = 0x98;
while(PIR1bits.SSPIF==0);
SSP1BUF = 0x00;
while(PIR1bits.SSPIF==0);
PORTCbits.RC7 = 1;
PORTCbits.RC7 = 0;
SSP1BUF = 0x98;
while(PIR1bits.SSPIF==0);
SSP1BUF = 0x04;
while(PIR1bits.SSPIF==0);
PORTCbits.RC7 = 1;
}
}

How to read FIFO Data from MAX30100 using STM32F4

i'm monitorating heart rate by the MAX30100(https://img.filipeflop.com/files/download/Datasheet_MAX30100.pdf) using the MCU STM32F4. I'm trying read the IR and RED data from the FIFO, but all returns are ZERO. The method MAX30100_Get_Num_Samples() returns 8. I modeled the code using the pseudo code from datasheet of MAX30100. I tried several solutions for my problem but doesn't work. I dont know if i'm following the right way to get data from the FIFO. My code:
I2C_HandleTypeDef hi2c3; //i2c used
uint16_t RED[50] = {0}, IR[50] = {0}; //buffers for RED and IR
uint8_t buffOximeter[5];
char buffer[32];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C3_Init();
MX_USB_DEVICE_Init();
MAX30100_Init();
while (1)
{
uint8_t numSamples = MAX30100_Get_Num_Samples();
MAX30100_Read_HeartBeat(numSamples);
for(int i = 0; i < numSamples; i++)
{
sprintf(buffer, "Amostra %d: %d / %d\n\r", i , IR[i], RED[i]);
CDC_Transmit_FS((char*)buffer, 50);
}
}
}
static void MX_I2C3_Init(void)
{
hi2c3.Instance = I2C3;
hi2c3.Init.ClockSpeed = 400000;
hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c3.Init.OwnAddress1 = 0;
hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c3.Init.OwnAddress2 = 0;
hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c3) != HAL_OK)
{
Error_Handler();
}
}
void MAX30100_Init(void)
{
HAL_I2C_Mem_Write(&hi2c3,0xAE,0x06,1,0x02,1,1000); //set heart rate mode
HAL_I2C_Mem_Write(&hi2c3,0xAE,0x09,1,0xFF,1,1000); //i50 ledCurrent
uint8_t sr = 0x01, pw = 0x3;
HAL_I2C_Mem_Write(&hi2c3,0xAE,0x07,1,(sr<<2)|pw,1,1000); //sr100, pw1600
HAL_Delay(50);
}
void MAX30100_Read_HeartBeat(uint8_t num)
{
for(int i=0;i<num;i++)
{
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAE,1,1000); //adress + write mode
HAL_I2C_Master_Transmit(&hi2c3,0x57,0x02,1,1000); //send fifo_wr_ptr
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAF,1,1000); //adress + read mode
uint8_t data;
HAL_I2C_Master_Receive(&hi2c3,0x57,&data,1,1000); //read fifo_wr_ptr
HAL_Delay(100); //STOP
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAE,1,1000); //adress + write mode
HAL_I2C_Master_Transmit(&hi2c3,0x57,0x05,1,1000); //send adress fifo data
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAF,1,1000); //adress + read mode
HAL_I2C_Mem_Read(&hi2c3,0x57,0x05,1,&buffOximeter,4,1000); //read fifo data
IR[i] = (buffOximeter[0] << 8) | buffOximeter[1];
RED[i] = (buffOximeter[2] << 8) | buffOximeter[3];
HAL_Delay(100); //STOP
HAL_I2C_Master_Transmit(&hi2c3,0x57,0xAE,1,1000); //adress + write mode
HAL_I2C_Master_Transmit(&hi2c3,0x57,0x04,1,1000); //send adress fifo_rd_ptr
HAL_Delay(100);
}
}
int MAX30100_Get_Num_Samples(void)
{
uint8_t wrPtr, rdPtr;
HAL_I2C_Mem_Read(&hi2c3,0x57,0x02,1,&wrPtr,1,1000);
HAL_Delay(50);
HAL_I2C_Mem_Read(&hi2c3,0x57,0x04,1,&rdPtr,1,1000);
HAL_Delay(50);
return (abs( 16 + wrPtr - rdPtr ) % 16);
}
connect the interrupt output pin (pin 13 active low) to an interrupt control pin on the cpu.
setup your cpu to be interrupted when the device indicates some event ready
Then, still in your interrupt handler function, read register 0 and determine which data is ready,.
the referenced datasheet, approx page 14 lists the order of communication events needed. Note that the posted code is NOT following the specified order of communication events
page 16, register 7, indicates how to initialize the SPO2 for the desired mode of operation

No response from UART

I'm able to receive with the following code, but unfortunately, nothing is sent back. What am I doing wrong?
#include <pic18f25k80.h>
#include "config.h"
#include <usart.h>
int i = 0;
unsigned char MessageBuffer[200];
void main() {
OSCCONbits.IRCF = 0b110; // 8MHz
TRISB6 = 0; // TX set as output
TRISB7 = 0; // RX set as output
// Clear TX interrupt
// Set RX interrupt
// 8-bit Asynch. mode
// BRGH = 1 = high baud mode
// 51 = ((8MHz/baud)/16)-1 with baud = 9600
Open2USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE
& USART_EIGHT_BIT & USART_BRGH_HIGH, 51 );
RC2IF = 0; // reset RX2 flag
RC2IP = 0; // not high priority
RC2IE = 1; // Eneble RX2 interrupt
INTCONbits.PEIE = 1; // enable peripheral interrupts
INTCONbits.GIE = 1; // enable interrupts
RCSTA2bits.SPEN = 1; // enable USART
while(1){
}
}
void interrupt ISR () {
if(PIR3bits.RC2IF == 1) {
if(i<200) { // buffer size
MessageBuffer[i] = Read2USART(); // read byte from RX reg
if (MessageBuffer[i] == 0x0D) { // check for return key
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i=0;
return;
}
i++;
RC2IF = 0; // clear RX flag
} else {
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i = 0;
return;
}
}
}
I'm transmitting the 0x41 hex code, I checked with the scope and see that is is being received. And according to the code I have, an echo of the received data should be sent back. When I check the TX pin, nothing is happening.
Add USART_CONT_RX to Open2USART to enable continuous receive.
Also, it's a good idea to do the minimum necessary in the interrupt service routine. Consider something like:
void interrupt ISR () {
char data;
if(PIR3bits.RC2IF == 1) {
data = Read2USART(); // always read byte from RX reg (clears RC2IF)
if(i<200) { // buffer size
MessageBuffer[i] = data; // read byte from RX reg
i++;
}
else{
// flag buffer full error
}
}
}
and doing the rest of what you are doing in the while(1) loop.

MicroC rs-485, pic16f887 String sending problems

How can I send the string "MY STRING" from a master pic to a slave?
I'm using MicroC RS-485 library example:
http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/rs-485_library.htm
Im trying to send string from master to slave: and seting dat[7] = "my string"; expecting dat[7] on slave with my string but im geting empty value...
Original code in the link bottom.
Master
char dat[10]; // buffer for receving/sending messages
char i,j;
sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
// Interrupt routine
void interrupt() {
RS485Master_Receive(dat);
}
void main(){
long cnt = 0;
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISD = 0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Master_Init(); // initialize MCU as Master
dat[0] = 0xAA;
dat[1] = 0xF0;
dat[2] = 0x0F;
dat[4] = 0; // ensure that message received flag is 0
dat[5] = 0; // ensure that error flag is 0
dat[6] = 0;
dat[7] = "MY STRING";
RS485Master_Send(dat,1,160);
RCIE_bit = 1; // enable interrupt on UART1 receive
TXIE_bit = 0; // disable interrupt on UART1 transmit
PEIE_bit = 1; // enable peripheral interrupts
GIE_bit = 1; // enable all interrupts
while (1){
// upon completed valid message receiving
// data[4] is set to 255
cnt++;
if (dat[5]) { // if an error detected, signal it
PORTD = 0xAA; // by setting portd to 0xAA
}
if (dat[4]) { // if message received successfully
cnt = 0;
dat[4] = 0; // clear message received flag
j = dat[3];
for (i = 1; i <= dat[3]; i++) { // show data on PORTB
PORTB = dat[i-1];
} // increment received dat[0]
dat[0] = dat[0]+1; // send back to master
Delay_ms(1);
RS485Master_Send(dat,1,160);
}
if (cnt > 100000) {
PORTD ++;
cnt = 0;
RS485Master_Send(dat,1,160);
if (PORTD > 10) // if sending failed 10 times
RS485Master_Send(dat,1,50); // send message on broadcast address
}
}
}
Slave:
char dat[9]; // buffer for receving/sending messages
char i,j;
sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
// Interrupt routine
void interrupt() {
RS485Slave_Receive(dat);
}
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
PORTB = 0;
PORTD = 0;
TRISB = 0;
TRISD = 0;
PORTA = 0;
TRISA = 0;
UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize MCU as slave, address 160
dat[4] = 0; // ensure that message received flag is 0
dat[5] = 0; // ensure that message received flag is 0
dat[6] = 0; // ensure that error flag is 0
RCIE_bit = 1; // enable interrupt on UART1 receive
TXIE_bit = 0; // disable interrupt on UART1 transmit
PEIE_bit = 1; // enable peripheral interrupts
GIE_bit = 1; // enable all interrupts
while (1) {
if(dat[7]=="MY STRING"){
RCA0_bit = 1;
}
if (dat[5]) { // if an error detected, signal it by
PORTD = 0xAA; // setting portd to 0xAA
dat[5] = 0;
}
if (dat[4]) { // upon completed valid message receive
dat[4] = 0; // data[4] is set to 0xFF
j = dat[3];
for (i = 1; i <= dat[3];i++){
PORTB = dat[i-1];
}
dat[0] = dat[0]+1; // increment received dat[0]
Delay_ms(1);
RS485Slave_Send(dat,1); // and send it back to master
}
}
}
char dat[9]; defines an array of 9 bytes, so dat[7] = "MY STRING"; will only store a truncated address not the actual string. memcpy can be used to copy strings from one buffer to another.
see: http://www.cplusplus.com/reference/cstring/memcpy/
dat[7]=="MY STRING" will not work as your comparing the 2 byte address of the string literal "MY STRING" with the single byte stored at dat[7]. If you need to compare strings for differences, strcmp works.
see: http://www.cplusplus.com/reference/cstring/strcmp/

Resources