//库函数 #include "stm32f10x.h" #include "stm32_eval.h" #include <stdio.h> #define VREF 3.3 //延时函数 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_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; //配置时钟函数 void RCC_Configuration(void) { 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);//使能PA USART1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX位于PA9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//TX初始化PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RX位于PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//悬空输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//RX初始化PA10 USART_InitStructure.USART_BaudRate = BaudRate;//传输速率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长8比特 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);//初始化USART1时钟 USART_Init(USART1, &USART_InitStructure);//初始化USART1 USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); } // void PWM_Config() { uint16_t PrescalerValue = 0; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定义结构体 TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//打开TIM2,TIM2挂在AP1上 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);//一、开启功能复用 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//所需的3个管脚,复用输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_Cmd(TIM2, ENABLE); PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//二、配置TIM2 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;//三、脉冲宽度调制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//四、选择输出比较状态 TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//IM_Pulse设置了待装入捕获比较寄存器的脉冲值,取值在0x0000-0xFFFF之间 TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//(通道3) TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//0x0000最亮,0xFFFF熄灭 TIM_OC3Init(TIM2, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//(通道4) TIM_OCInitStructure.TIM_Pulse = 0xFFFF; TIM_OC4Init(TIM2, &TIM_OCInitStructure); TIM_ARRPreloadConfig(TIM2, ENABLE);//重新装载初始值 } void PWM_TEST() { unsigned int temp0=0,temp1=0,temp2=0; printf("PWM-RGB TEST......\r\n"); while(1) { TIM_SetCompare2(TIM2, temp0);//设置TIM2捕获比较2寄存器值 if (temp0>=2047)//2的11次方减一 { delay_ms(2000); temp0=0; break; } else {temp0++;delay_us(1000);} } while(1) { TIM_SetCompare3(TIM2, temp1);//设置TIM2捕获比较2寄存器值 if (temp1>=2047) { delay_ms(2000); temp1=0; break; } else {temp1++;delay_us(1000);} } while(1) { TIM_SetCompare4(TIM2, temp2); if (temp2>=2047) { delay_ms(2000); temp2=0; break; } else {temp2++;delay_us(1000);} } } int main(void) { RCC_Configuration(); USART_int(115200); printf(" config done...\r\n"); PWM_Config(); delay_ms(1000); while(1) { PWM_TEST(); } } #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 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
//代码未OK,先放个注释留个位置
视频地址:http://player.youku.com/player.php/sid/XODQxNzc3MzYw/v.swf
来更新按键中断作业的视频了
上周虽然吧代码写出来了,但是由于没有自己的板,所以没有能及时跟新视频,现在补上
谢谢8号木可叔叔的板子
再次更新pwm的
//库函数 #include "stm32f10x.h" #include "stm32_eval.h" #include #define VREF 3.3 unsigned int temp0,temp1,temp2; //延时函数 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_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; //配置时钟函数 void RCC_Configuration(void) { 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);//使能PA USART1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX位于PA9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//TX初始化PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RX位于PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//悬空输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//RX初始化PA10 USART_InitStructure.USART_BaudRate = BaudRate;//传输速率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长8比特 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);//初始化USART1时钟 USART_Init(USART1, &USART_InitStructure);//初始化USART1 USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); } // void PWM_Config() { uint16_t PrescalerValue = 0; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定义结构体 TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//打开TIM2,TIM2挂在AP1上 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);//一、开启功能复用 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//所需的3个管脚,复用输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_Cmd(TIM2, ENABLE); PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//二、配置TIM2 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;//三、脉冲宽度调制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//四、选择输出比较状态 TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//IM_Pulse设置了待装入捕获比较寄存器的脉冲值,取值在0x0000-0xFFFF之间 TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//(通道3) TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//0x0000最亮,0xFFFF熄灭 TIM_OC3Init(TIM2, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//(通道4) TIM_OCInitStructure.TIM_Pulse = 0xFFFF; TIM_OC4Init(TIM2, &TIM_OCInitStructure); TIM_ARRPreloadConfig(TIM2, ENABLE);//重新装载初始值 } void colour(int z)//自定义的函数colour { switch(z)//分别点亮相应的颜色 { case 0 : temp0=0,temp1=5000,temp2=0; /*红色*/ break; case 1 : temp0=0,temp1=5000,temp2=2500;/*橙色*/ break; case 2 : temp0=0,temp1=5000,temp2=5000;/*黄色*/ break; case 3 : temp0=0,temp1=0,temp2=5000; /*绿色*/ break; case 4 : temp0=2500,temp1=0,temp2=2500;/*青色*/ break; case 5: temp0=5000,temp1=0,temp2=0; /*蓝色*/ break; case 6 : temp0=2500,temp1=2500,temp2=0;/*紫色*/ break; default: printf(" error\n"); } } void PWM_TEST() { int i=0; for(i=0;i<7;i++) { colour(i); //temp1-红,temp2-绿,temp0-蓝 TIM_SetCompare2(TIM2,temp0);delay_ms(500); //设置TIM2捕获比较器2寄存器的值 TIM_SetCompare3(TIM2,temp1);delay_ms(500);//TIM——通道数(定时器,数值) TIM_SetCompare4(TIM2,temp2);delay_ms(500); } } int main(void)//主函数 { RCC_Configuration(); USART_int(115200); printf(" config done...\r\n");//屏幕打印出config done... PWM_Config();//配置pwm delay_ms(1000);//执行延时函数 while(1) { PWM_TEST(); }//当以上为真时,执行PWM_TEST();函数 } #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 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
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |