STM实验程序代码_第1页
STM实验程序代码_第2页
STM实验程序代码_第3页
STM实验程序代码_第4页
STM实验程序代码_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include stm32f10 x.h #include delay.h /#include sys.h #includestm32f10 x_exti.h/QHKJ TEB-CM5000实验箱STM32实验1 /固件库V3.5工程模板/QHKJGPIO_InitTypeDef GPIO_InitStructure;EXTI_InitTypeDef EXTI_InitStructure;/* Private function prototypes void RCC_Configuration(void);void NVIC_Configuration(void);void GPIO_Con

2、figuration(void);/* Private functions/* Function Name : main * Description : Main program.*/*/幵*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*int main(void)/* Configure the system clocks */ / RCC_Configuration();/ SysTick_Configuration();delay_init();/* NVIC Configuration */NVIC_Configuration();/* C

3、onfigure the GPIO ports */GPIO_Configuration();/* Connect EXTI Line9 to PA.9 */GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource9);/* Configure EXTI Line8 to generate an interrupt on falling edge */EXTI_InitStructure.EXTI_Line = EXTI_Line9;EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;*

4、Input: None* Output: None* Return: NoneEXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_Init(&EXTI_InitStructure);for(;)GPIO_Write(GPIOF, 0 xf80f);delay_ms(100);GPIO_Write(GPIOF, 0 xf817);delay_ms(100);GPIO_Write(GPIOF, 0 xf827);delay_ms(100);G

5、PIO_Write(GPIOF, 0 xf847);delay_ms(100);GPIO_Write(GPIOF, 0 xf887);delay_ms(100);GPIO_Write(GPIOF, 0 x8907);delay_ms(100);GPIO_Write(GPIOF, 0 xfa07);delay_ms(100);GPIO_Write(GPIOF, 0 xfc07);delay_ms(100);/* Function Name : NVIC_Configuration * Description : Configures VectorTable base location.* Inp

6、ut: None幵*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*void NVIC_Configuration(void)NVIC_InitTypeDef NVIC_InitStructure;/* Configure one bit for preemption priority */NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);/* Enable the EXTI9_5 Interrupt */ / NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQ

7、Channel;NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);* Output: None* Return: None/* Function Name : GPIO_C

8、onfiguration* Description : Configures the different GPIO ports.幵*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*void GPIO_Configuration(void)GPIO_InitTypeDef GPIO_InitStructure;/* Enable GPIOA, GPIOF and AFIO clocks */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOF |RCC_APB2Periph

9、_AFIO, ENABLE);/* Configure PF. as Output push-pull */GPIO_InitStructure.GPIO_PinGPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOF, &GPIO_InitSt

10、ructure);/* Configure PA9 as input floating (EXTI Line9) */* Input: None* Output: None* Return: NoneGPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;/ GPI0_InitStructure.GPI0_Mode = GPI0_Mode_IN_FL0ATING;GPI0_InitStructure.GPI0_Mode = GPI0_Mode_IPU;GPIO_Init(GPIOA, &GPIO_InitStructure);实验7 LED流水灯#includ

11、e stm32f10 x.h/固件库V3.5工程模板/QHKJ/*LED灯相关定义*/时钟*/GP10组*/管脚*/GP 10管脚*/GP 10管脚*/QHKJ TEB-CM5000实验箱STM32实验7#define RCC_GPI0_LEDRCC_APB2Periph_GPIOF /*LED使用的GPIO#define LEDn/*LED数量*/#define GPIO_LEDGPIOF/*LED灯使用的#define LD1_PINGPI0_Pin_3/*LD1使用的GPIO#define LD2_PINGPI0_Pin_4/*LD2#define LD3_PINGPI0_Pin_5/*

12、LD3#define LD4_PINGPI0_Pin_6/*LD4GP10管脚*/管脚*/GP 10管脚*/GP 10管脚*/#define LD8_PINGPI0_Pin_10/*LD8GP 10管脚*/* Private macro*/* Private variables*/GPI0_InitTypeDef GPI0_InitStructure;u8 count=0;/* Private function prototypes*/void RCC_Configuration(void);/voidNVIC_Configuration(void);void Delay(vu32 nCoun

13、t);void Turn_0n_LED(u8 LED_NUM);/* Private functions*/* Function Name : main#define LD5_PINGPI0_Pin_7/*LD5使用的GPIO#define LD6_PINGPI0_Pin_8/*LD6#define LD7_PINGPI0_Pin_9/*LD7* Description : Main program.幵*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*int main(void)/*配置LED灯使用的GPIO管脚模式*/RCC_APB2PeriphC

14、lockCmd(RCC_GPIO_LED, ENABLE); /*使能LED灯使用的GPIO时钟*/GPIO_InitStructure.GPIO_PinLD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN|LD5_PIN|LD6_PIN|LD7_PIN|LD8_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIO_LED, &GPIO_InitStructure); /* */GPIO_ResetB

15、its(GPIO_LED,LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN|LD5_PIN|LD6_PIN|LD7_PIN|LD8_PIN);/*关闭所有的LED指示灯*/while(1)GPIO_ResetBits(GPIO_LED,LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN|LD5_PIN|LD6_PIN|LD7* Input: None* Output: None* Return: None使用的LED灯相关的GPIO口初始化_PIN|LD8_PIN);/*关闭所有的LED指示灯*/Turn_On_LED(count%8); /点亮一个count+;De

16、lay(0 x0FFFFF);/*点亮对应灯*/ void Turn_On_LED(u8 LED_NUM)switch(LED_NUM)case 0:break;LED灯GPIO_SetBits(GPIO_LED,LD1_PIN); /*点亮LD1灯*/break;case 1:GPIO_SetBits(GPIO_LED,LD2_PIN); /*点亮LD2灯*/break;case 2:GPIO_SetBits(GPIO_LED,LD3_PIN); /*点亮LD3灯*/break;case 3:GPIO_SetBits(GPIO_LED,LD4_PIN); /*点亮LD4灯*/* Input:

17、 nCount: specifies the delay time length.case 4:break;default:GPIO_SetBits(GPIO_LED,LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN|LD5_PIN|LD6_PIN|LD7_PIN|LD8_PIN); /*点亮所有的灯*/break;/* Function Name : Delay * Description : Inserts a delay time.GPIO_SetBits(GPIO_LED,LD5_PIN); /*点亮LD5灯*/break;case 5:GPIO_SetBits(GPIO

18、_LED,LD6_PIN); /*点亮LD6灯*/break;case 6:GPIO_SetBits(GPIO_LED,LD7_PIN); /*点亮LD7灯*/break;case 7:GPIO_SetBits(GPIO_LED,LD8_PIN); /*点亮LD8灯*/* 0utput : None * Return : None幵*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*卄*void Delay(vu32 nCount)for(; nCount != 0; nCount-);实验11串口收发#include stm32f10 x.h #incl

19、ude stm32f10 x_usart.h/QHKJ TEB-CM5000实验箱STM32实验11 /固件库V3.5工程模板/QHKJ/* Private typedef /*LED灯相关定义*/时钟*/GP10组*/#define RCC_GPI0_LEDRCC_APB2Periph_GPIOF /*LED使用的GPIO#define LEDn/*LED数量*/#define GPIO_LEDGPIOF/*LED灯使用的*/#define LD1_PINGPI0_Pin_3/*LD1使用的GPIO管脚*/#define LD2_PINGPI0_Pin_4/*LD2GP10管脚*/#defi

20、ne LD3_PINGPI0_Pin_5/*LD3GP 10管脚*/#define LD4_PINGPI0_Pin_6/*LD4GP 10管脚*/*串口相关定义*/#define USART1_GPI0GPI0A#define USART1_CLKRCC_APB2Periph_USART1#define USART1_GPI0_CLKRCC_APB2Periph_GPI0A#define USART1_RxPinGPI0_Pin_10#define USART1_TxPinGPI0_Pin_9/#define USART1_IRQnUSART1_IRQn/#define USART1_IRQH

21、andler USART1_IRQHandler#define USART2_GPI0GPI0A#define USART2_CLKRCC_APB1Periph_USART2#define USART2_GPI0_CLKRCC_APB2Periph_GPI0A#define USART2_RxPinGPI0_Pin_3#define USART2_TxPinGPI0_Pin_2/#define USART2_IRQnUSART2_IRQn/#define USART2_IRQHandler USART2_IRQHandlerGPIO_InitTypeDef GPIO_InitStructure

22、;/* Private typedef typedef enum FAILED = 0, PASSED = !FAILEDTestStatus;/* Private define/* Private macro #define countof(a) (sizeof(a) / sizeof(*(a)/* Private variablesUSART_InitTypeDef USART_InitStructure;u8 TxBuffer1 = 串口中断收发示例:串口1 -串口2 (中断收发);u8 TxBuffer2 = 串口中断收发示例:串口2 -串口1 (中断收发);u8 RxBuffer1R

23、xBufferSize1;u8 RxBuffer2RxBufferSize2;u8 TxCounter1 = 0 x00;u8 TxCounter2 = 0 x00;u8 RxCounter1 = 0 x00;#define TxBufferSize1(countof(TxBuffer1) - 1)#define TxBufferSize2(countof(TxBuffer2) - 1)#define RxBufferSize1TxBufferSize2#define RxBufferSize2TxBufferSize1*/*/*/*/u8 RxCounter2 = 0 x00;u8 NbrO

24、fDataToTransfer1 = TxBufferSize1;u8 NbrOfDataToTransfer2 = TxBufferSize2;u8 NbrOfDataToRead1 = RxBufferSize1;u8 NbrOfDataToRead2 = RxBufferSize2;u8 TransferStatus1 = FAILED;u8 TransferStatus2 = FAILED;/* Private function prototypes void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC

25、_Configuration(void);TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);/* Private functions/* brief Main program* param None* retval None*/ int main(void)/* System Clocks Configuration */RCC_Configuration();*/*/* NVIC configuration */NVIC_Configuration();/* Configure the GPIO ports

26、*/GPIO_Configuration();GPIO_ResetBits(GPIO_LED,LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN);/*灯*/*/* USART1 and USART2 configured as follow:- BaudRate = 9600 baud- Word Length = 8 Bits- One Stop Bit- No parity- Hardware flow control disabled (RTS and CTS signals)- Receive and transmit enabled*/115200*/USART_Ini

27、tStructure.USART_WordLength = USART_WordLength_8b;/*USART_InitStructure.USART_StopBits = USART_StopBits_1; /* */USART_InitStructure.USART_Parity = USART_Parity_No;/*无奇偶校验*/关闭所有的LED指示/*USART1andUSART2configurationUSART_InitStructure.USART_BaudRate = 115200;/*设置波特率为设置数据位为8*/设置停止位为1位USART_InitStructure

28、.USART_HardwareFlowControlUSART_HardwareFlowControl_None;/*无硬件流控*/USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*发送和接收*/*配置串口1 */USART_Init(USART1, &USART_InitStructure);/*配置串口2*/USART_Init(USART2, &USART_InitStructure);/*使能串口1的发送和接收中断*/USART_ITConfig(USART1, USART_IT_RXNE

29、, ENABLE);USART_ITConfig(USART1, USART_IT_TXE, ENABLE);/*使能串口2的发送和接收中断*/USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);USART_ITConfig(USART2, USART_IT_TXE, ENABLE);/*使能串口1 */USART_Cmd(USART1, ENABLE);/*使能串口2 */USART_Cmd(USART2, ENABLE);/* Wait until end of transmission from USART1 to USART2 */while(R

30、xCounter2 RxBufferSize2)/* Wait until end of transmission from USART2 to USART1 */while(RxCounter1 RxBufferSize1)/* Check the received data with the send ones */TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1);/* TransferStatus1 = PASSED, if the data transmitted from USART2 andreceiv

31、ed by USART1 are the same */* TransferStatus1 = FAILED, if the data transmitted from USART2 andreceived by USART1 are different */TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);/* TransferStatus2 = PASSED, if the data transmitted from USART1 andreceived by USART2 are the same */* T

32、ransferStatus2 = FAILED, if the data transmitted from USART1 andreceived by USART2 are different */while (1)if(TransferStatus1 = PASSED)GPIO_SetBits(GPIO_LED,LD1_PIN);/*发送的数据相同*/点亮LD1,串口1接收的数据与串口2else if(TransferStatus1 = FAILED)GPIO_SetBits(GPIO_LED,LD2_PIN);/*发送的数据不相同*/点亮LD2, 串口接收的数据与串口if(Transfer

33、Status2 = PASSED)GPIO_SetBits(GPIO_LED,LD3_PIN);/*发送的数据相同*/点亮LD3, 串口接收的数据与串口else if(TransferStatus2 = FAILED)GPIO_SetBits(GPIO_LED,LD4_PIN);/*发送的数据不相同*/点亮LD4, 串口接收的数据与串口/* brief Configures the different system clocks.* param None* retval None*/GPI0_InitStructure.GPI0_Pin = USART1_RxPin;void RCC_Conf

34、iguration(void)/*使能串口1和串口2使用的GP10时钟*/RCC_APB2PeriphClockCmd(USART1_GPIO_CLK |USART2_GPIO_CLK,ENABLE);/* Enable USART1 Clock */*使能串口1时钟*/RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE);/*使能串口2时钟*/RCC_APB1PeriphClockCmd(USART2_CLK, ENABLE);/*使能LED灯使用的GPI0时钟*/RCC_APB2PeriphClockCmd(RCC_GPI0_LED, ENABLE);/*

35、brief Configures the different GPI0 ports.* param None* retval None*/ void GPI0_Configuration(void)GPI0_InitTypeDef GPI0_InitStructure;/*串口1 RX管脚配置*/* Configure USART1 Rx as input floating */GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_In

36、it(USART1_GPIO, &GPIO_InitStructure);/*串口2 RX管脚配置*/ /* Configure USART2 Rx as input floating*/GPIO_InitStructure.GPIO_Pin = USART2_RxPin;GPIO_Init(USART2_GPIO, &GPIO_InitStructure);/*串口1 TX管脚配置*/ /* Configure USART1 Tx as alternatefunction push-pull */GPIO_InitStructure.GPIO_Pin = USART1_TxP

37、in;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(USART1_GPIO, &GPIO_InitStructure);/*串口2 TX管脚配置*/ /* Configure USART2 Tx as alternatefunction push-pull */GPIO_InitStructure.GPIO_Pin = USART2_TxPin;GPIO_Init(USART2_GPIO, &GPIO_InitSt

38、ructure);/*配置LED灯使用的GPIO管脚模式*/GPIO_InitStructure.GPIO_Pin = LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIO_LED, &GPIO_InitStructure);NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/* brief Configures the nested vectored interrupt controller.* param

39、 None* retval None*/ void NVIC_Configuration(void)NVIC_InitTypeDef NVIC_InitStructure;/* Configure the NVIC Preemption Priority Bits */NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);/* Enable the USART1 Interrupt */NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelSu

40、bPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/* Enable the USART2 Interrupt */NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_Init(&NVIC_InitStructure);/* brief Compares two buffers.* param pBuffer1, pBuffer2: buffers to be compared.* param BufferLength: buffers length* retval PASSED: pBuffer1 identical to pBuffer2* FAILED: pBuffer1 differs from pBuffer2*/TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength)while(BufferLength-)if(*pBuffer1 != *pBuffer2)return FAILED;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论