在 C library for
Broadcom BCM 2835 as used in Raspberry Pi 提及:
in order for bcm2835 library SPI to work, you may need to
disable the SPI kernel module using:
sudo raspi-config
under Advanced Options
- enable Device Tree
under Advanced Options
- disable SPI
Reboot.
所以我就跟著做了…..
程式:
#include <cstdlib>
#include <bcm2835.h>
#include <unistd.h>
#include <stdio.h>
using namespace std;
int main(int argc, char** argv) {
bcm2835_init();
//setup spi pins
if(bcm2835_spi_begin()==0)
{
printf("init failed
\r\n");
return -1;
}
//set CS pins polarity to low
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0,0);
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1,0);
//set clock speed
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32768);
//set SPI data mode
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
//Set with CS pin to use for
next transfers
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
//transfer byte
char buf[100];
for(int j=0;j<100;j++)
{
buf[j]=0x55;
}
for(int j=0;j<10;j++)
{
bcm2835_spi_transfern(&buf[0],100);
bcm2835_delay(1000);
}
//return spi pins to default
input state
bcm2835_spi_end();
return 0;
}
從示波器上,發現只有j=0才會有CLK,其他時間CLK都不見(是否是因為沒接裝置沒有正常回應…)...不知勒
沒有留言:
張貼留言