The reason for this post is only to share some arduino sample code of mine. In this case we are speaking of a BTS7960 motor driver controlled by Arduino and a modification for a previous project of mine, the big 7 segment display. This modification consist in I2C comunication betwen the display and arduino.

Used products:

43A motor driver

arduino pro mini

7 segment display

BTS7960

BTS7960

Pinning:

BTS7960 Arduino
RPWM 5
LPWM 6
L_EN 7
R_EN 8
VCC +5V
GND GND
KNOB   A0
BUTTON   10

 

Code:

//variabile folosite pentru definirea pinilor butonului de schimbare a directiei stanga dreapta
const int pin_buton1=10;
int stare_b1=0;
int buttonState;             // retine valoarea curenta a butonului
int v_stare_b1 = LOW;   // retine valoarea anterioara a butonului
long v_timp_b1 = 0;  // ultima durata de debounce a intrarii
long durataDebounce = 50;    // the debounce time; increase if the output flickers

//conexiunea driverului BTS7960 este facuta pe acesti pini
int RPWM=5;
int LPWM=6;
int L_EN=7;
int R_EN=8;

const int analogInPin = A0;//pinul pe care este conectat potentiometrul

char directia='R';
char odirectia;
int valPot = 0;
int viteza = 0;
int old=0;


void setPWMfrequency(int freq){
  TCCR0B = TCCR0B & 0b11111000 | freq ;
}

void MotorActiveStatus(char Side,boolean s){
  boolean state=s;
  if(Side=='R'){
    digitalWrite(R_EN,s);
  }
  if(Side=='L'){
    digitalWrite(L_EN,s);
  }    
}
void setMotor(char side,byte pwm){
  if(side=='R'){
    analogWrite(RPWM,pwm);
  }
  if(side=='L'){
    analogWrite(LPWM,pwm);
  }
}
void closeMotor(char side){
  if(side=='R'){
    digitalWrite(RPWM,LOW);
  }
  if(side=='L'){
    digitalWrite(LPWM,LOW);
  }

}
void setup() {
  pinMode(pin_buton1,INPUT_PULLUP);
  setPWMfrequency(0x02);// timer 0 , 3.92KHz
  for(int i=5;i<9;i++){
    pinMode(i,OUTPUT);
  }
  for(int i=5;i<9;i++){
    digitalWrite(i,LOW);
  }
  delay(1000);
  MotorActiveStatus('R',true);
  MotorActiveStatus('L',true);
  Serial.begin(9600);
}

void loop() {
  stare_b1=digitalRead(pin_buton1);

  if (stare_b1 != v_stare_b1) {
    // reset the debouncing timer
    v_timp_b1 = millis();
  } 

  if ((millis() - v_timp_b1) > durataDebounce) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (stare_b1 != buttonState) {
      buttonState = stare_b1;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        b1();
      }
    }
  }

  v_stare_b1 = stare_b1;


  valPot = analogRead(analogInPin);            
  viteza = map(valPot, 0, 1023, 0, 255);  
  if ((viteza!=old)||(directia!=odirectia)){
    old=viteza;
    odirectia=directia;
    setMotor(directia,viteza);
    Serial.print(directia);  
    Serial.print(" viteza = " );                       
    Serial.println(viteza);    

  }
}
//schimbare directie
void b1(){ 
  delay(500);
  closeMotor(directia);
  delay(1000);
  if (directia=='R') directia='L';
  else directia='R';
}

I2C -7 segment display

I2C 7 segment

Pinning:

7 segments Arduino
D1 4
D2 3
D3 2
D4 5
D11 (data) 11
D8(clock) 8
D12(latch) 12
+5V +5V
GND GND
+12V +12V

I2C comunication is made on Arduino pin A4 and A5.

Code:

Slave

#include <Wire.h>
char comanda[10]; //vectorul in care va fi receptionata comanda pe I2c
char *i;
String p="";
int nr;
//pini pe care sunt legati anozii afisajului
int d1 = 4;
int d2 = 3;
int d3 = 2;
int d4 = 5;

int j=0;
byte u,z,s,m;
byte u1,z1,s1,m1;

int latchPin = 12;//ST_CP de la 74HC595 este conectat pe pinul 12
int clockPin = 8;//SH_CP de la 74HC595 este conectat pe pinul 8
int dataPin = 11;//DS de la 74HC595 este conectat pe pinul 11

//definim un vector care sa contina valorile in zecimal
// a fiecarui byte ce reprezinta un numar
int numere[10] = {
  63,6,91,79,102,109,125,7,127,111};

void setup() {                
  pinMode(d1, OUTPUT);     
  pinMode(d2, OUTPUT);     
  pinMode(d3, OUTPUT);     
  pinMode(d4, OUTPUT);    
  Serial.begin(9600);
  pinMode(latchPin , OUTPUT);     
  pinMode(clockPin, OUTPUT);   
  pinMode(dataPin, OUTPUT);
  //comennzi pentru i2c
  Wire.begin(4);//suntem dispozitivul de la adresa #4
  Wire.onReceive(receptie);//datele primite pe i2c le interpretam in functia receptie

}

void ionii(){//functia timer. aceasta functie se reia o data la 8kHz  

  j++;
  if (j==1){
    digitalWrite(d1, HIGH);
    digitalWrite(d2, LOW);
    digitalWrite(d3, LOW);
    digitalWrite(d4, LOW);  
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numere[u]);  
    digitalWrite(latchPin, HIGH);
  }
  if (j==2){
    digitalWrite(d1, LOW);
    digitalWrite(d2, HIGH);
    digitalWrite(d3, LOW);
    digitalWrite(d4, LOW);  
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numere[z]);  
    digitalWrite(latchPin, HIGH);
  }

  if (j==3){
    digitalWrite(d1, LOW);
    digitalWrite(d2, LOW);
    digitalWrite(d3, HIGH);
    digitalWrite(d4, LOW);  
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numere[s]);  
    digitalWrite(latchPin, HIGH);
  }

  if (j==4){
    digitalWrite(d1, LOW);
    digitalWrite(d2, LOW);
    digitalWrite(d3, LOW);
    digitalWrite(d4, HIGH);  
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numere[m]);  
    digitalWrite(latchPin, HIGH);
    j=0;
  }

  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);  
  digitalWrite(latchPin, HIGH);  
}

void loop(){
  numar(nr);
  ionii();
 //delay(100);

}

//functia numar folosita pentru a calcula cifrele din numar
//si afisarea datelor
void numar(int nr){ 

  u = (nr%10);
  z = ((nr/10)%10);
  s = ((nr/100)%10);
  m = (nr/1000);
}

// aici interpretam datele
void receptie(int howMany)
{
byte index = 0;
//se salveaza comanda primita pe I2C intr-un string
while(Wire.available())
{
comanda[index++] = Wire.read();
}
//se interpreteaza comanda
p=strtok_r(comanda,";",&i);
while(p!="")
{
if (p.startsWith("N")) //se verifica daca trebuie sa afisam un nr
{//daca este numar stim ca urmatoarele 4 caractere reprezinta numarul
String tmp=p.substring(1,5);//transforma string in int
nr=tmp.toInt();
}
p=strtok_r(NULL,";",&i);//verific daca mai am ceva de executat
}
 
}

 Master:

#include <Wire.h>
char buffer[6];
void setup()
{
// initializeaza bus pentru i2c
Wire.begin();
Serial.begin(9600);
}
 
void loop()
{
for (int i=0;i<=5468;i++)
{
Wire.beginTransmission(4); // transmit dispozitivului de la adresa #4
String myNR = String(i);  
String detrimis="N"+myNR;
detrimis=detrimis+";";
detrimis.toCharArray(buffer,sizeof(buffer));
Wire.write(buffer); // trimite numarul 
Wire.endTransmission(); // intrerupe transmisia
//delay(10);
}

delay(10000); //asteptam un pic dupa care reluam cu 0
}

 

Arduino sample code: BTS7960 [43A motor driver] and I2C 7 segment display
Etichetat pe:            

3 comentarii la „Arduino sample code: BTS7960 [43A motor driver] and I2C 7 segment display

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest sit folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.