这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 【11.16】10号作业千辛万苦更新- -。

共11条 1/2 1 2 跳转至

【11.16】10号作业千辛万苦更新- -。

菜鸟
2015-04-02 18:37:45     打赏


菜鸟
2015-05-07 23:53:41     打赏
2楼

画许多元器件的方法和上次画核心板的方法一样,在这里就不做赘述。接下来讲讲我觉得此次作业不一样的地方。

1.在原理图库里画一个文本框,文本框外的线调成蓝色,在用线在文本框里画两个躺着的日

2.在框外面画线并设置就画好了,就可以调用它了。

3.当画到两个成镜面关系的NPN管时,选择一个NPN管放左边,右边NPN管的画法就是选择和左边一样型号的NPN管并选中它,按住鼠标左键不放,接着按ctrl+X,这样两个成镜面关系的管就画好了

4.接着画剩下的东西

5.最终成果



 


菜鸟
2015-05-08 00:18:25     打赏
3楼

部分元器件的封装:

1.首先新建一个工程,把之前画的sheet2添加到工程里,再建立一个PCB Libraries

2.在PCB里画所需的元器件封装,比如蜂鸣器

在之前画的Sheet2中找到蜂鸣器,然后将新画的封装添加进去

最后自己画的封装就添加好了

3.接下来画剩下的一些原器件的封装

 

 

 

 

 

 

 

 

 


菜鸟
2015-06-15 11:07:49     打赏
4楼

拖了那么久才发布,实在是忘记了。OK,话不多说,上图~~

1.老师给的一些元器件没有封装,但是老师已经画好封装了,所以添加进去就行了~

这样就把缺少的添加进去啦~

2.把元器件摆放好,就可以自动布线了~但就我摆放的样子来看,呵呵,还是能看的~

会发现,并没有自动布线完全,然后我就调了一下线的大小

然后再布线就可以了

3.附铜,分别在top layer 和bottom layer附铜

大功告成啦~~~附铜成这样,我也是醉了


菜鸟
2015-10-19 13:28:10     打赏
5楼

1.点开图标开始安装

2.出现图标后一直点next.

.


3.等待它安装


4.打开破解文件


5.选择ARM,CID从刚下好的软件中找出来并复制到破解文件中。



6.将破解文件最下面得出的条码在复制到上图中的new license中再点击close就OK了。


菜鸟
2015-11-02 17:17:07     打赏
6楼

/**
******************************************************************************
* @file    GPIO/IOToggle/main.c
* @author  MCD Application Team
* @version V3.5.0
* @date    08-April-2011
* @brief   Main program body.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*

© COPYRIGHT 2011 STMicroelectronics
******************************************************************************
*/


/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"

GPIO_InitTypeDef GPIO_InitStructure;

void RCC_Configuration(void)
{
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 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_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);



while(1)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_0);
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_1);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_2);
GPIO_ResetBits(GPIOC,GPIO_Pin_3);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_3);
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_5);
GPIO_ResetBits(GPIOC,GPIO_Pin_6);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_6);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
}
}

视频网址:http://v.youku.com/v_show/id_XMTQ0MTQyNjk3Ng==.html


菜鸟
2015-11-16 16:01:31     打赏
7楼

#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
GPIO_InitTypeDef GPIO_InitStructure;
void RCC_Configuration(void);
void GPIO_INIT(void) ;
void Function(void) ;

int main(void)
{
RCC_Configuration();
GPIO_INIT();
Function();
}

void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);
//RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
}

GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
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 delay_us(u32 n)
{
u8 j;
while(n--)
for(j=0;j<10;j++);
}
void  delay_ms(u32 n)
{
while(n--)
delay_us(50);
}

void GPIO_INIT(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_1|GPIO_Pin_15|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;//???
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void Number(int a)
{
switch(a)
{
case 0 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;
case 1 : GPIO_ResetBits(GPIOB,GPIO_Pin_9|GPIO_Pin_12);break;
case 2 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_13|GPIO_Pin_14);break;
case 3 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14);break;
case 4 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12);break;
case 5 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_14);break;
case 6 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;
case 7 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12);break;
case 8 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;
case 9 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14); break;
}
}


void Function(void)
{
int i=0,j=0;

GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);
delay_ms(50);

while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_1);
Number(i);
delay_ms(200);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);

GPIO_SetBits(GPIOB,GPIO_Pin_15);
Number(j);
delay_ms(200);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);

if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))
{
delay_ms(100);
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))
{
i++;
if(i==10)
{
j++;
i=0;
}
if(j==10)
{
j=0;
i=0;
}
}
}

}}


视频网址:http://v.youku.com/v_show/id_XMTQ0MTQzMDg0NA==.html


菜鸟
2015-11-16 16:45:02     打赏
8楼

#include "stm32f10x.h"
#include    "stdio.h"
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef  NVIC_InitStructure;
USART_InitTypeDef  USART_InitStructure;
USART_ClockInitTypeDef  USART_ClockInitStructure;
#define buff_size  16;
char rx_buff[],rx_buff_count=0;
void RCC_Configuration(void) ;
void GPIO_INIT(void)  ;
void USART_int(long BaudRate);
void USART_SendStr(char *str);
void delay_ms(u32 n)  ;
void delay_us(u32 n);
/****??? ****/
int main()
{
RCC_Configuration();
GPIO_INIT();
USART_int(9600);
GPIO_ResetBits(GPIOC,0xffff);
delay_ms(200);
GPIO_SetBits(GPIOC,0xffff);
USART_SendStr("USART Led Speed\r\n");
USART_SendStr("\n>");//
while(1);
}

void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);
//RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*??PLL?????72M
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
}
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
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 delay_us(u32 n)
{
u8 j;
while(n--)
for(j=0;j<10;j++);
}
void delay_ms(u32 n)
{
while(n--)
delay_us(1000);
}

void GPIO_INIT(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

void USART_int(long BaudRate)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

void USART_SendStr(char *str)
{
while((*str)!='\0')
{
USART_SendData(USART1,*str++);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}

unsigned int translate(char* pstr)
{
int s = 0;
while(*pstr != '\0')
{
if(*pstr >= '0' && *pstr <= '9')
{
s = s * 10 + *pstr - '0';
}
pstr++;
}
return s;
}

void LED(char *S,char LEN)
{
int m,i;
int a[8]={GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3,GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7};
m=translate(S);
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);
delay_ms(100);
for(i=0;i<8;i++)
{
GPIO_ResetBits(GPIOC,a[i]);
delay_ms(m);
GPIO_SetBits(GPIOC,a[i]);
delay_ms(m);
}
}
void input_ASK()
{
char j;
LED(rx_buff,rx_buff_count);
rx_buff_count=0;
for (j=0;j<10;j++)
{
rx_buff[j]='\0';
}
USART_SendStr("\n>");
}

void USART1_IRQHandler(void)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)  { }
if(USART_ReceiveData(USART1)==0x0d)
{
input_ASK();
}
else
{
USART_SendData(USART1,USART_ReceiveData(USART1));
rx_buff[rx_buff_count]= USART_ReceiveData(USART1);
rx_buff_count++;
}
USART_ClearFlag(USART1, USART_FLAG_RXNE);
}


视频网址:http://v.youku.com/v_show/id_XMTQ0MTQzMjA4MA==.html

               http://v.youku.com/v_show/id_XMTQ0MTQzMjcxNg==.html


菜鸟
2015-11-16 17:21:33     打赏
9楼


/**
******************************************************************************
* @file    EXTI/EXTI_Config/main.c
* @author  MCD Application Team
* @version V3.5.0
* @date    08-April-2011
* @brief   Main program body
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*

© COPYRIGHT 2011 STMicroelectronics
******************************************************************************
*/


/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include
/** @addtogroup STM32F10x_StdPeriph_Examples
* @{
*/

/** @addtogroup EXTI_Config
* @{
*/

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
EXTI_InitTypeDef   EXTI_InitStructure;
GPIO_InitTypeDef   GPIO_InitStructure;
NVIC_InitTypeDef   NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
unsigned int flag;
unsigned char led;
void RCC_Configuration(void)
{/*
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);
*/
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
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);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}

void USART_int(long BaudRate)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* PA10 USART1_Rx  */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = BaudRate;//??????
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???????8bit
USART_InitStructure.USART_StopBits = USART_StopBits_1;//????1
USART_InitStructure.USART_Parity = USART_Parity_No;//????
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//??????none
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//??????????
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);
}

/* Private functions ---------------------------------------------------------*/

/**
* @brief  Main program.
* @param  None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/

/* System Clocks Configuration */
RCC_Configuration();

NVIC_Configuration();
//USART_int(115200);
// printf("config done...\r\n");

/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 36000;
TIM_TimeBaseStructure.TIM_Prescaler = 100;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE );

/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);

while (1){
//if(flag == 1)
// {printf("TIM2 interrupt......\r\n");
//flag = 0;
}
//else{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);}
//}
}

void TIM2_IRQHandler(void) //TIM3 ??
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //?? TIM3 ????????
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //?? TIM3 ??????
//  flag = ~flag;
// if(flag){
// GPIO_SetBits(GPIOC,GPIO_Pin_1);}
// else {GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
flag++;
if(flag == 1)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
}
else if(flag == 2)
{
GPIO_SetBits(GPIOC,GPIO_Pin_0);}
else if(flag == 3)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
else if(flag == 4)
{
GPIO_SetBits(GPIOC,GPIO_Pin_1);}
else if(flag == 5)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_2);}
else if(flag == 6)
{
GPIO_SetBits(GPIOC,GPIO_Pin_2);}
else if(flag == 7)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_3);}
else if(flag == 8)
{
GPIO_SetBits(GPIOC,GPIO_Pin_3);}
else if(flag == 9)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_4);}
else if(flag == 10)
{
GPIO_SetBits(GPIOC,GPIO_Pin_4);}
else if(flag == 11)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_5);}
else if(flag == 12)
{
GPIO_SetBits(GPIOC,GPIO_Pin_5);}
else if(flag == 13)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_6);}
else if(flag == 14)
{
GPIO_SetBits(GPIOC,GPIO_Pin_6);}
else if(flag == 15)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_7);}
else if(flag == 16)
{
GPIO_SetBits(GPIOC,GPIO_Pin_7);
flag = 0;
}

}
}


#ifdef  USE_FULL_ASSERT

/**
* @brief  Reports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @param  file: pointer to the source file name
* @param  line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}

#endif

/**
* @}
*/

/**
* @}
*/

#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */


/**
* @brief  Retargets the C library printf function to the USART.
* @param  None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(EVAL_COM1, (uint8_t) ch);

/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}

return ch;
}

#ifdef  USE_FULL_ASSERT

/**
* @brief  Reports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @param  file: pointer to the source file name
* @param  line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}

#endif

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/


视频网址:http://v.youku.com/v_show/id_XMTQ0MTQyNjk3Ng==.html


菜鸟
2016-01-19 11:59:35     打赏
10楼
view plaincopy to clipboardprint?
  1. #include "stm32f10x.h"    
  2. #include "stm32_eval.h"    
  3. #include <stdio.h>    
  4. #define VREF 3.3    
  5. GPIO_InitTypeDef   GPIO_InitStructure;    
  6. USART_InitTypeDef USART_InitStructure;    
  7. USART_ClockInitTypeDef USART_ClockInitStructure;    
  8. int volt;    
  9. unsigned int temp0,temp1,temp2;    
  10. void RCC_Configuration(void)    
  11. {/*  
  12.   RCC_DeInit();  
  13.       
  14.   RCC_HSICmd(ENABLE);  
  15.   while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);  
  16.     
  17.   RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);  
  18.     
  19.   RCC_HSEConfig(RCC_HSE_OFF);  
  20.   RCC_LSEConfig(RCC_LSE_OFF);  
  21.   RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz  
  22.   RCC_PLLCmd(ENABLE);  
  23.   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  
  24.   RCC_ADCCLKConfig(RCC_PCLK2_Div4);  
  25.   RCC_PCLK2Config(RCC_HCLK_Div1);  
  26.   RCC_PCLK1Config(RCC_HCLK_Div2);  
  27.   RCC_HCLKConfig(RCC_SYSCLK_Div1);  
  28.   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  
  29.   while(RCC_GetSYSCLKSource() != 0x08);  
  30. */    
  31.     SystemInit();    
  32.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
  33.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
  34. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
  35.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
  36.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    
  37.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  38.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
  39.   GPIO_Init(GPIOD, &GPIO_InitStructure);    
  40.     GPIO_ResetBits(GPIOD,GPIO_Pin_2);    
  41.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);    
  42.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
  43.   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;    
  44.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  45.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
  46.   GPIO_Init(GPIOC, &GPIO_InitStructure);    
  47.     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);    
  48.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    
  49. }     
  50.     
  51. void USART_int(long BaudRate)    
  52. {    
  53.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);    
  54.        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    
  55.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;    
  56.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     
  57.     GPIO_Init(GPIOA, &GPIO_InitStructure);    
  58.     /* PA10 USART1_Rx  */    
  59.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    
  60.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
  61.     GPIO_Init(GPIOA, &GPIO_InitStructure);    
  62.   /* USARTx configured as follow:  
  63.         - BaudRate = 115200 baud    
  64.         - Word Length = 8 Bits  
  65.         - One Stop Bit  
  66.         - No parity  
  67.         - Hardware flow control disabled (RTS and CTS signals)  
  68.         - Receive and transmit enabled  
  69.   */    
  70.   USART_InitStructure.USART_BaudRate = BaudRate;   
  71.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;   
  72.   USART_InitStructure.USART_StopBits = USART_StopBits_1;    
  73.   USART_InitStructure.USART_Parity = USART_Parity_No;    
  74.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    
  75.     USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;         
  76.     USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;          
  77.     USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;          
  78.     USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;    
  79.     USART_ClockInit(USART1, &USART_ClockInitStructure);    
  80.   USART_Init(USART1, &USART_InitStructure);    
  81.   USART_Cmd(USART1, ENABLE);    
  82.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);    
  83.  USART_Cmd(USART1, ENABLE);    
  84. }    
  85. void delay_us(u32 n)    
  86. {    
  87.     u8 j;    
  88.     while(n--)    
  89.     for(j=0;j<10;j++);    
  90. }    
  91. void  delay_ms(u32 n)    
  92. {    
  93.     while(n--)    
  94.     delay_us(1000);    
  95. }    
  96. void PWM_Config()    
  97. {    
  98.     uint16_t PrescalerValue = 0;    
  99.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;    
  100.   TIM_OCInitTypeDef  TIM_OCInitStructure;    
  101.     /* TIM2 clock enable */    
  102.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    
  103.   /* GPIOA  enable */    
  104.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);    
  105.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PWM&RGB- PA1 PA2 PA3    
  106.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    
  107.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  108.   GPIO_Init(GPIOA, &GPIO_InitStructure);    
  109.         TIM_Cmd(TIM2, ENABLE);    
  110.       /* Compute the prescaler value */    
  111.   PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;    
  112.   /* Time base configuration */    
  113.   TIM_TimeBaseStructure.TIM_Period = 0x07FF;    
  114.   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;    
  115.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;    
  116.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;    
  117.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);    
  118.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;    
  119.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;    
  120.   /* PWM1 Mode configuration: Channel2 ,PA1在通道2*/    
  121.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;    
  122.   TIM_OCInitStructure.TIM_Pulse = 0xFFFF;    
  123.   TIM_OC2Init(TIM2, &TIM_OCInitStructure);    
  124.     /* PWM1 Mode configuration: Channel3 PA2在通道3*/    
  125.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;    
  126.   TIM_OCInitStructure.TIM_Pulse = 0xFFFF;    
  127.   TIM_OC3Init(TIM2, &TIM_OCInitStructure);    
  128.     /* PWM1 Mode configuration: Channel4 PA3在通道4*/    
  129.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;    
  130.   TIM_OCInitStructure.TIM_Pulse = 0xFFFF;    
  131.   TIM_OC4Init(TIM2, &TIM_OCInitStructure);    
  132.   TIM_ARRPreloadConfig(TIM2, ENABLE);    
  133. }    
  134. void ADC_CONFIG(){    
  135.     ADC_InitTypeDef ADC_InitStructure;    
  136.     #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)    
  137.   /* ADCCLK = PCLK2/2 */    
  138.   RCC_ADCCLKConfig(RCC_PCLK2_Div2);     
  139. #else    
  140.   /* ADCCLK = PCLK2/4 */    
  141.   RCC_ADCCLKConfig(RCC_PCLK2_Div4);     
  142. #endif    
  143. ADC_DeInit(ADC1);    
  144.   /* Enable ADC1 and GPIOC clock */    
  145.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);    
  146.     /* Configure PB0 (ADC Channel14) as analog input -------------------------*/    
  147.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//ADC所在端口PB0    
  148.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式    
  149.   GPIO_Init(GPIOB, &GPIO_InitStructure);    
  150.   /* ADC1 configuration ------------------------------------------------------*/    
  151.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;    
  152.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;    
  153.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;    
  154.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;    
  155.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐    
  156.   ADC_InitStructure.ADC_NbrOfChannel = 1;//ADC通道数为1    
  157.   ADC_Init(ADC1, &ADC_InitStructure);//初始化ADC1    
  158.   /* Enable ADC1 DMA */    
  159.   ADC_DMACmd(ADC1, ENABLE);    
  160.   /* Enable ADC1 */    
  161.   ADC_Cmd(ADC1, ENABLE);    
  162. }    
  163.     
  164. int Get_ADC(){    
  165.      /* ADC1 regular channel configuration */     
  166.   ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);//通道:8 ,采样时间    
  167.     /* Enable ADC1 reset calibration register */       
  168.   ADC_ResetCalibration(ADC1);//重置ADC1的校准寄存器    
  169.   /* Check the end of ADC1 reset calibration register */    
  170.   while(ADC_GetResetCalibrationStatus(ADC1));//确认重置完毕    
  171.   /* Start ADC1 calibration */    
  172.   ADC_StartCalibration(ADC1);    
  173.   /* Check the end of ADC1 calibration */    
  174.   while(ADC_GetCalibrationStatus(ADC1));    
  175.   /* Start ADC1 Software Conversion */     
  176.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);  
  177.     return ADC_GetConversionValue(ADC1);    
  178. }    
  179. void PWM_TEST()    
  180. {    
  181.      
  182.     unsigned int temp0=volt,temp1=0,temp2=volt;    
  183.     printf("PWM-RGB TEST......\r\n");    
  184.    for(;(temp0>0)||(temp1<volt);temp0--,temp1++)     
  185.    {    
  186.        TIM_SetCompare2(TIM2, temp0);//temp0:volt~0    
  187.      TIM_SetCompare3(TIM2, temp1);//temp1:0~volt    
  188.        delay_us(1000);    
  189.    }    
  190.      for(;(temp0<volt)||(temp2>0);temp0++,temp2--)    
  191.      {    
  192.        TIM_SetCompare2(TIM2, temp0);//temp0:0~volt    
  193.        TIM_SetCompare4(TIM2, temp2);//temp2:volt~0    
  194.        delay_us(1000);    
  195.    }    
  196.    for(;(temp1>0)||(temp2<volt);temp1--,temp2++)  
  197.    {    
  198.        TIM_SetCompare4(TIM2, temp2);//temp2:0~volt    
  199.        TIM_SetCompare3(TIM2, temp1);//temp1:volt~0    
  200.        delay_us(1000);    
  201.    }    
  202. }    
  203. int main(void)    
  204. {    
  205.   float Volt=0.00;    
  206.     int ADValue = 0;    
  207.   RCC_Configuration();    
  208.   USART_int(115200);    
  209.     ADC_CONFIG();    
  210.     Get_ADC();    
  211.     PWM_Config();    
  212.     delay_ms(1000);    
  213.     printf(" config done...\r\n");    
  214.     while(1)    
  215.     {    
  216.         ADValue = Get_ADC();    
  217.         Volt = VREF*ADValue/4095;    
  218.         volt=Volt*1000;    
  219.         printf("===============================\r\n");    
  220.         printf("The ADC value is:%d\r\n",ADValue);    
  221.         printf("The Volt is:%f V\r\n",Volt);    
  222.         printf("The volt is:%d \r\n",volt);    
  223.         PWM_TEST();    
  224.         delay_ms(500);    
  225.     }    
  226. }    
  227.     
  228. #ifdef  USE_FULL_ASSERT    
  229.     
  230. void assert_failed(uint8_t* file, uint32_t line)    
  231. {     
  232.   while (1)    
  233.   {    
  234.   }    
  235. }    
  236.     
  237. #endif    
  238.     
  239. #ifdef __GNUC__    
  240.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)    
  241. #else    
  242.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)    
  243. #endif /* __GNUC__ */    
  244.       
  245. PUTCHAR_PROTOTYPE    
  246. {    
  247.     
  248.   USART_SendData(EVAL_COM1, (uint8_t) ch);    
  249.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)    
  250.   {}    
  251.     
  252.   return ch;    
  253. }    
  254.     
  255. #ifdef  USE_FULL_ASSERT    
  256.     
  257. void assert_failed(uint8_t* file, uint32_t line)    
  258. {     
  259.   while (1)    
  260.   {    
  261.   }    
  262. }    
  263. #endif    
#include "stm32f10x.h"  
#include "stm32_eval.h"  
#include <stdio.h>  
#define VREF 3.3  
GPIO_InitTypeDef   GPIO_InitStructure;  
USART_InitTypeDef USART_InitStructure;  
USART_ClockInitTypeDef USART_ClockInitStructure;  
int volt;  
unsigned int temp0,temp1,temp2;  
void RCC_Configuration(void)  
{/* 
  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); 
*/  
    SystemInit();  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);  
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG  
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);  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);  
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG  
  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;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
    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);  
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  
}   
  
void USART_int(long BaudRate)  
{  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);  
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
    /* PA10 USART1_Rx  */  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
  /* USARTx configured as follow: 
        - BaudRate = 115200 baud   
        - Word Length = 8 Bits 
        - One Stop Bit 
        - No parity 
        - Hardware flow control disabled (RTS and CTS signals) 
        - Receive and transmit enabled 
  */  
  USART_InitStructure.USART_BaudRate = BaudRate; 
  USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
  USART_InitStructure.USART_StopBits = USART_StopBits_1;  
  USART_InitStructure.USART_Parity = USART_Parity_No;  
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;       
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;        
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;        
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;  
    USART_ClockInit(USART1, &USART_ClockInitStructure);  
  USART_Init(USART1, &USART_InitStructure);  
  USART_Cmd(USART1, ENABLE);  
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  
 USART_Cmd(USART1, ENABLE);  
}  
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);  
}  
void PWM_Config()  
{  
    uint16_t PrescalerValue = 0;  
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  TIM_OCInitTypeDef  TIM_OCInitStructure;  
    /* TIM2 clock enable */  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  
  /* GPIOA  enable */  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PWM&RGB- PA1 PA2 PA3  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
        TIM_Cmd(TIM2, ENABLE);  
      /* Compute the prescaler value */  
  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;  
  /* Time base configuration */  
  TIM_TimeBaseStructure.TIM_Period = 0x07FF;  
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;  
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;  
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);  
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  
  /* PWM1 Mode configuration: Channel2 ,PA1在通道2*/  
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
  TIM_OC2Init(TIM2, &TIM_OCInitStructure);  
    /* PWM1 Mode configuration: Channel3 PA2在通道3*/  
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
  TIM_OC3Init(TIM2, &TIM_OCInitStructure);  
    /* PWM1 Mode configuration: Channel4 PA3在通道4*/  
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
  TIM_OC4Init(TIM2, &TIM_OCInitStructure);  
  TIM_ARRPreloadConfig(TIM2, ENABLE);  
}  
void ADC_CONFIG(){  
    ADC_InitTypeDef ADC_InitStructure;  
    #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)  
  /* ADCCLK = PCLK2/2 */  
  RCC_ADCCLKConfig(RCC_PCLK2_Div2);   
#else  
  /* ADCCLK = PCLK2/4 */  
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);   
#endif  
ADC_DeInit(ADC1);  
  /* Enable ADC1 and GPIOC clock */  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);  
    /* Configure PB0 (ADC Channel14) as analog input -------------------------*/  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//ADC所在端口PB0  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式  
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  /* ADC1 configuration ------------------------------------------------------*/  
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;  
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐  
  ADC_InitStructure.ADC_NbrOfChannel = 1;//ADC通道数为1  
  ADC_Init(ADC1, &ADC_InitStructure);//初始化ADC1  
  /* Enable ADC1 DMA */  
  ADC_DMACmd(ADC1, ENABLE);  
  /* Enable ADC1 */  
  ADC_Cmd(ADC1, ENABLE);  
}  
  
int Get_ADC(){  
     /* ADC1 regular channel configuration */   
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);//通道:8 ,采样时间  
    /* Enable ADC1 reset calibration register */     
  ADC_ResetCalibration(ADC1);//重置ADC1的校准寄存器  
  /* Check the end of ADC1 reset calibration register */  
  while(ADC_GetResetCalibrationStatus(ADC1));//确认重置完毕  
  /* Start ADC1 calibration */  
  ADC_StartCalibration(ADC1);  
  /* Check the end of ADC1 calibration */  
  while(ADC_GetCalibrationStatus(ADC1));  
  /* Start ADC1 Software Conversion */   
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    return ADC_GetConversionValue(ADC1);  
}  
void PWM_TEST()  
{  
   
    unsigned int temp0=volt,temp1=0,temp2=volt;  
    printf("PWM-RGB TEST......\r\n");  
   for(;(temp0>0)||(temp1<volt);temp0--,temp1++)   
   {  
       TIM_SetCompare2(TIM2, temp0);//temp0:volt~0  
     TIM_SetCompare3(TIM2, temp1);//temp1:0~volt  
       delay_us(1000);  
   }  
     for(;(temp0<volt)||(temp2>0);temp0++,temp2--)  
     {  
       TIM_SetCompare2(TIM2, temp0);//temp0:0~volt  
       TIM_SetCompare4(TIM2, temp2);//temp2:volt~0  
       delay_us(1000);  
   }  
   for(;(temp1>0)||(temp2<volt);temp1--,temp2++)
   {  
       TIM_SetCompare4(TIM2, temp2);//temp2:0~volt  
       TIM_SetCompare3(TIM2, temp1);//temp1:volt~0  
       delay_us(1000);  
   }  
}  
int main(void)  
{  
  float Volt=0.00;  
    int ADValue = 0;  
  RCC_Configuration();  
  USART_int(115200);  
    ADC_CONFIG();  
    Get_ADC();  
    PWM_Config();  
    delay_ms(1000);  
    printf(" config done...\r\n");  
    while(1)  
    {  
        ADValue = Get_ADC();  
        Volt = VREF*ADValue/4095;  
        volt=Volt*1000;  
        printf("===============================\r\n");  
        printf("The ADC value is:%d\r\n",ADValue);  
        printf("The Volt is:%f V\r\n",Volt);  
        printf("The volt is:%d \r\n",volt);  
        PWM_TEST();  
        delay_ms(500);  
    }  
}  
  
#ifdef  USE_FULL_ASSERT  
  
void assert_failed(uint8_t* file, uint32_t line)  
{   
  while (1)  
  {  
  }  
}  
  
#endif  
  
#ifdef __GNUC__  
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)  
#else  
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)  
#endif /* __GNUC__ */  
    
PUTCHAR_PROTOTYPE  
{  
  
  USART_SendData(EVAL_COM1, (uint8_t) ch);  
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)  
  {}  
  
  return ch;  
}  
  
#ifdef  USE_FULL_ASSERT  
  
void assert_failed(uint8_t* file, uint32_t line)  
{   
  while (1)  
  {  
  }  
}  
#endif  

 


共11条 1/2 1 2 跳转至

回复

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