برنامه زیر کاربرد ADC ، SPi رو نشون می ده !
کد:
#include "stm32f10x.h"
#include <stdio.h>
#include "stm32f10x_ADC.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStructure;
int a=0,b=0;
void main(void){
/********************** GPIO configuration*********************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // we want the pins to be an output
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this sets the GPIO modules clock speed
//GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)
//GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // this sets the pullup / pulldown resistors to be inactive
GPIO_Init(GPIOD, &GPIO_InitStruct); // this finally passes all the values to the GPIO_Init function which takes care of setting the corresponding bits.
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; // we want to configure PA0
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; // we want it to be an input
GPIO_Init(GPIOA, &GPIO_InitStruct); // this passes the configuration to the Init function which takes care of the low level stuff
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC configuration */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_13Cycles5);
//*********start ADC************/
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
//*****************SPI Configuration**************/
// tabe marbot be tanzimate clock haste(CORE=72MHz) mikro va faal kardane clock portha va SPI1
//RCC_INIT();
//tanzime pinhaye PORTe SPI1: SCK , MISO , MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// tanzime pinhaye PORTe SPI1 : NSS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Tanzime parametrhaye porte SPI
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
/***************** motor on/off controll********************* */
while(1){
while(GPIO_Pin_0 ==1) //when pinA.0 =1 start under code's.(agar klide controle motor zade shavad)
{
if (GPIO_Pin_2 & 0x1 !=0 ) //pinA.2 & 1 ...if()= 1 then ...(barrasi mikonad ke motor khamoosh ya roshan )
{
GPIO_ResetBits (GPIOA, GPIO_Pin_2);//if motor was on then it be off !
}
else if(GPIO_Pin_2 & 0x1 ==0 )
{//if motor was off :
GPIO_setBits (GPIOA, GPIO_Pin_2);//the motor be on !
}
}
a = ADC_GetConversionValue(ADC1);
b=a*(1000)+29; //sample of ADC(Motor sensor) formul !
SPI_I2S_SendData(SPI1, b);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)== RESET){}
}}
برنامه بالا قراره که یه خروجی مثلا موتور رو خاموش و روشن کنه و توسط یه سنسور که دورسنج یا دما می تونه باشه ، مقدار رو دریافت و در پورت spi قرار بده ...
اساتید لطف می کنید بررسی کنید که این برنامه درسته و آیا کار می کنه ؟ من برد آرم رو ندارم .
خواستم کل فایل رو هم که فشرده کرده بودم رو بذارم ، اما خطا گرفت !