Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************** (C) COPYRIGHT *******************************
- * File Name : main.c
- * Author : WCH
- * Version : V1.0.0
- * Date : 2021/06/06
- * Description : Main program body.
- *********************************************************************************
- * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
- * Attention: This software (modified or not) and binary are used for
- * microcontroller manufactured by Nanjing Qinheng Microelectronics.
- *******************************************************************************/
- /*
- *@Note
- USART Print debugging routine:
- USART1_Tx(PA9).
- This example demonstrates using USART1(PA9) as a print debug port output.
- */
- #include "debug.h"
- /* Global typedef */
- /* Global define */
- /* Global Variable */
- void SPI1_Init(){
- RCC->APB2PCENR |= RCC_APB2Periph_GPIOA;
- //SPI1_NSS(PA4 GPIO_Mode_AF_PP)
- GPIOA->CFGLR = (GPIOA->CFGLR & ~ GPIO_CFGLR_CNF4_0) | GPIO_CFGLR_CNF4_1;
- GPIOA->CFGLR |= GPIO_CFGLR_MODE4;
- GPIOA->BSHR = GPIO_BSHR_BS4;
- //SPI1_SCK(PA5 GPIO_Mode_AF_PP)
- GPIOA->CFGLR = (GPIOA->CFGLR & ~ GPIO_CFGLR_CNF5_0)|(GPIO_CFGLR_CNF5_1);
- GPIOA->CFGLR |= GPIO_CFGLR_MODE5;
- //SPI1_MOSI(PA7 GPIO_Mode_AF_PP)
- GPIOA->CFGLR = (GPIOA->CFGLR & ~(GPIO_CFGLR_CNF7_0))|(GPIO_CFGLR_CNF7_1);
- GPIOA->CFGLR |= GPIO_CFGLR_MODE7;
- //SPI1_MISO(PA6 GPIO_Mode_IN_FLOATING)
- GPIOA->CFGLR = (GPIOA->CFGLR & ~(GPIO_CFGLR_CNF6_0))|(GPIO_CFGLR_CNF6_1);
- GPIOA->CFGLR = GPIOA->CFGLR & ~GPIO_CFGLR_MODE6;
- // Настройка SPI1
- RCC->APB2PCENR |= RCC_APB2Periph_SPI1; // вкл. тактирование SPI1
- SPI1->CTLR1 |= SPI_CTLR1_MSTR; // вкл. МАСТЕР
- SPI1->CTLR1 |= SPI_BaudRatePrescaler_64; //
- SPI1->CTLR1 &= ~SPI_CTLR1_SSM; // Hard_NSS
- SPI1->CTLR2 |= SPI_CTLR2_SSOE; // включаем драйвер NSS
- //SPI1->CTLR1 |= SPI_CTLR1_SPE; // вкл SPI
- }
- void SPIx_Init(void)
- {
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO| RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);
- // SPI1_SCK & SPI1_MOSI
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //SPI1_NSS
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //SPI1_MOSI
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //SPI1
- //GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE);
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI1, &SPI_InitStructure);
- SPI_SSOutputCmd(SPI1, ENABLE);
- SPI_Cmd(SPI1, ENABLE);
- }
- uint8_t Spi1_Write_Data(uint8_t data)
- {
- //SPI1->CTLR1 |= SPI_CTLR1_SPE;
- while(!(SPI1->STATR & SPI_STATR_TXE)); //ждём пока опустошится Tx буфер
- //CS_LOW();
- SPI1->DATAR = (uint8_t)data; //отправляем данные
- //while(!(SPI1->STATR & SPI_STATR_RXNE)); //ждём пока придёт ответ
- //data = (uint8_t)(SPI1->DATAR); //считываем полученные данные
- //CS_HIGH();
- //SPI1->CTLR1 &= ~SPI_CTLR1_SPE;
- return data;
- }
- /*********************************************************************
- * @fn main
- *
- * @brief Main program.
- *
- * @return none
- */
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- SystemCoreClockUpdate();
- Delay_Init();
- USART_Printf_Init(115200);
- printf("SystemClk:%d\r\n",SystemCoreClock);
- printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
- printf("Test SPI Hard_NSS\r\n");
- SPI1_Init();
- //SPIx_Init();
- while(1)
- {
- Spi1_Write_Data(0x55);
- Delay_Us(20);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement