这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 40SPI代码

共2条 1/1 1 跳转至

40SPI代码

菜鸟
2014-12-25 22:44:20     打赏
view plaincopy to clipboardprint?
  1. #include "stm32f10x.h"
  2. #include "stm32_eval.h"
  3. #include
  4. #include "spi_flash.h"
  5. #define VREF 3.3
  6. void delay_us(u32 n)
  7. {
  8. u8 j;
  9. while(n--)
  10. for(j=0;j<10;j++);
  11. }
  12. void delay_ms(u32 n)
  13. {
  14. while(n--)
  15. delay_us(1000);
  16. }
  17. #define TxBufferSize1 (countof(TxBuffer1) - 1)
  18. #define RxBufferSize1 (countof(TxBuffer1) - 1)
  19. #define countof(a) (sizeof(a) / sizeof(*(a)))
  20. #define BufferSize (countof(Tx_Buffer)-1)
  21. typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
  22. #define FLASH_WriteAddress 0x00000
  23. #define FLASH_ReadAddress FLASH_WriteAddress
  24. #define FLASH_SectorToErase FLASH_WriteAddress
  25. #define sFLASH_ID 0xEF3015 //W25X16
  26. //#define sFLASH_ID 0xEF4015 //W25Q16
  27. #define buff_size 16;
  28. char rx_buff[],rx_buff_count=0;
  29. /* ???????? */
  30. uint8_t Tx_Buffer[4096] ;
  31. uint8_t Rx_Buffer[BufferSize];
  32. __IO uint32_t DeviceID = 0;
  33. __IO uint32_t FlashID = 0;
  34. __IO TestStatus TransferStatus1 = FAILED;
  35. // ??????
  36. void Delay(__IO uint32_t nCount);
  37. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  38. /** @addtogroup STM32F10x_StdPeriph_Examples
  39. * @{
  40. */
  41. /** @addtogroup EXTI_Config
  42. * @{
  43. */
  44. /* Private typedef -----------------------------------------------------------*/
  45. /* Private define ------------------------------------------------------------*/
  46. /* Private macro -------------------------------------------------------------*/
  47. /* Private variables ---------------------------------------------------------*/
  48. GPIO_InitTypeDef GPIO_InitStructure;
  49. USART_InitTypeDef USART_InitStructure;
  50. USART_ClockInitTypeDef USART_ClockInitStructure;
  51. char *int_to_string(int number,char *strnum)//整形数据转换为字符型
  52. {
  53. int j=0,i=0,n=0;
  54. char temp;
  55. while(number>0)
  56. {
  57. *(strnum+j)=number%10+48;
  58. j++;
  59. number=number/10;
  60. n++;
  61. }
  62. for(i=0;iLibraries->Small printf
  63. set to 'Yes') calls __io_putchar() */
  64. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  65. #else
  66. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  67. #endif /* __GNUC__ */
  68. /**
  69. * @brief Retargets the C library printf function to the USART.
  70. * @param None
  71. * @retval None
  72. */
  73. PUTCHAR_PROTOTYPE
  74. {
  75. /* Place your implementation of fputc here */
  76. /* e.g. write a character to the USART */
  77. USART_SendData(EVAL_COM1, (uint8_t) ch);
  78. /* Loop until the end of transmission */
  79. while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  80. {}
  81. return ch;
  82. }
  83. #ifdef USE_FULL_ASSERT
  84. /**
  85. * @brief Reports the name of the source file and the source line number
  86. * where the assert_param error has occurred.
  87. * @param file: pointer to the source file name
  88. * @param line: assert_param error line source number
  89. * @retval None
  90. */
  91. void assert_failed(uint8_t* file, uint32_t line)
  92. {
  93. /* User can add his own implementation to report the file name and line number,
  94. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  95. /* Infinite loop */
  96. while (1)
  97. {
  98. }
  99. }
  100. #endif

 


菜鸟
2014-12-25 22:46:41     打赏
2楼
view plaincopy to clipboardprint?
  1. #include "stm32f10x.h"
  2. #include "stm32_eval.h"
  3. #include
  4. volatile int flag;
  5. #define Set_B20() GPIO_SetBits(GPIOC, GPIO_Pin_12) //上拉关闭PC12
  6. #define Reset_B20() GPIO_ResetBits(GPIOC, GPIO_Pin_12) //下拉打开PC12
  7. #define Read_B20() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12) //读PC12状态
  8. unsigned char Error_Flag=0;
  9. unsigned char zf=0;
  10. void SysTick_Configuration(void)
  11. {
  12. /* Setup SysTick Timer for 10 msec interrupts */
  13. if (SysTick_Config(48000)) //SysTick配置
  14. {
  15. /* Capture error */
  16. while (1);
  17. }
  18. /* Configure the SysTick handler priority */
  19. NVIC_SetPriority(SysTick_IRQn, 0x0); //SysTick中断优先级
  20. }
  21. /** @addtogroup STM32F10x_StdPeriph_Examples
  22. * @{
  23. */
  24. /** @addtogroup EXTI_Config
  25. * @{
  26. */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* Private define ------------------------------------------------------------*/
  29. /* Private macro -------------------------------------------------------------*/
  30. /* Private variables ---------------------------------------------------------*/
  31. GPIO_InitTypeDef GPIO_InitStructure; //结构体的命名
  32. USART_InitTypeDef USART_InitStructure; //结构体的命名
  33. USART_ClockInitTypeDef USART_ClockInitStructure; //结构体的命名
  34. void RCC_Configuration(void)
  35. {
  36. RCC_DeInit(); //将外设RCC的所有寄存器重新设为缺省值
  37. RCC_HSICmd(ENABLE); //使能内部高速晶振
  38. while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); //当SHI晶振就绪则重新设定
  39. RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); //设置系统时钟,选择SHI时钟为系统时钟
  40. RCC_HSEConfig(RCC_HSE_OFF); //设置外部高速晶振,HSE晶振OFF
  41. RCC_LSEConfig(RCC_LSE_OFF); //设置外部低速晶振,LSE晶振OFF
  42. //******配置PLL时钟频率为48MHZ*******//
  43. RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_8); //RCC_PLLMul_x 即设置PLL时钟频率为 6*x MHz
  44. //************************************//
  45. RCC_PLLCmd(ENABLE); ////*******************使能PLL
  46. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //PLL就绪
  47. RCC_ADCCLKConfig(RCC_PCLK2_Div4); // ADC时钟=PCLK/2
  48. RCC_PCLK2Config(RCC_HCLK_Div1); // APB2时钟=HCLK
  49. RCC_PCLK1Config(RCC_HCLK_Div2); /// APB1时钟=HCLK/2
  50. RCC_HCLKConfig(RCC_SYSCLK_Div1); // AHB时钟=系统时钟
  51. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // 选择PLL为系统时钟
  52. while(RCC_GetSYSCLKSource() != 0x08); //当PLL不是系统时钟
  53. // SystemInit();
  54. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //使能APB2外设时钟/****GPIOD时钟和功能复用IO时钟***/
  55. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG SW_DP使能
  56. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
  57. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  58. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // //选择设置GPIO管脚
  59. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; ////设置管脚速率
  60. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; ////设置管脚工作状态,此为推挽输出
  61. GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化GPIOD
  62. GPIO_ResetBits(GPIOD,GPIO_Pin_2); //上拉关闭蜂鸣器
  63. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
  64. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  65. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//LED
  66. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  67. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  68. GPIO_Init(GPIOC, &GPIO_InitStructure);
  69. GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); // GPIOC.0到GPIOC.7输出胃叩缙姜
  70. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM2时钟
  71. }
  72. void USART_int(long BaudRate)
  73. {
  74. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能GPIOA、USART1外设时钟
  75. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  76. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO的输出速率为50MHz
  77. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  78. GPIO_Init(GPIOA, &GPIO_InitStructure);
  79. /* PA10 USART1_Rx */
  80. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  81. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //使能外设GPIOC端口时钟
  82. GPIO_Init(GPIOA, &GPIO_InitStructure);
  83. /* USARTx configured as follow:
  84. - BaudRate = 115200 baud
  85. - Word Length = 8 Bits
  86. - One Stop Bit
  87. - No parity
  88. - Hardware flow control disabled (RTS and CTS signals)
  89. - Receive and transmit enabled
  90. */
  91. USART_InitStructure.USART_BaudRate = BaudRate;//设置USART传输波特率 BaudRate = 9600 可以直接写9600
  92. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//一帧传输或者接收的数据位数为8bit
  93. USART_InitStructure.USART_StopBits = USART_StopBits_1;//在帧结尾传输一个停止位
  94. USART_InitStructure.USART_Parity = USART_Parity_No;//奇偶模式失能
  95. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件流控制失能
  96. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//使能接收发模式
  97. USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; //时钟低电平活动
  98. USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //引脚时钟输出低电平时钟
  99. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; //第二个时钟边沿开始捕获数据
  100. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据的时钟脉冲不从SCLK输出
  101. USART_ClockInit(USART1, &USART_ClockInitStructure); //引用结构体的成员
  102. USART_Init(USART1, &USART_InitStructure);//USART1初始化
  103. USART_Cmd(USART1, ENABLE);//使能USART1时钟外设
  104. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能接受中断
  105. USART_Cmd(USART1, ENABLE); //使能 USART
  106. }
  107. void delay_18b20(u32 nus) //18b20按照严格的时序工作,这是特定的一个延时函数(自定义)
  108. {
  109. u16 i;
  110. while(nus--)
  111. for(i=12;i>0;i--);
  112. }
  113. void Init18B20(void) //18B20初始化
  114. {
  115. u8 aa=0;
  116. u8 count =0;
  117. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能PC时钟
  118. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //配置端口GPIOC.12
  119. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//开漏输出
  120. GPIO_Init(GPIOC, &GPIO_InitStructure); //引用结构体的变量
  121. Set_B20() ; // GPIO_SetBits(GPIOC, GPIO_Pin_12)
  122. delay_18b20(1);
  123. Reset_B20(); //重置18B20
  124. delay_18b20(480);
  125. Set_B20();
  126. // delay_18b20(500);
  127. delay_18b20(480);
  128. count=0;
  129. aa=Read_B20(); //温度读取
  130. /****个人认为限制温度不超过99度,作为一个保护*/
  131. while(!aa && count<100) //判断aa的非和计数器count的值是否都小于100
  132. {
  133. aa=Read_B20(); //
  134. count++; //count自加1
  135. }
  136. if(count>=99)
  137. Error_Flag=1; //错误返回值1
  138. else
  139. Error_Flag=0; //错误返回值0
  140. }
  141. unsigned char Read18B20(void)//按位读取数据
  142. {
  143. unsigned char i=0;
  144. unsigned char date=0;
  145. u8 tempp;
  146. for(i=8;i>0;i--)
  147. {
  148. Reset_B20(); //打开PC12
  149. date>>=1; //标志右移一位
  150. delay_18b20(1);
  151. Set_B20(); //关闭
  152. delay_18b20(1);
  153. tempp=Read_B20(); //读取温度
  154. if(tempp) //判断tempp是否为1
  155. date|=0x80; // 1000 0000 将最高位填1 ,然后右移,每次最高位由0变1,使8位全部传递完毕 0xff = 1111 1111
  156. delay_18b20(60); //延时
  157. }
  158. return(date); //返回值是无符号的字符型的类型 date
  159. }
  160. void Write18B20(unsigned char date)//向18b20写数据
  161. {
  162. unsigned char i=0;
  163. for (i=8; i>0; i--)
  164. {
  165. Reset_B20();
  166. delay_18b20(1);
  167. if(date & 0x01)
  168. {
  169. Set_B20();
  170. }
  171. else
  172. { Reset_B20();}
  173. delay_18b20(60);
  174. date>>=1;
  175. Set_B20();
  176. delay_18b20(1);
  177. }
  178. delay_18b20(15);
  179. }
  180. float Read_T()//读温度
  181. {
  182. unsigned char TUp,TDown;
  183. unsigned char fTemp;
  184. u8 TT=0;
  185. float Temp = 0;
  186. Init18B20();
  187. Write18B20(0xcc);
  188. Write18B20(0x44);
  189. Init18B20();
  190. Write18B20(0xcc);
  191. Write18B20(0xbe);
  192. TDown = Read18B20();
  193. TUp = Read18B20();
  194. if(TUp>0x7f)
  195. {
  196. TDown=~TDown;
  197. TUp=~TUp+1;
  198. TUp/=8;
  199. zf=1;
  200. }
  201. else
  202. zf=0;
  203. fTemp=TDown&0x0f;
  204. TUp<<=4;
  205. TDown>>=4;
  206. TT=TUp|TDown;
  207. Temp=TT+(float)fTemp/16;
  208. return(Temp);
  209. }
  210. int main(void)
  211. {
  212. /*!< At this stage the microcontroller clock setting is already configured,
  213. this is done through SystemInit() function which is called from startup
  214. file (startup_stm32f10x_xx.s) before to branch to application main.
  215. To reconfigure the default setting of SystemInit() function, refer to
  216. system_stm32f10x.c file
  217. */
  218. /* System Clocks Configuration */
  219. char ID[10]; //定义一个字符型数组,长度为10
  220. int i;
  221. RCC_Configuration(); //配置RCC时钟
  222. USART_int(115200); //波特率的设置
  223. SysTick_Configuration(); //系统滴答时钟的调用
  224. printf(" config done...\r\n"); //打印输出
  225. //delay_ms(1000);
  226. Init18B20(); //初始化18b20;
  227. Write18B20(0x34); //写入读取地址的命令
  228. delay_18b20(20); //延时
  229. while(1)
  230. {
  231. if(flag == 300) //判断flag是否等于300
  232. {
  233. printf(" the ID is:") ;
  234. for(i=0;i<8;i++) //字符不能直接输出,要按位输出
  235. {
  236. ID[i] = Read18B20();//读取地址
  237. printf("%d", ID[i]);//输出地址
  238. }
  239. printf("\r\n") ; //换行
  240. }
  241. if(flag == 500) //判断flag是否等于500
  242. {
  243. printf("The Temperature is:%f\r\n",Read_T());//读取温度并输出
  244. printf("===================================================\r\n");
  245. }
  246. }
  247. }
  248. #ifdef USE_FULL_ASSERT
  249. /**
  250. * @brief Reports the name of the source file and the source line number
  251. * where the assert_param error has occurred.
  252. * @param file: pointer to the source file name
  253. * @param line: assert_param error line source number
  254. * @retval None
  255. */
  256. void assert_failed(uint8_t* file, uint32_t line)
  257. {
  258. /* User can add his own implementation to report the file name and line number,
  259. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  260. /* Infinite loop */
  261. while (1)
  262. {
  263. }
  264. }
  265. #endif
  266. /**
  267. * @}
  268. */
  269. /**
  270. * @}
  271. */
  272. #ifdef __GNUC__
  273. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  274. set to 'Yes') calls __io_putchar() */
  275. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  276. #else
  277. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  278. #endif /* __GNUC__ */
  279. /**
  280. * @brief Retargets the C library printf function to the USART.
  281. * @param None
  282. * @retval None
  283. */
  284. PUTCHAR_PROTOTYPE
  285. {
  286. /* Place your implementation of fputc here */
  287. /* e.g. write a character to the USART */
  288. USART_SendData(EVAL_COM1, (uint8_t) ch);
  289. /* Loop until the end of transmission */
  290. while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  291. {}
  292. return ch;
  293. }
  294. #ifdef USE_FULL_ASSERT
  295. /**
  296. * @brief Reports the name of the source file and the source line number
  297. * where the assert_param error has occurred.
  298. * @param file: pointer to the source file name
  299. * @param line: assert_param error line source number
  300. * @retval None
  301. */
  302. void assert_failed(uint8_t* file, uint32_t line)
  303. {
  304. /* User can add his own implementation to report the file name and line number,
  305. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  306. /* Infinite loop */
  307. while (1)
  308. {
  309. }
  310. }
  311. #endif

 


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]