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

共8条 1/1 1 跳转至

28号作业作业

菜鸟
2015-11-13 15:16:16     打赏

大三上学期嵌入式系统作业:

作业1:软件的安装http://forum.eepw.com.cn/thread/277733/1#2

作业2:流水灯http://forum.eepw.com.cn/thread/277733/1#3

作业3:0~99按键计数http://forum.eepw.com.cn/thread/277733/1#4

作业4:串口输出0~1000http://forum.eepw.com.cn/thread/277733/1#5

作业5:定时器控制流水灯http://forum.eepw.com.cn/thread/277733/1#6

作业6:ADC改变灯的颜色http://forum.eepw.com.cn/thread/277733/1#7

期末作业:触摸DS18B02改变灯的颜色http://forum.eepw.com.cn/thread/277733/1#8



菜鸟
2015-11-13 15:32:34     打赏
2楼

MDK的安装

1.找到找到安装包的,双击mdk472_a.exe,开始安装

2、按照安装向导来,勾选后,点击next、next、next




3、出现安装中的界面,等待中


4、安装成功后,点击桌面MDK快捷方式,出现如下界面:

5、点击File—License management,出现如下界面:


6、打开和谐文件,将上图中的CID复制到下图的对应位置,勾选如下图所示的选项:


7、多次点击上图下方中的Generate,将其上方框框中的一大串字符,复制到New LIC中,如下图:


8、点击Add LIC,出现界面如下:



9、关闭该软件后重新打开,和谐版完成!


菜鸟
2015-11-18 11:04:06     打赏
3楼
#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 GPIO_INIT()    
{    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, 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 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(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);  
  
while(1)
{
  GPIO_ResetBits(GPIOC,GPIO_Pin_0);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_0);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_1);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_1);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_2);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_2);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_3);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_3);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_4);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_5);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_5);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_6);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_6);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_7);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_7);
  delay_ms(150);

}
}

 
视频地址:http://v.youku.com/v_show/id_XMTM4NTI2NjIzMg==.html?from=y1.7-1.2


菜鸟
2015-11-22 20:30:39     打赏
4楼
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)    //复位所有的RCC外围设备寄存器  
{     
    ErrorStatus HSEStartUpStatus;   //  设置错误标志量  
  RCC_DeInit();                 //复位RCC模块的寄存器,复位成缺省值  
 RCC_HSEConfig(RCC_HSE_ON);      //开启HSE时钟  
  HSEStartUpStatus = RCC_WaitForHSEStartUp();  //获取HSE启动状态:   
  if(HSEStartUpStatus==SUCCESS) //如果HSE启动成功 
    {  
        FLASH_PrefetchBufferCmd(ENABLE);          //开启FLASH的预取功能
        FLASH_SetLatency(FLASH_Latency_2);      //FLASH延迟2个周期
        RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置HCLK,PCLK2,PCLK1,PLL   
        RCC_PCLK2Config(RCC_HCLK_Div1);
        RCC_PCLK1Config(RCC_HCLK_Div2);  
        RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz 
        RCC_PLLCmd(ENABLE); //启动PLL            
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);  
        {}   //等待PLL启动完成
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//配置系统时钟  
        while(RCC_GetSYSCLKSource() != 0x08); //检查是否将HSE 9倍频后作为系统时钟      
    }  
      
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//改变管脚的映射SWJ使能选择用于事件输出的GPIO端口  
      
    /*蜂鸣器*/  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //使能所有要用APB2的外设时钟
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;       //选中2引脚 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        // 引脚频率50M 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     //引脚设置推拉输出    
  GPIO_Init(GPIOD, &GPIO_InitStructure);    //初始化GPIOD   
    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|RCC_APB2Periph_GPIOB, ENABLE);   //使能所有要用APB2的外设时钟 
      
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;  //选中8,9,10,11引脚 
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   // 引脚频率50M     
        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;       // 引脚频率50M 
        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;          //i被按下的次数,n延时变量    
  
        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);                                //用switch函数显示00    
                delay_ms(50);                              
                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(50);    
                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))   //判断按键是s1否被按下    
                {       
                    delay_ms(50);            //延时消抖    
                    if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))  //再次判断按键是s1否被按下      
                    {     
                        i++;                     //按键被按下,个位+1    
                        if(i==10)       
                        {    
                            j++;                 
                            i=0;                   
                        }  
                        if(j==10)  
                        {  
                             j=0;  
                                                 i=0;  
                        }  
                    }  
                }     
  

 


菜鸟
2015-11-29 17:20:21     打赏
5楼
#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);//led全亮,提示程序开始
delay_ms(200);
GPIO_SetBits(GPIOC,0xffff);//led全灭
USART_SendStr("USART Led Speed\r\n");//
USART_SendStr("\n>");//
while(1);
}
/****   配置RCC ****/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //  设置错误标志量
RCC_DeInit(); //*将外设寄存器RCC重设为初始值
RCC_HSEConfig(RCC_HSE_ON);//*打开外部高速时钟晶振HSE:
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //*等待外部高速时钟晶振工作:
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);//*AHB使用系统时钟,负责外部存储器的时钟
RCC_PCLK1Config(RCC_HCLK_Div2);//*APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
RCC_PCLK2Config(RCC_HCLK_Div1);//*APB2负责AD,I/O,高级TIM,串口1
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*设置PLL输入时钟源72M
RCC_PLLCmd(ENABLE);//*打开PLL:
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
while(RCC_GetSYSCLKSource() != 0x08);//* 判断PLL是否是系统时钟:
}
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);
}
/**** 初始化GPIO ****/
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);
}
/**** USART传输的波特率 ****/
void USART_int(long BaudRate)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA和 usart1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 PA9作为US1的TX端,打开复用,负责发送数据
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //RX位于PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入,PA10作为US1的RX端,负责接收数据
//RX初始化PA10
USART_InitStructure.USART_BaudRate = BaudRate;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//USART_WordLength 在一个帧中传输8位数据(字节)
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 使能)
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //SCLK输出时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//时钟第二个边沿开始数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据不从SCLK发出
USART_ClockInit(USART1, &USART_ClockInitStructure);//初始化USART1时钟
USART_Init(USART1, &USART_InitStructure);//初始化USART1
USART_Cmd(USART1, ENABLE);//USART1使能
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能接收中断,在接受移位寄存器中有数据时产生
USART_Cmd(USART1, ENABLE);
//配置NVIC ,设置优先级分组,使用NVIC_Init对NVIC进行初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择中断分组4(先占优先级四位,从优先级0)
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级1-15
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//响应优先级1-15
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure); //初始化中断
}
/**** usart发送数据 ****/
void USART_SendStr(char *str) //USART发送数据
{
while((*str)!='\0')
{
USART_SendData(USART1,*str++); //通过外设 USARTx 发送单个数据
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //检查指定的 USART 标志位设置与否
}
}
/**** 取字符串中的数字 ****/
unsigned int translate(char* pstr)
{
int s = 0;
while(*pstr != '\0') //如果没扫到字符串的最后不停止
{
if(*pstr >= '0' && *pstr <= '9') //如果字符串中存在0-9的数字
{
s = s * 10 + *pstr - '0';
}
pstr++;
}
return s;
}
/****  点亮led****/
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]); // 点亮LED
delay_ms(m);
GPIO_SetBits(GPIOC,a[i]); //熄灭LED
delay_ms(m);
}
}
void input_ASK()
{
char j;
LED(rx_buff,rx_buff_count);
rx_buff_count=0;
for (j=0;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); //清除中断接受完成标志位
}

 


菜鸟
2015-12-01 16:38:39     打赏
6楼
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
/** @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;
int count=0;

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) 
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
//	flag = ~flag;	
//	if(flag){
//	GPIO_SetBits(GPIOC,GPIO_Pin_1);}
//	else {GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
/*flag++;
	if(flag == 50)
		{led = ~led;
		 flag = 0;}
	if(led){
	GPIO_SetBits(GPIOC,GPIO_Pin_1);}
	else {GPIO_ResetBits(GPIOC,GPIO_Pin_1);}*/
	count++;
	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);
        if(count==1) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_0);
	if(count==2) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_1);
	if(count==3) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_2);
	if(count==4) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_3);
	if(count==5) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_4);
	if(count==6) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_5);
	if(count==7) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_6);
	if(count==8) 
	GPIO_ResetBits(GPIOC,GPIO_Pin_7);
	if(count==9)
	count=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****/

 


菜鸟
2016-01-19 11:59:41     打赏
7楼
#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  

 


菜鸟
2016-01-27 12:08:27     打赏
8楼
#include "stdio.h"  
#include "stm32f10x.h"  
int fputc(int ch,FILE *f);  
  
/********************RCC配置*********************/  
void RCC_Configuration(void)      
{  
    
    GPIO_InitTypeDef  GPIO_InitStructure;  
    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输入时钟源8x9=72M  
        RCC_PLLCmd(ENABLE);//*打开PLL:            
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:  
        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);  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, 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);     
}  
  
  
  
/*****************************GPIO初始********************************/  
void GPIO_Inits(void)  
{   /*结构体初始化及开启外设时钟*/  
        GPIO_InitTypeDef  GPIO_InitStructure;  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);  
        /*GPIOA--RGB初始化*/  
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;  
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;   
        GPIO_Init(GPIOA,&GPIO_InitStructure);  
        GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3)      
          
}  
  
/**************************DS18B20**********************/  
  
uint8_t DS18B20_Init(void)  
{  
    GPIO_InitTypeDef GPIO_InitStructure;  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
    Set_B20();  
  
    DS18B20_Rst();  
    return DS18B20_Presence ();  
}  
  
static void DS18B20_Mode_IPU(void)  
{  
      GPIO_InitTypeDef GPIO_InitStructure;  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
}  
  
static void DS18B20_Mode_Out_PP(void)  
{  
    GPIO_InitTypeDef GPIO_InitStructure;  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
}  
  
  
  
static void DS18B20_Rst(void)  
{  
    /* 主机设置为输出 */  
    DS18B20_Mode_Out_PP();  
    Reset_B20() ;  
    delay_us(700);   
    Set_B20()   ;  
    delay_us(15);  
}  
  
  
  
static uint8_t DS18B20_Presence(void)  
{  
    uint8_t pulse_time = 0;  
      
    /* 主机设置为上拉输入 */  
    DS18B20_Mode_IPU();  
      
   
    while( Read_B20() && pulse_time<100 )  
    {  
        pulse_time++;  
        delay_us(1);  
    }     
    /* 经过100us后,存在脉冲都还没有到来*/  
    if( pulse_time >=100 )  
        return 1;  
    else  
        pulse_time = 0;  
      
    /* 存在脉冲到来,且存在的时间不能超过240us */  
    while( !Read_B20() && pulse_time<240 )  
    {  
        pulse_time++;  
        delay_us(1);  
    }     
    if( pulse_time >=240 )  
        return 1;  
    else  
        return 0;  
}  
  
  
static uint8_t DS18B20_ReadBit(void)  
{  
    uint8_t dat;  
      
    /* 读0和读1的时间至少要大于60us */   
    DS18B20_Mode_Out_PP();  
    Reset_B20();  
    delay_us(10);  
    DS18B20_Mode_IPU();  
    //Delay_us(2);  
      
    if( Read_B20() == SET )  
        dat = 1;  
    else  
        dat = 0;  
       
    delay_us(45);  
      
    return dat;  
}  
  
  
static uint8_t DS18B20_ReadByte(void)  
{  
    uint8_t i, j, dat = 0;    
      
    for(i=0; i<8; i++)   
    {  
        j = DS18B20_ReadBit();        
        dat = (dat) | (j<<i);  
    }  
      
    return dat;  
}  
  
  
  
static void DS18B20_WriteByte(uint8_t dat)  
{  
    uint8_t i, testb;  
    DS18B20_Mode_Out_PP();  
      
    for( i=0; i<8; i++ )  
    {  
        testb = dat&0x01;  
        dat = dat>>1;       
        /* 写0和写1的时间至少要大于60us */  
        if (testb)  
        {             
            Reset_B20();  
            /* 1us < 这个延时 < 15us */  
            delay_us(8);  
              
            Set_B20();  
            delay_us(58);  
        }         
        else  
        {             
            Reset_B20();  
            /* 60us < Tx 0 < 120us */  
            delay_us(70);  
              
            Set_B20();            
            delay_us(2);  
        }  
    }  
}  
  
  
  
static void DS18B20_SkipRom ( void )  
{  
    DS18B20_Rst();       
      
    DS18B20_Presence();    
      
    DS18B20_WriteByte(0XCC);        /* 跳过 ROM */  
      
}  
  
  
static void DS18B20_MatchRom ( void )  
{  
    DS18B20_Rst();       
      
    DS18B20_Presence();    
      
    DS18B20_WriteByte(0X55);        /* 匹配 ROM */  
      
}  
  
float DS18B20_GetTemp_SkipRom ( void )  
{  
    uint8_t tpmsb, tplsb;  
    short s_tem;  
    float f_tem;  
      
      
    DS18B20_SkipRom ();  
    DS18B20_WriteByte(0X44);                /* 开始转换 */  
      
      
    DS18B20_SkipRom ();  
  DS18B20_WriteByte(0XBE);              /* 读温度值 */  
      
    tplsb = DS18B20_ReadByte();        
    tpmsb = DS18B20_ReadByte();   
      
      
    s_tem = tpmsb<<8;  
    s_tem = s_tem | tplsb;  
      
    if( s_tem < 0 )      /* 负温度 */  
        f_tem = (~s_tem+1) * 0.0625;      
    else  
        f_tem = s_tem * 0.0625;  
      
    return f_tem;     
      
      
}  
  
  
  
void DS18B20_ReadId ( uint8_t * ds18b20_id )  
{  
    uint8_t uc;  
      
      
    DS18B20_WriteByte(0x33);       //读取序列号  
      
    for ( uc = 0; uc < 8; uc ++ )  
      ds18b20_id [ uc ] = DS18B20_ReadByte();  
      
}  
  
  
float DS18B20_GetTemp_MatchRom ( uint8_t * ds18b20_id )  
{  
    uint8_t tpmsb, tplsb, i;  
    short s_tem;  
    float f_tem;  
      
      
    DS18B20_MatchRom ();            //匹配ROM  
      
  for(i=0;i<8;i++)  
        DS18B20_WriteByte ( ds18b20_id [ i ] );   
      
    DS18B20_WriteByte(0X44);                /* 开始转换 */  
  
      
    DS18B20_MatchRom ();            //匹配ROM  
      
    for(i=0;i<8;i++)  
        DS18B20_WriteByte ( ds18b20_id [ i ] );   
      
    DS18B20_WriteByte(0XBE);                /* 读温度值 */  
      
    tplsb = DS18B20_ReadByte();        
    tpmsb = DS18B20_ReadByte();   
      
      
    s_tem = tpmsb<<8;  
    s_tem = s_tem | tplsb;  
      
    if( s_tem < 0 )      /* 负温度 */  
        f_tem = (~s_tem+1) * 0.0625;      
    else  
        f_tem = s_tem * 0.0625;  
      
    return f_tem;     
      
      
}  
  
  
  
  
  
int main()  
{    
uint8_t uc, ucDs18b20Id [ 8 ];  
RCC_Configuration();  
GPIO_Inits();  
USART_int(9600);  
while( DS18B20_Init() )   
printf("\r\n no ds18b20 exit \r\n");  
  
printf("\r\n Yes ds18b20 \r\n");  
  
DS18B20_ReadId ( ucDs18b20Id  );           // 读取 DS18B20 的序列号  
  
printf("\r\nDS18B20的序列号是: 0x");  
  
for ( uc = 0; uc < 8; uc ++ )             // 打印 DS18B20 的序列号  
printf ( "%.2x", ucDs18b20Id [ uc ] );  
  
  
for(;;)  
{     
static unsigned int i=0;  
float a,b;  
if(i==10)  
{  
printf ( "\r\n第10秒:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );       // 打印通过 DS18B20 序列号获取的温度值           
a=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );  
delay_ms(1000);     /* 1s 读取一次温度值 */  
i++;  
}  
else{}  
if(i<10)  
{  
printf ( "\r\n温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );        // 打印通过 DS18B20 序列号获取的温度值           
delay_ms(1000);     /* 1s 读取一次温度值 */  
i++;  
}  
if(i>10)  
{  
printf ( "\r\n10秒后:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );       // 打印通过 DS18B20 序列号获取的温度值           
b=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );  
delay_ms(1000);     /* 1s 读取一次温度值 */  
i++;  
if((b-a)>0.9)  
{GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);  
  
}  
else{}  
if((b-a)<0.9)  
{  
        GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);  
  
  
}  
else{}  
  
}  
else{}  
  
}     
}  
int fputc(int ch,FILE *f)  
{  
USART_SendData(USART1, (uint8_t) ch);  
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  
{}  
return ch;  
}  

 


共8条 1/1 1 跳转至

回复

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