OpenEdv-开源电子网

 找回密码
 立即注册

扫一扫,访问微社区

正点原子新作:阿波罗STM32F767&F429&探索者STM32F4开发板&赶快来下载资料哦。

查看: 121|回复: 1

求stm32f4+CC1101的接发数据代码?

[复制链接]

  离线 

1

主题

2

帖子

0

精华

新手上路

Rank: 1

积分
14
金钱
14
注册时间
2017-9-7
在线时间
0 小时
发表于 2017-9-7 17:58:37 | 显示全部楼层 |阅读模式
1金钱
我用 stm32f407 驱动SPI CC1101片子一直不成功,代码如下: 希望好心人给我发一份示例:lzzy.ha@qq.com
[C] 纯文本查看 复制代码
#define SPI1_NSS			GPIO_Pin_4
#define SPI1_SCK			GPIO_Pin_5
#define SPI1_MISO 			GPIO_Pin_6
#define SPI1_MOSI			GPIO_Pin_7
#define GPIO_SPI1			GPIOA
#define RCC_GPIO_SPI1		RCC_AHB1Periph_GPIOA

void SPI1_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	SPI_InitTypeDef SPI_InitStruct;
//	NVIC_InitTypeDef NVIC_InitStruct;

	{// GPIO SPI
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
		GPIO_InitStruct.GPIO_Pin = SPI1_SCK | SPI1_MISO | SPI1_MOSI;
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
		GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
		GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_Init(GPIO_SPI1, &GPIO_InitStruct);

		// connect SPI1 pins to SPI alternate function
		GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource5, GPIO_AF_SPI1);
		GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource6, GPIO_AF_SPI1);
		GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource7, GPIO_AF_SPI1);

		// CS
		GPIO_InitStruct.GPIO_Pin = SPI1_NSS;
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
		GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
		GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
		GPIO_Init(GPIO_SPI1, &GPIO_InitStruct);
		SPI1_Unselect();
	}
	{// SPI
		RCC_AHB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
		SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
		SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
		SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
		SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
		SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
		SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // set the NSS management to internal and pull internal NSS high
		SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; // SPI frequency is APB2 frequency / 4
		SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
		SPI_InitStruct.SPI_CRCPolynomial = 7;
		SPI_Init(SPI1, &SPI_InitStruct);
		SPI_Cmd(SPI1, ENABLE); // enable SPI1
	}
}


void SPI1_Select()
{
	GPIO_ResetBits(GPIO_SPI1, SPI1_NSS);
}

void SPI1_Unselect()
{
	GPIO_SetBits(GPIO_SPI1, SPI1_NSS);
}

uint8_t spi1_send_byte(uint8_t byte)
{
	while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
	SPI_I2S_SendData(SPI1, byte);
	while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
	return SPI_I2S_ReceiveData(SPI1);
}

uint8_t drv_spi_read_write_byte(uint8_t ch)
{
	return spi1_send_byte(ch);
}


[C] 纯文本查看 复制代码
int main(void)
{
	SystemInit();
	LED_GPIO_Config();
	//init_USART1(115200);
	//printf("System starting...1\r\n");
//	CC1101_Init();
	SPI1_Init();
	uint8_t value;
	SPI1_Select();
	spi1_send_byte( CC1101_VERSION| READ_SINGLE );
	value = spi1_send_byte(0xff);
	SPI1_Unselect();
}



回复

使用道具 举报

  离线 

0

主题

4

帖子

0

精华

新手上路

Rank: 1

积分
20
金钱
20
注册时间
2017-11-14
在线时间
1 小时
发表于 5 天前 | 显示全部楼层
楼主收据代码调试好了吗?可否给我发一份,2663326465@qq.com,感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则




QQ|联系我们|手机版|官方淘宝店|新浪微博|微信公众平台|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2017-11-19 16:33

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表
/* */