这个程序是用来做信号调制的,如果一位是1,发一种频率,是0,发另一种频率(都发8个脉冲,TIM2计数PA1引脚),,现在问题是我进不了中断,有些注释比较神经,请自动忽略。
#include "stm32f10x.h"
#include "stm32f10x_it.h"
#include "stm32f10x_gpio.h"
unsigned int fre;
u8 t,tem;
/***************************/
/* GPIO外部信号输入口使能 */
/***************************/
void GPIO_PWM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能LED对应GPIO的Clock时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO , ENABLE); //使能PWM信号输出对应端口时钟
/* 初始化LED的GPIO管脚,配置为推挽输出 */ //设置端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ; //PWM对应的GPIO_Pin_1
//设置对端口的模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置为复用口推挽(Push-Pull)输出
//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最大输出速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //调用GPIO初始化函数
}
void GPIO_IC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能LED对应GPIO的Clock时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO , ENABLE); //使能信号采集口对应端口时钟
/* 初始化LED的GPIO管脚,配置为浮空输入 */ //设置端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //信号采集对应的GPIO_Pin_1
//设置对端口的模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置为浮空输入
//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最大输出速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //调用GPIO初始化函数
}
void TIM2_IC_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟配置
//配置TIMER1作为计数器
//TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 0x0008;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted, 0x0);
TIM_SetCounter(TIM2, 0);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);
TIM_ARRPreloadConfig(TIM2, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM2, ENABLE);
}
/***************************/
/* 定时器模式及通道选择 */
/***************************/
void TIM3_OC_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟配置
/* The following example illustrates how to initialize a
TIM_OCInitTypeDef structure */
//不分频。PWM频率=72000000/900=80Khz
/* ---------------------------------------------------------------
TIM3CLK 即PCLK1=36MHz
TIM3 Configuration: generate 1 PWM signals :
TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
TIM3 ARR Register = 900 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
TIM3 Frequency = 36 KHz.
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100
TIM3CLK = 36 MHz, Prescaler = 0, TIM3 counter clock = 36MHz
--------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = fre; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);//打开中断,中断需要这行代码
//TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //设定为输出,需要PWM输出才需要这行代码
TIM_OCInitStructure.TIM_Pulse = fre/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR2上的预装载寄存器
//上面两句中的OC2确定了是channle几,要是OC3则是channel 3
TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
/* TIM3 enable counter */
//TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
//TIM_Cmd(TIM3, DISABLE); //失能TIMx外设
}
/***************************/
/* 设置中断模式及优先级 */
/***************************/
void NVIC_TIM2_IC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the Priority Grouping with 1 bit */
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);//设定中断向量表基址0x08000000
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable TIM3 global interrupt with Preemption Priority 0 and Sub
Priority as 2 */
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable USART1 global interrupt with Preemption Priority 1 and Sub
Priority as 5 */
//NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
//NVIC_InitStructure(&NVIC_InitStructure);
/* Enable RTC global interrupt with Preemption Priority 1 and Sub
Priority as 7 */
//NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;
//NVIC_InitStructure(&NVIC_InitStructure);
/* Enable EXTI4 interrupt with Preemption Priority 1 and Sub
Priority as 7 */
//NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;
//NVIC_InitStructure(&NVIC_InitStructure);
/* TIM3 interrupt priority is higher than USART1, RTC and EXTI4
interrupts priorities. USART1 interrupt priority is higher than RTC
and EXTI4 interrupts priorities. RTC interrupt priority is higher
than EXTI4 interrupt prioriy. */
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
TIM_Cmd(TIM3, DISABLE); //失能TIMx外设
t=1;
TIM_SetCounter(TIM2, 0);
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
/* Clear the TIM2 Capture Compare 1 interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
void sendbybe()
{
u8 k=0;
while(!t);
TIM_Cmd(TIM3, DISABLE);
TIM_Cmd(TIM2, DISABLE);
TIM_SetCounter(TIM2, 0);
t=0;
while(k<8)
{
if(tem&0x80)
{
fre=5000;
TIM3_OC_Config();
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
else
{
fre=30000;
TIM3_OC_Config();
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
while(!t);
t=0;
tem<<=1;
TIM_Cmd(TIM3, DISABLE);
TIM_Cmd(TIM2, DISABLE);
TIM_SetCounter(TIM2, 0);
k++;
}
fre=10000;
TIM3_OC_Config();
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM2, ENABLE);
while(!t);
t=0;
TIM_Cmd(TIM3, DISABLE);
TIM_Cmd(TIM2, DISABLE);
//delay1s();
//t0=3456;
//TH0 = (65536-t0)/256;
//TL0 = (65536-t0)%256;
//TH1 = (65536-8)/256;
//TL1 = (65536-8)%256;
//TR0=1;
//TR1=1;
}
int main(void)
{
// TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
fre=10000;
GPIO_PWM_Config();
GPIO_IC_Config();
NVIC_TIM2_IC_Config();
TIM2_IC_Config();
TIM3_OC_Config();
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
while(1)
{
tem=34;
sendbybe();
}
}
|
|