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++) {
}
}
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;
}
}
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
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.
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/