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

共2条 1/1 1 跳转至

36号LED&RGB代码

菜鸟
2014-12-22 16:32:50     打赏
view plaincopy to clipboardprint?
/* Includes ------------------------------------------------------------------*/  
#include "stm32f10x.h"  
#include "stm32_eval.h"  
GPIO_InitTypeDef GPIO_InitStructure;  
  
void RCC_Configuration(void)//RCC时钟配置函数  
{  
  RCC_DeInit();  
      
  RCC_HSICmd(ENABLE);  
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);  
    
  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);  
    
  RCC_HSEConfig(RCC_HSE_OFF);  
  RCC_LSEConfig(RCC_LSE_OFF);  
  RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz  
  RCC_PLLCmd(ENABLE);  
  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);  
  RCC_PCLK2Config(RCC_HCLK_Div1);  
  RCC_PCLK1Config(RCC_HCLK_Div2);  
  RCC_HCLKConfig(RCC_SYSCLK_Div1);  
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  
  while(RCC_GetSYSCLKSource() != 0x08);  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);  
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  GPIO_Init(GPIOD, &GPIO_InitStructure);  
    GPIO_ResetBits(GPIOD,GPIO_Pin_2);//关掉蜂鸣器  
}  
  
void GPIO_INIT()    
{    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;      
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_3;    
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
  GPIO_Init(GPIOA, &GPIO_InitStructure);     
    GPIO_Init(GPIOC, &GPIO_InitStructure);    
}    
  
void delay_us(u32 n)//声明延时函数  
{  
    u8 j;  
    while(n--)  
    for(j=0;j<10;j++);  
}  
  
void  delay_ms(u32 n)  
{  
    while(n--)  
    delay_us(1000);  
}  
  
  
/** 
  * @brief  Main program. 
  * @param  None 
  * @retval None 
  */  
int main(void)  
{   
    RCC_Configuration();  
 GPIO_INIT();  
     GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);//开始运行时RGB灭  
     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);//开始运行时LED全灭  
    while(1){  
      GPIO_ResetBits(GPIOC,GPIO_Pin_3);//D4亮  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_3);//D4灭  
        delay_ms(80);  
              GPIO_ResetBits(GPIOC,GPIO_Pin_2);//D3亮  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_2);//D3灭  
        delay_ms(80);  
              GPIO_ResetBits(GPIOC,GPIO_Pin_1);  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_1);  
        delay_ms(80);  
              GPIO_ResetBits(GPIOC,GPIO_Pin_0);  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_0);  
        delay_ms(80);  
              GPIO_ResetBits(GPIOC,GPIO_Pin_7);  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_7);  
        delay_ms(80);  
              GPIO_ResetBits(GPIOC,GPIO_Pin_6);  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_6);  
        delay_ms(80);  
              GPIO_ResetBits(GPIOC,GPIO_Pin_5);  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_5);  
        delay_ms(80);  
              GPIO_ResetBits(GPIOC,GPIO_Pin_4);  
        delay_ms(80);  
        GPIO_SetBits(GPIOC,GPIO_Pin_4);  
        delay_ms(80);  
           GPIO_ResetBits(GPIOA,GPIO_Pin_1); //RGB蓝灯亮   
        delay_ms(1500);          
        GPIO_SetBits(GPIOA,GPIO_Pin_1);  //RGB蓝灯灭  
        GPIO_ResetBits(GPIOA,GPIO_Pin_2);  //RGB红灯亮  
        delay_ms(1500);          
        GPIO_SetBits(GPIOA,GPIO_Pin_2); //RGB红灯灭   
         GPIO_ResetBits(GPIOA,GPIO_Pin_3); //RGB绿灯亮   
        delay_ms(1500);          
        GPIO_SetBits(GPIOA,GPIO_Pin_3);  //RGB绿灯灭    
        GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2);  //RGB亮蓝灯和绿灯组合的颜色  
        delay_ms(1500);          
        GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2);  //RGB灭蓝灯和绿灯组合的颜色  
        }  
           
          
}  
 

菜鸟
2014-12-25 23:04:52     打赏
2楼
36号时钟滴答代码 view plaincopy to clipboardprint?
  1. /**
  2. ******************************************************************************
  3. * @file EXTI/EXTI_Config/main.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 08-April-2011
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. *
  19. ?COPYRIGHT 2011 STMicroelectronics
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f10x.h"
  24. #include "stm32_eval.h"
  25. //#include "delay.h"
  26. #include
  27. volatile int flag;
  28. #define Set_B20() GPIO_SetBits(GPIOC, GPIO_Pin_12)
  29. #define Reset_B20() GPIO_ResetBits(GPIOC, GPIO_Pin_12)
  30. #define Read_B20() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12)
  31. unsigned char Error_Flag=0;
  32. unsigned char zf=0;
  33. void SysTick_Configuration(void)
  34. {
  35. /* Setup SysTick Timer for 10 msec interrupts */
  36. if (SysTick_Config(48000)) //SysTick??
  37. {
  38. /* Capture error */
  39. while (1);
  40. }
  41. /* Configure the SysTick handler priority */
  42. NVIC_SetPriority(SysTick_IRQn, 0x0); //SysTick?????
  43. }
  44. /** @addtogroup STM32F10x_StdPeriph_Examples
  45. * @{
  46. */
  47. /** @addtogroup EXTI_Config
  48. * @{
  49. */
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. GPIO_InitTypeDef GPIO_InitStructure;
  55. USART_InitTypeDef USART_InitStructure;
  56. USART_ClockInitTypeDef USART_ClockInitStructure;
  57. void RCC_Configuration(void)
  58. {
  59. RCC_DeInit();
  60. RCC_HSICmd(ENABLE);
  61. while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  62. RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
  63. RCC_HSEConfig(RCC_HSE_OFF);
  64. RCC_LSEConfig(RCC_LSE_OFF);
  65. //******48MHZ*******//
  66. RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_6);//6*8=24MHz
  67. //************************************//
  68. RCC_PLLCmd(ENABLE);
  69. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //检查指定的RCC标志位(PLL)设置与否
  70. RCC_ADCCLKConfig(RCC_PCLK2_Div4); //设置ADC时钟频率
  71. RCC_PCLK2Config(RCC_HCLK_Div1); // APB2=HCLK/1 PCLK2=HCLK/1
  72. RCC_PCLK1Config(RCC_HCLK_Div2); /// APB1=HCLK/2 PCLK1=HCLK/2
  73. RCC_HCLKConfig(RCC_SYSCLK_Div1); // AHB使用系统时钟 HCLK=SYSCLK
  74. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // 配置系统时钟
  75. while(RCC_GetSYSCLKSource() != 0x08) //检查是否将HES 6倍频后作为系统时钟
  76. // SystemInit();
  77. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
  78. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  79. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
  80. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  81. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  82. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  83. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  84. GPIO_Init(GPIOD, &GPIO_InitStructure);
  85. GPIO_ResetBits(GPIOD,GPIO_Pin_2);
  86. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
  87. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  88. 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
  89. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  90. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  91. GPIO_Init(GPIOC, &GPIO_InitStructure);
  92. 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);
  93. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //TIM2
  94. }
  95. void USART_int(long BaudRate)
  96. {
  97. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
  98. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  99. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  100. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  101. GPIO_Init(GPIOA, &GPIO_InitStructure);
  102. /* PA10 USART1_Rx */
  103. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  104. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  105. GPIO_Init(GPIOA, &GPIO_InitStructure);
  106. /* USARTx configured as follow:
  107. - BaudRate = 115200 baud
  108. - Word Length = 8 Bits
  109. - One Stop Bit
  110. - No parity
  111. - Hardware flow control disabled (RTS and CTS signals)
  112. - Receive and transmit enabled
  113. */
  114. USART_InitStructure.USART_BaudRate = BaudRate;
  115. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  116. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  117. USART_InitStructure.USART_Parity = USART_Parity_No;
  118. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  119. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  120. USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  121. USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  122. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  123. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  124. USART_ClockInit(USART1, &USART_ClockInitStructure);
  125. USART_Init(USART1, &USART_InitStructure);
  126. USART_Cmd(USART1, ENABLE);
  127. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  128. USART_Cmd(USART1, ENABLE);
  129. }
  130. void delay_18b20(u32 nus) //18b20延时
  131. {
  132. u16 i;
  133. while(nus--)
  134. for(i=12;i>0;i--);
  135. }
  136. void delay_us(u32 n) //延时
  137. {
  138. u8 j;
  139. while(n--)
  140. for(j=0;j<10;j++);
  141. }
  142. void delay_ms(u32 n)
  143. {
  144. while(n--)
  145. delay_us(1000);
  146. }
  147. void Init18B20(void) //18B20
  148. {
  149. u8 aa=0;
  150. u8 count =0;
  151. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能GPIOC时钟
  152. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  153. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//开漏输出
  154. GPIO_Init(GPIOC, &GPIO_InitStructure);
  155. Set_B20() ; // GPIO_SetBits(GPIOC, GPIO_Pin_12)
  156. delay_18b20(1);
  157. Reset_B20(); // GPIO_ResetBits(GPIOC, GPIO_Pin_12)
  158. delay_18b20(480);
  159. Set_B20();
  160. // delay_18b20(500);
  161. delay_18b20(480);
  162. count=0;
  163. aa=Read_B20(); //读取温度
  164. while(!aa && count<100) //温度不宜超过99°C
  165. {
  166. aa=Read_B20();
  167. count++;
  168. }
  169. if(count>=99)
  170. Error_Flag=1;
  171. else
  172. Error_Flag=0;
  173. }
  174. unsigned char Read18B20(void)//读数据
  175. {
  176. unsigned char i=0;
  177. unsigned char date=0;
  178. u8 tempp;
  179. for(i=8;i>0;i--)
  180. {
  181. Reset_B20(); // GPIO_ResetBits(GPIOC, GPIO_Pin_12)
  182. date>>=1; //右移一位
  183. delay_18b20(1);
  184. Set_B20(); //置1
  185. delay_18b20(1);
  186. tempp=Read_B20(); //读取温度
  187. if(tempp)
  188. date|=0x80;// 1000 0000将最高位填1 ,然后右移,每次最高位由0变1,使8位全部传递完毕 0xff = 1111 1111
  189. delay_18b20(60);
  190. }
  191. return(date);
  192. }
  193. void Write18B20(unsigned char date)//写数据
  194. {
  195. unsigned char i=0;
  196. for (i=8; i>0; i--)
  197. {
  198. Reset_B20(); //置0
  199. delay_18b20(1);
  200. if(date & 0x01)
  201. {
  202. Set_B20();
  203. }
  204. else
  205. { Reset_B20();}
  206. delay_18b20(60);
  207. date>>=1; //数据右移一位
  208. Set_B20();
  209. delay_18b20(1);
  210. }
  211. delay_18b20(15);
  212. }
  213. float Read_T()//读温度
  214. {
  215. unsigned char TUp,TDown;
  216. unsigned char fTemp;
  217. u8 TT=0;
  218. float Temp = 0;
  219. Init18B20(); //18B20的初始化
  220. Write18B20(0xcc);//写指令:跳过ROM,仅一个DS18B20
  221. Write18B20(0x44);//启动温度转换
  222. Init18B20();
  223. Write18B20(0xcc);//写指令:跳过ROM,仅一个DS18B20
  224. Write18B20(0xbe);//读暂存寄存器
  225. TDown = Read18B20();//低字节
  226. TUp = Read18B20();//高字节
  227. if(TUp>0x7f)
  228. {
  229. TDown=~TDown;
  230. TUp=~TUp+1;
  231. TUp/=8;
  232. zf=1;
  233. }
  234. else
  235. zf=0;
  236. fTemp=TDown&0x0f;
  237. TUp<<=4;
  238. TDown>>=4;
  239. TT=TUp|TDown;
  240. Temp=TT+(float)fTemp/16;
  241. return(Temp);
  242. }
  243. int main(void)
  244. {
  245. /*!< At this stage the microcontroller clock setting is already configured,
  246. this is done through SystemInit() function which is called from startup
  247. file (startup_stm32f10x_xx.s) before to branch to application main.
  248. To reconfigure the default setting of SystemInit() function, refer to
  249. system_stm32f10x.c file
  250. */
  251. /* System Clocks Configuration */
  252. char ID[8];
  253. int i;
  254. RCC_Configuration();
  255. USART_int(115200);
  256. SysTick_Configuration();
  257. printf(" config done...\r\n");
  258. delay_ms(1000);
  259. Init18B20();
  260. Write18B20(0x33);
  261. delay_18b20(20);
  262. for(i=0;i<8;i++)
  263. {
  264. ID[i] = Read18B20();
  265. }
  266. while(1)
  267. {
  268. if(flag == 300)
  269. {
  270. printf("At the moment of ID is:");// 每300ms输出ID
  271. for(i=0;i<8;i++)
  272. {
  273. printf("%x",ID[i]);
  274. }
  275. printf("\r\n") ;
  276. }
  277. if(flag == 500)
  278. {
  279. printf("The Temperature is:%f\r\n",Read_T());//每500ms输出温度
  280. printf("===================================================\r\n");
  281. }
  282. }
  283. }
  284. #ifdef USE_FULL_ASSERT
  285. /**
  286. * @brief Reports the name of the source file and the source line number
  287. * where the assert_param error has occurred.
  288. * @param file: pointer to the source file name
  289. * @param line: assert_param error line source number
  290. * @retval None
  291. */
  292. void assert_failed(uint8_t* file, uint32_t line)
  293. {
  294. /* User can add his own implementation to report the file name and line number,
  295. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  296. /* Infinite loop */
  297. while (1)
  298. {
  299. }
  300. }
  301. #endif
  302. /**
  303. * @}
  304. */
  305. /**
  306. * @}
  307. */
  308. #ifdef __GNUC__
  309. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  310. set to 'Yes') calls __io_putchar() */
  311. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  312. #else
  313. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  314. #endif /* __GNUC__ */
  315. /**
  316. * @brief Retargets the C library printf function to the USART.
  317. * @param None
  318. * @retval None
  319. */
  320. PUTCHAR_PROTOTYPE
  321. {
  322. /* Place your implementation of fputc here */
  323. /* e.g. write a character to the USART */
  324. USART_SendData(EVAL_COM1, (uint8_t) ch);
  325. /* Loop until the end of transmission */
  326. while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  327. {}
  328. return ch;
  329. }
  330. #ifdef USE_FULL_ASSERT
  331. /**
  332. * @brief Reports the name of the source file and the source line number
  333. * where the assert_param error has occurred.
  334. * @param file: pointer to the source file name
  335. * @param line: assert_param error line source number
  336. * @retval None
  337. */
  338. void assert_failed(uint8_t* file, uint32_t line)
  339. {
  340. /* User can add his own implementation to report the file name and line number,
  341. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  342. /* Infinite loop */
  343. while (1)
  344. {
  345. }
  346. }
  347. #endif

共2条 1/1 1 跳转至

回复

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