Skip to content

Commit

Permalink
spi
Browse files Browse the repository at this point in the history
  • Loading branch information
B414 authored and B414 committed Nov 15, 2017
1 parent a7cd7a3 commit 14852b6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 23 deletions.
36 changes: 15 additions & 21 deletions lab6/pwm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <stdio.h>
#include "pin.h"
#include "pwm.h"
#include "spi.h"
#include "serial.h"
// PWM output on RPi Plug P1 pin 12 (which is GPIO pin 18)
// in alt fun 5.
// Note that this is the _only_ PWM pin available on the RPi IO headers
Expand All @@ -31,29 +33,21 @@
#define RANGE 1024
int main(int argc, char **argv)
{
SPI *c;
bcm2835_set_debug(1);
if (!bcm2835_init())
return 1;
// Set the output pin to Alt Fun 5, to allow PWM channel 0 to be output there
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_ALT5);
// Clock divider is set to 16.
// With a divider of 16 and a RANGE of 1024, in MARKSPACE mode,
// the pulse repetition frequency will be
// 1.2MHz/1024 = 1171.875Hz, suitable for driving a DC motor with PWM
bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16);
bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1);
bcm2835_pwm_set_range(PWM_CHANNEL, RANGE);
// Vary the PWM m/s ratio between 1/RANGE and (RANGE-1)/RANGE
// over the course of a a few seconds

Pwm *pw =new Pwm(1024,0);

while (1)
{
if(pw->get_factor_umplere()<100)
pw->set_factor_umplere(pw->get_factor_umplere()+10);
else
pw->set_factor_umplere(0);
printf("bcm2835_init failed. Are you running as root??\n");
return 1;
}
bcm2835_close();
if (!bcm2835_spi_begin())
{
printf("bcm2835_spi_begin failed. Are you running as root??\n");
return 1;
}
c=new SPI(BCM2835_SPI_BIT_ORDER_MSBFIRST,BCM2835_SPI_MODE0,BCM2835_SPI_CLOCK_DIVIDER_65536,BCM2835_SPI_CS0,LOW);
uint8_t send_data = 0x23;
uint8_t read_data = c->transfer(send_data);
printf("Sent to SPI: 0x%02X. Read back from SPI: 0x%02X.\n", send_data, read_data);
return 0;
}
8 changes: 6 additions & 2 deletions lab6/pwm/pwm.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ TEMPLATE = app
SOURCES += main.cpp \
bcm2835_stub.cpp \
pin.cpp \
pwm.cpp
pwm.cpp \
serial.cpp \
spi.cpp

HEADERS += \
pin.h \
bcm2835.h \
pwm.h
pwm.h \
serial.h \
spi.h

FORMS += mainwindow.ui
6 changes: 6 additions & 0 deletions lab6/pwm/serial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "serial.h"

SERIAL::SERIAL()
{

}
12 changes: 12 additions & 0 deletions lab6/pwm/serial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef SERIAL_H
#define SERIAL_H
#include<bcm2835.h>

class SERIAL
{
public:
SERIAL();
virtual uint8_t transfer(uint8_t data)=0;
};

#endif // SERIAL_H
14 changes: 14 additions & 0 deletions lab6/pwm/spi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "spi.h"
#include "bcm2835.h"
SPI::SPI(int bitorder,int datamode, int clockdivider, int chipselect, int chipselectpolarity)
{
bcm2835_spi_setBitOrder(bitorder); // The default
bcm2835_spi_setDataMode(datamode); // The default
bcm2835_spi_setClockDivider(clockdivider); // The default
bcm2835_spi_chipSelect(chipselect); // The default
bcm2835_spi_setChipSelectPolarity(chipselect,chipselectpolarity);
}

uint8_t SPI::transfer(uint8_t data){
return bcm2835_spi_transfer(data);
}
12 changes: 12 additions & 0 deletions lab6/pwm/spi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef SPI_H
#define SPI_H
#include"serial.h"

class SPI: public SERIAL
{
public:
SPI(int bitorder,int datamode, int clockdivider, int chipselect, int chipselectpolarity);
uint8_t transfer(uint8_t data);
};

#endif // SPI_H

0 comments on commit 14852b6

Please sign in to comment.