SPI and click board usage

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
smsmsm
Posts: 3
Joined: 17 May 2022 11:17

SPI and click board usage

#1 Post by smsmsm » 29 Jun 2022 17:10

Hello everyone, first of all I am pretty new at this so maybe these questions look weird but I'm struggling to find a solution.

Anyway, I'm using EasyAVR and Stepper 16 click to run my stepper motor but can't get it running, I tried using SPI to alter slave registers in order to set MONTEN=1 (motor enable flag) and SLP=0 but unsuccessfully.

I read the datasheet, so basically it's 16 bit SPI communication, and to write to register first of all I need to send MSB as 1 (write) then four bit address and odd parity bit, and then data (10 bits). But nothing happens when I do that. (Obviously it's my first time using SPI so not completely sure what I'm doing).

I connected a click board to microbus 3rd port, and enabled SW5 switches (SCK, MISO, MOSI).
Here is the datasheet: https://download.mikroe.com/documents/d ... asheet.PDF
Here is my code:

Code: Select all

sbit Chip_Select at PORTB4_bit; // CS is at PB4
sbit Chip_Select_Direction at DDB4_bit;

// Clickboard is connected to microbus 3rd port

void main() {
  DDRB.F3 = 1; // NXT pin
  DDRA.F4 = 1; // DIR pin
  
  // Only one slave device
  Chip_Select = 1;
  Chip_Select_Direction = 1;
  
  // CSB is active low, data sampling on raising edge
  SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV16, _SPI_CLK_LO_LEADING);

  SPI_Set_Active(&SPI1_Read, &SPI1_Write); // Sets the SPI2 module active

  // Set CSB low for between 20us and 200us to "wake ti up"
  PORTB.F4 = 0;
  delay_us(100);
  PORTB.F4 = 1;
  delay_us(350); // 250us - wake up time
  
  // Write SLP=0
  Chip_Select = 0;
  SPI1_Write(0x91);
  SPI1_Write(0x00);
  Chip_Select = 1;
  
  // Set MOTEN=1 (8th bit in lower byte)
  Chip_Select = 0;
  SPI1_Write(0x88);
  SPI1_Write(0x00);
  Chip_Select = 1;
  
  delay_us(1000);
  
  while (1) {
    PORTB.F3 = 1; // Next micro-step
    delay_us(1000);
    PORTB.F3 = 0;
    delay_us(1000);
  }
}
Last edited by smsmsm on 30 Jun 2022 15:18, edited 1 time in total.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: SPI and click board usage

#2 Post by filip » 30 Jun 2022 09:45

Hi,

Why don't you take a look at the library and example given here :
https://github.com/MikroElektronika/mik ... /stepper16

Regards,
Filip.

smsmsm
Posts: 3
Joined: 17 May 2022 11:17

Re: SPI and click board usage

#3 Post by smsmsm » 30 Jun 2022 15:23

I looked at it, I basically copied configuration (same register values) but still no changes. I have a feeling that I'm not even sending stuff over SPI.

This is how it looks after looking at library:

Code: Select all

sbit Chip_Select at PORTA5_bit;
sbit Nxt_Step at PORTD4_bit;
sbit Dir_Step at PORTA6_bit;
sbit ErrB at PORTD2_bit;

sbit Chip_Select_Direction at DDA5_bit;
sbit Nxt_Step_Direction at DDD4_bit;
sbit Dir_Step_Direction at DDA6_bit;
sbit ErrB_Direction at DDD2_bit;


// Funkcija za hard reset drajvera
void hardReset() {
     Dir_Step = 0;
     Nxt_Step = 1;
     delay_us(1);
     delay_us(1);
     delay_us(1);
     
     Dir_Step = 1;
     delay_us(1);
     delay_us(1);
     delay_us(1);
     Dir_Step = 0;
     delay_us(22);
     
     Dir_Step = 1;
     delay_us(1);
     delay_us(1);
     delay_us(1);
     Dir_Step = 0;
     delay_us(22);
     
     Dir_Step = 1;
     delay_us(1);
     delay_us(1);
     delay_us(1);
     Dir_Step = 0;
     delay_us(22);
     
     Dir_Step = 1;
     delay_us(1);
     delay_us(1);
     delay_us(1);
     Dir_Step = 0;
     delay_us(22);
     
     Dir_Step = 1;
     delay_us(1);
     delay_us(1);
     delay_us(1);
     Dir_Step = 0;
     delay_us(22);

     delay_ms(100);
}

// Funkcija za budjenje drajvera
void wakeUp() {
     // Uredjaj se budi tako sto se posalje nizak signal na chip select pin
     Chip_Select = 0;
     delay_us(10);
     Chip_Select = 1;
     delay_us(500);
}

void initMain() {
     // Nxt i dir su izlazi
     Nxt_Step_Direction = 1;
     Dir_Step_Direction = 1;

     // ErrB je ulaz
     ErrB_Direction = 0;
     
     // Jedan slave uredjaj
     Chip_Select = 1; // Deselektujemo drajver
     Chip_Select_Direction = 1; // U pitanju je izlaz
     
     // Iz datasheeta CSB je aktivno nizak, a uzimamo podatke na uzlaznu ivicu
     SPI1_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV16, _SPI_CLK_LO_LEADING);
     
     // Aktiviramo SPI
     SPI_Set_Active(&SPI1_Read, &SPI1_Write);
}


void main() {
     
     initMain();

     hardReset();
     wakeUp();
  
     // Podesavanje drivera na osnovu datasheeta (upisivanje registara)
     
     // CR1 registar
     Chip_Select = 0;
     SPI1_Write(0x88);
     SPI1_Write(0x8F);
     Chip_Select = 1;
  
     delay_ms(10);
  
     // CR2 registar (svi biti 0)
     Chip_Select = 0;
     SPI1_Write(0x94);
     SPI1_Write(0x00);
     Chip_Select = 1;

     // CR3 registar
     Chip_Select = 0;
     SPI1_Write(0x9B);
     SPI1_Write(0x88);
     Chip_Select = 1;

     delay_ms(10);
  
     // CR4 registar (svi biti 0)
     Chip_Select = 0;
     SPI1_Write(0xA4);
     SPI1_Write(0x00);
     Chip_Select = 1;

     delay_ms(10);

     // CR5 registar
     Chip_Select = 0;
     SPI1_Write(0xAB);
     SPI1_Write(0xFF);
     Chip_Select = 1;

     delay_ms(10);
     
     // CR6 registar (sve nule)
     Chip_Select = 0;
     SPI1_Write(0xB0);
     SPI1_Write(0x00);
     Chip_Select = 1;

     Dir_Step = 1;

  while (1) {
        Nxt_Step = 1;
        delay_ms(10);
        Nxt_Step = 0;
        delay_ms(10);
  }
}

smsmsm
Posts: 3
Joined: 17 May 2022 11:17

Re: SPI and click board usage

#4 Post by smsmsm » 03 Jul 2022 18:41

If somebody cares, the solution was to set SS pin as output (so it doesn't block SPI transmit function). The second thing was to read status registers (SR1-6) before altering control registers.

Post Reply

Return to “mikroC PRO for AVR General”